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.Node; 16 import org.dom4j.XPath; 17 import test.dom4j.AbstractTestCase; 18 19 24 public class TestNumber extends AbstractTestCase { 25 26 protected static boolean VERBOSE = false; 27 28 protected static String [] paths = { 29 "2+2", 30 "2 + 2", 31 "2 + number(1) + 2", 32 "number(1) * 2", 33 "2 + count(//author) + 2", 34 "2 + (2 * 5)", 35 "count(//author) + count(//author/attribute::*)", 36 "(12 + count(//author) + count(//author/attribute::*)) div 2", 37 "count(//author)", 38 "count(//author/attribute::*)", 39 "2 + number(1) * 2", 40 "count(descendant::author)", 41 "count(ancestor::author)", 42 "count(descendant::*)", 43 "count(descendant::author)+1", 44 "count(ancestor::*)", 45 "10 + count(descendant::author) * 5", 47 "10 + (count(descendant::author) * 5)", 48 }; 49 50 51 public static void main( String [] args ) { 52 TestRunner.run( suite() ); 53 } 54 55 public static Test suite() { 56 return new TestSuite( TestNumber.class ); 57 } 58 59 public TestNumber(String name) { 60 super(name); 61 } 62 63 public void testXPaths() throws Exception { 66 Node element = document.selectSingleNode( "//author" ); 67 int size = paths.length; 68 for ( int i = 0; i < size; i++ ) { 69 testXPath( document, paths[i] ); 70 testXPath( element, paths[i] ); 71 } 72 log( "Finished successfully" ); 73 } 74 75 protected void testXPath(Node node, String xpathText) throws Exception { 78 try { 79 XPath xpath = node.createXPath( xpathText ); 80 81 if ( VERBOSE ) { 82 log( " xpath: " + xpath ); 83 log( " for: " + node ); 84 } 85 86 Number number = xpath.numberValueOf( node ); 87 88 log( "Searched path: " + xpathText + " found: " + number ); 89 90 } 91 catch (Throwable e) { 92 log( "Caught exception: " + e ); 93 e.printStackTrace(); 94 assertTrue( "Failed to process: " + xpathText + " caught exception: " + e, false ); 95 } 96 } 97 98 } 99 100 101 102 103 147 | Popular Tags |