1   
2   
3   
4   
5   
6   
7   
8   package org.dom4j;
9   
10  import junit.textui.TestRunner;
11  
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.List;
15  import java.util.Map;
16  
17  import org.dom4j.io.SAXReader;
18  
19  /***
20   * A test harness to test the use of Namespaces.
21   * 
22   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
23   * @version $Revision: 1.4 $
24   */
25  public class NamespaceTest extends AbstractTestCase {
26      /*** Input XML file to read */
27      private static final String INPUT_XML_FILE = "/xml/namespaces.xml";
28  
29      /*** Namespace to use in tests */
30      private static final Namespace XSL_NAMESPACE = Namespace.get("xsl",
31              "http://www.w3.org/1999/XSL/Transform");
32  
33      private static final QName XSL_TEMPLATE = QName.get("template",
34              XSL_NAMESPACE);
35  
36      public static void main(String[] args) {
37          TestRunner.run(NamespaceTest.class);
38      }
39  
40      
41      
42      public void debugShowNamespaces() throws Exception {
43          Element root = getRootElement();
44  
45          for (Iterator iter = root.elementIterator(); iter.hasNext();) {
46              Element element = (Element) iter.next();
47  
48              log("Found element:    " + element);
49              log("Namespace:        " + element.getNamespace());
50              log("Namespace prefix: " + element.getNamespacePrefix());
51              log("Namespace URI:    " + element.getNamespaceURI());
52          }
53      }
54  
55      public void testGetElement() throws Exception {
56          Element root = getRootElement();
57  
58          Element firstTemplate = root.element(XSL_TEMPLATE);
59          assertTrue(
60                  "Root element contains at least one <xsl:template/> element",
61                  firstTemplate != null);
62  
63          log("Found element: " + firstTemplate);
64      }
65  
66      public void testGetElements() throws Exception {
67          Element root = getRootElement();
68  
69          List list = root.elements(XSL_TEMPLATE);
70          assertTrue(
71                  "Root element contains at least one <xsl:template/> element",
72                  list.size() > 0);
73  
74          log("Found elements: " + list);
75      }
76  
77      public void testElementIterator() throws Exception {
78          Element root = getRootElement();
79          Iterator iter = root.elementIterator(XSL_TEMPLATE);
80          assertTrue(
81                  "Root element contains at least one <xsl:template/> element",
82                  iter.hasNext());
83  
84          do {
85              Element element = (Element) iter.next();
86              log("Found element: " + element);
87          } while (iter.hasNext());
88      }
89  
90      /***
91       * Tests the use of namespace URI Mapping associated with a DocumentFactory
92       * 
93       * @throws Exception
94       *             DOCUMENT ME!
95       */
96      public void testNamespaceUriMap() throws Exception {
97          
98          Map uris = new HashMap();
99          uris.put("x", "fooNamespace");
100         uris.put("y", "barNamespace");
101 
102         DocumentFactory factory = new DocumentFactory();
103         factory.setXPathNamespaceURIs(uris);
104 
105         
106         SAXReader reader = new SAXReader();
107         reader.setDocumentFactory(factory);
108 
109         Document doc = getDocument("/xml/test/nestedNamespaces.xml", reader);
110 
111         
112         
113         String value = doc.valueOf("/x:pizza/y:cheese/x:pepper");
114 
115         log("Found value: " + value);
116 
117         assertEquals("XPath used default namesapce URIS", "works", value);
118     }
119 
120     
121     
122     protected void setUp() throws Exception {
123         super.setUp();
124         document = getDocument(INPUT_XML_FILE);
125     }
126 
127     /***
128      * DOCUMENT ME!
129      * 
130      * @return the root element of the document
131      */
132     protected Element getRootElement() {
133         Element root = document.getRootElement();
134         assertTrue("Document has root element", root != null);
135 
136         return root;
137     }
138 }
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175