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