KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestNodeTypeName


1 /*
2  * Copyright 2001 (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.2 2003/11/02 18:31:28 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.*;
16 import org.dom4j.io.SAXReader;
17
18 import java.util.Iterator JavaDoc;
19
20 /** Tests the getNodeNameType() method
21   *
22   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
23   * @version $Revision: 1.2 $
24   */

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

173
Popular Tags