1 7 8 package org.dom4j; 9 10 import junit.textui.TestRunner; 11 12 import java.util.List ; 13 14 import org.dom4j.tree.DefaultElement; 15 import org.dom4j.xpath.DefaultXPath; 16 17 23 public class XPathTest extends AbstractTestCase { 24 protected static String [] paths = { 25 ".", 26 "*", 27 "/", 28 "/.", 29 "/*", 30 "/node()", 31 "/child::node()", 32 "/self::node()", 33 "root", 34 "/root", 35 "/root/author", 36 "text()", 37 "//author", 38 "//author/text()", 39 "//@location", 40 "//attribute::*", 41 "//namespace::*", 42 "normalize-space(/root)", 43 "//author[@location]", 44 "//author[@location='UK']", 45 "root|author", 46 "//*[.='James Strachan']", 47 "//root/author[1]", 48 "normalize-space(/root/author)", 49 "normalize-space(' a b c d ')", 50 "//root|//author[1]|//author[2]", 51 "//root/author[2]", 52 "//root/author[3]"}; 53 54 public static void main(String [] args) { 55 TestRunner.run(XPathTest.class); 56 } 57 58 public void testBug1116471() throws Exception { 61 String xml = "<a><b>Water T & D-46816</b></a>"; 62 String expected = "Water T & D-46816"; 63 64 Document doc = DocumentHelper.parseText(xml); 65 String result = (String ) doc.selectObject("string(a/b[1])"); 66 67 assertEquals("xpath result not correct", expected, result); 68 69 Node node = doc.selectSingleNode("a/b"); 70 String result2 = node.getStringValue(); 71 72 assertEquals("xpath result not correct", expected, result2); 73 } 74 75 public void testXPaths() throws Exception { 76 int size = paths.length; 77 78 for (int i = 0; i < size; i++) { 79 testXPath(paths[i]); 80 } 81 } 82 83 public void testCreateXPathBug() throws Exception { 84 Element element = new DefaultElement("foo"); 85 XPath xpath = element.createXPath("//bar"); 86 87 assertTrue(("created a valid XPath: " + xpath) != null); 88 } 89 90 public void testBug857704() throws Exception { 91 Document doc = DocumentHelper 92 .parseText("<foo xmlns:bar='http://blort'/>"); 93 doc.selectNodes("//*[preceding-sibling::*]"); } 95 96 public void testBooleanValueOf() throws Exception { 97 Document doc = DocumentHelper.parseText("<root><foo>blah</foo></root>"); 98 99 XPath path = new DefaultXPath("//root"); 100 assertTrue(path.booleanValueOf(doc)); 101 102 path = new DefaultXPath("//root2"); 103 assertFalse(path.booleanValueOf(doc)); 104 } 105 106 protected void testXPath(String xpathExpression) { 109 log("Searched path: " + xpathExpression); 110 111 XPath xpath = DocumentHelper.createXPath(xpathExpression); 112 113 List list = xpath.selectNodes(document); 114 115 if (list == null) { 116 log("null"); 117 } else { 118 log("["); 119 120 for (int i = 0, size = list.size(); i < size; i++) { 121 Object object = list.get(i); 122 123 String text = "null"; 124 125 if (object instanceof Node) { 126 Node node = (Node) object; 127 128 text = node.asXML(); 129 } else if (object != null) { 130 text = object.toString(); 131 } 132 133 log(" " + text); 134 } 135 136 log("]"); 137 } 138 139 log("..........................................."); 140 } 141 } 142 143 179
| Popular Tags
|