KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > SAXContentHandlerTest


1 /*
2  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  */

7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.util.List JavaDoc;
13
14 import javax.xml.parsers.SAXParser JavaDoc;
15 import javax.xml.parsers.SAXParserFactory JavaDoc;
16
17 import org.dom4j.io.SAXContentHandler;
18
19 import org.xml.sax.XMLReader JavaDoc;
20
21 public class SAXContentHandlerTest extends AbstractTestCase {
22     private XMLReader JavaDoc xmlReader;
23
24     protected String JavaDoc[] testDocuments = {"/xml/test/test_schema.xml",
25             "/xml/test/encode.xml", "/xml/fibo.xml",
26             "/xml/test/schema/personal-prefix.xsd", "/xml/test/soap2.xml"};
27
28     public static void main(String JavaDoc[] args) {
29         TestRunner.run(SAXContentHandlerTest.class);
30     }
31
32     protected void setUp() throws Exception JavaDoc {
33         super.setUp();
34
35         SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
36         spf.setNamespaceAware(true);
37
38         SAXParser JavaDoc parser = spf.newSAXParser();
39         xmlReader = parser.getXMLReader();
40     }
41
42     public void testSAXContentHandler() throws Exception JavaDoc {
43         SAXContentHandler contentHandler = new SAXContentHandler();
44         xmlReader.setContentHandler(contentHandler);
45         xmlReader.setDTDHandler(contentHandler);
46         xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler",
47                 contentHandler);
48
49         for (int i = 0, size = testDocuments.length; i < size; i++) {
50             Document docFromSAXReader = getDocument(testDocuments[i]);
51
52             xmlReader.parse(getFile(testDocuments[i]).toString());
53
54             Document docFromSAXContentHandler = contentHandler.getDocument();
55
56             docFromSAXContentHandler.setName(docFromSAXReader.getName());
57
58             assertDocumentsEqual(docFromSAXReader, docFromSAXContentHandler);
59             assertEquals(docFromSAXReader.asXML(), docFromSAXContentHandler
60                     .asXML());
61         }
62     }
63
64     public void testBug926713() throws Exception JavaDoc {
65         Document doc = getDocument("/xml/test/cdata.xml");
66         Element foo = doc.getRootElement();
67         Element bar = foo.element("bar");
68         List JavaDoc content = bar.content();
69         assertEquals(3, content.size());
70         assertEquals(Node.TEXT_NODE, ((Node) content.get(0)).getNodeType());
71         assertEquals(Node.CDATA_SECTION_NODE, ((Node) content.get(1))
72                 .getNodeType());
73         assertEquals(Node.TEXT_NODE, ((Node) content.get(2)).getNodeType());
74     }
75 }
76
77 /*
78  * Redistribution and use of this software and associated documentation
79  * ("Software"), with or without modification, are permitted provided that the
80  * following conditions are met:
81  *
82  * 1. Redistributions of source code must retain copyright statements and
83  * notices. Redistributions must also contain a copy of this document.
84  *
85  * 2. Redistributions in binary form must reproduce the above copyright notice,
86  * this list of conditions and the following disclaimer in the documentation
87  * and/or other materials provided with the distribution.
88  *
89  * 3. The name "DOM4J" must not be used to endorse or promote products derived
90  * from this Software without prior written permission of MetaStuff, Ltd. For
91  * written permission, please contact dom4j-info@metastuff.com.
92  *
93  * 4. Products derived from this Software may not be called "DOM4J" nor may
94  * "DOM4J" appear in their names without prior written permission of MetaStuff,
95  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
96  *
97  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
98  *
99  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
100  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
103  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
104  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
105  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
106  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
107  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
108  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
109  * POSSIBILITY OF SUCH DAMAGE.
110  *
111  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
112  */

113
Popular Tags