1 /* 2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. 3 * 4 * This software is open source. 5 * See the bottom of this file for the licence. 6 * 7 * $Id: TestSubstring.java,v 1.6 2004/06/25 08:03:51 maartenc Exp $ 8 */ 9 10 package org.dom4j.xpath; 11 12 import java.io.File; 13 import java.util.List; 14 15 import junit.framework.Test; 16 import junit.framework.TestSuite; 17 import junit.textui.TestRunner; 18 19 import org.dom4j.AbstractTestCase; 20 import org.dom4j.Element; 21 import org.dom4j.io.SAXReader; 22 23 /*** Test harness for the substring function 24 * 25 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 26 * @version $Revision: 1.6 $ 27 */ 28 public class TestSubstring extends AbstractTestCase { 29 30 public static void main( String[] args ) { 31 TestRunner.run( suite() ); 32 } 33 34 public static Test suite() { 35 return new TestSuite( TestSubstring.class ); 36 } 37 38 public TestSubstring(String name) { 39 super(name); 40 } 41 42 // Test case(s) 43 //------------------------------------------------------------------------- 44 public void testSubstring() throws Exception { 45 String[] results1 = { 46 "1100", 47 "1101" 48 }; 49 50 testSubstring( "//field[substring(@id,1,2)='11']", results1 ); 51 52 String[] results2 = { 53 "2111", 54 "3111" 55 }; 56 testSubstring( "//field[substring(@id,3)='11']", results2 ); 57 } 58 59 // Implementation methods 60 //------------------------------------------------------------------------- 61 protected void testSubstring(String path, String[] results) throws Exception { 62 log( "Using XPath: " + path ); 63 64 List list = document.selectNodes( path ); 65 66 log( "Found: " + list ); 67 68 //Object object = list.get(0); 69 //log( "(0) = " + object + " type: " + object.getClass() ); 70 71 int size = results.length; 72 assertTrue( "List should contain " + size + " results: " + list, list.size() == size ); 73 74 for ( int i = 0; i < size; i++ ) { 75 Element element = (Element) list.get(i); 76 assertEquals( element.attributeValue( "id" ), results[i] ); 77 } 78 } 79 80 protected void setUp() throws Exception { 81 document = new SAXReader().read( new File( "xml/test/fields.xml" ) ); 82 } 83 } 84 85 86 87 88 /* 89 * Redistribution and use of this software and associated documentation 90 * ("Software"), with or without modification, are permitted provided 91 * that the following conditions are met: 92 * 93 * 1. Redistributions of source code must retain copyright 94 * statements and notices. Redistributions must also contain a 95 * copy of this document. 96 * 97 * 2. Redistributions in binary form must reproduce the 98 * above copyright notice, this list of conditions and the 99 * following disclaimer in the documentation and/or other 100 * materials provided with the distribution. 101 * 102 * 3. The name "DOM4J" must not be used to endorse or promote 103 * products derived from this Software without prior written 104 * permission of MetaStuff, Ltd. For written permission, 105 * please contact dom4j-info@metastuff.com. 106 * 107 * 4. Products derived from this Software may not be called "DOM4J" 108 * nor may "DOM4J" appear in their names without prior written 109 * permission of MetaStuff, Ltd. DOM4J is a registered 110 * trademark of MetaStuff, Ltd. 111 * 112 * 5. Due credit should be given to the DOM4J Project - 113 * http://www.dom4j.org 114 * 115 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 116 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 117 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 118 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 119 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 120 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 121 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 122 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 123 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 124 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 125 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 126 * OF THE POSSIBILITY OF SUCH DAMAGE. 127 * 128 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. 129 * 130 * $Id: TestSubstring.java,v 1.6 2004/06/25 08:03:51 maartenc Exp $ 131 */