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