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.Element; 16 import org.dom4j.Node; 17 import org.dom4j.XPath; 18 19 25 public class ValueOfTest extends AbstractTestCase { 26 protected static String [] paths = {"/root", "//author", "//author/@name", 27 "/root/author[1]", "/root/author[1]/@name", "/root/author[2]", 28 "/root/author[2]/@name", "/root/author[3]", 29 "/root/author[3]/@name", "name()", "name(.)", "name(..)", 30 "name(child::node())", "name(parent::*)", "name(../*)", 31 "name(../child::node())", "local-name()", "local-name(..)", 32 "local-name(parent::*)", "local-name(../*)", "parent::*", 33 "name(/.)", "name(/child::node())", "name(/*)", ".", "..", "../*", 34 "../child::node()", "/.", "/*", "*", "/child::node()"}; 35 36 public static void main(String [] args) { 37 TestRunner.run(ValueOfTest.class); 38 } 39 40 public void testXPaths() throws Exception { 43 Element root = document.getRootElement(); 44 List children = root.elements("author"); 45 Element child1 = (Element) children.get(0); 46 47 testXPath(document); 48 testXPath(root); 49 testXPath(child1); 50 } 51 52 protected void testXPath(Node node) throws Exception { 53 log("Testing XPath on: " + node); 54 log("==============================="); 55 56 int size = paths.length; 57 58 for (int i = 0; i < size; i++) { 59 testXPath(node, paths[i]); 60 } 61 } 62 63 protected void testXPath(Node node, String xpathExpr) throws Exception { 64 try { 65 XPath xpath = node.createXPath(xpathExpr); 66 String value = xpath.valueOf(node); 67 68 log("valueOf: " + xpathExpr + " is: " + value); 69 } catch (Throwable e) { 70 e.printStackTrace(); 71 assertTrue("Failed with exception: " + e, false); 72 } 73 } 74 } 75 76 112 | Popular Tags |