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.Document; 16 import org.dom4j.Element; 17 import org.dom4j.Node; 18 import org.dom4j.XPathFactory; 19 import org.dom4j.io.SAXReader; 20 import org.dom4j.rule.Pattern; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 26 31 public class TestXPathExamples extends AbstractTestCase { 32 33 protected static boolean VERBOSE = true; 34 35 protected SAXReader xmlReader = new SAXReader(); 36 37 38 protected Document testDocument; 39 40 41 protected Node testContext; 42 43 44 protected XPathFactory factory; 45 46 47 public static void main( String [] args ) { 48 TestRunner.run( suite() ); 49 } 50 51 public static Test suite() { 52 return new TestSuite( TestXPathExamples.class ); 53 } 54 55 public TestXPathExamples(String name) { 56 super(name); 57 } 58 59 public void log(String text) { 60 System.out.println(text); 61 } 62 63 public void testXPaths() throws Exception { 66 Document document = xmlReader.read( "xml/test/xpath/tests.xml" ); 67 Element root = document.getRootElement(); 68 for ( Iterator iter = root.elementIterator( "document" ); iter.hasNext(); ) { 69 Element documentTest = (Element) iter.next(); 70 testDocument( documentTest ); 71 } 72 } 73 74 protected void testDocument(Element documentTest) throws Exception { 77 String url = documentTest.attributeValue( "url" ); 78 testDocument = xmlReader.read( url ); 79 assertTrue( "Loaded test document: " + url, testDocument != null ); 80 81 log( "Loaded document: " + url ); 82 83 for ( Iterator iter = documentTest.elementIterator( "context" ); iter.hasNext(); ) { 84 Element context = (Element) iter.next(); 85 testContext( documentTest, context ); 86 } 87 } 88 89 protected void testContext(Element documentTest, Element context) throws Exception { 90 String xpath = context.attributeValue( "select" ); 91 92 if ( VERBOSE ) { 93 log( "Selecting nodes for XPath: " + testDocument.createXPath( xpath ) ); 94 } 95 96 List list = testDocument.selectNodes( xpath ); 97 98 assertTrue( "Found at least one context nodes to test for path: " + xpath, list != null && list.size() > 0 ); 99 100 for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 101 Object object = iter.next(); 102 assertTrue( "Context node is a Node: " + object, object instanceof Node ); 103 testContext = (Node) object; 104 105 log( "Context is now: " + testContext ); 106 runTests( documentTest, context ); 107 log( "" ); 108 } 109 } 110 111 protected void runTests(Element documentTest, Element context) throws Exception { 112 for ( Iterator iter = context.elementIterator( "test" ); iter.hasNext(); ) { 113 Element test = (Element) iter.next(); 114 runTest( documentTest, context, test ); 115 } 116 for ( Iterator iter = context.elementIterator( "valueOf" ); iter.hasNext(); ) { 117 Element valueOf = (Element) iter.next(); 118 testValueOf( documentTest, context, valueOf ); 119 } 120 for ( Iterator iter = context.elementIterator( "pattern" ); iter.hasNext(); ) { 121 Element pattern = (Element) iter.next(); 122 testPattern( documentTest, context, pattern ); 123 } 124 for ( Iterator iter = context.elementIterator( "filter" ); iter.hasNext(); ) { 125 Element filter = (Element) iter.next(); 126 testFilter( documentTest, context, filter ); 127 } 128 } 129 130 protected void runTest(Element documentTest, Element context, Element test) throws Exception { 131 String xpath = test.attributeValue( "select" ); 132 133 String description = "Path: " + xpath; 134 135 if ( VERBOSE ) { 136 log( "" ); 137 log( "XPath for: " + xpath ); 138 log( "is: " + testContext.createXPath( xpath ) ); 139 log( "" ); 140 } 141 142 String count = test.attributeValue( "count" ); 143 if ( count != null ) { 144 int expectedSize = Integer.parseInt( count ); 145 List results = testContext.selectNodes( xpath ); 146 147 log( description + " found result size: " + results.size() ); 148 149 assertEquals( description + " wrong result size", expectedSize, results.size() ); 150 } 151 152 Element valueOf = test.element( "valueOf" ); 153 if ( valueOf != null ) { 154 Node node = testContext.selectSingleNode( xpath ); 155 assertTrue( description + " found node", node != null ); 156 157 String expected = valueOf.getText(); 158 String result = node.valueOf( valueOf.attributeValue( "select" ) ); 159 160 log( description ); 161 log( "\texpected: " + expected + " result: " + result ); 162 163 assertEquals( description, expected, result ); 164 } 165 } 166 167 protected void testValueOf(Element documentTest, Element context, Element valueOf) throws Exception { 168 String xpath = valueOf.attributeValue( "select" ); 169 String description = "valueOf: " + xpath; 170 171 if ( VERBOSE ) { 172 log( "XPath: " + testContext.createXPath( xpath ) ); 173 } 174 175 String expected = valueOf.getText(); 176 String result = testContext.valueOf( xpath ); 177 178 log( description ); 179 log( "\texpected: " + expected + " result: " + result ); 180 181 assertEquals( description, expected, result ); 182 } 183 184 protected void testPattern(Element documentTest, Element context, Element patternElement) throws Exception { 185 String match = patternElement.attributeValue( "match" ); 186 String description = "match: " + match; 187 188 log( "" ); 189 log( description ); 190 191 Pattern pattern = factory.createPattern( match ); 192 193 if ( VERBOSE ) { 194 log( "Pattern: " + pattern ); 195 } 196 197 assertTrue( description, pattern.matches( testContext ) ); 198 199 } 200 201 protected void testFilter(Element documentTest, Element context, Element pattern) throws Exception { 202 String match = pattern.attributeValue( "match" ); 203 String description = "match: " + match; 204 205 log( "" ); 206 log( description ); 207 208 if ( VERBOSE ) { 209 log( "Pattern: " + factory.createXPathFilter( match ) ); 210 } 211 212 assertTrue( description, testContext.matches( match ) ); 213 } 214 } 215 216 217 218 219 263 | Popular Tags |