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;
13 import java.util.List;
14 
15 import org.dom4j.AbstractTestCase;
16 import org.dom4j.DocumentHelper;
17 import org.dom4j.XPath;
18 import org.dom4j.io.SAXReader;
19 
20 import org.jaxen.SimpleNamespaceContext;
21 
22 /**
23  * Tests finding items using a namespace prefix
24  *
25  * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
26  * @version $Revision: 1.3 $
27  */
28 public class PrefixTest extends AbstractTestCase {
29     protected static String[] paths = {"//xplt:anyElement", "//xpl:insertText",
30             "/Template/Application1/xpl:insertText",
31             "/Template/Application2/xpl:insertText"};
32 
main(String[] args)33     public static void main(String[] args) {
34         TestRunner.run(PrefixTest.class);
35     }
36 
37     // Test case(s)
38     // -------------------------------------------------------------------------
testXPaths()39     public void testXPaths() throws Exception {
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     // -------------------------------------------------------------------------
testXPath(String xpathText)49     protected void testXPath(String xpathText) {
50         XPath xpath = DocumentHelper.createXPath(xpathText);
51 
52         SimpleNamespaceContext context = new SimpleNamespaceContext();
53         context.addNamespace("xplt", "www.xxxx.com");
54         context.addNamespace("xpl", "www.xxxx.com");
55         xpath.setNamespaceContext(context);
56 
57         List list = xpath.selectNodes(document);
58 
59         log("Searched path: " + xpathText + " found: " + list.size()
60                 + " result(s)");
61 
62         assertTrue("Should have found at lest one result", list.size() > 0);
63     }
64 
setUp()65     protected void setUp() throws Exception {
66         super.setUp();
67         document = new SAXReader().read(new File("xml/testNamespaces.xml"));
68     }
69 }
70 
71 /*
72  * Redistribution and use of this software and associated documentation
73  * ("Software"), with or without modification, are permitted provided that the
74  * following conditions are met:
75  *
76  * 1. Redistributions of source code must retain copyright statements and
77  * notices. Redistributions must also contain a copy of this document.
78  *
79  * 2. Redistributions in binary form must reproduce the above copyright notice,
80  * this list of conditions and the following disclaimer in the documentation
81  * and/or other materials provided with the distribution.
82  *
83  * 3. The name "DOM4J" must not be used to endorse or promote products derived
84  * from this Software without prior written permission of MetaStuff, Ltd. For
85  * written permission, please contact dom4j-info@metastuff.com.
86  *
87  * 4. Products derived from this Software may not be called "DOM4J" nor may
88  * "DOM4J" appear in their names without prior written permission of MetaStuff,
89  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
90  *
91  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
92  *
93  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
94  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
95  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
96  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
97  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
98  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
99  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
100  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
101  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
103  * POSSIBILITY OF SUCH DAMAGE.
104  *
105  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
106  */
107