1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 junit.framework.TestCase;
27 import org.springframework.core.io.ClassPathResource;
28 import org.springframework.core.io.Resource;
29 import org.springframework.xml.transform.ResourceSource;
30 import org.w3c.dom.Document;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXParseException;
33
34 public abstract class AbstractValidatorFactoryTestCase extends TestCase {
35
36 private XmlValidator validator;
37
38 private InputStream validInputStream;
39
40 private InputStream invalidInputStream;
41
42 protected void setUp() throws Exception {
43 Resource[] schemaResource =
44 new Resource[]{new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class)};
45 validator = createValidator(schemaResource, XmlValidatorFactory.SCHEMA_W3C_XML);
46 validInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("validDocument.xml");
47 invalidInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("invalidDocument.xml");
48 }
49
50 protected void tearDown() throws Exception {
51 validInputStream.close();
52 invalidInputStream.close();
53 }
54
55 protected abstract XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws Exception;
56
57 public void testHandleValidMessageStream() throws Exception {
58 SAXParseException[] errors = validator.validate(new StreamSource(validInputStream));
59 assertNotNull("Null returned for errors", errors);
60 assertEquals("ValidationErrors returned", 0, errors.length);
61 }
62
63 public void testValidateTwice() throws Exception {
64 validator.validate(new StreamSource(validInputStream));
65 validInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("validDocument.xml");
66 validator.validate(new StreamSource(validInputStream));
67 }
68
69 public void testHandleInvalidMessageStream() throws Exception {
70 SAXParseException[] errors = validator.validate(new StreamSource(invalidInputStream));
71 assertNotNull("Null returned for errors", errors);
72 assertEquals("ValidationErrors returned", 3, errors.length);
73 }
74
75 public void testHandleValidMessageSax() throws Exception {
76 SAXParseException[] errors = validator.validate(new SAXSource(new InputSource(validInputStream)));
77 assertNotNull("Null returned for errors", errors);
78 assertEquals("ValidationErrors returned", 0, errors.length);
79 }
80
81 public void testHandleInvalidMessageSax() throws Exception {
82 SAXParseException[] errors = validator.validate(new SAXSource(new InputSource(invalidInputStream)));
83 assertNotNull("Null returned for errors", errors);
84 assertEquals("ValidationErrors returned", 3, errors.length);
85 }
86
87 public void testHandleValidMessageDom() throws Exception {
88 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
89 documentBuilderFactory.setNamespaceAware(true);
90 Document document = documentBuilderFactory.newDocumentBuilder()
91 .parse(new InputSource(validInputStream));
92 SAXParseException[] errors = validator.validate(new DOMSource(document));
93 assertNotNull("Null returned for errors", errors);
94 assertEquals("ValidationErrors returned", 0, errors.length);
95 }
96
97 public void testHandleInvalidMessageDom() throws Exception {
98 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
99 documentBuilderFactory.setNamespaceAware(true);
100 Document document = documentBuilderFactory.newDocumentBuilder()
101 .parse(new InputSource(invalidInputStream));
102 SAXParseException[] errors = validator.validate(new DOMSource(document));
103 assertNotNull("Null returned for errors", errors);
104 assertEquals("ValidationErrors returned", 3, errors.length);
105 }
106
107 public void testMultipleSchemasValidMessage() throws Exception {
108 Resource[] schemaResources = new Resource[]{
109 new ClassPathResource("multipleSchemas1.xsd", AbstractValidatorFactoryTestCase.class),
110 new ClassPathResource("multipleSchemas2.xsd", AbstractValidatorFactoryTestCase.class)};
111 validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
112
113 Source document = new ResourceSource(
114 new ClassPathResource("multipleSchemas1.xml", AbstractValidatorFactoryTestCase.class));
115 SAXParseException[] errors = validator.validate(document);
116 assertEquals("ValidationErrors returned", 0, errors.length);
117 validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
118 document = new ResourceSource(
119 new ClassPathResource("multipleSchemas2.xml", AbstractValidatorFactoryTestCase.class));
120 errors = validator.validate(document);
121 assertEquals("ValidationErrors returned", 0, errors.length);
122 }
123
124 }