|
| 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 | +} |
0 commit comments