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.Element; 16 import org.dom4j.io.SAXReader; 17 import test.dom4j.AbstractTestCase; 18 19 import java.io.File ; 20 import java.util.List ; 21 22 27 public class TestSubstring 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( TestSubstring.class ); 35 } 36 37 public TestSubstring(String name) { 38 super(name); 39 } 40 41 public void testSubstring() throws Exception { 44 String [] results1 = { 45 "1100", 46 "1101" 47 }; 48 49 testSubstring( "//field[substring(@id,1,2)='11']", results1 ); 50 51 String [] results2 = { 52 "2111", 53 "3111" 54 }; 55 testSubstring( "//field[substring(@id,3)='11']", results2 ); 56 } 57 58 protected void testSubstring(String path, String [] results) throws Exception { 61 log( "Using XPath: " + path ); 62 63 List list = document.selectNodes( path ); 64 65 log( "Found: " + list ); 66 67 70 int size = results.length; 71 assertTrue( "List should contain " + size + " results: " + list, list.size() == size ); 72 73 for ( int i = 0; i < size; i++ ) { 74 Element element = (Element) list.get(i); 75 assertEquals( element.attributeValue( "id" ), results[i] ); 76 } 77 } 78 79 protected void setUp() throws Exception { 80 document = new SAXReader().read( new File ( "xml/test/fields.xml" ) ); 81 } 82 } 83 84 85 86 87 131 | Popular Tags |