1 7 8 package org.dom4j; 9 10 import junit.textui.TestRunner; 11 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 import org.dom4j.io.SAXReader; 16 import org.dom4j.rule.Pattern; 17 18 24 public class XPathExamplesTest extends AbstractTestCase { 25 protected SAXReader xmlReader = new SAXReader(); 26 27 28 protected Document testDocument; 29 30 31 protected Node testContext; 32 33 34 protected DocumentFactory factory = DocumentFactory.getInstance(); 35 36 public static void main(String [] args) { 37 TestRunner.run(XPathExamplesTest.class); 38 } 39 40 public void testXPaths() throws Exception { 43 Document document = getDocument("/xml/test/xpath/tests.xml"); 44 Element root = document.getRootElement(); 45 46 Iterator iter = root.elementIterator("document"); 47 48 while (iter.hasNext()) { 49 Element documentTest = (Element) iter.next(); 50 testDocument(documentTest); 51 } 52 } 53 54 protected void testDocument(Element documentTest) throws Exception { 57 String url = documentTest.attributeValue("url"); 58 testDocument = xmlReader.read(getFile(url)); 59 assertTrue("Loaded test document: " + url, testDocument != null); 60 61 log("Loaded document: " + url); 62 63 for (Iterator iter = documentTest.elementIterator("context"); iter 64 .hasNext();) { 65 Element context = (Element) iter.next(); 66 testContext(documentTest, context); 67 } 68 } 69 70 protected void testContext(Element documentTest, Element context) 71 throws Exception { 72 String xpath = context.attributeValue("select"); 73 74 List list = testDocument.selectNodes(xpath); 75 76 assertTrue("Found at least one context nodes to test for path: " 77 + xpath, (list != null) && (list.size() > 0)); 78 79 for (Iterator iter = list.iterator(); iter.hasNext();) { 80 Object object = iter.next(); 81 assertTrue("Context node is a Node: " + object, 82 object instanceof Node); 83 testContext = (Node) object; 84 85 log("Context is now: " + testContext); 86 runTests(documentTest, context); 87 log(""); 88 } 89 } 90 91 protected void runTests(Element documentTest, Element context) 92 throws Exception { 93 for (Iterator iter = context.elementIterator("test"); iter.hasNext();) { 94 Element test = (Element) iter.next(); 95 runTest(documentTest, context, test); 96 } 97 98 for (Iterator it = context.elementIterator("valueOf"); it.hasNext();) { 99 Element valueOf = (Element) it.next(); 100 testValueOf(documentTest, context, valueOf); 101 } 102 103 for (Iterator it = context.elementIterator("pattern"); it.hasNext();) { 104 Element pattern = (Element) it.next(); 105 testPattern(documentTest, context, pattern); 106 } 107 108 Iterator it = context.elementIterator("fileter"); 109 110 while (it.hasNext()) { 111 Element filter = (Element) it.next(); 112 testFilter(documentTest, context, filter); 113 } 114 } 115 116 protected void runTest(Element documentTest, Element context, Element test) 117 throws Exception { 118 String xpath = test.attributeValue("select"); 119 120 String description = "Path: " + xpath; 121 122 String exception = test.attributeValue("exception"); 123 if ((exception != null) && exception.equals("true")) { 124 try { 125 testContext.selectNodes(xpath); 126 fail("Exception was not thrown"); 127 } catch (XPathException e) { 128 return; 129 } 130 } 131 132 String count = test.attributeValue("count"); 133 134 if (count != null) { 135 int expectedSize = Integer.parseInt(count); 136 List results = testContext.selectNodes(xpath); 137 138 log(description + " found result size: " + results.size()); 139 140 assertEquals(description + " wrong result size", expectedSize, 141 results.size()); 142 } 143 144 Element valueOf = test.element("valueOf"); 145 146 if (valueOf != null) { 147 Node node = testContext.selectSingleNode(xpath); 148 assertTrue(description + " found node", node != null); 149 150 String expected = valueOf.getText(); 151 String result = node.valueOf(valueOf.attributeValue("select")); 152 153 log(description); 154 log("\texpected: " + expected + " result: " + result); 155 156 assertEquals(description, expected, result); 157 } 158 } 159 160 protected void testValueOf(Element documentTest, Element context, 161 Element valueOf) throws Exception { 162 String xpath = valueOf.attributeValue("select"); 163 String description = "valueOf: " + xpath; 164 165 String expected = valueOf.getText(); 166 String result = testContext.valueOf(xpath); 167 168 log(description); 169 log("\texpected: " + expected + " result: " + result); 170 171 assertEquals(description, expected, result); 172 } 173 174 protected void testPattern(Element documentTest, Element context, 175 Element patternElement) throws Exception { 176 String match = patternElement.attributeValue("match"); 177 String description = "match: " + match; 178 179 log(""); 180 log(description); 181 182 Pattern pattern = factory.createPattern(match); 183 184 assertTrue(description, pattern.matches(testContext)); 185 } 186 187 protected void testFilter(Element documentTest, Element context, 188 Element pattern) throws Exception { 189 String match = pattern.attributeValue("match"); 190 String description = "match: " + match; 191 192 assertTrue(description, testContext.matches(match)); 193 } 194 } 195 196 232 | Popular Tags |