1 7 8 package org.dom4j.xpath; 9 10 import junit.textui.TestRunner; 11 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 import org.dom4j.AbstractTestCase; 16 import org.dom4j.Attribute; 17 import org.dom4j.DocumentHelper; 18 import org.dom4j.XPath; 19 20 26 public class AttributeTest extends AbstractTestCase { 27 protected static String [] paths = {"attribute::*", 28 "/root/author/attribute::*", "//attribute::*", "@name"}; 29 30 public static void main(String [] args) { 31 TestRunner.run(AttributeTest.class); 32 } 33 34 public void testXPaths() throws Exception { 37 int size = paths.length; 38 39 for (int i = 0; i < size; i++) { 40 testXPath(paths[i]); 41 } 42 } 43 44 protected void testXPath(String xpathText) { 47 XPath xpath = DocumentHelper.createXPath(xpathText); 48 List list = xpath.selectNodes(document); 49 50 log("Searched path: " + xpathText + " found: " + list.size() 51 + " result(s)"); 52 53 for (Iterator iter = list.iterator(); iter.hasNext();) { 54 Object object = iter.next(); 55 56 log("Found Result: " + object); 57 58 assertTrue("Results should be Attribute objects", 59 object instanceof Attribute); 60 61 Attribute attribute = (Attribute) object; 62 63 assertTrue("Results should support the parent relationship", 64 attribute.supportsParent()); 65 assertTrue( 66 "Results should contain reference to the parent element", 67 attribute.getParent() != null); 68 assertTrue("Resulting document not correct", attribute 69 .getDocument() != null); 70 } 71 } 72 } 73 74 110 | Popular Tags |