1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.Set;
31
32 import org.apache.commons.jxpath.ri.model.NodePointer;
33 import org.junit.jupiter.api.BeforeEach;
34
35
36
37
38 public abstract class AbstractJXPathTest {
39
40 protected static List list() {
41 return Collections.EMPTY_LIST;
42 }
43
44 protected static List list(final Object o1) {
45 final List list = new ArrayList();
46 list.add(o1);
47 return list;
48 }
49
50 protected static List list(final Object o1, final Object o2) {
51 final List list = new ArrayList();
52 list.add(o1);
53 list.add(o2);
54 return list;
55 }
56
57 protected static List list(final Object o1, final Object o2, final Object o3) {
58 final List list = new ArrayList();
59 list.add(o1);
60 list.add(o2);
61 list.add(o3);
62 return list;
63 }
64
65 protected static List list(final Object o1, final Object o2, final Object o3, final Object o4) {
66 final List list = new ArrayList();
67 list.add(o1);
68 list.add(o2);
69 list.add(o3);
70 list.add(o4);
71 return list;
72 }
73
74 protected static List list(final Object o1, final Object o2, final Object o3, final Object o4, final Object o5) {
75 final List list = new ArrayList();
76 list.add(o1);
77 list.add(o2);
78 list.add(o3);
79 list.add(o4);
80 list.add(o5);
81 return list;
82 }
83
84 protected static List list(final Object o1, final Object o2, final Object o3, final Object o4, final Object o5, final Object o6) {
85 final List list = new ArrayList();
86 list.add(o1);
87 list.add(o2);
88 list.add(o3);
89 list.add(o4);
90 list.add(o5);
91 list.add(o6);
92 return list;
93 }
94
95 protected static List list(final Object o1, final Object o2, final Object o3, final Object o4, final Object o5, final Object o6, final Object o7) {
96 final List list = new ArrayList();
97 list.add(o1);
98 list.add(o2);
99 list.add(o3);
100 list.add(o4);
101 list.add(o5);
102 list.add(o6);
103 list.add(o7);
104 return list;
105 }
106
107 protected static Set set(final Object o1, final Object o2) {
108 final Set list = new HashSet();
109 list.add(o1);
110 list.add(o2);
111 return list;
112 }
113
114 protected static Set set(final Object o1, final Object o2, final Object o3) {
115 final Set list = new HashSet();
116 list.add(o1);
117 list.add(o2);
118 list.add(o3);
119 return list;
120 }
121
122 protected static Set set(final Object o1, final Object o2, final Object o3, final Object o4) {
123 final Set list = new HashSet();
124 list.add(o1);
125 list.add(o2);
126 list.add(o3);
127 list.add(o4);
128 return list;
129 }
130
131 protected static Set set(final Object o1, final Object o2, final Object o3, final Object o4, final Object o5) {
132 final Set list = new HashSet();
133 list.add(o1);
134 list.add(o2);
135 list.add(o3);
136 list.add(o4);
137 list.add(o5);
138 return list;
139 }
140
141 protected static Set set(final Object o1, final Object o2, final Object o3, final Object o4, final Object o5, final Object o6) {
142 final Set list = new HashSet();
143 list.add(o1);
144 list.add(o2);
145 list.add(o3);
146 list.add(o4);
147 list.add(o5);
148 list.add(o6);
149 return list;
150 }
151
152 protected static Set set(final Object o1, final Object o2, final Object o3, final Object o4, final Object o5, final Object o6, final Object o7) {
153 final Set list = new HashSet();
154 list.add(o1);
155 list.add(o2);
156 list.add(o3);
157 list.add(o4);
158 list.add(o5);
159 list.add(o6);
160 list.add(o7);
161 return list;
162 }
163
164 protected void assertDocumentOrder(final JXPathContext context, final String path1, final String path2, final int expected) {
165 final NodePointer np1 = (NodePointer) context.getPointer(path1);
166 final NodePointer np2 = (NodePointer) context.getPointer(path2);
167 int res = np1.compareTo(np2);
168 if (res < 0) {
169 res = -1;
170 } else if (res > 0) {
171 res = 1;
172 }
173 assertEquals(expected, res, "Comparing paths '" + path1 + "' and '" + path2 + "'");
174 }
175
176 protected void assertXPathCreatePath(final JXPathContext ctx, final String xpath, final Object expectedValue, final String expectedPath) {
177 final Pointer pointer = ctx.createPath(xpath);
178 assertEquals(expectedPath, pointer.asPath(), "Creating path <" + xpath + ">");
179 assertEquals(expectedValue, pointer.getValue(), "Creating path (pointer value) <" + xpath + ">");
180 assertEquals(expectedValue, ctx.getValue(pointer.asPath()), "Creating path (context value) <" + xpath + ">");
181 }
182
183 protected void assertXPathCreatePathAndSetValue(final JXPathContext ctx, final String xpath, final Object value, final String expectedPath) {
184 final Pointer pointer = ctx.createPathAndSetValue(xpath, value);
185 assertEquals(expectedPath, pointer.asPath(), "Creating path <" + xpath + ">");
186 assertEquals(value, pointer.getValue(), "Creating path (pointer value) <" + xpath + ">");
187 assertEquals(value, ctx.getValue(pointer.asPath()), "Creating path (context value) <" + xpath + ">");
188 }
189
190 protected void assertXPathNodeType(final JXPathContext ctx, final String xpath, final Class clazz) {
191 ctx.setLenient(false);
192 final Pointer actual = ctx.getPointer(xpath);
193 assertTrue(clazz.isAssignableFrom(actual.getNode().getClass()), "Evaluating <" + xpath + "> = " + actual.getNode().getClass());
194 }
195
196 protected void assertXPathPointer(final JXPathContext ctx, final String xpath, final String expected) {
197 ctx.setLenient(false);
198 final Pointer pointer = ctx.getPointer(xpath);
199 final String actual = pointer.toString();
200 assertEquals(expected, actual, "Evaluating pointer <" + xpath + ">");
201 }
202
203 protected void assertXPathPointerIterator(final JXPathContext ctx, final String xpath, final Collection expected) {
204 Collection actual;
205 if (expected instanceof List) {
206 actual = new ArrayList();
207 } else {
208 actual = new HashSet();
209 }
210 final Iterator<Pointer> it = ctx.iteratePointers(xpath);
211 while (it.hasNext()) {
212 final Pointer pointer = it.next();
213 actual.add(pointer.toString());
214 }
215 assertEquals(expected, actual, "Evaluating pointer iterator <" + xpath + ">");
216 }
217
218 protected void assertXPathPointerLenient(final JXPathContext ctx, final String xpath, final String expected) {
219 ctx.setLenient(true);
220 final Pointer pointer = ctx.getPointer(xpath);
221 final String actual = pointer.toString();
222 assertEquals(expected, actual, "Evaluating pointer <" + xpath + ">");
223 }
224
225 protected void assertXPathSetValue(final JXPathContext ctx, final String xpath, final Object value) {
226 assertXPathSetValue(ctx, xpath, value, value);
227 }
228
229 protected void assertXPathSetValue(final JXPathContext ctx, final String xpath, final Object value, final Object expected) {
230 ctx.setValue(xpath, value);
231 final Object actual = ctx.getValue(xpath);
232 assertEquals(expected, actual, "Modifying <" + xpath + ">");
233 }
234
235 protected void assertXPathValue(final JXPathContext ctx, final String xpath, final Object expected) {
236 ctx.setLenient(false);
237 final Object actual = ctx.getValue(xpath);
238 assertEquals(expected, actual, "Evaluating <" + xpath + ">");
239 }
240
241 protected void assertXPathValue(final JXPathContext ctx, final String xpath, final Object expected, final Class resultType) {
242 ctx.setLenient(false);
243 final Object actual = ctx.getValue(xpath, resultType);
244 assertEquals(expected, actual, "Evaluating <" + xpath + ">");
245 }
246
247 protected void assertXPathValueAndPointer(final JXPathContext ctx, final String xpath, final Object expectedValue, final String expectedPointer) {
248 assertXPathValue(ctx, xpath, expectedValue);
249 assertXPathPointer(ctx, xpath, expectedPointer);
250 }
251
252 protected <E> void assertXPathValueIterator(final JXPathContext ctx, final String xpath, final Collection<E> expected) {
253 Collection<E> actual;
254 if (expected instanceof List) {
255 actual = new ArrayList<>();
256 } else {
257 actual = new HashSet<>();
258 }
259 final Iterator<E> it = ctx.iterate(xpath);
260 while (it.hasNext()) {
261 actual.add(it.next());
262 }
263 assertEquals(expected.hashCode(), actual.hashCode(),
264 String.format("[hashCode()] Evaluating value iterator <%s>, expected.class %s(%,d): %s, actual.class %s(%,d): %s", xpath, expected.getClass(),
265 expected.size(), expected, actual.getClass(), actual.size(), actual));
266 assertEquals(expected, actual, String.format("[equals()] Evaluating value iterator <%s>, expected.class %s(%,d): %s, actual.class %s(%,d): %s", xpath,
267 expected.getClass(), expected.size(), expected, actual.getClass(), actual.size(), actual));
268 }
269
270 protected void assertXPathValueLenient(final JXPathContext ctx, final String xpath, final Object expected) {
271 ctx.setLenient(true);
272 final Object actual = ctx.getValue(xpath);
273 ctx.setLenient(false);
274 assertEquals(expected, actual, "Evaluating lenient <" + xpath + ">");
275 }
276
277 protected void assertXPathValueType(final JXPathContext ctx, final String xpath, final Class clazz) {
278 ctx.setLenient(false);
279 final Object actual = ctx.getValue(xpath);
280 assertTrue(clazz.isAssignableFrom(actual.getClass()), "Evaluating <" + xpath + "> = " + actual.getClass());
281 }
282
283
284
285
286
287
288 @BeforeEach
289 protected void setUp() throws Exception {
290 Locale.setDefault(Locale.US);
291 }
292 }