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