View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Set;
27  
28  import junit.framework.TestCase;
29  
30  import org.apache.commons.jxpath.ri.model.NodePointer;
31  
32  /**
33   * Abstract superclass for various JXPath tests.
34   *
35   * @author Dmitri Plotnikov
36   * @version $Revision: 652845 $ $Date: 2008-05-02 13:46:46 -0400 (Fri, 02 May 2008) $
37   */
38  public abstract class JXPathTestCase extends TestCase {
39  
40      /**
41       * Construct a new instance of this test case.
42       */
43      public JXPathTestCase() {
44          Locale.setDefault(Locale.US);
45      }
46      
47      protected void assertXPathValue(JXPathContext ctx,
48                  String xpath, Object expected)
49      {
50          ctx.setLenient(false);
51          Object actual = ctx.getValue(xpath);
52          assertEquals("Evaluating <" + xpath + ">", expected, actual);
53      }
54  
55      protected void assertXPathValue(JXPathContext ctx,
56                  String xpath, Object expected, Class resultType)
57      {
58          ctx.setLenient(false);
59          Object actual = ctx.getValue(xpath, resultType);
60          assertEquals("Evaluating <" + xpath + ">", expected, actual);
61      }
62  
63      protected void assertXPathValueLenient(JXPathContext ctx,
64                  String xpath, Object expected)
65      {
66          ctx.setLenient(true);
67          Object actual = ctx.getValue(xpath);
68          ctx.setLenient(false);
69          assertEquals("Evaluating lenient <" + xpath + ">", expected, actual);
70      }
71  
72      protected void assertXPathSetValue(JXPathContext ctx,
73                  String xpath, Object value)
74      {
75          assertXPathSetValue(ctx, xpath, value, value);
76      }
77      
78      protected void assertXPathSetValue(JXPathContext ctx,
79                  String xpath, Object value, Object expected)
80      {
81          ctx.setValue(xpath, value);
82          Object actual = ctx.getValue(xpath);
83          assertEquals("Modifying <" + xpath + ">", expected, actual);
84      }
85      
86      protected void assertXPathCreatePath(JXPathContext ctx,
87                  String xpath, 
88                  Object expectedValue, String expectedPath)
89      {
90          Pointer pointer = ctx.createPath(xpath);
91          assertEquals("Creating path <" + xpath + ">", 
92                  expectedPath, pointer.asPath());
93                  
94          assertEquals("Creating path (pointer value) <" + xpath + ">", 
95                  expectedValue, pointer.getValue());
96                  
97          assertEquals("Creating path (context value) <" + xpath + ">", 
98                  expectedValue, ctx.getValue(pointer.asPath()));
99      }
100     
101     protected void assertXPathCreatePathAndSetValue(JXPathContext ctx,
102                 String xpath, Object value,
103                 String expectedPath)
104     {
105         Pointer pointer = ctx.createPathAndSetValue(xpath, value);
106         assertEquals("Creating path <" + xpath + ">", 
107                 expectedPath, pointer.asPath());
108                 
109         assertEquals("Creating path (pointer value) <" + xpath + ">", 
110                 value, pointer.getValue());
111                 
112         assertEquals("Creating path (context value) <" + xpath + ">", 
113                 value, ctx.getValue(pointer.asPath()));
114     }    
115     
116     protected void assertXPathPointer(JXPathContext ctx,
117                 String xpath, String expected)
118     {
119         ctx.setLenient(false);
120         Pointer pointer = ctx.getPointer(xpath);
121         String actual = pointer.toString();
122         assertEquals("Evaluating pointer <" + xpath + ">", expected, actual);
123     }
124 
125     protected void assertXPathPointerLenient(JXPathContext ctx,
126                 String xpath, String expected)
127     {
128         ctx.setLenient(true);
129         Pointer pointer = ctx.getPointer(xpath);
130         String actual = pointer.toString();
131         assertEquals("Evaluating pointer <" + xpath + ">", expected, actual);
132     }
133 
134     protected void assertXPathValueAndPointer(JXPathContext ctx,
135                 String xpath, Object expectedValue, String expectedPointer)
136     {
137         assertXPathValue(ctx, xpath, expectedValue);
138         assertXPathPointer(ctx, xpath, expectedPointer);
139     }
140     
141     protected void assertXPathValueIterator(JXPathContext ctx,
142                 String xpath, Collection expected)
143     {
144         Collection actual;
145         if (expected instanceof List) {
146             actual = new ArrayList();
147         }
148         else {
149             actual = new HashSet();
150         }
151         Iterator it = ctx.iterate(xpath);
152         while (it.hasNext()) {
153             actual.add(it.next());
154         }
155         assertEquals("Evaluating value iterator <" + xpath + ">",
156                 expected, actual);
157     }
158 
159     protected void assertXPathPointerIterator(
160         JXPathContext ctx,
161         String xpath,
162         Collection expected) 
163     {
164         Collection actual;
165         if (expected instanceof List) {
166             actual = new ArrayList();
167         }
168         else {
169             actual = new HashSet();
170         }
171         Iterator it = ctx.iteratePointers(xpath);
172         while (it.hasNext()) {
173             Pointer pointer = (Pointer) it.next();
174             actual.add(pointer.toString());
175         }
176         assertEquals(
177             "Evaluating pointer iterator <" + xpath + ">",
178             expected,
179             actual);
180     }
181 
182     protected void assertDocumentOrder(
183         JXPathContext context,
184         String path1,
185         String path2,
186         int expected) 
187     {
188         NodePointer np1 = (NodePointer) context.getPointer(path1);
189         NodePointer np2 = (NodePointer) context.getPointer(path2);
190         int res = np1.compareTo(np2);
191         if (res < 0) {
192             res = -1;
193         }
194         else if (res > 0) {
195             res = 1;
196         }
197         assertEquals(
198             "Comparing paths '" + path1 + "' and '" + path2 + "'",
199             expected,
200             res);
201     }
202     
203     protected void assertXPathValueType(
204             JXPathContext ctx,
205             String xpath,
206             Class clazz) 
207     {
208         ctx.setLenient(false);
209         Object actual = ctx.getValue(xpath);
210         assertTrue("Evaluating <" + xpath + "> = " + actual.getClass(), 
211                 clazz.isAssignableFrom(actual.getClass()));
212     }
213     
214     protected void assertXPathNodeType(
215             JXPathContext ctx,
216             String xpath,
217             Class clazz) 
218     {
219         ctx.setLenient(false);
220         Pointer actual = ctx.getPointer(xpath);
221         assertTrue("Evaluating <" + xpath + "> = " + actual.getNode().getClass(), 
222                 clazz.isAssignableFrom(actual.getNode().getClass()));
223     }
224     
225     protected static List list() {
226         return Collections.EMPTY_LIST;
227     }
228 
229     protected static List list(Object o1) {
230         List list = new ArrayList();
231         list.add(o1);
232         return list;
233     }
234 
235     protected static List list(Object o1, Object o2) {
236         List list = new ArrayList();
237         list.add(o1);
238         list.add(o2);
239         return list;
240     }
241 
242     protected static List list(Object o1, Object o2, Object o3) {
243         List list = new ArrayList();
244         list.add(o1);
245         list.add(o2);
246         list.add(o3);
247         return list;
248     }
249 
250     protected static Set set(Object o1, Object o2) {
251         Set list = new HashSet();
252         list.add(o1);
253         list.add(o2);
254         return list;
255     }
256 
257     protected static Set set(Object o1, Object o2, Object o3) {
258         Set list = new HashSet();
259         list.add(o1);
260         list.add(o2);
261         list.add(o3);
262         return list;
263     }
264 
265     protected static List list(Object o1, Object o2, Object o3, Object o4) {
266         List list = new ArrayList();
267         list.add(o1);
268         list.add(o2);
269         list.add(o3);
270         list.add(o4);
271         return list;
272     }
273 
274     protected static Set set(Object o1, Object o2, Object o3, Object o4) {
275         Set list = new HashSet();
276         list.add(o1);
277         list.add(o2);
278         list.add(o3);
279         list.add(o4);
280         return list;
281     }
282 
283     protected static List list(Object o1, Object o2, Object o3,
284                 Object o4, Object o5)
285     {
286         List list = new ArrayList();
287         list.add(o1);
288         list.add(o2);
289         list.add(o3);
290         list.add(o4);
291         list.add(o5);
292         return list;
293     }
294 
295     protected static Set set(Object o1, Object o2, Object o3, 
296                 Object o4, Object o5) 
297     {
298         Set list = new HashSet();
299         list.add(o1);
300         list.add(o2);
301         list.add(o3);
302         list.add(o4);
303         list.add(o5);
304         return list;
305     }
306 
307     protected static List list(Object o1, Object o2, Object o3,
308                 Object o4, Object o5, Object o6)
309     {
310         List list = new ArrayList();
311         list.add(o1);
312         list.add(o2);
313         list.add(o3);
314         list.add(o4);
315         list.add(o5);
316         list.add(o6);
317         return list;
318     }
319 
320     protected static Set set(Object o1, Object o2, Object o3,
321                 Object o4, Object o5, Object o6)
322     {
323         Set list = new HashSet();
324         list.add(o1);
325         list.add(o2);
326         list.add(o3);
327         list.add(o4);
328         list.add(o5);
329         list.add(o6);
330         return list;
331     }
332     
333     protected static List list(Object o1, Object o2, Object o3,
334                 Object o4, Object o5, Object o6, Object o7)
335     {
336         List list = new ArrayList();
337         list.add(o1);
338         list.add(o2);
339         list.add(o3);
340         list.add(o4);
341         list.add(o5);
342         list.add(o6);
343         list.add(o7);
344         return list;
345     }
346 
347     protected static Set set(Object o1, Object o2, Object o3,
348                 Object o4, Object o5, Object o6, Object o7)
349     {
350         Set list = new HashSet();
351         list.add(o1);
352         list.add(o2);
353         list.add(o3);
354         list.add(o4);
355         list.add(o5);
356         list.add(o6);
357         list.add(o7);
358         return list;
359     }
360     
361 }