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;
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  
30  import org.custommonkey.xmlunit.XMLUnit;
31  import org.junit.Assert;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.w3c.dom.Document;
35  
36  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
37  
38  public abstract class AbstractXsdSchemaTestCase {
39  
40      private DocumentBuilder documentBuilder;
41  
42      protected Transformer transformer;
43  
44      @Before
45      public final void setUp() throws Exception {
46          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
47          documentBuilderFactory.setNamespaceAware(true);
48          documentBuilder = documentBuilderFactory.newDocumentBuilder();
49          TransformerFactory transformerFactory = TransformerFactory.newInstance();
50          transformer = transformerFactory.newTransformer();
51          XMLUnit.setIgnoreWhitespace(true);
52      }
53  
54      @Test
55      public void testSingle() throws Exception {
56          Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
57          XsdSchema single = createSchema(resource);
58          String namespace = "http://www.springframework.org/spring-ws/single/schema";
59          Assert.assertEquals("Invalid target namespace", namespace, single.getTargetNamespace());
60          resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
61          Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
62          DOMResult domResult = new DOMResult();
63          transformer.transform(single.getSource(), domResult);
64          Document result = (Document) domResult.getNode();
65          assertXMLEqual("Invalid Source returned", expected, result);
66      }
67  
68      @Test
69      public void testIncludes() throws Exception {
70          Resource resource = new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class);
71          XsdSchema including = createSchema(resource);
72          String namespace = "http://www.springframework.org/spring-ws/include/schema";
73          Assert.assertEquals("Invalid target namespace", namespace, including.getTargetNamespace());
74          resource = new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class);
75          Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
76          DOMResult domResult = new DOMResult();
77          transformer.transform(including.getSource(), domResult);
78          Document result = (Document) domResult.getNode();
79          assertXMLEqual("Invalid Source returned", expected, result);
80      }
81  
82      @Test
83      public void testImports() throws Exception {
84          Resource resource = new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class);
85          XsdSchema importing = createSchema(resource);
86          String namespace = "http://www.springframework.org/spring-ws/importing/schema";
87          Assert.assertEquals("Invalid target namespace", namespace, importing.getTargetNamespace());
88          resource = new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class);
89          Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
90          DOMResult domResult = new DOMResult();
91          transformer.transform(importing.getSource(), domResult);
92          Document result = (Document) domResult.getNode();
93          assertXMLEqual("Invalid Source returned", expected, result);
94      }
95  
96      @Test
97      public void testXmlNamespace() throws Exception {
98          Resource resource = new ClassPathResource("xmlNamespace.xsd", AbstractXsdSchemaTestCase.class);
99          XsdSchema importing = createSchema(resource);
100         String namespace = "http://www.springframework.org/spring-ws/xmlNamespace";
101         Assert.assertEquals("Invalid target namespace", namespace, importing.getTargetNamespace());
102         resource = new ClassPathResource("xmlNamespace.xsd", AbstractXsdSchemaTestCase.class);
103         Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
104         DOMResult domResult = new DOMResult();
105         transformer.transform(importing.getSource(), domResult);
106         Document result = (Document) domResult.getNode();
107         assertXMLEqual("Invalid Source returned", expected, result);
108     }
109 
110     @Test
111     public void testCreateValidator() throws Exception {
112         Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
113         XsdSchema single = createSchema(resource);
114         XmlValidator validator = single.createValidator();
115         Assert.assertNotNull("No XmlValidator returned", validator);
116     }
117 
118     protected abstract XsdSchema createSchema(Resource resource) throws Exception;
119 }