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.*; 16 import org.dom4j.io.SAXReader; 17 import test.dom4j.AbstractTestCase; 18 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 27 public class TestGetPath extends AbstractTestCase { 28 29 public static void main( String [] args ) { 30 TestRunner.run( suite() ); 31 } 32 33 public static Test suite() { 34 return new TestSuite( TestGetPath.class ); 35 } 36 37 public TestGetPath(String name) { 38 super(name); 39 } 40 41 public void testGetPath() throws Exception { 44 log( "Testing paths" ); 45 46 48 testPath( document, "/" ); 49 50 Element root = document.getRootElement(); 51 52 testPath( root, "/root" ); 53 54 List elements = root.elements(); 55 56 testPath( (Node) elements.get(0), "/root/author", "/root/author[1]" ); 57 58 for ( int i = 0, size = elements.size(); i < size; i++ ) { 59 String path = "/root/author"; 60 String uniquePath = "/root/author"; 61 String pathRel = "author"; 62 String uniquePathRel = "author"; 63 if ( size > 1 ) { 64 uniquePath = "/root/author[" + (i + 1) + "]"; 65 uniquePathRel = "author[" + (i + 1) + "]"; 66 } 67 Element element = (Element) elements.get(i); 68 testPath( element, path, uniquePath ); 69 testRelativePath( root, element, pathRel, uniquePathRel ); 70 71 Attribute attribute = element.attribute( "name" ); 72 testPath( attribute, path + "/@name", uniquePath + "/@name" ); 73 testRelativePath( root, attribute, pathRel + "/@name", uniquePathRel + "/@name" ); 74 75 Element child = element.element( "url" ); 76 testPath( child, path + "/url", uniquePath + "/url" ); 77 testRelativePath( root, child, pathRel + "/url", uniquePathRel + "/url" ); 78 } 79 } 80 81 public void testDefaultNamespace() throws Exception { 82 SAXReader reader = new SAXReader(); 83 Document doc = reader.read( "xml/test/defaultNamespace.xml" ); 84 Element root = doc.getRootElement(); 85 testPath( root, "/*[name()='a']" ); 86 87 Element child = (Element) root.elements().get(0); 88 testPath( child, "/*[name()='a']/*[name()='b']" ); 89 testRelativePath( root, child, "*[name()='b']" ); 90 } 91 92 93 protected void testPath(Node node, String value) { 94 testPath( node, value, value ); 95 } 96 97 protected void testPath(Node node, String path, String uniquePath) { 98 assertEquals( "getPath expression should be what is expected", path, node.getPath() ); 99 assertEquals( "getUniquePath expression should be what is expected", uniquePath, node.getUniquePath() ); 100 } 101 102 protected void testRelativePath( Element context, Node node, String pathRel ) { 103 testRelativePath( context, node, pathRel, pathRel ); 104 } 105 106 protected void testRelativePath( Element context, Node node, String pathRel, String uniquePathRel ) { 107 assertEquals( "relative getPath expression should be what is expected", pathRel, node.getPath( context ) ); 108 assertEquals( "relative getUniquePath expression should be what is expected", uniquePathRel, node.getUniquePath( context ) ); 109 } 110 111 112 protected void testBranchPath(Branch branch) { 113 testNodePath( branch ); 114 115 if ( branch instanceof Element ) { 116 Element element = (Element) branch; 117 for ( Iterator iter = element.attributeIterator(); iter.hasNext(); ) { 118 Node node = (Node) iter.next(); 119 testNodePath( node ); 120 } 121 } 122 123 for ( Iterator iter = branch.nodeIterator(); iter.hasNext(); ) { 124 Node node = (Node) iter.next(); 125 if ( node instanceof Branch ) { 126 testBranchPath( (Branch) node ); 127 } 128 else { 129 testNodePath( node ); 130 } 131 } 132 } 133 134 protected void testNodePath(Node node) { 135 136 String path = node.getPath(); 137 138 log( "Path: " + path + " node: " + node ); 139 } 140 } 141 142 143 144 145 189 | Popular Tags |