KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jxpath > JXPathTestCase


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 JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.apache.commons.jxpath.ri.model.NodePointer;
30
31 /**
32  * Abstract superclass for various JXPath tests.
33  *
34  * @author Dmitri Plotnikov
35  * @version $Revision: 1.36 $ $Date: 2004/07/16 22:52:32 $
36  */

37
38 public abstract class JXPathTestCase extends TestCase {
39     /**
40      * Construct a new instance of this test case.
41      *
42      * @param name Name of the test case
43      */

44     public JXPathTestCase(String JavaDoc name) {
45         super(name);
46         Locale.setDefault(Locale.US);
47     }
48     
49     protected void assertXPathValue(JXPathContext ctx,
50                 String JavaDoc xpath, Object JavaDoc expected)
51     {
52         ctx.setLenient(false);
53         Object JavaDoc actual = ctx.getValue(xpath);
54         assertEquals("Evaluating <" + xpath + ">", expected, actual);
55     }
56
57     protected void assertXPathValue(JXPathContext ctx,
58                 String JavaDoc xpath, Object JavaDoc expected, Class JavaDoc resultType)
59     {
60         ctx.setLenient(false);
61         Object JavaDoc actual = ctx.getValue(xpath, resultType);
62         assertEquals("Evaluating <" + xpath + ">", expected, actual);
63     }
64
65     protected void assertXPathValueLenient(JXPathContext ctx,
66                 String JavaDoc xpath, Object JavaDoc expected)
67     {
68         ctx.setLenient(true);
69         Object JavaDoc actual = ctx.getValue(xpath);
70         ctx.setLenient(false);
71         assertEquals("Evaluating lenient <" + xpath + ">", expected, actual);
72     }
73
74     protected void assertXPathSetValue(JXPathContext ctx,
75                 String JavaDoc xpath, Object JavaDoc value)
76     {
77         assertXPathSetValue(ctx, xpath, value, value);
78     }
79     
80     protected void assertXPathSetValue(JXPathContext ctx,
81                 String JavaDoc xpath, Object JavaDoc value, Object JavaDoc expected)
82     {
83         ctx.setValue(xpath, value);
84         Object JavaDoc actual = ctx.getValue(xpath);
85         assertEquals("Modifying <" + xpath + ">", expected, actual);
86     }
87     
88     protected void assertXPathCreatePath(JXPathContext ctx,
89                 String JavaDoc xpath,
90                 Object JavaDoc expectedValue, String JavaDoc expectedPath)
91     {
92         Pointer pointer = ctx.createPath(xpath);
93         assertEquals("Creating path <" + xpath + ">",
94                 expectedPath, pointer.asPath());
95                 
96         assertEquals("Creating path (pointer value) <" + xpath + ">",
97                 expectedValue, pointer.getValue());
98                 
99         assertEquals("Creating path (context value) <" + xpath + ">",
100                 expectedValue, ctx.getValue(pointer.asPath()));
101     }
102     
103     protected void assertXPathCreatePathAndSetValue(JXPathContext ctx,
104                 String JavaDoc xpath, Object JavaDoc value,
105                 String JavaDoc expectedPath)
106     {
107         Pointer pointer = ctx.createPathAndSetValue(xpath, value);
108         assertEquals("Creating path <" + xpath + ">",
109                 expectedPath, pointer.asPath());
110                 
111         assertEquals("Creating path (pointer value) <" + xpath + ">",
112                 value, pointer.getValue());
113                 
114         assertEquals("Creating path (context value) <" + xpath + ">",
115                 value, ctx.getValue(pointer.asPath()));
116     }
117     
118     protected void assertXPathPointer(JXPathContext ctx,
119                 String JavaDoc xpath, String JavaDoc expected)
120     {
121         ctx.setLenient(false);
122         Pointer pointer = ctx.getPointer(xpath);
123         String JavaDoc actual = pointer.toString();
124         assertEquals("Evaluating pointer <" + xpath + ">", expected, actual);
125     }
126
127     protected void assertXPathPointerLenient(JXPathContext ctx,
128                 String JavaDoc xpath, String JavaDoc expected)
129     {
130         ctx.setLenient(true);
131         Pointer pointer = ctx.getPointer(xpath);
132         String JavaDoc actual = pointer.toString();
133         assertEquals("Evaluating pointer <" + xpath + ">", expected, actual);
134     }
135
136     protected void assertXPathValueAndPointer(JXPathContext ctx,
137                 String JavaDoc xpath, Object JavaDoc expectedValue, String JavaDoc expectedPointer)
138     {
139         assertXPathValue(ctx, xpath, expectedValue);
140         assertXPathPointer(ctx, xpath, expectedPointer);
141     }
142     
143     protected void assertXPathValueIterator(JXPathContext ctx,
144                 String JavaDoc xpath, Collection JavaDoc expected)
145     {
146         Collection JavaDoc actual;
147         if (expected instanceof List JavaDoc) {
148             actual = new ArrayList JavaDoc();
149         }
150         else {
151             actual = new HashSet JavaDoc();
152         }
153         Iterator JavaDoc it = ctx.iterate(xpath);
154         while (it.hasNext()) {
155             actual.add(it.next());
156         }
157         assertEquals("Evaluating value iterator <" + xpath + ">",
158                 expected, actual);
159     }
160
161     protected void assertXPathPointerIterator(
162         JXPathContext ctx,
163         String JavaDoc xpath,
164         Collection JavaDoc expected)
165     {
166         Collection JavaDoc actual;
167         if (expected instanceof List JavaDoc) {
168             actual = new ArrayList JavaDoc();
169         }
170         else {
171             actual = new HashSet JavaDoc();
172         }
173         Iterator JavaDoc it = ctx.iteratePointers(xpath);
174         while (it.hasNext()) {
175             Pointer pointer = (Pointer) it.next();
176             actual.add(pointer.toString());
177         }
178         assertEquals(
179             "Evaluating pointer iterator <" + xpath + ">",
180             expected,
181             actual);
182     }
183
184     protected void assertDocumentOrder(
185         JXPathContext context,
186         String JavaDoc path1,
187         String JavaDoc path2,
188         int expected)
189     {
190         NodePointer np1 = (NodePointer) context.getPointer(path1);
191         NodePointer np2 = (NodePointer) context.getPointer(path2);
192         int res = np1.compareTo(np2);
193         if (res < 0) {
194             res = -1;
195         }
196         else if (res > 0) {
197             res = 1;
198         }
199         assertEquals(
200             "Comparing paths '" + path1 + "' and '" + path2 + "'",
201             expected,
202             res);
203     }
204     
205     protected void assertXPathValueType(
206             JXPathContext ctx,
207             String JavaDoc xpath,
208             Class JavaDoc clazz)
209     {
210         ctx.setLenient(false);
211         Object JavaDoc actual = ctx.getValue(xpath);
212         assertTrue("Evaluating <" + xpath + "> = " + actual.getClass(),
213                 clazz.isAssignableFrom(actual.getClass()));
214     }
215     
216     protected void assertXPathNodeType(
217             JXPathContext ctx,
218             String JavaDoc xpath,
219             Class JavaDoc clazz)
220     {
221         ctx.setLenient(false);
222         Pointer actual = ctx.getPointer(xpath);
223         assertTrue("Evaluating <" + xpath + "> = " + actual.getNode().getClass(),
224                 clazz.isAssignableFrom(actual.getNode().getClass()));
225     }
226     
227     protected static List JavaDoc list() {
228         return Collections.EMPTY_LIST;
229     }
230
231     protected static List JavaDoc list(Object JavaDoc o1) {
232         List JavaDoc list = new ArrayList JavaDoc();
233         list.add(o1);
234         return list;
235     }
236
237     protected static List JavaDoc list(Object JavaDoc o1, Object JavaDoc o2) {
238         List JavaDoc list = new ArrayList JavaDoc();
239         list.add(o1);
240         list.add(o2);
241         return list;
242     }
243
244     protected static List JavaDoc list(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3) {
245         List JavaDoc list = new ArrayList JavaDoc();
246         list.add(o1);
247         list.add(o2);
248         list.add(o3);
249         return list;
250     }
251
252     protected static Set JavaDoc set(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3) {
253         Set JavaDoc list = new HashSet JavaDoc();
254         list.add(o1);
255         list.add(o2);
256         list.add(o3);
257         return list;
258     }
259
260     protected static List JavaDoc list(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3, Object JavaDoc o4) {
261         List JavaDoc list = new ArrayList JavaDoc();
262         list.add(o1);
263         list.add(o2);
264         list.add(o3);
265         list.add(o4);
266         return list;
267     }
268
269     protected static Set JavaDoc set(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3, Object JavaDoc o4) {
270         Set JavaDoc list = new HashSet JavaDoc();
271         list.add(o1);
272         list.add(o2);
273         list.add(o3);
274         list.add(o4);
275         return list;
276     }
277
278     protected static List JavaDoc list(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3,
279                 Object JavaDoc o4, Object JavaDoc o5)
280     {
281         List JavaDoc list = new ArrayList JavaDoc();
282         list.add(o1);
283         list.add(o2);
284         list.add(o3);
285         list.add(o4);
286         list.add(o5);
287         return list;
288     }
289
290     protected static Set JavaDoc set(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3,
291                 Object JavaDoc o4, Object JavaDoc o5)
292     {
293         Set JavaDoc list = new HashSet JavaDoc();
294         list.add(o1);
295         list.add(o2);
296         list.add(o3);
297         list.add(o4);
298         list.add(o5);
299         return list;
300     }
301
302     protected static List JavaDoc list(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3,
303                 Object JavaDoc o4, Object JavaDoc o5, Object JavaDoc o6)
304     {
305         List JavaDoc list = new ArrayList JavaDoc();
306         list.add(o1);
307         list.add(o2);
308         list.add(o3);
309         list.add(o4);
310         list.add(o5);
311         list.add(o6);
312         return list;
313     }
314
315     protected static Set JavaDoc set(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3,
316                 Object JavaDoc o4, Object JavaDoc o5, Object JavaDoc o6)
317     {
318         Set JavaDoc list = new HashSet JavaDoc();
319         list.add(o1);
320         list.add(o2);
321         list.add(o3);
322         list.add(o4);
323         list.add(o5);
324         list.add(o6);
325         return list;
326     }
327     
328     protected static List JavaDoc list(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3,
329                 Object JavaDoc o4, Object JavaDoc o5, Object JavaDoc o6, Object JavaDoc o7)
330     {
331         List JavaDoc list = new ArrayList JavaDoc();
332         list.add(o1);
333         list.add(o2);
334         list.add(o3);
335         list.add(o4);
336         list.add(o5);
337         list.add(o6);
338         list.add(o7);
339         return list;
340     }
341
342     protected static Set JavaDoc set(Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3,
343                 Object JavaDoc o4, Object JavaDoc o5, Object JavaDoc o6, Object JavaDoc o7)
344     {
345         Set JavaDoc list = new HashSet JavaDoc();
346         list.add(o1);
347         list.add(o2);
348         list.add(o3);
349         list.add(o4);
350         list.add(o5);
351         list.add(o6);
352         list.add(o7);
353         return list;
354     }
355     
356 }
Popular Tags