KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestValueOf.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.Element;
16 import org.dom4j.Node;
17 import org.dom4j.XPath;
18 import test.dom4j.AbstractTestCase;
19
20 import java.util.List JavaDoc;
21
22 /** Test harness for the valueOf() function
23   *
24   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
25   * @version $Revision: 1.1 $
26   */

27 public class TestValueOf extends AbstractTestCase {
28
29     protected static boolean VERBOSE = false;
30
31     protected static String JavaDoc[] paths = {
32         "/root",
33         "//author",
34         "//author/@name",
35         "/root/author[1]",
36         "/root/author[1]/@name",
37         "/root/author[2]",
38         "/root/author[2]/@name",
39         "/root/author[3]",
40         "/root/author[3]/@name",
41         "name()",
42         "name(.)",
43         "name(..)",
44         "name(child::node())",
45         "name(parent::*)",
46         "name(../*)",
47         "name(../child::node())",
48         "local-name()",
49         "local-name(..)",
50         "local-name(parent::*)",
51         "local-name(../*)",
52         "parent::*",
53         "name(/.)",
54         "name(/child::node())",
55         "name(/*)",
56         ".",
57         "..",
58         "../*",
59         "../child::node()",
60         "/.",
61         "/*",
62         "*",
63         "/child::node()",
64     };
65
66
67     public static void main( String JavaDoc[] args ) {
68         TestRunner.run( suite() );
69     }
70
71     public static Test suite() {
72         return new TestSuite( TestValueOf.class );
73     }
74
75     public TestValueOf(String JavaDoc name) {
76         super(name);
77     }
78
79     // Test case(s)
80
//-------------------------------------------------------------------------
81
public void testXPaths() throws Exception JavaDoc {
82         Element root = document.getRootElement();
83         List JavaDoc children = root.elements( "author" );
84         Element child1 = (Element) children.get(0);
85
86         testXPath( document );
87         testXPath( root );
88         testXPath( child1 );
89     }
90
91     protected void testXPath(Node node) throws Exception JavaDoc {
92         log( "Testing XPath on: " + node );
93         log( "===============================" );
94
95         int size = paths.length;
96         for ( int i = 0; i < size; i++ ) {
97             testXPath( node, paths[i] );
98         }
99     }
100
101     protected void testXPath(Node node, String JavaDoc xpathExpr) throws Exception JavaDoc {
102         try {
103             XPath xpath = node.createXPath( xpathExpr );
104             String JavaDoc value = xpath.valueOf(node);
105
106             log( "valueOf: " + xpathExpr + " is: " + value );
107
108             if ( VERBOSE ) {
109                 log( "xpath object: " + xpath );
110                 log( "===============================" );
111             }
112         }
113         catch (Throwable JavaDoc e) {
114             e.printStackTrace();
115             assertTrue( "Failed with exception: " + e, false );
116         }
117     }
118
119 }
120
121
122
123
124 /*
125  * Redistribution and use of this software and associated documentation
126  * ("Software"), with or without modification, are permitted provided
127  * that the following conditions are met:
128  *
129  * 1. Redistributions of source code must retain copyright
130  * statements and notices. Redistributions must also contain a
131  * copy of this document.
132  *
133  * 2. Redistributions in binary form must reproduce the
134  * above copyright notice, this list of conditions and the
135  * following disclaimer in the documentation and/or other
136  * materials provided with the distribution.
137  *
138  * 3. The name "DOM4J" must not be used to endorse or promote
139  * products derived from this Software without prior written
140  * permission of MetaStuff, Ltd. For written permission,
141  * please contact dom4j-info@metastuff.com.
142  *
143  * 4. Products derived from this Software may not be called "DOM4J"
144  * nor may "DOM4J" appear in their names without prior written
145  * permission of MetaStuff, Ltd. DOM4J is a registered
146  * trademark of MetaStuff, Ltd.
147  *
148  * 5. Due credit should be given to the DOM4J Project
149  * (http://dom4j.org/).
150  *
151  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
152  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
153  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
154  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
155  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
156  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
157  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
158  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
159  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
160  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
161  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
162  * OF THE POSSIBILITY OF SUCH DAMAGE.
163  *
164  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
165  *
166  * $Id: TestValueOf.java,v 1.1 2003/07/07 10:30:30 per_nyfelt Exp $
167  */

168
Popular Tags