1 7 8 package org.dom4j.xpath; 9 10 import junit.textui.TestRunner; 11 12 import java.io.File ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.dom4j.AbstractTestCase; 17 import org.dom4j.DocumentHelper; 18 import org.dom4j.Namespace; 19 import org.dom4j.XPath; 20 import org.dom4j.io.SAXReader; 21 22 28 public class NamespaceTest extends AbstractTestCase { 29 protected static String [] paths = {"namespace::*", 30 "/Template/Application1/namespace::*", 31 "/Template/Application1/namespace::xplt", "//namespace::*"}; 32 33 public static void main(String [] args) { 34 TestRunner.run(NamespaceTest.class); 35 } 36 37 public void testXPaths() throws Exception { 40 int size = paths.length; 41 42 for (int i = 0; i < size; i++) { 43 testXPath(paths[i]); 44 } 45 } 46 47 protected void testXPath(String xpathText) { 50 XPath xpath = DocumentHelper.createXPath(xpathText); 51 List list = xpath.selectNodes(document); 52 53 log("Searched path: " + xpathText + " found: " + list.size() 54 + " result(s)"); 55 56 for (Iterator iter = list.iterator(); iter.hasNext();) { 57 Object object = iter.next(); 58 59 log("Found Result: " + object); 60 61 assertTrue("Results should be Namespace objects", 62 object instanceof Namespace); 63 64 Namespace namespace = (Namespace) object; 65 66 log("Parent node: " + namespace.getParent()); 67 68 assertTrue("Results should support the parent relationship", 69 namespace.supportsParent()); 70 assertTrue( 71 "Results should contain reference to the parent element", 72 namespace.getParent() != null); 73 assertTrue("Results should contain reference to the document", 74 namespace.getDocument() != null); 75 } 76 } 77 78 protected void setUp() throws Exception { 79 super.setUp(); 80 document = new SAXReader().read(new File ("xml/testNamespaces.xml")); 81 } 82 } 83 84 120 | Popular Tags |