KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > xpath > TestSubstring


1 /*
2  * Copyright 2001 (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.1 2003/07/07 10:30:30 per_nyfelt Exp $
8  */

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 JavaDoc;
20 import java.util.List JavaDoc;
21
22 /** Test harness for the substring function
23   *
24   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
25   * @version $Revision: 1.1 $
26   */

27 public class TestSubstring extends AbstractTestCase {
28
29     public static void main( String JavaDoc[] args ) {
30         TestRunner.run( suite() );
31     }
32
33     public static Test suite() {
34         return new TestSuite( TestSubstring.class );
35     }
36
37     public TestSubstring(String JavaDoc name) {
38         super(name);
39     }
40
41     // Test case(s)
42
//-------------------------------------------------------------------------
43
public void testSubstring() throws Exception JavaDoc {
44         String JavaDoc[] results1 = {
45             "1100",
46             "1101"
47         };
48
49         testSubstring( "//field[substring(@id,1,2)='11']", results1 );
50
51         String JavaDoc[] results2 = {
52             "2111",
53             "3111"
54         };
55         testSubstring( "//field[substring(@id,3)='11']", results2 );
56     }
57
58     // Implementation methods
59
//-------------------------------------------------------------------------
60
protected void testSubstring(String JavaDoc path, String JavaDoc[] results) throws Exception JavaDoc {
61         log( "Using XPath: " + path );
62
63         List JavaDoc list = document.selectNodes( path );
64
65         log( "Found: " + list );
66
67         //Object object = list.get(0);
68
//log( "(0) = " + object + " type: " + object.getClass() );
69

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 JavaDoc {
80         document = new SAXReader().read( new File JavaDoc( "xml/test/fields.xml" ) );
81     }
82 }
83
84
85
86
87 /*
88  * Redistribution and use of this software and associated documentation
89  * ("Software"), with or without modification, are permitted provided
90  * that the following conditions are met:
91  *
92  * 1. Redistributions of source code must retain copyright
93  * statements and notices. Redistributions must also contain a
94  * copy of this document.
95  *
96  * 2. Redistributions in binary form must reproduce the
97  * above copyright notice, this list of conditions and the
98  * following disclaimer in the documentation and/or other
99  * materials provided with the distribution.
100  *
101  * 3. The name "DOM4J" must not be used to endorse or promote
102  * products derived from this Software without prior written
103  * permission of MetaStuff, Ltd. For written permission,
104  * please contact dom4j-info@metastuff.com.
105  *
106  * 4. Products derived from this Software may not be called "DOM4J"
107  * nor may "DOM4J" appear in their names without prior written
108  * permission of MetaStuff, Ltd. DOM4J is a registered
109  * trademark of MetaStuff, Ltd.
110  *
111  * 5. Due credit should be given to the DOM4J Project
112  * (http://dom4j.org/).
113  *
114  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
115  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
116  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
117  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
118  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
119  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
120  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
121  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
122  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
123  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
124  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
125  * OF THE POSSIBILITY OF SUCH DAMAGE.
126  *
127  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
128  *
129  * $Id: TestSubstring.java,v 1.1 2003/07/07 10:30:30 per_nyfelt Exp $
130  */

131
Popular Tags