KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestNamespaces


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: TestNamespaces.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.DOMReader;
17 import org.dom4j.io.SAXReader;
18 import org.dom4j.tree.AbstractNamespace;
19 import org.xml.sax.InputSource JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 /** Test the use of namespaces
28   *
29   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
30   * @version $Revision: 1.2 $
31   */

32 public class TestNamespaces extends AbstractTestCase {
33
34     public static void main( String JavaDoc[] args ) {
35         TestRunner.run( suite() );
36     }
37
38     public static Test suite() {
39         return new TestSuite( TestNamespaces.class );
40     }
41
42     public TestNamespaces(String JavaDoc name) {
43         super(name);
44     }
45
46     // Test case(s)
47
//-------------------------------------------------------------------------
48
public void testNamespaces() throws Exception JavaDoc {
49         testNamespaces( document );
50         testNamespaces( saxRoundTrip( document ) );
51         testNamespaces( domRoundTrip( document ) );
52     }
53
54     public void testNamespaces(Document document) throws Exception JavaDoc {
55         Document doc2 = (Document) document.clone();
56
57         Element root = doc2.getRootElement();
58         assertNamespace( root.getNamespace(), "", "http://www.w3.org/2001/XMLSchema" );
59         assertEquals( "xmlns=\"http://www.w3.org/2001/XMLSchema\"", root.getNamespace().asXML());
60         assertEquals( "namespace::*[name()='']", root.getNamespace().getPath());
61         assertEquals( "namespace::*[name()='']", root.getNamespace().getUniquePath());
62
63         List JavaDoc additionalNS = root.additionalNamespaces();
64         assertTrue( "at least one additional namespace", additionalNS != null && additionalNS.size() > 0 );
65
66         Namespace ns = (Namespace) additionalNS.get(0);
67         assertNamespace( ns, "t", "http://www.w3.org/namespace/" );
68         assertEquals( "xmlns:t=\"http://www.w3.org/namespace/\"", ns.asXML());
69         assertEquals( "namespace::t", ns.getPath());
70         assertEquals( "namespace::t", ns.getUniquePath());
71
72         Node node = root.node(0);
73         assertTrue( "First node is a namespace", node instanceof Namespace );
74
75         // now lets try change the namespace
76
root.remove(ns);
77         root.addNamespace( "t", "myNewURI" );
78
79         additionalNS = root.additionalNamespaces();
80         assertTrue( "at least one additional namespace", additionalNS != null && additionalNS.size() > 0 );
81
82         ns = (Namespace) additionalNS.get(0);
83         assertNamespace( ns, "t", "myNewURI" );
84
85         // lets test the list is backed
86
additionalNS.remove(0);
87         additionalNS.add( AbstractNamespace.get("t", "myNewURI-2" ) );
88
89         additionalNS = root.additionalNamespaces();
90         assertTrue( "at least one additional namespace", additionalNS != null && additionalNS.size() > 0 );
91
92         ns = (Namespace) additionalNS.get(0);
93         assertNamespace( ns, "t", "myNewURI-2" );
94
95         additionalNS.clear();
96         root.addNamespace( "t", "myNewURI" );
97
98         additionalNS = root.additionalNamespaces();
99         assertTrue( "at least one additional namespace", additionalNS != null && additionalNS.size() > 0 );
100
101         ns = (Namespace) additionalNS.get(0);
102         assertNamespace( ns, "t", "myNewURI" );
103
104
105         log( "Namespaces: " + additionalNS );
106         log( "XML is now" );
107         log( root.asXML() );
108     }
109
110     public void testNamespaceForPrefix() throws Exception JavaDoc {
111         testNamespaceForPrefix( document );
112         testNamespaceForPrefix( saxRoundTrip( document ) );
113         testNamespaceForPrefix( domRoundTrip( document ) );
114     }
115
116     public void testNamespaceForPrefix(Document document) throws Exception JavaDoc {
117         Element root = document.getRootElement();
118         Namespace ns = root.getNamespaceForPrefix( "t" );
119
120         assertNamespace( ns, "t", "http://www.w3.org/namespace/" );
121
122         Element element = (Element) root.elements().get(0);
123         Namespace ns2 = element.getNamespaceForPrefix( "t" );
124
125         assertNamespace( ns2, "t", "http://www.w3.org/namespace/" );
126         assertTrue( "Same namespace instance returned", ns == ns2 );
127
128         log( "found: " + ns.asXML() );
129     }
130
131     public void testNamespaceForDefaultPrefix() throws Exception JavaDoc {
132         SAXReader reader = new SAXReader();
133         Document document = reader.read("xml/test/defaultNamespace.xml");
134
135         testNamespaceForDefaultPrefix( document );
136         testNamespaceForDefaultPrefix( saxRoundTrip( document ) );
137         testNamespaceForDefaultPrefix( domRoundTrip( document ) );
138     }
139
140     public void testNamespaceForDefaultPrefix(Document document) throws Exception JavaDoc {
141         List JavaDoc list = document.selectNodes( "//*" );
142
143         for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
144             Element element = (Element) iter.next();
145             Namespace ns = element.getNamespaceForPrefix( "" );
146             assertNamespace( ns, "", "dummyNamespace" );
147             ns = element.getNamespaceForPrefix( null );
148             assertNamespace( ns, "", "dummyNamespace" );
149             log( "found: " + ns.asXML() );
150         }
151     }
152
153     public void testAttributeDefaultPrefix() throws Exception JavaDoc {
154         SAXReader reader = new SAXReader();
155         Document document = reader.read("xml/test/soap3.xml");
156
157         testAttributeDefaultPrefix( document );
158         testAttributeDefaultPrefix( saxRoundTrip( document ) );
159         testAttributeDefaultPrefix( domRoundTrip( document ) );
160     }
161
162     public void testAttributeDefaultPrefix(Document document) throws Exception JavaDoc {
163         List JavaDoc list = document.selectNodes( "//@*[local-name()='actor']" );
164
165         assertTrue( "Matched at least one 'actor' attribute", list.size() > 0 );
166
167         for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
168             Attribute attribute = (Attribute) iter.next();
169
170             log( "found: " + attribute.asXML() );
171
172             Element element = attribute.getParent();
173             assertTrue( "Attribute has a parent", element != null );
174
175             Namespace ns = element.getNamespaceForPrefix( "" );
176
177             assertNamespace( ns, "", "http://schemas.xmlsoap.org/soap/envelope/" );
178
179             Namespace ns2 = attribute.getNamespace();
180
181             // Note that namespaces do not inherit the default namespace!
182
assertNamespace( ns2, "", "" );
183             //assertNamespace( ns2, "", "http://schemas.xmlsoap.org/soap/envelope/" );
184
}
185     }
186
187     public void testNamespaceForURI() throws Exception JavaDoc {
188         testNamespaceForURI(document);
189         testNamespaceForURI( saxRoundTrip( document ) );
190         testNamespaceForURI( domRoundTrip( document ) );
191     }
192
193     public void testNamespaceForURI(Document document) throws Exception JavaDoc {
194         Element root = document.getRootElement();
195
196         Namespace ns = root.getNamespaceForURI( "http://www.w3.org/namespace/" );
197
198         assertNamespace( ns, "t", "http://www.w3.org/namespace/" );
199
200         Element element = (Element) root.elements().get(0);
201         Namespace ns2 = element.getNamespaceForURI( "http://www.w3.org/namespace/" );
202
203         assertNamespace( ns2, "t", "http://www.w3.org/namespace/" );
204
205         assertTrue( "Same namespace instance returned", ns == ns2 );
206
207         log( "found: " + ns.asXML() );
208     }
209
210
211     public void testRedeclareNamespaces() throws Exception JavaDoc {
212         SAXReader reader = new SAXReader();
213         Document document = reader.read("xml/test/soap2.xml");
214         testRedeclareNamespaces( document );
215         testRedeclareNamespaces( saxRoundTrip( document ) );
216         testRedeclareNamespaces( domRoundTrip( document ) );
217     }
218
219     public void testRedeclareNamespaces(Document document) throws Exception JavaDoc {
220         assertNamespaces(
221             document.selectNodes( "//*[local-name()='Envelope'" ),
222             "SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"
223         );
224         assertNamespaces(
225             document.selectNodes( "//*[local-name()='Body'" ),
226             "SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"
227         );
228         assertNamespaces(
229             document.selectNodes( "//*[local-name()='bar'" ),
230             "a", "barURI"
231         );
232         assertNamespaces(
233             document.selectNodes( "//*[local-name()='newBar'" ),
234             "a", "newBarURI"
235         );
236         assertNamespaces(
237             document.selectNodes( "//*[local-name()='foo'" ),
238             "", "fooURI"
239         );
240         assertNamespaces(
241             document.selectNodes( "//*[local-name()='newFoo'" ),
242             "", "newFooURI"
243         );
244     }
245
246     public void testDefaultNamespaceIssue() throws Exception JavaDoc {
247         SAXReader reader = new SAXReader();
248         Document document = reader.read("xml/test/defaultNamespaceIssue.xsd");
249         testDefaultNamespaceIssue( document );
250         testDefaultNamespaceIssue( saxRoundTrip( document ) );
251         testDefaultNamespaceIssue( domRoundTrip( document ) );
252     }
253
254     public void testDefaultNamespaceIssue(Document document) throws Exception JavaDoc {
255         // When writing documents using a default namespace with XMLWriter
256
// a redeclaration of the default namespace to "" was dropped in the output.
257
// Test that <xsd:schema><xsd:element><xsd:annotation><xsd:documentation><text>
258
// is in no namespace.
259
assertNotNull("default namespace redeclaration", document.selectSingleNode(
260             "/xsd:schema/xsd:element/xsd:annotation/xsd:documentation/text"));
261
262         // The test document has a default namespace declaration on the root
263
// element ("schema"), but the element itself is not in the default
264
// namespace. Test that declaredNamespaces on the root element also
265
// returns the default namespace declaration.
266
Iterator JavaDoc iter = document.getRootElement().declaredNamespaces().iterator();
267             while (iter.hasNext()) {
268                 Namespace ns = (Namespace)iter.next();
269                     if ("urn:wapforum:devicesheet".equals(ns.getURI())
270                         && "".equals(ns.getPrefix())) {
271                         return;
272                     }
273             }
274         fail("Default namespace declaration not present on root element");
275     }
276
277     // Implementation methods
278
//-------------------------------------------------------------------------
279
protected void setUp() throws Exception JavaDoc {
280         SAXReader reader = new SAXReader();
281         document = reader.read( "xml/test/test_schema.xml" );
282     }
283
284     protected Document saxRoundTrip(Document document) throws Exception JavaDoc {
285         return org.ozoneDB.xml.dom4j.O3DocumentHelper.parseText( document.asXML() );
286     }
287
288     protected Document domRoundTrip(Document document) throws Exception JavaDoc {
289         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
290         factory.setNamespaceAware( true );
291         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
292         org.w3c.dom.Document JavaDoc domDocument = builder.parse( new InputSource JavaDoc( new StringReader JavaDoc( document.asXML() ) ) );
293
294         // now lets read it back as a DOM4J object
295
DOMReader domReader = new DOMReader();
296         return domReader.read( domDocument );
297     }
298
299     protected void assertNamespaces( List JavaDoc elements, String JavaDoc prefix, String JavaDoc uri ) throws Exception JavaDoc {
300         log( "Validating: " + elements.size() + " element(s) are in URI: " + uri );
301         for ( Iterator JavaDoc iter = elements.iterator(); iter.hasNext(); ) {
302             Element element = (Element) iter.next();
303             assertNamespace( element.getNamespace(), prefix, uri );
304         }
305     }
306
307     protected void assertNamespace(Namespace ns, String JavaDoc prefix, String JavaDoc uri) throws Exception JavaDoc {
308         assertEquals( "namespace prefix", prefix, ns.getPrefix() );
309         assertEquals( "namespace URI", uri, ns.getURI() );
310     }
311 }
312
313
314
315
316 /*
317  * Redistribution and use of this software and associated documentation
318  * ("Software"), with or without modification, are permitted provided
319  * that the following conditions are met:
320  *
321  * 1. Redistributions of source code must retain copyright
322  * statements and notices. Redistributions must also contain a
323  * copy of this document.
324  *
325  * 2. Redistributions in binary form must reproduce the
326  * above copyright notice, this list of conditions and the
327  * following disclaimer in the documentation and/or other
328  * materials provided with the distribution.
329  *
330  * 3. The name "DOM4J" must not be used to endorse or promote
331  * products derived from this Software without prior written
332  * permission of MetaStuff, Ltd. For written permission,
333  * please contact dom4j-info@metastuff.com.
334  *
335  * 4. Products derived from this Software may not be called "DOM4J"
336  * nor may "DOM4J" appear in their names without prior written
337  * permission of MetaStuff, Ltd. DOM4J is a registered
338  * trademark of MetaStuf, Ltd.
339  *
340  * 5. Due credit should be given to the DOM4J Project
341  * (http://dom4j.org/).
342  *
343  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
344  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
345  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
346  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
347  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
348  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
349  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
350  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
352  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
353  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
354  * OF THE POSSIBILITY OF SUCH DAMAGE.
355  *
356  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
357  *
358  * $Id: TestNamespaces.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
359  */

360
Popular Tags