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.Node; 16 import org.dom4j.XPath; 17 import org.jaxen.SimpleVariableContext; 18 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 19 import test.dom4j.AbstractTestCase; 20 21 import java.util.List ; 22 23 28 public class TestVariable extends AbstractTestCase { 29 30 protected static boolean VERBOSE = true; 31 32 protected static String [] paths = { 33 "$author", 34 "$author/@name", 35 "$root/author", 36 "$root/author[1]", 37 "$root/author[1]/@name", 38 "$author/@name" 39 }; 40 41 private SimpleVariableContext variableContext = new SimpleVariableContext(); 42 private Node rootNode; 43 private Node authorNode; 44 45 46 public static void main( String [] args ) { 47 TestRunner.run( suite() ); 48 } 49 50 public static Test suite() { 51 return new TestSuite( TestVariable.class ); 52 } 53 54 public TestVariable(String name) { 55 super(name); 56 } 57 58 public void testXPaths() throws Exception { 61 int size = paths.length; 62 for ( int i = 0; i < size; i++ ) { 63 testXPath( paths[i] ); 64 } 65 } 66 67 protected void testXPath(String xpathText) throws Exception { 68 XPath xpath = createXPath( xpathText ); 69 List list = xpath.selectNodes( document ); 70 71 log( "Searched path: " + xpathText + " found: " + list.size() + " result(s)" ); 72 73 if ( VERBOSE ) { 74 log( "" ); 75 log( "xpath: " + xpath ); 76 log( "" ); 77 log( "results: " + list ); 78 } 79 80 assertTrue( "Results should not contain the root node", ! list.contains( rootNode ) ); 81 } 82 83 protected XPath createXPath( String xpath ) throws Exception { 84 return O3DocumentHelper.createXPath( xpath, variableContext ); 85 } 86 87 protected void setUp() throws Exception { 88 super.setUp(); 89 90 rootNode = document.selectSingleNode( "/root" ); 91 authorNode = document.selectSingleNode( "/root/author[1]" ); 92 93 variableContext.setVariableValue( "root", rootNode ); 94 variableContext.setVariableValue( "author", authorNode ); 95 } 96 } 97 98 99 100 101 145 | Popular Tags |