1 /*
2 * Copyright 2001-2004 (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 * $Id: TestNodeTypeName.java,v 1.7 2004/06/25 08:03:47 maartenc Exp $
8 */
9
10 package org.dom4j;
11
12 import java.net.URL;
13 import java.util.Iterator;
14
15 import junit.framework.Test;
16 import junit.framework.TestSuite;
17 import junit.textui.TestRunner;
18
19 import org.dom4j.io.SAXReader;
20
21 /*** Tests the getNodeNameType() method
22 *
23 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
24 * @version $Revision: 1.7 $
25 */
26 public class TestNodeTypeName extends AbstractTestCase {
27
28 public static void main( String[] args ) {
29 TestRunner.run( suite() );
30 }
31
32 public static Test suite() {
33 return new TestSuite( TestNodeTypeName.class );
34 }
35
36 public TestNodeTypeName(String name) {
37 super(name);
38 }
39
40 // Test case(s)
41 //-------------------------------------------------------------------------
42 public void testDocument() throws Exception {
43 testDocument(document);
44 }
45
46 public void testCDATA() throws Exception {
47 testDocument( "/xml/cdata.xml" );
48 }
49
50 public void testNamespaces() throws Exception {
51 testDocument( "/xml/namespaces.xml" );
52 testDocument( "/xml/testNamespaces.xml" );
53 }
54 public void testPI() throws Exception {
55 testDocument( "/xml/testPI.xml" );
56 }
57
58 public void testInline() throws Exception {
59 testDocument( "/xml/inline.xml" );
60 }
61
62 // Implementation methods
63 //-------------------------------------------------------------------------
64 protected void testDocument(String fileName) throws Exception {
65 SAXReader reader = new SAXReader();
66 URL url = getClass().getResource(fileName);
67 Document document = reader.read(url);
68 testDocument(document);
69 }
70
71 protected void testDocument(Document document) throws Exception {
72 assertEquals( document.getNodeTypeName(), "Document" );
73 DocumentType docType = document.getDocType();
74 if ( docType != null ) {
75 assertEquals( docType.getNodeTypeName(), "DocumentType" );
76 }
77
78 testElement( document.getRootElement() );
79 }
80
81 protected void testElement( Element element ) {
82 assertEquals( element.getNodeTypeName(), "Element" );
83
84 for ( Iterator iter = element.attributeIterator(); iter.hasNext(); ) {
85 Attribute attribute = (Attribute) iter.next();
86 assertEquals( attribute.getNodeTypeName(), "Attribute" );
87 }
88
89 for ( Iterator iter = element.nodeIterator(); iter.hasNext(); ) {
90 Node node = (Node) iter.next();
91 String nodeTypeName = node.getNodeTypeName();
92
93 if ( node instanceof Attribute ) {
94 assertEquals( nodeTypeName, "Attribute" );
95 }
96 else if ( node instanceof CDATA ) {
97 assertEquals( nodeTypeName, "CDATA" );
98 }
99 else if ( node instanceof Comment ) {
100 assertEquals( nodeTypeName, "Comment" );
101 }
102 else if ( node instanceof Element ) {
103 assertEquals( nodeTypeName, "Element" );
104 testElement( (Element) node );
105 }
106 else if ( node instanceof Entity ) {
107 assertEquals( nodeTypeName, "Entity" );
108 }
109 else if ( node instanceof Element ) {
110 assertEquals( nodeTypeName, "Element" );
111 }
112 else if ( node instanceof Namespace ) {
113 assertEquals( nodeTypeName, "Namespace" );
114 }
115 else if ( node instanceof ProcessingInstruction ) {
116 assertEquals( nodeTypeName, "ProcessingInstruction" );
117 }
118 else if ( node instanceof Text ) {
119 assertEquals( nodeTypeName, "Text" );
120 }
121 else {
122 assertTrue( "Invalid node type: " + nodeTypeName + " for node: " + node, false );
123 }
124 }
125 }
126 }
127
128
129
130
131 /*
132 * Redistribution and use of this software and associated documentation
133 * ("Software"), with or without modification, are permitted provided
134 * that the following conditions are met:
135 *
136 * 1. Redistributions of source code must retain copyright
137 * statements and notices. Redistributions must also contain a
138 * copy of this document.
139 *
140 * 2. Redistributions in binary form must reproduce the
141 * above copyright notice, this list of conditions and the
142 * following disclaimer in the documentation and/or other
143 * materials provided with the distribution.
144 *
145 * 3. The name "DOM4J" must not be used to endorse or promote
146 * products derived from this Software without prior written
147 * permission of MetaStuff, Ltd. For written permission,
148 * please contact dom4j-info@metastuff.com.
149 *
150 * 4. Products derived from this Software may not be called "DOM4J"
151 * nor may "DOM4J" appear in their names without prior written
152 * permission of MetaStuff, Ltd. DOM4J is a registered
153 * trademark of MetaStuff, Ltd.
154 *
155 * 5. Due credit should be given to the DOM4J Project -
156 * http://www.dom4j.org
157 *
158 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
159 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
160 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
161 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
162 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
163 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
164 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
165 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
166 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
167 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
168 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
169 * OF THE POSSIBILITY OF SUCH DAMAGE.
170 *
171 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
172 *
173 * $Id: TestNodeTypeName.java,v 1.7 2004/06/25 08:03:47 maartenc Exp $
174 */