1 7 8 package org.dom4j.xpath; 9 10 import junit.textui.TestRunner; 11 12 import java.io.File ; 13 import java.util.List ; 14 15 import org.dom4j.AbstractTestCase; 16 import org.dom4j.Element; 17 import org.dom4j.io.SAXReader; 18 19 25 public class SubstringTest extends AbstractTestCase { 26 public static void main(String [] args) { 27 TestRunner.run(SubstringTest.class); 28 } 29 30 public void testSubstring() throws Exception { 33 String [] results1 = {"1100", "1101"}; 34 35 testSubstring("//field[substring(@id,1,2)='11']", results1); 36 37 String [] results2 = {"2111", "3111"}; 38 testSubstring("//field[substring(@id,3)='11']", results2); 39 } 40 41 protected void testSubstring(String path, String [] results) 44 throws Exception { 45 log("Using XPath: " + path); 46 47 List list = document.selectNodes(path); 48 49 log("Found: " + list); 50 51 int size = results.length; 54 assertTrue("List should contain " + size + " results: " + list, list 55 .size() == size); 56 57 for (int i = 0; i < size; i++) { 58 Element element = (Element) list.get(i); 59 assertEquals(element.attributeValue("id"), results[i]); 60 } 61 } 62 63 protected void setUp() throws Exception { 64 super.setUp(); 65 document = new SAXReader().read(new File ("xml/test/fields.xml")); 66 } 67 } 68 69 105 | Popular Tags |