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.XPath; 16 import org.dom4j.io.SAXReader; 17 import org.jaxen.SimpleNamespaceContext; 18 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 19 import test.dom4j.AbstractTestCase; 20 21 import java.io.File ; 22 import java.util.List ; 23 24 29 public class TestPrefix extends AbstractTestCase { 30 31 protected static boolean VERBOSE = false; 32 33 protected static String [] paths = { 34 "//xplt:anyElement", 35 "//xpl:insertText", 36 "/Template/Application1/xpl:insertText", 37 "/Template/Application2/xpl:insertText" 38 }; 39 40 41 public static void main( String [] args ) { 42 TestRunner.run( suite() ); 43 } 44 45 public static Test suite() { 46 return new TestSuite( TestPrefix.class ); 47 } 48 49 public TestPrefix(String name) { 50 super(name); 51 } 52 53 public void testXPaths() throws Exception { 56 int size = paths.length; 57 for ( int i = 0; i < size; i++ ) { 58 testXPath( paths[i] ); 59 } 60 } 61 62 protected void testXPath(String xpathText) { 65 XPath xpath = O3DocumentHelper.createXPath(xpathText); 66 67 SimpleNamespaceContext context = new SimpleNamespaceContext(); 68 context.addNamespace( "xplt", "www.xxxx.com" ); 69 context.addNamespace( "xpl", "www.xxxx.com" ); 70 xpath.setNamespaceContext( context ); 71 72 List list = xpath.selectNodes( document ); 73 74 log( "Searched path: " + xpathText + " found: " + list.size() + " result(s)" ); 75 76 assertTrue( "Should have found at lest one result", list.size() > 0 ); 77 78 if ( VERBOSE ) { 79 log( "xpath: " + xpath ); 80 log( "results: " + list ); 81 } 82 } 83 84 protected void setUp() throws Exception { 85 document = new SAXReader().read( new File ( "xml/testNamespaces.xml" ) ); 86 } 87 } 88 89 90 91 92 136 | Popular Tags |