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 junit.framework.AssertionFailedError;
040: import junit.framework.TestSuite;
041: import org.w3c.dom.Document;
042: import org.xml.sax.InputSource;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.FileReader;
047: import java.io.FileWriter;
048: import java.io.StringBufferInputStream;
049: import java.io.StringReader;
050:
051: /**
052: * JUnit test for Validator
053: * Also includes tests for XMLTestCase <code>assertValidXML</code> methods
054: * because test values etc are here
055: */
056: public class test_Validator extends XMLTestCase {
057: private Validator validator;
058: private File tempDTDFile;
059:
060: public void testXSchema() throws Exception {
061: File xsdFile = new File(test_Constants.BASEDIR
062: + "/tests/etc/Book.xsd");
063: assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists",
064: xsdFile.exists());
065:
066: File xmlFile = new File(test_Constants.BASEDIR
067: + "/tests/etc/BookXsdGenerated.xml");
068: assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists",
069: xmlFile.exists());
070: validator = new Validator(new InputSource(new FileInputStream(
071: xmlFile)));
072:
073: validator.useXMLSchema(true);
074: assertTrue("Schema " + validator.toString(), validator
075: .isValid());
076: }
077:
078: public void testIsValidGood() throws Exception {
079: String toonXML = test_Constants.XML_DECLARATION
080: + test_Constants.CHUCK_JONES_RIP_DTD_DECL
081: + test_Constants.CHUCK_JONES_RIP_XML;
082: validator = new Validator(new StringReader(toonXML));
083: assertTrue("toonXML " + validator.toString(), validator
084: .isValid());
085: // test XMLTestCase
086: passXMLTestCaseTest(toonXML);
087: passXMLTestCaseTest(validator);
088:
089: String noXMLDeclaration = test_Constants.CHUCK_JONES_RIP_DTD_DECL
090: + test_Constants.CHUCK_JONES_RIP_XML;
091: validator = new Validator(new StringReader(noXMLDeclaration));
092: assertEquals("noXMLDeclaration " + validator.toString(), true,
093: validator.isValid());
094: // test XMLTestCase
095: passXMLTestCaseTest(noXMLDeclaration);
096: passXMLTestCaseTest(validator);
097: }
098:
099: public void testIsValidExternalSystemId() throws Exception {
100: writeTempDTDFile();
101: assertTrue(tempDTDFile.getAbsolutePath(), tempDTDFile.exists());
102:
103: String externalDTD = test_Constants.XML_DECLARATION
104: + test_Constants.DOCUMENT_WITH_GOOD_EXTERNAL_DTD;
105: String tempDTDUrl = tempDTDFile.toURL().toExternalForm();
106: validator = new Validator(new StringReader(externalDTD),
107: tempDTDUrl);
108:
109: assertTrue("externalDTD " + validator.toString(), validator
110: .isValid());
111: // test XMLTestCase
112: passXMLTestCaseTest(externalDTD, tempDTDFile.toURL()
113: .toExternalForm());
114: passXMLTestCaseTest(validator);
115:
116: String noDTD = test_Constants.XML_DECLARATION
117: + test_Constants.DOCUMENT_WITH_NO_EXTERNAL_DTD;
118: validator = new Validator(new StringReader(noDTD), tempDTDFile
119: .toURL().toExternalForm());
120:
121: assertFalse("noDTD " + validator.toString(), validator
122: .isValid());
123: // test XMLTestCase
124: failXMLTestCaseTest(noDTD, tempDTDFile.toURL().toExternalForm());
125: failXMLTestCaseTest(validator);
126: }
127:
128: public void testIsValidNoDTD() throws Exception {
129: writeTempDTDFile();
130: assertTrue(tempDTDFile.getAbsolutePath(), tempDTDFile.exists());
131:
132: String noDTD = test_Constants.CHUCK_JONES_RIP_XML;
133: String systemid = tempDTDFile.toURL().toExternalForm();
134: String doctype = "cartoons";
135: String notDoctype = "anima";
136: validator = new Validator(new StringReader(noDTD), systemid,
137: doctype);
138: assertTrue(validator.toString(), validator.isValid());
139: // test XMLTestCase
140: passXMLTestCaseTest(noDTD, systemid, doctype);
141: passXMLTestCaseTest(validator);
142: // and Document constructor
143: Document document = getDocument(noDTD);
144: validator = new Validator(document, systemid, doctype);
145: assertTrue("Document " + validator.toString(), validator
146: .isValid());
147:
148: validator = new Validator(new StringReader(noDTD), systemid,
149: notDoctype);
150: assertFalse(validator.toString(), validator.isValid());
151: // test XMLTestCase
152: failXMLTestCaseTest(noDTD, systemid, notDoctype);
153: failXMLTestCaseTest(validator);
154: // and Document constructor
155: validator = new Validator(document, systemid, notDoctype);
156: assertFalse("Document " + validator.toString(), validator
157: .isValid());
158: }
159:
160: public void testIsValidBad() throws Exception {
161: String noDTD = test_Constants.XML_DECLARATION
162: + test_Constants.CHUCK_JONES_RIP_XML;
163: validator = new Validator(new StringReader(noDTD));
164: assertFalse("noDTD " + validator.toString(), validator
165: .isValid());
166: // test XMLTestCase
167: failXMLTestCaseTest(noDTD);
168: failXMLTestCaseTest(validator);
169:
170: String dtdTwice = test_Constants.XML_DECLARATION
171: + test_Constants.CHUCK_JONES_RIP_DTD_DECL
172: + test_Constants.CHUCK_JONES_RIP_DTD_DECL
173: + test_Constants.CHUCK_JONES_RIP_XML;
174: validator = new Validator(new StringReader(dtdTwice));
175: assertFalse("dtdTwice " + validator.toString(), validator
176: .isValid());
177: // test XMLTestCase
178: failXMLTestCaseTest(dtdTwice);
179: failXMLTestCaseTest(validator);
180:
181: String invalidXML = test_Constants.XML_DECLARATION
182: + test_Constants.CHUCK_JONES_RIP_DTD_DECL
183: + test_Constants.CHUCK_JONES_SPINNING_IN_HIS_GRAVE_XML;
184: validator = new Validator(new StringReader(invalidXML));
185: assertFalse("invalidXML " + validator.toString(), validator
186: .isValid());
187: // test XMLTestCase
188: failXMLTestCaseTest(invalidXML);
189: failXMLTestCaseTest(validator);
190: }
191:
192: private Document getDocument(String fromXML) throws Exception {
193: return XMLUnit.buildControlDocument(fromXML);
194: }
195:
196: private void removeTempDTDFile() throws Exception {
197: if (tempDTDFile.exists()) {
198: tempDTDFile.delete();
199: }
200: }
201:
202: private void writeTempDTDFile() throws Exception {
203: FileWriter writer = new FileWriter(tempDTDFile);
204: writer.write(test_Constants.CHUCK_JONES_RIP_DTD);
205: writer.close();
206: }
207:
208: public void setUp() throws Exception {
209: tempDTDFile = new File(test_Constants.EXTERNAL_DTD);
210: removeTempDTDFile();
211: }
212:
213: public void tearDown() throws Exception {
214: removeTempDTDFile();
215: }
216:
217: // ---- XMLTestCase methods ----
218: private void passXMLTestCaseTest(String xml) throws Exception {
219: assertXMLValid(xml);
220: assertXMLValid(new InputSource(new StringReader(xml)));
221: assertXMLValid(new InputSource(new StringBufferInputStream(xml)));
222: }
223:
224: private void passXMLTestCaseTest(String xml, String systemId)
225: throws Exception {
226: assertXMLValid(xml, systemId);
227: assertXMLValid(new InputSource(new StringReader(xml)), systemId);
228: assertXMLValid(
229: new InputSource(new StringBufferInputStream(xml)),
230: systemId);
231: }
232:
233: private void passXMLTestCaseTest(String xml, String systemId,
234: String doctype) throws Exception {
235: assertXMLValid(xml, systemId, doctype);
236: assertXMLValid(new InputSource(new StringReader(xml)),
237: systemId, doctype);
238: assertXMLValid(
239: new InputSource(new StringBufferInputStream(xml)),
240: systemId, doctype);
241: }
242:
243: private void passXMLTestCaseTest(Validator validator)
244: throws Exception {
245: assertXMLValid(validator);
246: }
247:
248: private void failXMLTestCaseTest(String xml, String systemId)
249: throws Exception {
250: try {
251: assertXMLValid(xml, systemId);
252: fail("Expected assertion to fail!");
253: } catch (AssertionFailedError e) {
254: // expecting this
255: }
256: try {
257: assertXMLValid(new InputSource(new StringReader(xml)),
258: systemId);
259: fail("Expected assertion to fail!");
260: } catch (AssertionFailedError e) {
261: // expecting this
262: }
263: try {
264: assertXMLValid(new InputSource(new StringBufferInputStream(
265: xml)), systemId);
266: fail("Expected assertion to fail!");
267: } catch (AssertionFailedError e) {
268: // expecting this
269: }
270: }
271:
272: private void failXMLTestCaseTest(String xml) throws Exception {
273: try {
274: assertXMLValid(xml);
275: fail("Expected assertion to fail!");
276: } catch (AssertionFailedError e) {
277: // expecting this
278: }
279: try {
280: assertXMLValid(new InputSource(new StringReader(xml)));
281: fail("Expected assertion to fail!");
282: } catch (AssertionFailedError e) {
283: // expecting this
284: }
285: try {
286: assertXMLValid(new InputSource(new StringBufferInputStream(
287: xml)));
288: fail("Expected assertion to fail!");
289: } catch (AssertionFailedError e) {
290: // expecting this
291: }
292: }
293:
294: private void failXMLTestCaseTest(String xml, String systemId,
295: String doctype) throws Exception {
296: try {
297: assertXMLValid(xml, systemId, doctype);
298: fail("Expected assertion to fail!");
299: } catch (AssertionFailedError e) {
300: // expecting this
301: }
302: try {
303: assertXMLValid(new InputSource(new StringReader(xml)),
304: systemId, doctype);
305: fail("Expected assertion to fail!");
306: } catch (AssertionFailedError e) {
307: // expecting this
308: }
309: try {
310: assertXMLValid(new InputSource(new StringBufferInputStream(
311: xml)), systemId, doctype);
312: fail("Expected assertion to fail!");
313: } catch (AssertionFailedError e) {
314: // expecting this
315: }
316: }
317:
318: private void failXMLTestCaseTest(Validator validator)
319: throws Exception {
320: try {
321: assertXMLValid(validator);
322: fail("Expected assertion to fail!");
323: } catch (AssertionFailedError e) {
324: // expecting this
325: }
326: }
327:
328: public test_Validator(String name) {
329: super (name);
330: }
331:
332: public static TestSuite suite() {
333: return new TestSuite(test_Validator.class);
334: }
335:
336: }
|