1   /*
2    * $Header: /home/projects/jaxen/scm/jaxen/src/java/test/org/jaxen/JaxenHandlerTest.java,v 1.10 2005/01/29 20:16:29 elharo Exp $
3    * $Revision: 1.10 $
4    * $Date: 2005/01/29 20:16:29 $
5    *
6    * ====================================================================
7    *
8    * Copyright (C) 2000-2002 bob mcwhirter & James Strachan.
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions
13   * are met:
14   * 
15   * 1. Redistributions of source code must retain the above copyright
16   *    notice, this list of conditions, and the following disclaimer.
17   *
18   * 2. Redistributions in binary form must reproduce the above copyright
19   *    notice, this list of conditions, and the disclaimer that follows 
20   *    these conditions in the documentation and/or other materials 
21   *    provided with the distribution.
22   *
23   * 3. The name "Jaxen" must not be used to endorse or promote products
24   *    derived from this software without prior written permission.  For
25   *    written permission, please contact license@jaxen.org.
26   * 
27   * 4. Products derived from this software may not be called "Jaxen", nor
28   *    may "Jaxen" appear in their name, without prior written permission
29   *    from the Jaxen Project Management (pm@jaxen.org).
30   * 
31   * In addition, we request (but do not require) that you include in the 
32   * end-user documentation provided with the redistribution and/or in the 
33   * software itself an acknowledgement equivalent to the following:
34   *     "This product includes software developed by the
35   *      Jaxen Project (http://www.jaxen.org/)."
36   * Alternatively, the acknowledgment may be graphical using the logos 
37   * available at http://www.jaxen.org/
38   *
39   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42   * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
43   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50   * SUCH DAMAGE.
51   *
52   * ====================================================================
53   * This software consists of voluntary contributions made by many 
54   * individuals on behalf of the Jaxen Project and was originally 
55   * created by bob mcwhirter <bob@werken.com> and 
56   * James Strachan <jstrachan@apache.org>.  For more information on the 
57   * Jaxen Project, please see <http://www.jaxen.org/>.
58   * 
59   * $Id: JaxenHandlerTest.java,v 1.10 2005/01/29 20:16:29 elharo Exp $
60   */
61  
62  
63  
64  package org.jaxen;
65  
66  import junit.framework.TestCase;
67  
68  import org.jaxen.expr.DefaultXPathFactory;
69  import org.jaxen.expr.XPathExpr;
70  import org.jaxen.saxpath.XPathReader;
71  import org.jaxen.saxpath.XPathSyntaxException;
72  import org.jaxen.saxpath.helpers.XPathReaderFactory;
73  
74  public class JaxenHandlerTest extends TestCase
75  {
76      String[] ignore_paths = {
77          "foo[.='bar']",
78          "foo[.!='bar']",
79      };
80      
81      String[] paths = {
82          "/",
83          "*",
84          "//foo",
85          "/*",
86          "/.",
87          "/foo[/bar[/baz]]",
88          "/foo/bar/baz[(1 or 2) + 3 * 4 + 8 and 9]",
89          "/foo/bar/baz",
90          ".[1]",
91          "self::node()",
92          ".",
93          "count(/)",
94          "foo[1]",
95          "/baz[(1 or 2) + 3 * 4 + 8 and 9]",
96          "foo/bar[/baz[(1 or 2) - 3 mod 4 + 8 and 9 div 8]]",
97          "foo/bar/yeah:baz[a/b/c and toast]",
98          "/foo/bar[../x='123']",
99          "/foo[@bar='1234']",
100         "foo|bar",
101         "/foo|/bar[@id='1234']",
102         "count(//author/attribute::*)",
103         "$author",
104          "10 + $foo",
105         "10 + (count(descendant::author) * 5)",
106         "10 + count(descendant::author) * 5",
107         "2 + (2 * 5)",
108         "sum(count(//author), 5)",
109         "sum(count(//author),count(//author/attribute::*))",
110         "12 + sum(count(//author),count(//author/attribute::*)) div 2",
111         "text()[.='foo']",
112         "/*/*[@id='123']",
113         "/child::node()/child::node()[@id='_13563275']",
114         "$foo:bar",
115         //"foo:bar()",
116         "/foo/bar[@a='1' and @c!='2']",
117     };
118 
119     String[] bogusPaths = { };
120     
121     String[] ignore_bogusPaths = {        
122         // this path is bogus because of a trailing /
123         "/foo/bar/",
124         
125         // This path is bogus because '/' is not division, but
126         // rather just the step separator.
127         "12 + sum(count(//author),count(//author/attribute::*)) / 2",
128         "id()/2",
129         "+"
130     };
131     
132     public JaxenHandlerTest(String name)
133     {
134         super( name );
135     }
136 
137     public void setUp()
138     {
139     }
140 
141     public void tearDown()
142     {
143     }
144 
145     public void testValidPaths()
146     {
147             
148         String path = null;
149 
150         try
151         {
152             XPathReader reader = XPathReaderFactory.createReader();
153             
154             JaxenHandler handler = new JaxenHandler();
155             
156             handler.setXPathFactory( new DefaultXPathFactory() );
157             
158             reader.setXPathHandler( handler );
159 
160             for ( int i = 0; i < paths.length; i++ ) {
161                 path = paths[i];
162                 
163                 // System.err.println("-----------------");
164                 // System.err.println( "parsing: " + path );
165                 // System.err.println("-----------------");
166 
167                 reader.parse(path);
168 
169                 XPathExpr xpath = handler.getXPathExpr(false);
170 
171                 // System.err.println("-----------------");
172                 // System.err.println( xpath.toString() );
173                 // System.err.println("-----------------");
174                 // System.err.println( xpath.getText() );
175 
176                 xpath = handler.getXPathExpr();
177 
178                 // System.err.println("-----------------");
179                 // System.err.println("-----------------");
180 
181                 // System.err.println( xpath.toString() );
182                 // System.err.println("-----------------");
183                 // System.err.println( xpath.getText() );
184             }
185         }
186         catch (Exception e)
187         {
188             e.printStackTrace();
189             fail( path + " -> " + e.getMessage() );
190         }
191     }
192 
193     public void testBogusPaths()
194     {
195         try
196         {
197             XPathReader reader = XPathReaderFactory.createReader();
198             
199             JaxenHandler handler = new JaxenHandler();
200             
201             handler.setXPathFactory( new DefaultXPathFactory() );
202             
203             reader.setXPathHandler( handler );
204             
205             for ( int i = 0; i < bogusPaths.length; i++ ) {
206                 String path = bogusPaths[i];
207                 
208                 System.out.println("-----------------");
209                 System.out.println( "parsing bogus path : " + path );
210                 System.out.println("-----------------");
211 
212                 try
213                 {                    
214                     reader.parse(path);
215 
216                     XPathExpr xpath = handler.getXPathExpr(false);
217 
218                     System.out.println( "Parsed as: " + xpath );
219                     
220                     fail( "Parsed bogus path as: " + xpath );
221                 }
222                 catch (XPathSyntaxException e)
223                 {                    
224                     
225                     System.out.println("-----------------");
226                     //System.err.println( "Exception: " + e.getMessage() );
227                     System.out.println( "Exception: ");
228                     System.out.println( e.getMultilineMessage() );
229                     System.out.println("-----------------");
230                 }
231             }
232         }
233         catch (Exception e)
234         {
235             e.printStackTrace();
236             fail( e.getMessage() );
237         }
238     }
239 }