1 9 10 package test.dom4j.xpath; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.Attribute; 16 import org.dom4j.XPath; 17 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 18 import test.dom4j.AbstractTestCase; 19 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 28 public class TestAttribute extends AbstractTestCase { 29 30 protected static boolean VERBOSE = false; 31 32 protected static String [] paths = { 33 "attribute::*", 34 "/root/author/attribute::*", 35 "//attribute::*", 36 "@name" 37 }; 38 39 40 public static void main( String [] args ) { 41 TestRunner.run( suite() ); 42 } 43 44 public static Test suite() { 45 return new TestSuite( TestAttribute.class ); 46 } 47 48 public TestAttribute(String name) { 49 super(name); 50 } 51 52 public void testXPaths() throws Exception { 55 int size = paths.length; 56 for ( int i = 0; i < size; i++ ) { 57 testXPath( paths[i] ); 58 } 59 } 60 61 protected void testXPath(String xpathText) { 64 XPath xpath = O3DocumentHelper.createXPath(xpathText); 65 List list = xpath.selectNodes( document ); 66 67 log( "Searched path: " + xpathText + " found: " + list.size() + " result(s)" ); 68 69 if ( VERBOSE ) { 70 log( "xpath: " + xpath ); 71 log( "results: " + list ); 72 } 73 74 for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 75 Object object = iter.next(); 76 77 log( "Found Result: " + object ); 78 79 assertTrue( "Results should be Attribute objects", object instanceof Attribute ); 80 81 Attribute attribute = (Attribute) object; 82 83 assertTrue( "Results should support the parent relationship", attribute.supportsParent() ); 84 assertTrue( "Results should contain reference to the parent element", attribute.getParent() != null ); 85 assertTrue( "Results should contain reference to the owning document", attribute.getDocument() != null ); 86 } 87 } 88 } 89 90 91 92 93 137 | Popular Tags |