1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.springframework.core.io.ClassPathResource;
26  import org.springframework.core.io.Resource;
27  import org.springframework.xml.sax.SaxUtils;
28  import org.springframework.xml.validation.XmlValidator;
29  import org.springframework.xml.xsd.AbstractXsdSchemaTestCase;
30  import org.springframework.xml.xsd.XsdSchema;
31  
32  import org.custommonkey.xmlunit.XMLUnit;
33  import org.junit.Assert;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.w3c.dom.Document;
37  
38  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
39  
40  public class CommonsXsdSchemaCollectionTest {
41  
42      private CommonsXsdSchemaCollection collection;
43  
44      private Transformer transformer;
45  
46      private DocumentBuilder documentBuilder;
47  
48      @Before
49      public void setUp() throws Exception {
50          collection = new CommonsXsdSchemaCollection();
51          TransformerFactory transformerFactory = TransformerFactory.newInstance();
52          transformer = transformerFactory.newTransformer();
53          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
54          documentBuilderFactory.setNamespaceAware(true);
55          documentBuilder = documentBuilderFactory.newDocumentBuilder();
56          XMLUnit.setIgnoreWhitespace(true);
57      }
58  
59      @Test
60      public void testSingle() throws Exception {
61          Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
62          collection.setXsds(new Resource[]{resource});
63          collection.afterPropertiesSet();
64          Assert.assertEquals("Invalid amount of XSDs loaded", 1, collection.getXsdSchemas().length);
65      }
66  
67      @Test
68      public void testInlineComplex() throws Exception {
69          Resource a = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
70          collection.setXsds(new Resource[]{a});
71          collection.setInline(true);
72          collection.afterPropertiesSet();
73          XsdSchema[] schemas = collection.getXsdSchemas();
74          Assert.assertEquals("Invalid amount of XSDs loaded", 2, schemas.length);
75  
76          Assert.assertEquals("Invalid target namespace", "urn:1", schemas[0].getTargetNamespace());
77          Resource abc = new ClassPathResource("ABC.xsd", AbstractXsdSchemaTestCase.class);
78          Document expected = documentBuilder.parse(SaxUtils.createInputSource(abc));
79          DOMResult domResult = new DOMResult();
80          transformer.transform(schemas[0].getSource(), domResult);
81          assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
82  
83          Assert.assertEquals("Invalid target namespace", "urn:2", schemas[1].getTargetNamespace());
84          Resource cd = new ClassPathResource("CD.xsd", AbstractXsdSchemaTestCase.class);
85          expected = documentBuilder.parse(SaxUtils.createInputSource(cd));
86          domResult = new DOMResult();
87          transformer.transform(schemas[1].getSource(), domResult);
88          assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
89      }
90  
91      @Test
92      public void testCircular() throws Exception {
93          Resource resource = new ClassPathResource("circular-1.xsd", AbstractXsdSchemaTestCase.class);
94          collection.setXsds(new Resource[]{resource});
95          collection.setInline(true);
96          collection.afterPropertiesSet();
97          XsdSchema[] schemas = collection.getXsdSchemas();
98          Assert.assertEquals("Invalid amount of XSDs loaded", 1, schemas.length);
99      }
100 
101     @Test
102     public void testXmlNamespace() throws Exception {
103         Resource resource = new ClassPathResource("xmlNamespace.xsd", AbstractXsdSchemaTestCase.class);
104         collection.setXsds(new Resource[]{resource});
105         collection.setInline(true);
106         collection.afterPropertiesSet();
107         XsdSchema[] schemas = collection.getXsdSchemas();
108         Assert.assertEquals("Invalid amount of XSDs loaded", 1, schemas.length);
109     }
110 
111     @Test
112     public void testCreateValidator() throws Exception {
113         Resource a = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
114         collection.setXsds(new Resource[]{a});
115         collection.setInline(true);
116         collection.afterPropertiesSet();
117 
118         XmlValidator validator = collection.createValidator();
119         Assert.assertNotNull("No XmlValidator returned", validator);
120     }
121 
122     @Test
123     public void testInvalidSchema() throws Exception {
124         Resource invalid = new ClassPathResource("invalid.xsd", AbstractXsdSchemaTestCase.class);
125         collection.setXsds(new Resource[]{invalid});
126         try {
127             collection.afterPropertiesSet();
128             Assert.fail("CommonsXsdSchemaException expected");
129         }
130         catch (CommonsXsdSchemaException ex) {
131             // expected
132         }
133     }
134 
135     @Test
136     public void testIncludesAndImports() throws Exception {
137         Resource hr = new ClassPathResource("hr.xsd", getClass());
138         collection.setXsds(new Resource[]{hr});
139         collection.setInline(true);
140         collection.afterPropertiesSet();
141 
142         XsdSchema[] schemas = collection.getXsdSchemas();
143         Assert.assertEquals("Invalid amount of XSDs loaded", 2, schemas.length);
144 
145         Assert.assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas", schemas[0].getTargetNamespace());
146         Resource hr_employee = new ClassPathResource("hr_employee.xsd", getClass());
147         Document expected = documentBuilder.parse(SaxUtils.createInputSource(hr_employee));
148         DOMResult domResult = new DOMResult();
149         transformer.transform(schemas[0].getSource(), domResult);
150         assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
151 
152         Assert.assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas/holiday", schemas[1].getTargetNamespace());
153         Resource holiday = new ClassPathResource("holiday.xsd", getClass());
154         expected = documentBuilder.parse(SaxUtils.createInputSource(holiday));
155         domResult = new DOMResult();
156         transformer.transform(schemas[1].getSource(), domResult);
157         assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
158 
159     }
160 }