1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.xsd.commons;
18
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.TransformerFactory;
23 import javax.xml.transform.dom.DOMResult;
24
25 import org.custommonkey.xmlunit.XMLTestCase;
26 import org.custommonkey.xmlunit.XMLUnit;
27 import org.w3c.dom.Document;
28
29 import org.springframework.core.io.ClassPathResource;
30 import org.springframework.core.io.Resource;
31 import org.springframework.xml.sax.SaxUtils;
32 import org.springframework.xml.validation.XmlValidator;
33 import org.springframework.xml.xsd.AbstractXsdSchemaTestCase;
34 import org.springframework.xml.xsd.XsdSchema;
35
36 public class CommonsXsdSchemaCollectionTest extends XMLTestCase {
37
38 private CommonsXsdSchemaCollection collection;
39
40 private Transformer transformer;
41
42 private DocumentBuilder documentBuilder;
43
44 protected void setUp() throws Exception {
45 collection = new CommonsXsdSchemaCollection();
46 TransformerFactory transformerFactory = TransformerFactory.newInstance();
47 transformer = transformerFactory.newTransformer();
48 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
49 documentBuilderFactory.setNamespaceAware(true);
50 documentBuilder = documentBuilderFactory.newDocumentBuilder();
51 XMLUnit.setIgnoreWhitespace(true);
52 }
53
54 public void testSingle() throws Exception {
55 Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
56 collection.setXsds(new Resource[]{resource});
57 collection.afterPropertiesSet();
58 assertEquals("Invalid amount of XSDs loaded", 1, collection.getXsdSchemas().length);
59 }
60
61 public void testInlineComplex() throws Exception {
62 Resource a = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
63 collection.setXsds(new Resource[]{a});
64 collection.setInline(true);
65 collection.afterPropertiesSet();
66 XsdSchema[] schemas = collection.getXsdSchemas();
67 assertEquals("Invalid amount of XSDs loaded", 2, schemas.length);
68
69 assertEquals("Invalid target namespace", "urn:1", schemas[0].getTargetNamespace());
70 Resource abc = new ClassPathResource("ABC.xsd", AbstractXsdSchemaTestCase.class);
71 Document expected = documentBuilder.parse(SaxUtils.createInputSource(abc));
72 DOMResult domResult = new DOMResult();
73 transformer.transform(schemas[0].getSource(), domResult);
74 assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
75
76 assertEquals("Invalid target namespace", "urn:2", schemas[1].getTargetNamespace());
77 Resource cd = new ClassPathResource("CD.xsd", AbstractXsdSchemaTestCase.class);
78 expected = documentBuilder.parse(SaxUtils.createInputSource(cd));
79 domResult = new DOMResult();
80 transformer.transform(schemas[1].getSource(), domResult);
81 assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
82 }
83
84 public void testCircular() throws Exception {
85 Resource resource = new ClassPathResource("circular-1.xsd", AbstractXsdSchemaTestCase.class);
86 collection.setXsds(new Resource[]{resource});
87 collection.setInline(true);
88 collection.afterPropertiesSet();
89 XsdSchema[] schemas = collection.getXsdSchemas();
90 assertEquals("Invalid amount of XSDs loaded", 1, schemas.length);
91 }
92
93 public void testXmlNamespace() throws Exception {
94 Resource resource = new ClassPathResource("xmlNamespace.xsd", AbstractXsdSchemaTestCase.class);
95 collection.setXsds(new Resource[]{resource});
96 collection.setInline(true);
97 collection.afterPropertiesSet();
98 XsdSchema[] schemas = collection.getXsdSchemas();
99 assertEquals("Invalid amount of XSDs loaded", 1, schemas.length);
100 }
101
102 public void testCreateValidator() throws Exception {
103 Resource a = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
104 collection.setXsds(new Resource[]{a});
105 collection.setInline(true);
106 collection.afterPropertiesSet();
107
108 XmlValidator validator = collection.createValidator();
109 assertNotNull("No XmlValidator returned", validator);
110 }
111
112 public void testInvalidSchema() throws Exception {
113 Resource invalid = new ClassPathResource("invalid.xsd", AbstractXsdSchemaTestCase.class);
114 collection.setXsds(new Resource[]{invalid});
115 try {
116 collection.afterPropertiesSet();
117 fail("CommonsXsdSchemaException expected");
118 }
119 catch (CommonsXsdSchemaException ex) {
120
121 }
122 }
123
124 public void testIncludesAndImports() throws Exception {
125 Resource hr = new ClassPathResource("hr.xsd", getClass());
126 collection.setXsds(new Resource[]{hr});
127 collection.setInline(true);
128 collection.afterPropertiesSet();
129
130 XsdSchema[] schemas = collection.getXsdSchemas();
131 assertEquals("Invalid amount of XSDs loaded", 2, schemas.length);
132
133 assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas", schemas[0].getTargetNamespace());
134 Resource hr_employee = new ClassPathResource("hr_employee.xsd", getClass());
135 Document expected = documentBuilder.parse(SaxUtils.createInputSource(hr_employee));
136 DOMResult domResult = new DOMResult();
137 transformer.transform(schemas[0].getSource(), domResult);
138 assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
139
140 assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas/holiday", schemas[1].getTargetNamespace());
141 Resource holiday = new ClassPathResource("holiday.xsd", getClass());
142 expected = documentBuilder.parse(SaxUtils.createInputSource(holiday));
143 domResult = new DOMResult();
144 transformer.transform(schemas[1].getSource(), domResult);
145 assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
146
147 }
148 }