KickJava   Java API By Example, From Geeks To Geeks.

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


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: TestFilter.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.Node;
16 import org.dom4j.NodeFilter;
17 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
18 import test.dom4j.AbstractTestCase;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 /** Test harness for XPath filters
24   *
25   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
26   * @version $Revision: 1.1 $
27   */

28 public class TestFilter extends AbstractTestCase {
29
30     protected static boolean VERBOSE = false;
31
32     protected static String JavaDoc[] paths = {
33         ".[name()='author']",
34         ".[name()='XXXX']",
35         ".[.='James Strachan']",
36         ".[.='XXXX']"
37     };
38
39
40     public static void main( String JavaDoc[] args ) {
41         TestRunner.run( suite() );
42     }
43
44     public static Test suite() {
45         return new TestSuite( TestFilter.class );
46     }
47
48     public TestFilter(String JavaDoc name) {
49         super(name);
50     }
51
52     // Test case(s)
53
//-------------------------------------------------------------------------
54
public void testXPaths() throws Exception JavaDoc {
55         int size = paths.length;
56         for ( int i = 0; i < size; i++ ) {
57             testXPath( paths[i] );
58         }
59     }
60
61     // Implementation methods
62
//-------------------------------------------------------------------------
63
protected void testXPath(String JavaDoc xpathExpression) throws Exception JavaDoc {
64         NodeFilter nodeFilter = O3DocumentHelper.createXPathFilter( xpathExpression );
65         assertTrue( "No NodeFilter object was created", nodeFilter != null );
66
67         if ( VERBOSE ) {
68             log( "Evaluating XPathFilter: " + xpathExpression + " using NodeFilter: " + nodeFilter );
69         }
70
71         log( "Evaluating XPathFilter: " + xpathExpression );
72
73         List JavaDoc list = document.selectNodes("//author");
74         for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
75             Node node = (Node) iter.next();
76
77             if ( nodeFilter.matches( node ) ) {
78                 log( "Matches node: "+ node.asXML() );
79             }
80             else {
81                 log( "No match for node: "+ node.asXML() );
82             }
83
84         }
85     }
86 }
87
88
89
90
91 /*
92  * Redistribution and use of this software and associated documentation
93  * ("Software"), with or without modification, are permitted provided
94  * that the following conditions are met:
95  *
96  * 1. Redistributions of source code must retain copyright
97  * statements and notices. Redistributions must also contain a
98  * copy of this document.
99  *
100  * 2. Redistributions in binary form must reproduce the
101  * above copyright notice, this list of conditions and the
102  * following disclaimer in the documentation and/or other
103  * materials provided with the distribution.
104  *
105  * 3. The name "DOM4J" must not be used to endorse or promote
106  * products derived from this Software without prior written
107  * permission of MetaStuff, Ltd. For written permission,
108  * please contact dom4j-info@metastuff.com.
109  *
110  * 4. Products derived from this Software may not be called "DOM4J"
111  * nor may "DOM4J" appear in their names without prior written
112  * permission of MetaStuff, Ltd. DOM4J is a registered
113  * trademark of MetaStuff, Ltd.
114  *
115  * 5. Due credit should be given to the DOM4J Project
116  * (http://dom4j.org/).
117  *
118  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
119  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
120  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
121  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
122  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
123  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
124  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
125  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
126  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
127  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
128  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
129  * OF THE POSSIBILITY OF SUCH DAMAGE.
130  *
131  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
132  *
133  * $Id: TestFilter.java,v 1.1 2003/07/07 10:30:30 per_nyfelt Exp $
134  */

135
Popular Tags