Skip to content

Commit 304a7be

Browse files
committed
Test for issue #23.
1 parent f100417 commit 304a7be

File tree

7 files changed

+285
-0
lines changed

7 files changed

+285
-0
lines changed

tools/.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,15 @@
2222
<attribute name="maven.pomderived" value="true"/>
2323
</attributes>
2424
</classpathentry>
25+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
2535
<classpathentry kind="output" path="target/classes"/>
2636
</classpath>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
eclipse.preferences.version=1
22
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
4+
encoding//src/test/java=UTF-8
5+
encoding//src/test/resources=UTF-8
36
encoding/<project>=UTF-8
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.jvnet.jaxb2_commons.xjc.model.concrete.tests.ahpla;
2+
3+
import java.io.Serializable;
4+
5+
import javax.xml.bind.annotation.XmlAccessType;
6+
import javax.xml.bind.annotation.XmlAccessorType;
7+
import javax.xml.bind.annotation.XmlElement;
8+
import javax.xml.bind.annotation.XmlType;
9+
10+
@XmlAccessorType(XmlAccessType.FIELD)
11+
@XmlType(name = "KnownReferencedType", propOrder = { "str" })
12+
public class KnownReferencedType implements Serializable {
13+
protected String str;
14+
15+
public String getStr() {
16+
return str;
17+
}
18+
19+
public void setStr(String value) {
20+
this.str = value;
21+
}
22+
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@javax.xml.bind.annotation.XmlSchema(namespace = "urn:test", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = { @XmlNs(namespaceURI = "urn:test", prefix = "test") })
2+
package org.jvnet.jaxb2_commons.xjc.model.concrete.tests.ahpla;
3+
4+
import javax.xml.bind.annotation.XmlNs;
5+
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package org.jvnet.jaxb2_commons.xjc.model.concrete.tests.alpha;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
7+
import javax.xml.namespace.QName;
8+
9+
import org.junit.Assert;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
import org.jvnet.jaxb2_commons.xjc.model.concrete.XJCCMInfoFactory;
13+
import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo;
14+
import org.jvnet.jaxb2_commons.xml.bind.model.MElementInfo;
15+
import org.jvnet.jaxb2_commons.xml.bind.model.MModelInfo;
16+
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
17+
import org.jvnet.jaxb2_commons.xml.namespace.util.QNameUtils;
18+
import org.jvnet.jaxb2_commons.xmlschema.XmlSchemaConstants;
19+
20+
import com.sun.codemodel.JCodeModel;
21+
import com.sun.tools.xjc.BadCommandLineException;
22+
import com.sun.tools.xjc.ConsoleErrorReporter;
23+
import com.sun.tools.xjc.ModelLoader;
24+
import com.sun.tools.xjc.Options;
25+
import com.sun.tools.xjc.model.Model;
26+
import com.sun.tools.xjc.model.nav.NClass;
27+
import com.sun.tools.xjc.model.nav.NType;
28+
29+
public class AlphaMInfoFactoryTest {
30+
31+
private static MModelInfo<NType, NClass> MODEL_INFO;
32+
33+
@BeforeClass
34+
public static void createModel() throws BadCommandLineException,
35+
IOException {
36+
final String generateDirectory = "target/generated-sources/"
37+
+ AlphaMInfoFactoryTest.class.getPackage().getName();
38+
new File(generateDirectory).mkdirs();
39+
final URL schema = AlphaMInfoFactoryTest.class
40+
.getResource("schema.xsd");
41+
final URL binding = AlphaMInfoFactoryTest.class
42+
.getResource("binding.xjb");
43+
final String[] arguments = new String[] { "-xmlschema",
44+
schema.toExternalForm(), "-b", binding.toExternalForm(), "-d",
45+
generateDirectory, "-extension" };
46+
47+
Options options = new Options();
48+
options.parseArguments(arguments);
49+
ConsoleErrorReporter receiver = new ConsoleErrorReporter();
50+
Model model = ModelLoader.load(options, new JCodeModel(), receiver);
51+
Assert.assertNotNull(model);
52+
53+
final XJCCMInfoFactory factory = new XJCCMInfoFactory(model);
54+
55+
AlphaMInfoFactoryTest.MODEL_INFO = factory.createModel();
56+
57+
model.generateCode(options, receiver);
58+
com.sun.codemodel.CodeWriter cw = options.createCodeWriter();
59+
model.codeModel.build(cw);
60+
61+
}
62+
63+
@Test
64+
public void successfullyLoadsModel() {
65+
Assert.assertNotNull(MODEL_INFO);
66+
}
67+
68+
@Test
69+
public void createsCorrectTypeNameForNamedComplexType() {
70+
final MClassInfo<NType, NClass> classInfo = MODEL_INFO
71+
.getClassInfo(getClass().getPackage().getName() + "."
72+
+ "NamedComplexType");
73+
Assert.assertEquals(new QName("urn:test", "NamedComplexType"),
74+
classInfo.getTypeName());
75+
}
76+
77+
@Test
78+
public void createsCorrectTypeNameForUnnamedComplexType() {
79+
final MElementInfo<NType, NClass> elementInfo = MODEL_INFO
80+
.getGlobalElementInfo(new QName("urn:test",
81+
"UnnamedComplexTypeElement"));
82+
Assert.assertNotNull(elementInfo);
83+
Assert.assertNull(elementInfo.getTypeInfo().getTypeName());
84+
}
85+
86+
@Test
87+
public void createsCorrectTypeNameForNamedSimpleType() {
88+
final MElementInfo<NType, NClass> elementInfo = MODEL_INFO
89+
.getGlobalElementInfo(new QName("urn:test",
90+
"NamedSimpleTypeElement"));
91+
Assert.assertNotNull(elementInfo);
92+
Assert.assertEquals(new QName("urn:test", "NamedSimpleType"),
93+
elementInfo.getTypeInfo().getTypeName());
94+
}
95+
96+
@Test
97+
public void createsCorrectTypeNameForUnnamedSimpleType() {
98+
final MElementInfo<NType, NClass> elementInfo = MODEL_INFO
99+
.getGlobalElementInfo(new QName("urn:test",
100+
"UnnamedSimpleTypeElement"));
101+
Assert.assertNotNull(elementInfo);
102+
Assert.assertNull(elementInfo.getTypeInfo().getTypeName());
103+
}
104+
105+
@Test
106+
public void createsCorrectTypeNameForStringElementType() {
107+
final MElementInfo<NType, NClass> elementInfo = MODEL_INFO
108+
.getGlobalElementInfo(new QName("urn:test", "StringElement"));
109+
Assert.assertNotNull(elementInfo);
110+
Assert.assertEquals(XmlSchemaConstants.STRING, elementInfo
111+
.getTypeInfo().getTypeName());
112+
}
113+
114+
@Test
115+
public void createsCorrectTypeNameForKnownReferencedType() {
116+
final QName typeName = new QName("urn:test", "KnownReferencedType",
117+
"test");
118+
final MElementInfo<NType, NClass> elementInfo = MODEL_INFO
119+
.getGlobalElementInfo(new QName("urn:test",
120+
"KnownReferencedTypeElement"));
121+
final MTypeInfo<NType, NClass> typeInfo = elementInfo.getTypeInfo();
122+
Assert.assertNotNull(typeInfo);
123+
Assert.assertEquals(QNameUtils.getKey(typeName),
124+
QNameUtils.getKey(typeInfo.getTypeName()));
125+
}
126+
127+
@Test
128+
public void createsCorrectTypeNameForUnnownReferencedType() {
129+
final QName typeName = new QName("urn:test", "UnknownReferencedType",
130+
"test");
131+
final MElementInfo<NType, NClass> elementInfo = MODEL_INFO
132+
.getGlobalElementInfo(new QName("urn:test",
133+
"UnknownReferencedTypeElement"));
134+
final MTypeInfo<NType, NClass> typeInfo = elementInfo.getTypeInfo();
135+
Assert.assertNotNull(typeInfo);
136+
Assert.assertNull(typeInfo.getTypeName());
137+
}
138+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
2+
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
3+
jaxb:extensionBindingPrefixes="xjc" xmlns:test="urn:test">
4+
5+
<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
6+
<jaxb:globalBindings localScoping="toplevel">
7+
<jaxb:serializable />
8+
</jaxb:globalBindings>
9+
</jaxb:bindings>
10+
</jaxb:bindings>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<xsd:schema targetNamespace="urn:test" xmlns:test="urn:test"
2+
xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.1"
3+
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" elementFormDefault="qualified"
4+
attributeFormDefault="unqualified" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
5+
6+
jaxb:extensionBindingPrefixes="xjc">
7+
8+
<xsd:annotation>
9+
<xsd:appinfo>
10+
<jaxb:schemaBindings>
11+
<jaxb:package
12+
name="org.jvnet.jaxb2_commons.xjc.model.concrete.tests.alpha" />
13+
</jaxb:schemaBindings>
14+
</xsd:appinfo>
15+
</xsd:annotation>
16+
17+
<xsd:element name="NamedComplexTypeElement" type="test:NamedComplexType" />
18+
19+
<xsd:complexType name="NamedComplexType">
20+
<xsd:sequence>
21+
<xsd:element name="str" type="xsd:string" />
22+
</xsd:sequence>
23+
</xsd:complexType>
24+
25+
<xsd:element name="UnnamedComplexTypeElement">
26+
<xsd:complexType>
27+
<xsd:sequence>
28+
<xsd:element name="str" type="xsd:string" />
29+
</xsd:sequence>
30+
</xsd:complexType>
31+
</xsd:element>
32+
33+
<xsd:element name="StringElement" type="xsd:string">
34+
<xsd:annotation>
35+
<xsd:appinfo>
36+
<jaxb:class name="StringElement" />
37+
</xsd:appinfo>
38+
</xsd:annotation>
39+
</xsd:element>
40+
41+
<xsd:element name="KnownReferencedTypeElement" type="test:KnownReferencedType" />
42+
43+
<xsd:complexType name="KnownReferencedType">
44+
<xsd:annotation>
45+
<xsd:appinfo>
46+
<jaxb:class
47+
ref="org.jvnet.jaxb2_commons.xjc.model.concrete.tests.ahpla.KnownReferencedType" />
48+
</xsd:appinfo>
49+
</xsd:annotation>
50+
<xsd:sequence>
51+
<xsd:element name="str" type="xsd:string" />
52+
</xsd:sequence>
53+
</xsd:complexType>
54+
55+
<xsd:element name="UnknownReferencedTypeElement" type="test:UnknownReferencedType" />
56+
57+
<xsd:complexType name="UnknownReferencedType">
58+
<xsd:annotation>
59+
<xsd:appinfo>
60+
<jaxb:class
61+
ref="org.jvnet.jaxb2_commons.xjc.model.concrete.tests.ahpla.UnknownReferencedType" />
62+
</xsd:appinfo>
63+
</xsd:annotation>
64+
<xsd:sequence>
65+
<xsd:element name="str" type="xsd:string" />
66+
</xsd:sequence>
67+
</xsd:complexType>
68+
69+
<xsd:element name="NamedSimpleTypeElement" type="test:NamedSimpleType">
70+
<xsd:annotation>
71+
<xsd:appinfo>
72+
<jaxb:class name="NamedSimpleTypeElement" />
73+
</xsd:appinfo>
74+
</xsd:annotation>
75+
</xsd:element>
76+
77+
<xsd:simpleType name="NamedSimpleType">
78+
<xsd:annotation>
79+
<xsd:appinfo>
80+
<jaxb:class name="NamedSimpleType" />
81+
</xsd:appinfo>
82+
</xsd:annotation>
83+
<xsd:list itemType="xsd:string" />
84+
</xsd:simpleType>
85+
86+
<xsd:element name="UnnamedSimpleTypeElement">
87+
<xsd:annotation>
88+
<xsd:appinfo>
89+
<jaxb:class name="UnnamedSimpleTypeElement" />
90+
</xsd:appinfo>
91+
</xsd:annotation>
92+
<xsd:simpleType>
93+
<xsd:list itemType="xsd:string" />
94+
</xsd:simpleType>
95+
</xsd:element>
96+
</xsd:schema>

0 commit comments

Comments
 (0)