1   /*
2    * Copyright 2001-2004 (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.14 2004/06/25 08:03:47 maartenc Exp $
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      // Test case(s)
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         // register namespace prefix->uri mappings with factory
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         // parse or create a document
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         // evaluate XPath using registered namespace prefixes
118         // which do not appear in the document (though the URIs do!)
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     // Implementation methods
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  * Redistribution and use of this software and associated documentation
147  * ("Software"), with or without modification, are permitted provided
148  * that the following conditions are met:
149  *
150  * 1. Redistributions of source code must retain copyright
151  *    statements and notices.  Redistributions must also contain a
152  *    copy of this document.
153  *
154  * 2. Redistributions in binary form must reproduce the
155  *    above copyright notice, this list of conditions and the
156  *    following disclaimer in the documentation and/or other
157  *    materials provided with the distribution.
158  *
159  * 3. The name "DOM4J" must not be used to endorse or promote
160  *    products derived from this Software without prior written
161  *    permission of MetaStuff, Ltd.  For written permission,
162  *    please contact dom4j-info@metastuff.com.
163  *
164  * 4. Products derived from this Software may not be called "DOM4J"
165  *    nor may "DOM4J" appear in their names without prior written
166  *    permission of MetaStuff, Ltd. DOM4J is a registered
167  *    trademark of MetaStuff, Ltd.
168  *
169  * 5. Due credit should be given to the DOM4J Project - 
170  *    http://www.dom4j.org
171  *
172  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
173  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
174  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
175  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
176  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
177  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
178  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
179  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
180  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
181  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
182  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
183  * OF THE POSSIBILITY OF SUCH DAMAGE.
184  *
185  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
186  *
187  * $Id: TestNamespace.java,v 1.14 2004/06/25 08:03:47 maartenc Exp $
188  */