001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.validation;
018:
019: import java.io.InputStream;
020: import javax.xml.parsers.DocumentBuilderFactory;
021: import javax.xml.transform.dom.DOMSource;
022: import javax.xml.transform.sax.SAXSource;
023: import javax.xml.transform.stream.StreamSource;
024:
025: import junit.framework.TestCase;
026: import org.springframework.core.io.ClassPathResource;
027: import org.springframework.core.io.Resource;
028: import org.w3c.dom.Document;
029: import org.xml.sax.InputSource;
030: import org.xml.sax.SAXParseException;
031:
032: public abstract class AbstractValidatorFactoryTestCase extends TestCase {
033:
034: private XmlValidator validator;
035:
036: private InputStream validInputStream;
037:
038: private InputStream invalidInputStream;
039:
040: protected void setUp() throws Exception {
041: Resource[] schemaResource = new Resource[] { new ClassPathResource(
042: "schema.xsd", AbstractValidatorFactoryTestCase.class) };
043: validator = createValidator(schemaResource,
044: XmlValidatorFactory.SCHEMA_W3C_XML);
045: validInputStream = AbstractValidatorFactoryTestCase.class
046: .getResourceAsStream("validDocument.xml");
047: invalidInputStream = AbstractValidatorFactoryTestCase.class
048: .getResourceAsStream("invalidDocument.xml");
049: }
050:
051: protected void tearDown() throws Exception {
052: validInputStream.close();
053: invalidInputStream.close();
054: }
055:
056: protected abstract XmlValidator createValidator(
057: Resource[] schemaResources, String schemaLanguage)
058: throws Exception;
059:
060: public void testHandleValidMessageStream() throws Exception {
061: SAXParseException[] errors = validator
062: .validate(new StreamSource(validInputStream));
063: assertNotNull("Null returned for errors", errors);
064: assertEquals("ValidationErrors returned", 0, errors.length);
065: }
066:
067: public void testValidateTwice() throws Exception {
068: validator.validate(new StreamSource(validInputStream));
069: validInputStream = AbstractValidatorFactoryTestCase.class
070: .getResourceAsStream("validDocument.xml");
071: validator.validate(new StreamSource(validInputStream));
072: }
073:
074: public void testHandleInvalidMessageStream() throws Exception {
075: SAXParseException[] errors = validator
076: .validate(new StreamSource(invalidInputStream));
077: assertNotNull("Null returned for errors", errors);
078: assertEquals("ValidationErrors returned", 3, errors.length);
079: }
080:
081: public void testHandleValidMessageSax() throws Exception {
082: SAXParseException[] errors = validator.validate(new SAXSource(
083: new InputSource(validInputStream)));
084: assertNotNull("Null returned for errors", errors);
085: assertEquals("ValidationErrors returned", 0, errors.length);
086: }
087:
088: public void testHandleInvalidMessageSax() throws Exception {
089: SAXParseException[] errors = validator.validate(new SAXSource(
090: new InputSource(invalidInputStream)));
091: assertNotNull("Null returned for errors", errors);
092: assertEquals("ValidationErrors returned", 3, errors.length);
093: }
094:
095: public void testHandleValidMessageDom() throws Exception {
096: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
097: .newInstance();
098: documentBuilderFactory.setNamespaceAware(true);
099: Document document = documentBuilderFactory.newDocumentBuilder()
100: .parse(new InputSource(validInputStream));
101: SAXParseException[] errors = validator.validate(new DOMSource(
102: document));
103: assertNotNull("Null returned for errors", errors);
104: assertEquals("ValidationErrors returned", 0, errors.length);
105: }
106:
107: public void testHandleInvalidMessageDom() throws Exception {
108: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
109: .newInstance();
110: documentBuilderFactory.setNamespaceAware(true);
111: Document document = documentBuilderFactory.newDocumentBuilder()
112: .parse(new InputSource(invalidInputStream));
113: SAXParseException[] errors = validator.validate(new DOMSource(
114: document));
115: assertNotNull("Null returned for errors", errors);
116: assertEquals("ValidationErrors returned", 3, errors.length);
117: }
118:
119: }
|