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