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