KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > xpath > TestPrefix


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: TestPrefix.java,v 1.1 2003/07/07 10:30:30 per_nyfelt Exp $
8  */

9
10 package test.dom4j.xpath;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.XPath;
16 import org.dom4j.io.SAXReader;
17 import org.jaxen.SimpleNamespaceContext;
18 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
19 import test.dom4j.AbstractTestCase;
20
21 import java.io.File JavaDoc;
22 import java.util.List JavaDoc;
23
24 /** Tests finding items using a namespace prefix
25   *
26   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
27   * @version $Revision: 1.1 $
28   */

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

136
Popular Tags