01: /*
02: ******************************************************************
03: Copyright (c) 200, Jeff Martin, Tim Bacon
04: All rights reserved.
05:
06: Redistribution and use in source and binary forms, with or without
07: modification, are permitted provided that the following conditions
08: are met:
09:
10: * Redistributions of source code must retain the above copyright
11: notice, this list of conditions and the following disclaimer.
12: * Redistributions in binary form must reproduce the above
13: copyright notice, this list of conditions and the following
14: disclaimer in the documentation and/or other materials provided
15: with the distribution.
16: * Neither the name of the xmlunit.sourceforge.net nor the names
17: of its contributors may be used to endorse or promote products
18: derived from this software without specific prior written
19: permission.
20:
21: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32: POSSIBILITY OF SUCH DAMAGE.
33:
34: ******************************************************************
35: */
36:
37: package org.custommonkey.xmlunit;
38:
39: import junit.framework.TestSuite;
40:
41: import org.w3c.dom.Document;
42:
43: /**
44: * JUnit test for HTMLDocumentBuilder
45: */
46: public class test_HTMLDocumentBuilder extends XMLTestCase {
47: private static final String xHtml = "<html><head><title>test</title></head>"
48: + "<body><h1>hello</h1><p>world</p><hr/><div><img src=\"foo.bar\"/>"
49: + "<ul><li>one</li><li>two</li></ul></div></body></html>";
50: private Document xHtmlDocument;
51: private HTMLDocumentBuilder parser;
52: private TolerantSaxDocumentBuilder builder;
53:
54: public void testParseGoodHtml() throws Exception {
55: assertParsedDocumentEqual(xHtmlDocument, xHtml);
56: assertEquals(parser.getTrace(), -1, parser.getTrace().indexOf(
57: "WARNING"));
58: }
59:
60: public void testParseOldHtml() throws Exception {
61: String oldHTML = "<html><head><title>test</title></head>"
62: + "<body><h1>hello</h1><p>world<hr><div><img src=\"foo.bar\">"
63: + "<ul><li>one<li>two</ul></div></body></html>";
64: assertParsedDocumentEqual(xHtmlDocument, oldHTML);
65: assertEquals(parser.getTrace(), -1, parser.getTrace().indexOf(
66: "WARNING"));
67: }
68:
69: public void testParsePoorHtml() throws Exception {
70: String poorHTML = "<html><head><title>test</title></head>"
71: + "<body><h1>hello</h1><p>world<hr><div><img src=\"foo.bar\">"
72: + "<ul><li>one<li>two";
73: assertParsedDocumentEqual(xHtmlDocument, poorHTML);
74: assertEquals(parser.getTrace(), -1, parser.getTrace().indexOf(
75: "WARNING"));
76: }
77:
78: private void assertParsedDocumentEqual(Document control, String test)
79: throws Exception {
80: assertXMLEqual(control, parser.parse(test));
81: }
82:
83: public test_HTMLDocumentBuilder(String name) {
84: super (name);
85: }
86:
87: public void setUp() throws Exception {
88: xHtmlDocument = XMLUnit.buildControlDocument(xHtml);
89: builder = new TolerantSaxDocumentBuilder(XMLUnit
90: .newTestParser());
91: parser = new HTMLDocumentBuilder(builder);
92:
93: }
94:
95: public static TestSuite suite() {
96: return new TestSuite(test_HTMLDocumentBuilder.class);
97: }
98: }
|