1 7 8 package org.dom4j.xpath; 9 10 import junit.textui.TestRunner; 11 12 import org.dom4j.AbstractTestCase; 13 import org.dom4j.Node; 14 import org.dom4j.XPath; 15 16 22 public class NumberTest extends AbstractTestCase { 23 protected static String [] paths = {"2+2", "2 + 2", "2 + number(1) + 2", 24 "number(1) * 2", "2 + count(//author) + 2", "2 + (2 * 5)", 25 "count(//author) + count(//author/attribute::*)", 26 "(12 + count(//author) + count(//author/attribute::*)) div 2", 27 "count(//author)", "count(//author/attribute::*)", 28 "2 + number(1) * 2", "count(descendant::author)", 29 "count(ancestor::author)", "count(descendant::*)", 30 "count(descendant::author)+1", "count(ancestor::*)", 31 "10 + count(ancestor-or-self::author) + 5", 32 "10 + count(descendant::author) * 5", 33 "10 + (count(descendant::author) * 5)"}; 34 35 public static void main(String [] args) { 36 TestRunner.run(NumberTest.class); 37 } 38 39 public void testXPaths() throws Exception { 42 Node element = document.selectSingleNode("//author"); 43 int size = paths.length; 44 45 for (int i = 0; i < size; i++) { 46 testXPath(document, paths[i]); 47 testXPath(element, paths[i]); 48 } 49 50 log("Finished successfully"); 51 } 52 53 protected void testXPath(Node node, String xpathText) throws Exception { 56 try { 57 XPath xpath = node.createXPath(xpathText); 58 59 Number number = xpath.numberValueOf(node); 60 61 log("Searched path: " + xpathText + " found: " + number); 62 } catch (Throwable e) { 63 log("Caught exception: " + e); 64 e.printStackTrace(); 65 assertTrue("Failed to process: " + xpathText 66 + " caught exception: " + e, false); 67 } 68 } 69 } 70 71 107 | Popular Tags |