1 /*
2 * Copyright 2001-2005 (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
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 /***
20 * Test harness for the substring function
21 *
22 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
23 * @version $Revision: 1.3 $
24 */
25 public class SubstringTest extends AbstractTestCase {
26 public static void main(String[] args) {
27 TestRunner.run(SubstringTest.class);
28 }
29
30 // Test case(s)
31 // -------------------------------------------------------------------------
32 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 // Implementation methods
42 // -------------------------------------------------------------------------
43 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 // Object object = list.get(0);
52 // log( "(0) = " + object + " type: " + object.getClass() );
53 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 /*
70 * Redistribution and use of this software and associated documentation
71 * ("Software"), with or without modification, are permitted provided that the
72 * following conditions are met:
73 *
74 * 1. Redistributions of source code must retain copyright statements and
75 * notices. Redistributions must also contain a copy of this document.
76 *
77 * 2. Redistributions in binary form must reproduce the above copyright notice,
78 * this list of conditions and the following disclaimer in the documentation
79 * and/or other materials provided with the distribution.
80 *
81 * 3. The name "DOM4J" must not be used to endorse or promote products derived
82 * from this Software without prior written permission of MetaStuff, Ltd. For
83 * written permission, please contact dom4j-info@metastuff.com.
84 *
85 * 4. Products derived from this Software may not be called "DOM4J" nor may
86 * "DOM4J" appear in their names without prior written permission of MetaStuff,
87 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
88 *
89 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
90 *
91 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
92 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94 * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
95 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
96 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
97 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
98 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
99 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
100 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
101 * POSSIBILITY OF SUCH DAMAGE.
102 *
103 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
104 */