1   /*
2    * Copyright 2005-2011 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.validation;
18  
19  import java.io.InputStream;
20  import javax.xml.parsers.DocumentBuilderFactory;
21  import javax.xml.transform.Source;
22  import javax.xml.transform.dom.DOMSource;
23  import javax.xml.transform.sax.SAXSource;
24  import javax.xml.transform.stream.StreamSource;
25  
26  import org.springframework.core.io.ClassPathResource;
27  import org.springframework.core.io.Resource;
28  import org.springframework.xml.transform.ResourceSource;
29  
30  import org.junit.After;
31  import org.junit.Assert;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.w3c.dom.Document;
35  import org.xml.sax.InputSource;
36  import org.xml.sax.SAXException;
37  import org.xml.sax.SAXParseException;
38  
39  public abstract class AbstractValidatorFactoryTestCase {
40  
41      private XmlValidator validator;
42  
43      private InputStream validInputStream;
44  
45      private InputStream invalidInputStream;
46  
47      @Before
48      public void setUp() throws Exception {
49          Resource[] schemaResource =
50                  new Resource[]{new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class)};
51          validator = createValidator(schemaResource, XmlValidatorFactory.SCHEMA_W3C_XML);
52          validInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("validDocument.xml");
53          invalidInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("invalidDocument.xml");
54      }
55  
56      @After
57      public void tearDown() throws Exception {
58          validInputStream.close();
59          invalidInputStream.close();
60      }
61  
62      protected abstract XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws Exception;
63  
64      @Test
65      public void testHandleValidMessageStream() throws Exception {
66          SAXParseException[] errors = validator.validate(new StreamSource(validInputStream));
67          Assert.assertNotNull("Null returned for errors", errors);
68          Assert.assertEquals("ValidationErrors returned", 0, errors.length);
69      }
70  
71      @Test
72      public void testValidateTwice() throws Exception {
73          validator.validate(new StreamSource(validInputStream));
74          validInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("validDocument.xml");
75          validator.validate(new StreamSource(validInputStream));
76      }
77  
78      @Test
79      public void testHandleInvalidMessageStream() throws Exception {
80          SAXParseException[] errors = validator.validate(new StreamSource(invalidInputStream));
81          Assert.assertNotNull("Null returned for errors", errors);
82          Assert.assertEquals("ValidationErrors returned", 3, errors.length);
83      }
84  
85      @Test
86      public void testHandleValidMessageSax() throws Exception {
87          SAXParseException[] errors = validator.validate(new SAXSource(new InputSource(validInputStream)));
88          Assert.assertNotNull("Null returned for errors", errors);
89          Assert.assertEquals("ValidationErrors returned", 0, errors.length);
90      }
91  
92      @Test
93      public void testHandleInvalidMessageSax() throws Exception {
94          SAXParseException[] errors = validator.validate(new SAXSource(new InputSource(invalidInputStream)));
95          Assert.assertNotNull("Null returned for errors", errors);
96          Assert.assertEquals("ValidationErrors returned", 3, errors.length);
97      }
98  
99      @Test
100     public void testHandleValidMessageDom() throws Exception {
101         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
102         documentBuilderFactory.setNamespaceAware(true);
103         Document document = documentBuilderFactory.newDocumentBuilder()
104                 .parse(new InputSource(validInputStream));
105         SAXParseException[] errors = validator.validate(new DOMSource(document));
106         Assert.assertNotNull("Null returned for errors", errors);
107         Assert.assertEquals("ValidationErrors returned", 0, errors.length);
108     }
109 
110     @Test
111     public void testHandleInvalidMessageDom() throws Exception {
112         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
113         documentBuilderFactory.setNamespaceAware(true);
114         Document document = documentBuilderFactory.newDocumentBuilder()
115                 .parse(new InputSource(invalidInputStream));
116         SAXParseException[] errors = validator.validate(new DOMSource(document));
117         Assert.assertNotNull("Null returned for errors", errors);
118         Assert.assertEquals("ValidationErrors returned", 3, errors.length);
119     }
120 
121     @Test
122     public void testMultipleSchemasValidMessage() throws Exception {
123         Resource[] schemaResources = new Resource[]{
124                 new ClassPathResource("multipleSchemas1.xsd", AbstractValidatorFactoryTestCase.class),
125                 new ClassPathResource("multipleSchemas2.xsd", AbstractValidatorFactoryTestCase.class)};
126         validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
127 
128         Source document = new ResourceSource(
129                 new ClassPathResource("multipleSchemas1.xml", AbstractValidatorFactoryTestCase.class));
130         SAXParseException[] errors = validator.validate(document);
131         Assert.assertEquals("ValidationErrors returned", 0, errors.length);
132         validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
133         document = new ResourceSource(
134                 new ClassPathResource("multipleSchemas2.xml", AbstractValidatorFactoryTestCase.class));
135         errors = validator.validate(document);
136         Assert.assertEquals("ValidationErrors returned", 0, errors.length);
137     }
138 
139     @Test
140     public void customErrorHandler() throws Exception {
141         ValidationErrorHandler myHandler = new ValidationErrorHandler() {
142             public SAXParseException[] getErrors() {
143                 return new SAXParseException[0];
144             }
145 
146             public void warning(SAXParseException exception) throws SAXException {
147             }
148 
149             public void error(SAXParseException exception) throws SAXException {
150             }
151 
152             public void fatalError(SAXParseException exception) throws SAXException {
153             }
154         };
155         SAXParseException[] errors = validator.validate(new StreamSource(invalidInputStream), myHandler);
156         Assert.assertNotNull("Null returned for errors", errors);
157         Assert.assertEquals("ValidationErrors returned", 0, errors.length);
158     }
159 
160 }