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