001: /*
002: ******************************************************************
003: Copyright (c) 200, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit;
038:
039: import java.io.File;
040: import java.io.FileInputStream;
041: import java.io.FileReader;
042:
043: import org.xml.sax.InputSource;
044:
045: import junit.framework.TestCase;
046:
047: public class test_JAXP_1_2_Schema_Validation extends TestCase {
048:
049: private Validator validator;
050:
051: public void testUsingStringURI() throws Exception {
052: File xsdFile = new File(test_Constants.BASEDIR
053: + "/tests/etc/Book.xsd");
054: assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists",
055: xsdFile.exists());
056:
057: File xmlFile = new File(test_Constants.BASEDIR
058: + "/tests/etc/BookXsdGeneratedNoSchema.xml");
059: assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists",
060: xmlFile.exists());
061:
062: validator = new Validator(new FileReader(xmlFile));
063:
064: validator.useXMLSchema(true);
065: validator.setJAXP12SchemaSource(xsdFile.getAbsolutePath());
066:
067: validator.assertIsValid();
068: }
069:
070: public void testUsingInputStream() throws Exception {
071: File xsdFile = new File(test_Constants.BASEDIR
072: + "/tests/etc/Book.xsd");
073: assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists",
074: xsdFile.exists());
075:
076: File xmlFile = new File(test_Constants.BASEDIR
077: + "/tests/etc/BookXsdGeneratedNoSchema.xml");
078: assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists",
079: xmlFile.exists());
080:
081: validator = new Validator(new FileReader(xmlFile));
082:
083: validator.useXMLSchema(true);
084: validator.setJAXP12SchemaSource(new FileInputStream(xsdFile));
085:
086: validator.assertIsValid();
087: }
088:
089: public void testUsingInputSource() throws Exception {
090: File xsdFile = new File(test_Constants.BASEDIR
091: + "/tests/etc/Book.xsd");
092: assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists",
093: xsdFile.exists());
094:
095: File xmlFile = new File(test_Constants.BASEDIR
096: + "/tests/etc/BookXsdGeneratedNoSchema.xml");
097: assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists",
098: xmlFile.exists());
099:
100: validator = new Validator(new FileReader(xmlFile));
101:
102: validator.useXMLSchema(true);
103: validator.setJAXP12SchemaSource(new InputSource(new FileReader(
104: xsdFile)));
105:
106: validator.assertIsValid();
107: }
108:
109: public void testUsingAFile() throws Exception {
110: File xsdFile = new File(test_Constants.BASEDIR
111: + "/tests/etc/Book.xsd");
112: assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists",
113: xsdFile.exists());
114:
115: File xmlFile = new File(test_Constants.BASEDIR
116: + "/tests/etc/BookXsdGeneratedNoSchema.xml");
117: assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists",
118: xmlFile.exists());
119:
120: validator = new Validator(new FileReader(xmlFile));
121:
122: validator.useXMLSchema(true);
123: validator.setJAXP12SchemaSource(xsdFile);
124:
125: validator.assertIsValid();
126: }
127:
128: public void testUsingObjectArrayContainingStringURI()
129: throws Exception {
130: File xsdFile = new File(test_Constants.BASEDIR
131: + "/tests/etc/Book.xsd");
132: assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists",
133: xsdFile.exists());
134:
135: File xmlFile = new File(test_Constants.BASEDIR
136: + "/tests/etc/BookXsdGeneratedNoSchema.xml");
137: assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists",
138: xmlFile.exists());
139:
140: validator = new Validator(new FileReader(xmlFile));
141:
142: validator.useXMLSchema(true);
143: validator.setJAXP12SchemaSource(new Object[] { xsdFile
144: .getAbsolutePath() });
145:
146: validator.assertIsValid();
147: }
148:
149: public void testUsingNonExistentFile() throws Exception {
150: File xsdFile = new File(test_Constants.BASEDIR
151: + "/tests/etc/BookDoesNotExist.xsd");
152: assertFalse("xsdFile " + xsdFile.getAbsolutePath() + " exists",
153: xsdFile.exists());
154:
155: File xmlFile = new File(test_Constants.BASEDIR
156: + "/tests/etc/BookXsdGeneratedNoSchema.xml");
157: assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists",
158: xmlFile.exists());
159:
160: validator = new Validator(new FileReader(xmlFile));
161:
162: validator.useXMLSchema(true);
163: validator.setJAXP12SchemaSource(xsdFile);
164:
165: assertFalse("Isn't valid since no schema can be found",
166: validator.isValid());
167: }
168:
169: public void testUsingInvalidXML() throws Exception {
170: File xsdFile = new File(test_Constants.BASEDIR
171: + "/tests/etc/Book.xsd");
172: assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists",
173: xsdFile.exists());
174:
175: File xmlFile = new File(test_Constants.BASEDIR
176: + "/tests/etc/InvalidBookXsdGeneratedNoSchema.xml");
177: assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists",
178: xmlFile.exists());
179:
180: validator = new Validator(new FileReader(xmlFile));
181:
182: validator.useXMLSchema(true);
183: validator.setJAXP12SchemaSource(xsdFile);
184:
185: assertFalse("Isn't valid since no schema can be found",
186: validator.isValid());
187: }
188: }
|