1 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.Element; 16 import org.dom4j.Node; 17 import org.dom4j.XPath; 18 import test.dom4j.AbstractTestCase; 19 20 import java.util.List ; 21 22 27 public class TestValueOf extends AbstractTestCase { 28 29 protected static boolean VERBOSE = false; 30 31 protected static String [] paths = { 32 "/root", 33 "//author", 34 "//author/@name", 35 "/root/author[1]", 36 "/root/author[1]/@name", 37 "/root/author[2]", 38 "/root/author[2]/@name", 39 "/root/author[3]", 40 "/root/author[3]/@name", 41 "name()", 42 "name(.)", 43 "name(..)", 44 "name(child::node())", 45 "name(parent::*)", 46 "name(../*)", 47 "name(../child::node())", 48 "local-name()", 49 "local-name(..)", 50 "local-name(parent::*)", 51 "local-name(../*)", 52 "parent::*", 53 "name(/.)", 54 "name(/child::node())", 55 "name(/*)", 56 ".", 57 "..", 58 "../*", 59 "../child::node()", 60 "/.", 61 "/*", 62 "*", 63 "/child::node()", 64 }; 65 66 67 public static void main( String [] args ) { 68 TestRunner.run( suite() ); 69 } 70 71 public static Test suite() { 72 return new TestSuite( TestValueOf.class ); 73 } 74 75 public TestValueOf(String name) { 76 super(name); 77 } 78 79 public void testXPaths() throws Exception { 82 Element root = document.getRootElement(); 83 List children = root.elements( "author" ); 84 Element child1 = (Element) children.get(0); 85 86 testXPath( document ); 87 testXPath( root ); 88 testXPath( child1 ); 89 } 90 91 protected void testXPath(Node node) throws Exception { 92 log( "Testing XPath on: " + node ); 93 log( "===============================" ); 94 95 int size = paths.length; 96 for ( int i = 0; i < size; i++ ) { 97 testXPath( node, paths[i] ); 98 } 99 } 100 101 protected void testXPath(Node node, String xpathExpr) throws Exception { 102 try { 103 XPath xpath = node.createXPath( xpathExpr ); 104 String value = xpath.valueOf(node); 105 106 log( "valueOf: " + xpathExpr + " is: " + value ); 107 108 if ( VERBOSE ) { 109 log( "xpath object: " + xpath ); 110 log( "===============================" ); 111 } 112 } 113 catch (Throwable e) { 114 e.printStackTrace(); 115 assertTrue( "Failed with exception: " + e, false ); 116 } 117 } 118 119 } 120 121 122 123 124 168 | Popular Tags |