KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > xpath > NamespaceTest


1 /*
2  * Copyright 2001-2005 (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
8 package org.dom4j.xpath;
9
10 import junit.textui.TestRunner;
11
12 import java.io.File JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.dom4j.AbstractTestCase;
17 import org.dom4j.DocumentHelper;
18 import org.dom4j.Namespace;
19 import org.dom4j.XPath;
20 import org.dom4j.io.SAXReader;
21
22 /**
23  * Test harness for the namespace axis
24  *
25  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
26  * @version $Revision: 1.3 $
27  */

28 public class NamespaceTest extends AbstractTestCase {
29     protected static String JavaDoc[] paths = {"namespace::*",
30             "/Template/Application1/namespace::*",
31             "/Template/Application1/namespace::xplt", "//namespace::*"};
32
33     public static void main(String JavaDoc[] args) {
34         TestRunner.run(NamespaceTest.class);
35     }
36
37     // Test case(s)
38
// -------------------------------------------------------------------------
39
public void testXPaths() throws Exception JavaDoc {
40         int size = paths.length;
41
42         for (int i = 0; i < size; i++) {
43             testXPath(paths[i]);
44         }
45     }
46
47     // Implementation methods
48
// -------------------------------------------------------------------------
49
protected void testXPath(String JavaDoc xpathText) {
50         XPath xpath = DocumentHelper.createXPath(xpathText);
51         List JavaDoc list = xpath.selectNodes(document);
52
53         log("Searched path: " + xpathText + " found: " + list.size()
54                 + " result(s)");
55
56         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
57             Object JavaDoc object = iter.next();
58
59             log("Found Result: " + object);
60
61             assertTrue("Results should be Namespace objects",
62                     object instanceof Namespace);
63
64             Namespace namespace = (Namespace) object;
65
66             log("Parent node: " + namespace.getParent());
67
68             assertTrue("Results should support the parent relationship",
69                     namespace.supportsParent());
70             assertTrue(
71                     "Results should contain reference to the parent element",
72                     namespace.getParent() != null);
73             assertTrue("Results should contain reference to the document",
74                     namespace.getDocument() != null);
75         }
76     }
77
78     protected void setUp() throws Exception JavaDoc {
79         super.setUp();
80         document = new SAXReader().read(new File JavaDoc("xml/testNamespaces.xml"));
81     }
82 }
83
84 /*
85  * Redistribution and use of this software and associated documentation
86  * ("Software"), with or without modification, are permitted provided that the
87  * following conditions are met:
88  *
89  * 1. Redistributions of source code must retain copyright statements and
90  * notices. Redistributions must also contain a copy of this document.
91  *
92  * 2. Redistributions in binary form must reproduce the above copyright notice,
93  * this list of conditions and the following disclaimer in the documentation
94  * and/or other materials provided with the distribution.
95  *
96  * 3. The name "DOM4J" must not be used to endorse or promote products derived
97  * from this Software without prior written permission of MetaStuff, Ltd. For
98  * written permission, please contact dom4j-info@metastuff.com.
99  *
100  * 4. Products derived from this Software may not be called "DOM4J" nor may
101  * "DOM4J" appear in their names without prior written permission of MetaStuff,
102  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
103  *
104  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
105  *
106  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
107  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
108  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
109  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
110  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
111  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
112  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
113  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
114  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
115  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
116  * POSSIBILITY OF SUCH DAMAGE.
117  *
118  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
119  */

120
Popular Tags