1 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 27 public class VariableTest extends AbstractTestCase { 28 protected static String [] paths = { 31 "$author", 32 }; 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 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 121 | Popular Tags |