KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestNamespace


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: TestNamespace.java,v 1.3 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.Document;
16 import org.dom4j.Element;
17 import org.dom4j.Namespace;
18 import org.dom4j.QName;
19 import org.dom4j.io.SAXReader;
20
21 import java.io.File JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /** A test harness to test the use of Namespaces.
28   *
29   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
30   * @version $Revision: 1.3 $
31   */

32 public class TestNamespace extends AbstractTestCase {
33
34     /** Input XML file to read */
35     protected static String JavaDoc INPUT_XML_FILE = "xml/namespaces.xml";
36
37     /** AbstractNamespace to use in tests */
38     protected static Namespace XSL_NAMESPACE;
39
40     protected static QName XSL_TEMPLATE;
41
42
43     public static void main( String JavaDoc[] args ) {
44         TestRunner.run( suite() );
45     }
46
47     public static Test suite() {
48         return new TestSuite( TestNamespace.class );
49     }
50
51     public TestNamespace(String JavaDoc name) {
52         super(name);
53     }
54
55     // Test case(s)
56
//-------------------------------------------------------------------------
57
public void debugShowNamespaces() throws Exception JavaDoc {
58         Element root = getRootElement();
59
60         for ( Iterator JavaDoc iter = root.elementIterator(); iter.hasNext(); ) {
61             Element element = (Element) iter.next();
62
63             log( "Found element: " + element );
64             log( "AbstractNamespace: " + element.getNamespace() );
65             log( "AbstractNamespace prefix: " + element.getNamespacePrefix() );
66             log( "AbstractNamespace URI: " + element.getNamespaceURI() );
67         }
68     }
69
70     public void testGetElement() throws Exception JavaDoc {
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 JavaDoc {
81         Element root = getRootElement();
82
83         List JavaDoc 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 JavaDoc {
90         Element root = getRootElement();
91         Iterator JavaDoc 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 JavaDoc {
103         // register namespace prefix->uri mappings with factory
104
Map JavaDoc uris = new HashMap JavaDoc();
105         uris.put( "x", "fooNamespace" );
106         uris.put( "y", "barNamespace" );
107
108         xpathFactory.setXPathNamespaceURIs( uris );
109
110         // parse or create a document
111
SAXReader reader = new SAXReader();
112         reader.setNodeFactory( nodeFactory );
113         Document doc = reader.read( "xml/test/nestedNamespaces.xml" );
114
115         // evaluate XPath using registered namespace prefixes
116
// which do not appear in the document (though the URIs do!)
117
String JavaDoc value = doc.valueOf( "/x:pizza/y:cheese/x:pepper" );
118
119         log( "Found value: " + value );
120
121         assertEquals( "XPath used default namesapce URIS", "works", value );
122     }
123
124     // Implementation methods
125
//-------------------------------------------------------------------------
126
protected void setUp() throws Exception JavaDoc {
127         SAXReader reader = new SAXReader();
128         document = reader.read( new File JavaDoc( INPUT_XML_FILE ) );
129         XSL_NAMESPACE = nodeFactory.createNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
130         XSL_TEMPLATE = nodeFactory.createQName("template", XSL_NAMESPACE );
131     }
132
133     /** @return the root element of the document */
134     protected Element getRootElement() {
135         Element root = document.getRootElement();
136         assertTrue( "Document has root element", root != null );
137         return root;
138     }
139 }
140
141
142
143
144 /*
145  * Redistribution and use of this software and associated documentation
146  * ("Software"), with or without modification, are permitted provided
147  * that the following conditions are met:
148  *
149  * 1. Redistributions of source code must retain copyright
150  * statements and notices. Redistributions must also contain a
151  * copy of this document.
152  *
153  * 2. Redistributions in binary form must reproduce the
154  * above copyright notice, this list of conditions and the
155  * following disclaimer in the documentation and/or other
156  * materials provided with the distribution.
157  *
158  * 3. The name "DOM4J" must not be used to endorse or promote
159  * products derived from this Software without prior written
160  * permission of MetaStuff, Ltd. For written permission,
161  * please contact dom4j-info@metastuff.com.
162  *
163  * 4. Products derived from this Software may not be called "DOM4J"
164  * nor may "DOM4J" appear in their names without prior written
165  * permission of MetaStuff, Ltd. DOM4J is a registered
166  * trademark of MetaStuff, Ltd.
167  *
168  * 5. Due credit should be given to the DOM4J Project
169  * (http://dom4j.org/).
170  *
171  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
172  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
173  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
174  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
175  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
176  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
177  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
178  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
179  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
180  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
181  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
182  * OF THE POSSIBILITY OF SUCH DAMAGE.
183  *
184  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
185  *
186  * $Id: TestNamespace.java,v 1.3 2003/11/02 18:31:28 per_nyfelt Exp $
187  */

188
Popular Tags