1 9 10 package test.dom4j; 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 org.dom4j.tree.DefaultElement; 19 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 20 21 import java.util.List ; 22 23 28 29 public class TestXPath extends AbstractTestCase { 30 31 protected static boolean VERBOSE = true; 32 33 protected static String [] paths = 34 { 35 ".", 36 "*", 37 "/", 38 "/.", 39 "/*", 40 "/node()", 41 "/child::node()", 42 "/self::node()", 43 "root", 44 "/root", 45 "/root/author", 46 "text()", 47 "//author", 48 "//author/text()", 49 "//@location", 50 "//attribute::*", 51 "//namespace::*", 52 "normalize-space(/root)", 53 "//author[@location]", 54 "//author[@location='UK']", 55 "root|author", 56 "//*[.='James Strachan']", 57 "//root/author[1]", 58 "normalize-space(/root/author)", 59 "normalize-space(' a b c d ')", 60 "//root|//author[1]|//author[2]", 61 "//root/author[2]", 62 "//root/author[3]" }; 63 64 public static void main(String [] args) { 65 66 TestRunner.run(suite()); 67 68 } 69 70 public static Test suite() { 71 72 return new TestSuite(TestXPath.class); 73 74 } 75 76 public TestXPath(String name) { 77 78 super(name); 79 80 } 81 82 84 86 public void testXPaths() throws Exception { 87 try { 88 int size = paths.length; 89 for (int i = 0; i < size; i++) { 90 testXPath(paths[i]); 91 } 92 } catch (Exception e) { 93 e.printStackTrace(); 94 } 95 } 96 97 public void testCreateXPathBug() throws Exception { 98 99 Element element = new DefaultElement( "foo" ); 100 XPath xpath = element.createXPath( "//bar" ); 101 102 assertTrue("created a valid XPath: " + xpath != null ); 103 } 104 105 107 109 protected void testXPath(String xpathExpression) { 110 111 log("Searched path: " + xpathExpression); 112 113 XPath xpath = O3DocumentHelper.createXPath(xpathExpression); 114 115 List list = xpath.selectNodes(document); 116 117 log("Found : " + list.size() + " result(s)"); 118 119 if (VERBOSE) { 120 121 log("..........................................."); 122 123 log("XPath: :" + xpath); 124 125 log("..........................................."); 126 127 } 128 129 log("Results"); 130 131 if (list == null) { 132 133 log("null"); 134 135 } 136 137 else { 138 139 log("["); 140 141 for (int i = 0, size = list.size(); i < size; i++) { 142 143 Object object = list.get(i); 144 145 String text = "null"; 146 147 if (object instanceof Node) { 148 149 Node node = (Node) object; 150 151 text = node.asXML(); 152 153 } 154 155 else if (object != null) { 156 157 text = object.toString(); 158 159 } 160 161 log(" " + text); 162 163 } 164 165 log("]"); 166 167 } 168 169 log("..........................................."); 170 171 } 172 173 } 174 175 219 | Popular Tags |