1 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.Locale ; 25 import java.util.Set ; 26 27 import junit.framework.TestCase; 28 29 import org.apache.commons.jxpath.ri.model.NodePointer; 30 31 37 38 public abstract class JXPathTestCase extends TestCase { 39 44 public JXPathTestCase(String name) { 45 super(name); 46 Locale.setDefault(Locale.US); 47 } 48 49 protected void assertXPathValue(JXPathContext ctx, 50 String xpath, Object expected) 51 { 52 ctx.setLenient(false); 53 Object actual = ctx.getValue(xpath); 54 assertEquals("Evaluating <" + xpath + ">", expected, actual); 55 } 56 57 protected void assertXPathValue(JXPathContext ctx, 58 String xpath, Object expected, Class resultType) 59 { 60 ctx.setLenient(false); 61 Object actual = ctx.getValue(xpath, resultType); 62 assertEquals("Evaluating <" + xpath + ">", expected, actual); 63 } 64 65 protected void assertXPathValueLenient(JXPathContext ctx, 66 String xpath, Object expected) 67 { 68 ctx.setLenient(true); 69 Object 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 xpath, Object value) 76 { 77 assertXPathSetValue(ctx, xpath, value, value); 78 } 79 80 protected void assertXPathSetValue(JXPathContext ctx, 81 String xpath, Object value, Object expected) 82 { 83 ctx.setValue(xpath, value); 84 Object actual = ctx.getValue(xpath); 85 assertEquals("Modifying <" + xpath + ">", expected, actual); 86 } 87 88 protected void assertXPathCreatePath(JXPathContext ctx, 89 String xpath, 90 Object expectedValue, String 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 xpath, Object value, 105 String 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 xpath, String expected) 120 { 121 ctx.setLenient(false); 122 Pointer pointer = ctx.getPointer(xpath); 123 String actual = pointer.toString(); 124 assertEquals("Evaluating pointer <" + xpath + ">", expected, actual); 125 } 126 127 protected void assertXPathPointerLenient(JXPathContext ctx, 128 String xpath, String expected) 129 { 130 ctx.setLenient(true); 131 Pointer pointer = ctx.getPointer(xpath); 132 String actual = pointer.toString(); 133 assertEquals("Evaluating pointer <" + xpath + ">", expected, actual); 134 } 135 136 protected void assertXPathValueAndPointer(JXPathContext ctx, 137 String xpath, Object expectedValue, String expectedPointer) 138 { 139 assertXPathValue(ctx, xpath, expectedValue); 140 assertXPathPointer(ctx, xpath, expectedPointer); 141 } 142 143 protected void assertXPathValueIterator(JXPathContext ctx, 144 String xpath, Collection expected) 145 { 146 Collection actual; 147 if (expected instanceof List ) { 148 actual = new ArrayList (); 149 } 150 else { 151 actual = new HashSet (); 152 } 153 Iterator 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 xpath, 164 Collection expected) 165 { 166 Collection actual; 167 if (expected instanceof List ) { 168 actual = new ArrayList (); 169 } 170 else { 171 actual = new HashSet (); 172 } 173 Iterator 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 path1, 187 String 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 xpath, 208 Class clazz) 209 { 210 ctx.setLenient(false); 211 Object 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 xpath, 219 Class 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 list() { 228 return Collections.EMPTY_LIST; 229 } 230 231 protected static List list(Object o1) { 232 List list = new ArrayList (); 233 list.add(o1); 234 return list; 235 } 236 237 protected static List list(Object o1, Object o2) { 238 List list = new ArrayList (); 239 list.add(o1); 240 list.add(o2); 241 return list; 242 } 243 244 protected static List list(Object o1, Object o2, Object o3) { 245 List list = new ArrayList (); 246 list.add(o1); 247 list.add(o2); 248 list.add(o3); 249 return list; 250 } 251 252 protected static Set set(Object o1, Object o2, Object o3) { 253 Set list = new HashSet (); 254 list.add(o1); 255 list.add(o2); 256 list.add(o3); 257 return list; 258 } 259 260 protected static List list(Object o1, Object o2, Object o3, Object o4) { 261 List list = new ArrayList (); 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 set(Object o1, Object o2, Object o3, Object o4) { 270 Set list = new HashSet (); 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 list(Object o1, Object o2, Object o3, 279 Object o4, Object o5) 280 { 281 List list = new ArrayList (); 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 set(Object o1, Object o2, Object o3, 291 Object o4, Object o5) 292 { 293 Set list = new HashSet (); 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 list(Object o1, Object o2, Object o3, 303 Object o4, Object o5, Object o6) 304 { 305 List list = new ArrayList (); 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 set(Object o1, Object o2, Object o3, 316 Object o4, Object o5, Object o6) 317 { 318 Set list = new HashSet (); 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 list(Object o1, Object o2, Object o3, 329 Object o4, Object o5, Object o6, Object o7) 330 { 331 List list = new ArrayList (); 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 set(Object o1, Object o2, Object o3, 343 Object o4, Object o5, Object o6, Object o7) 344 { 345 Set list = new HashSet (); 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 |