1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jxpath;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Set;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.commons.jxpath.ri.model.NodePointer;
29  
30  /***
31   * Abstract superclass for various JXPath tests.
32   *
33   * @author Dmitri Plotnikov
34   * @version $Revision: 1.35 $ $Date: 2004/02/29 14:17:40 $
35   */
36  
37  public abstract class JXPathTestCase extends TestCase {
38      /***
39       * Construct a new instance of this test case.
40       *
41       * @param name Name of the test case
42       */
43      public JXPathTestCase(String name) {
44          super(name);
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, Object o3) {
251         Set list = new HashSet();
252         list.add(o1);
253         list.add(o2);
254         list.add(o3);
255         return list;
256     }
257 
258     protected static List list(Object o1, Object o2, Object o3, Object o4) {
259         List list = new ArrayList();
260         list.add(o1);
261         list.add(o2);
262         list.add(o3);
263         list.add(o4);
264         return list;
265     }
266 
267     protected static Set set(Object o1, Object o2, Object o3, Object o4) {
268         Set list = new HashSet();
269         list.add(o1);
270         list.add(o2);
271         list.add(o3);
272         list.add(o4);
273         return list;
274     }
275 
276     protected static List list(Object o1, Object o2, Object o3,
277                 Object o4, Object o5)
278     {
279         List list = new ArrayList();
280         list.add(o1);
281         list.add(o2);
282         list.add(o3);
283         list.add(o4);
284         list.add(o5);
285         return list;
286     }
287 
288     protected static Set set(Object o1, Object o2, Object o3, 
289                 Object o4, Object o5) 
290     {
291         Set list = new HashSet();
292         list.add(o1);
293         list.add(o2);
294         list.add(o3);
295         list.add(o4);
296         list.add(o5);
297         return list;
298     }
299 
300     protected static List list(Object o1, Object o2, Object o3,
301                 Object o4, Object o5, Object o6)
302     {
303         List list = new ArrayList();
304         list.add(o1);
305         list.add(o2);
306         list.add(o3);
307         list.add(o4);
308         list.add(o5);
309         list.add(o6);
310         return list;
311     }
312 
313     protected static Set set(Object o1, Object o2, Object o3,
314                 Object o4, Object o5, Object o6)
315     {
316         Set list = new HashSet();
317         list.add(o1);
318         list.add(o2);
319         list.add(o3);
320         list.add(o4);
321         list.add(o5);
322         list.add(o6);
323         return list;
324     }
325     
326     protected static List list(Object o1, Object o2, Object o3,
327                 Object o4, Object o5, Object o6, Object o7)
328     {
329         List list = new ArrayList();
330         list.add(o1);
331         list.add(o2);
332         list.add(o3);
333         list.add(o4);
334         list.add(o5);
335         list.add(o6);
336         list.add(o7);
337         return list;
338     }
339 
340     protected static Set set(Object o1, Object o2, Object o3,
341                 Object o4, Object o5, Object o6, Object o7)
342     {
343         Set list = new HashSet();
344         list.add(o1);
345         list.add(o2);
346         list.add(o3);
347         list.add(o4);
348         list.add(o5);
349         list.add(o6);
350         list.add(o7);
351         return list;
352     }
353     
354 }