KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

145
Popular Tags