001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Locale;
025: import java.util.Set;
026:
027: import junit.framework.TestCase;
028:
029: import org.apache.commons.jxpath.ri.model.NodePointer;
030:
031: /**
032: * Abstract superclass for various JXPath tests.
033: *
034: * @author Dmitri Plotnikov
035: * @version $Revision: 1.36 $ $Date: 2004/07/16 22:52:32 $
036: */
037:
038: public abstract class JXPathTestCase extends TestCase {
039: /**
040: * Construct a new instance of this test case.
041: *
042: * @param name Name of the test case
043: */
044: public JXPathTestCase(String name) {
045: super (name);
046: Locale.setDefault(Locale.US);
047: }
048:
049: protected void assertXPathValue(JXPathContext ctx, String xpath,
050: Object expected) {
051: ctx.setLenient(false);
052: Object actual = ctx.getValue(xpath);
053: assertEquals("Evaluating <" + xpath + ">", expected, actual);
054: }
055:
056: protected void assertXPathValue(JXPathContext ctx, String xpath,
057: Object expected, Class resultType) {
058: ctx.setLenient(false);
059: Object actual = ctx.getValue(xpath, resultType);
060: assertEquals("Evaluating <" + xpath + ">", expected, actual);
061: }
062:
063: protected void assertXPathValueLenient(JXPathContext ctx,
064: String xpath, Object expected) {
065: ctx.setLenient(true);
066: Object actual = ctx.getValue(xpath);
067: ctx.setLenient(false);
068: assertEquals("Evaluating lenient <" + xpath + ">", expected,
069: actual);
070: }
071:
072: protected void assertXPathSetValue(JXPathContext ctx, String xpath,
073: Object value) {
074: assertXPathSetValue(ctx, xpath, value, value);
075: }
076:
077: protected void assertXPathSetValue(JXPathContext ctx, String xpath,
078: Object value, Object expected) {
079: ctx.setValue(xpath, value);
080: Object actual = ctx.getValue(xpath);
081: assertEquals("Modifying <" + xpath + ">", expected, actual);
082: }
083:
084: protected void assertXPathCreatePath(JXPathContext ctx,
085: String xpath, Object expectedValue, String expectedPath) {
086: Pointer pointer = ctx.createPath(xpath);
087: assertEquals("Creating path <" + xpath + ">", expectedPath,
088: pointer.asPath());
089:
090: assertEquals("Creating path (pointer value) <" + xpath + ">",
091: expectedValue, pointer.getValue());
092:
093: assertEquals("Creating path (context value) <" + xpath + ">",
094: expectedValue, ctx.getValue(pointer.asPath()));
095: }
096:
097: protected void assertXPathCreatePathAndSetValue(JXPathContext ctx,
098: String xpath, Object value, String expectedPath) {
099: Pointer pointer = ctx.createPathAndSetValue(xpath, value);
100: assertEquals("Creating path <" + xpath + ">", expectedPath,
101: pointer.asPath());
102:
103: assertEquals("Creating path (pointer value) <" + xpath + ">",
104: value, pointer.getValue());
105:
106: assertEquals("Creating path (context value) <" + xpath + ">",
107: value, ctx.getValue(pointer.asPath()));
108: }
109:
110: protected void assertXPathPointer(JXPathContext ctx, String xpath,
111: String expected) {
112: ctx.setLenient(false);
113: Pointer pointer = ctx.getPointer(xpath);
114: String actual = pointer.toString();
115: assertEquals("Evaluating pointer <" + xpath + ">", expected,
116: actual);
117: }
118:
119: protected void assertXPathPointerLenient(JXPathContext ctx,
120: String xpath, String expected) {
121: ctx.setLenient(true);
122: Pointer pointer = ctx.getPointer(xpath);
123: String actual = pointer.toString();
124: assertEquals("Evaluating pointer <" + xpath + ">", expected,
125: actual);
126: }
127:
128: protected void assertXPathValueAndPointer(JXPathContext ctx,
129: String xpath, Object expectedValue, String expectedPointer) {
130: assertXPathValue(ctx, xpath, expectedValue);
131: assertXPathPointer(ctx, xpath, expectedPointer);
132: }
133:
134: protected void assertXPathValueIterator(JXPathContext ctx,
135: String xpath, Collection expected) {
136: Collection actual;
137: if (expected instanceof List) {
138: actual = new ArrayList();
139: } else {
140: actual = new HashSet();
141: }
142: Iterator it = ctx.iterate(xpath);
143: while (it.hasNext()) {
144: actual.add(it.next());
145: }
146: assertEquals("Evaluating value iterator <" + xpath + ">",
147: expected, actual);
148: }
149:
150: protected void assertXPathPointerIterator(JXPathContext ctx,
151: String xpath, Collection expected) {
152: Collection actual;
153: if (expected instanceof List) {
154: actual = new ArrayList();
155: } else {
156: actual = new HashSet();
157: }
158: Iterator it = ctx.iteratePointers(xpath);
159: while (it.hasNext()) {
160: Pointer pointer = (Pointer) it.next();
161: actual.add(pointer.toString());
162: }
163: assertEquals("Evaluating pointer iterator <" + xpath + ">",
164: expected, actual);
165: }
166:
167: protected void assertDocumentOrder(JXPathContext context,
168: String path1, String path2, int expected) {
169: NodePointer np1 = (NodePointer) context.getPointer(path1);
170: NodePointer np2 = (NodePointer) context.getPointer(path2);
171: int res = np1.compareTo(np2);
172: if (res < 0) {
173: res = -1;
174: } else if (res > 0) {
175: res = 1;
176: }
177: assertEquals("Comparing paths '" + path1 + "' and '" + path2
178: + "'", expected, res);
179: }
180:
181: protected void assertXPathValueType(JXPathContext ctx,
182: String xpath, Class clazz) {
183: ctx.setLenient(false);
184: Object actual = ctx.getValue(xpath);
185: assertTrue("Evaluating <" + xpath + "> = " + actual.getClass(),
186: clazz.isAssignableFrom(actual.getClass()));
187: }
188:
189: protected void assertXPathNodeType(JXPathContext ctx, String xpath,
190: Class clazz) {
191: ctx.setLenient(false);
192: Pointer actual = ctx.getPointer(xpath);
193: assertTrue("Evaluating <" + xpath + "> = "
194: + actual.getNode().getClass(), clazz
195: .isAssignableFrom(actual.getNode().getClass()));
196: }
197:
198: protected static List list() {
199: return Collections.EMPTY_LIST;
200: }
201:
202: protected static List list(Object o1) {
203: List list = new ArrayList();
204: list.add(o1);
205: return list;
206: }
207:
208: protected static List list(Object o1, Object o2) {
209: List list = new ArrayList();
210: list.add(o1);
211: list.add(o2);
212: return list;
213: }
214:
215: protected static List list(Object o1, Object o2, Object o3) {
216: List list = new ArrayList();
217: list.add(o1);
218: list.add(o2);
219: list.add(o3);
220: return list;
221: }
222:
223: protected static Set set(Object o1, Object o2, Object o3) {
224: Set list = new HashSet();
225: list.add(o1);
226: list.add(o2);
227: list.add(o3);
228: return list;
229: }
230:
231: protected static List list(Object o1, Object o2, Object o3,
232: Object o4) {
233: List list = new ArrayList();
234: list.add(o1);
235: list.add(o2);
236: list.add(o3);
237: list.add(o4);
238: return list;
239: }
240:
241: protected static Set set(Object o1, Object o2, Object o3, Object o4) {
242: Set list = new HashSet();
243: list.add(o1);
244: list.add(o2);
245: list.add(o3);
246: list.add(o4);
247: return list;
248: }
249:
250: protected static List list(Object o1, Object o2, Object o3,
251: Object o4, Object o5) {
252: List list = new ArrayList();
253: list.add(o1);
254: list.add(o2);
255: list.add(o3);
256: list.add(o4);
257: list.add(o5);
258: return list;
259: }
260:
261: protected static Set set(Object o1, Object o2, Object o3,
262: Object o4, Object o5) {
263: Set list = new HashSet();
264: list.add(o1);
265: list.add(o2);
266: list.add(o3);
267: list.add(o4);
268: list.add(o5);
269: return list;
270: }
271:
272: protected static List list(Object o1, Object o2, Object o3,
273: Object o4, Object o5, Object o6) {
274: List list = new ArrayList();
275: list.add(o1);
276: list.add(o2);
277: list.add(o3);
278: list.add(o4);
279: list.add(o5);
280: list.add(o6);
281: return list;
282: }
283:
284: protected static Set set(Object o1, Object o2, Object o3,
285: Object o4, Object o5, Object o6) {
286: Set list = new HashSet();
287: list.add(o1);
288: list.add(o2);
289: list.add(o3);
290: list.add(o4);
291: list.add(o5);
292: list.add(o6);
293: return list;
294: }
295:
296: protected static List list(Object o1, Object o2, Object o3,
297: Object o4, Object o5, Object o6, Object o7) {
298: List list = new ArrayList();
299: list.add(o1);
300: list.add(o2);
301: list.add(o3);
302: list.add(o4);
303: list.add(o5);
304: list.add(o6);
305: list.add(o7);
306: return list;
307: }
308:
309: protected static Set set(Object o1, Object o2, Object o3,
310: Object o4, Object o5, Object o6, Object o7) {
311: Set list = new HashSet();
312: list.add(o1);
313: list.add(o2);
314: list.add(o3);
315: list.add(o4);
316: list.add(o5);
317: list.add(o6);
318: list.add(o7);
319: return list;
320: }
321:
322: }
|