1 17 18 19 package org.apache.commons.digester; 20 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import junit.framework.Test; 26 import junit.framework.TestSuite; 27 28 29 35 36 37 public class EBRTestCase extends RulesBaseTestCase { 38 39 40 42 47 public EBRTestCase(String name) { 48 49 super(name); 50 } 51 52 53 55 60 protected Rules createMatchingRulesForTest() { 61 62 return new ExtendedBaseRules(); 63 } 64 65 66 69 public static Test suite() { 70 71 return (new TestSuite(EBRTestCase.class)); 72 73 } 74 75 76 82 public void testBasicParentMatch() { 83 84 digester.getRules().clear(); 86 87 assertEquals("Initial rules list is empty", 88 0, digester.getRules().rules().size()); 89 90 digester.addRule("alpha/beta/gamma/delta", new TestRule("exact")); 94 digester.addRule("*/beta/gamma/epsilon", new TestRule("wild_child")); 95 digester.addRule("alpha/beta/gamma/?", new TestRule("exact_parent")); 96 digester.addRule("*/beta/gamma/?", new TestRule("wild_parent")); 97 98 99 List list = null; 100 Iterator it = null; 101 102 list = digester.getRules().match(null, "alpha/beta/gamma/delta"); 104 105 assertEquals("Testing basic parent mismatch (A)", 1, list.size()); 107 108 it = list.iterator(); 109 assertEquals("Testing basic parent mismatch (B)", "exact", ((TestRule) it.next()).getIdentifier()); 110 111 112 list = digester.getRules().match(null, "alpha/beta/gamma/epsilon"); 114 115 assertEquals("Testing basic parent mismatch (C)", 1, list.size()); 117 118 it = list.iterator(); 119 assertEquals("Testing basic parent mismatch (D)", "exact_parent", ((TestRule) it.next()).getIdentifier()); 120 121 122 list = digester.getRules().match(null, "alpha/omega/beta/gamma/epsilon"); 124 125 assertEquals("Testing basic parent mismatch (E)", 1, list.size()); 127 128 it = list.iterator(); 129 assertEquals("Testing basic parent mismatch (F)", "wild_child", ((TestRule) it.next()).getIdentifier()); 130 131 132 list = digester.getRules().match(null, "alpha/omega/beta/gamma/zeta"); 134 135 assertEquals("Testing basic parent mismatch (G)", 1, list.size()); 137 138 it = list.iterator(); 139 assertEquals("Testing basic parent mismatch (H)", "wild_parent", ((TestRule) it.next()).getIdentifier()); 140 141 142 digester.getRules().clear(); 144 145 } 146 147 148 152 public void testBasicUniversal() { 153 154 digester.getRules().clear(); 156 157 assertEquals("Initial rules list is empty", 158 0, digester.getRules().rules().size()); 159 160 digester.addRule("alpha/beta/gamma", new TestRule("exact")); 163 digester.addRule("*/beta/gamma", new TestRule("non_wild_head")); 164 digester.addRule("!*/beta/gamma", new TestRule("universal_wild_head")); 165 digester.addRule("!alpha/beta/gamma/?", new TestRule("universal_wild_child")); 166 digester.addRule("alpha/beta/gamma/?", new TestRule("non_wild_child")); 167 digester.addRule("alpha/beta/gamma/epsilon", new TestRule("exact2")); 168 digester.addRule("alpha/epsilon/beta/gamma/zeta", new TestRule("exact3")); 169 digester.addRule("*/gamma/?", new TestRule("non_wildhead_child")); 170 digester.addRule("!*/epsilon/beta/gamma/?", new TestRule("universal_wildhead_child")); 171 172 173 List list = null; 174 Iterator it = null; 175 176 list = digester.getRules().match(null, "alpha/beta/gamma"); 178 179 assertEquals("Testing universal wildcard mismatch (A)", 2, list.size()); 180 181 it = list.iterator(); 182 assertEquals("Testing universal wildcard mismatch (B)", "exact", ((TestRule) it.next()).getIdentifier()); 183 assertEquals("Testing universal wildcard mismatch (C)", "universal_wild_head", ((TestRule) it.next()).getIdentifier()); 184 185 186 list = digester.getRules().match(null, "alpha/beta/gamma/epsilon"); 188 189 assertEquals("Testing universal wildcard mismatch (D)", 2, list.size()); 190 191 it = list.iterator(); 192 assertEquals("Testing universal wildcard mismatch (E)", "universal_wild_child", ((TestRule) it.next()).getIdentifier()); 193 assertEquals("Testing universal wildcard mismatch (F)", "exact2", ((TestRule) it.next()).getIdentifier()); 194 195 list = digester.getRules().match(null, "alpha/beta/gamma/zeta"); 197 198 assertEquals("Testing universal wildcard mismatch (G)", 2, list.size()); 199 200 it = list.iterator(); 201 assertEquals("Testing universal wildcard mismatch (H)", "universal_wild_child", ((TestRule) it.next()).getIdentifier()); 202 assertEquals("Testing universal wildcard mismatch (I)", "non_wild_child", ((TestRule) it.next()).getIdentifier()); 203 204 205 list = digester.getRules().match(null, "alpha/epsilon/beta/gamma/alpha"); 207 208 assertEquals("Testing universal wildcard mismatch (J)", 2, list.size()); 209 210 it = list.iterator(); 211 assertEquals("Testing universal wildcard mismatch (K)", "non_wildhead_child", ((TestRule) it.next()).getIdentifier()); 212 assertEquals("Testing universal wildcard mismatch (L)", "universal_wildhead_child", ((TestRule) it.next()).getIdentifier()); 213 214 list = digester.getRules().match(null, "alpha/epsilon/beta/gamma/zeta"); 216 217 assertEquals("Testing universal wildcard mismatch (M)", 2, list.size()); 218 219 it = list.iterator(); 220 assertEquals("Testing universal wildcard mismatch (M)", "exact3", ((TestRule) it.next()).getIdentifier()); 221 assertEquals("Testing universal wildcard mismatch (O)", "universal_wildhead_child", ((TestRule) it.next()).getIdentifier()); 222 223 224 digester.getRules().clear(); 226 227 } 228 229 230 236 public void testWildMatch() { 237 238 digester.getRules().clear(); 240 241 assertEquals("Initial rules list is empty", 242 0, digester.getRules().rules().size()); 243 244 digester.addRule("*", new TestRule("basic_wild")); 247 digester.addRule("!*", new TestRule("universal_wild")); 248 digester.addRule("alpha/beta/gamma/delta", new TestRule("exact")); 249 digester.addRule("*/beta/gamma/?", new TestRule("wild_parent")); 250 251 252 List list = null; 253 Iterator it = null; 254 255 list = digester.getRules().match(null, "alpha/beta/gamma/delta"); 257 258 assertEquals("Testing wild mismatch (A)", 2, list.size()); 260 261 it = list.iterator(); 262 assertEquals("Testing wild mismatch (B)", "universal_wild", ((TestRule) it.next()).getIdentifier()); 263 assertEquals("Testing wild mismatch (C)", "exact", ((TestRule) it.next()).getIdentifier()); 264 265 266 list = digester.getRules().match(null, "alpha/beta/gamma/epsilon"); 268 269 assertEquals("Testing wild mismatch (D)", 2, list.size()); 270 271 it = list.iterator(); 272 assertEquals("Testing wild mismatch (E)", "universal_wild", ((TestRule) it.next()).getIdentifier()); 273 assertEquals("Testing wild mismatch (F)", "wild_parent", ((TestRule) it.next()).getIdentifier()); 274 275 276 list = digester.getRules().match(null, "alpha/gamma"); 279 280 assertEquals("Testing wild mismatch (G)", 2, list.size()); 281 282 it = list.iterator(); 283 assertEquals("Testing wild mismatch (H)", "basic_wild", ((TestRule) it.next()).getIdentifier()); 284 assertEquals("Testing wild mismatch (I)", "universal_wild", ((TestRule) it.next()).getIdentifier()); 285 286 287 digester.getRules().clear(); 289 290 } 291 292 293 294 300 public void testRootTailMatch() { 301 302 digester.getRules().clear(); 304 305 assertEquals("Initial rules list is empty", 306 0, digester.getRules().rules().size()); 307 308 digester.addRule("*/a", new TestRule("a_tail")); 311 312 313 List list = null; 314 Iterator it = null; 315 316 list = digester.getRules().match(null, "a"); 317 318 assertEquals("Testing tail wrong size (A)", 1, list.size()); 319 assertEquals("Testing tail mismatch (B)", "a_tail", ((TestRule) list.get(0)).getIdentifier()); 320 321 322 list = digester.getRules().match(null, "beta/a"); 323 324 assertEquals("Testing tail wrong size (C)", 1, list.size()); 325 assertEquals("Testing tail mismatch (D)", "a_tail", ((TestRule) list.get(0)).getIdentifier()); 326 327 list = digester.getRules().match(null, "be/aaa"); 328 329 assertEquals("Testing tail no matches (E)", 0, list.size()); 330 331 list = digester.getRules().match(null, "aaa"); 332 333 assertEquals("Testing tail no matches (F)", 0, list.size()); 334 335 list = digester.getRules().match(null, "a/beta"); 336 337 assertEquals("Testing tail no matches (G)", 0, list.size()); 338 339 digester.getRules().clear(); 341 342 } 343 344 public void testAncesterMatch() throws Exception { 345 digester.getRules().clear(); 347 348 digester.addRule("!a/b/*", new TestRule("uni-a-b-star")); 349 digester.addRule("a/b/*", new TestRule("a-b-star")); 350 digester.addRule("a/b/c", new TestRule("a-b-c")); 351 digester.addRule("a/b/?", new TestRule("a-b-child")); 352 353 List 354 list = digester.getRules().match(null, "a/b/c"); 355 356 assertEquals("Simple ancester matches (1)", 2, list.size()); 357 assertEquals("Univeral ancester mismatch (1)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier()); 358 assertEquals("Parent precedence failure", "a-b-c" , ((TestRule) list.get(1)).getIdentifier()); 359 360 list = digester.getRules().match(null, "a/b/b"); 361 assertEquals("Simple ancester matches (2)", 2, list.size()); 362 assertEquals("Univeral ancester mismatch (2)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier()); 363 assertEquals("Child precedence failure", "a-b-child" , ((TestRule) list.get(1)).getIdentifier()); 364 365 list = digester.getRules().match(null, "a/b/d"); 366 assertEquals("Simple ancester matches (3)", 2, list.size()); 367 assertEquals("Univeral ancester mismatch (3)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier()); 368 assertEquals("Ancester mismatch (1)", "a-b-child" , ((TestRule) list.get(1)).getIdentifier()); 369 370 list = digester.getRules().match(null, "a/b/d/e/f"); 371 assertEquals("Simple ancester matches (4)", 2, list.size()); 372 assertEquals("Univeral ancester mismatch (4)", "uni-a-b-star" , ((TestRule) list.get(0)).getIdentifier()); 373 assertEquals("Ancester mismatch (2)", "a-b-star" , ((TestRule) list.get(1)).getIdentifier()); 374 375 digester.getRules().clear(); 377 378 digester.addRule("!*/a/b/*", new TestRule("uni-star-a-b-star")); 379 digester.addRule("*/b/c/*", new TestRule("star-b-c-star")); 380 digester.addRule("*/b/c/d", new TestRule("star-b-c-d")); 381 digester.addRule("a/b/c", new TestRule("a-b-c")); 382 383 list = digester.getRules().match(null, "a/b/c"); 384 assertEquals("Wild ancester match (1)", 2, list.size()); 385 assertEquals( 386 "Univeral ancester mismatch (5)", 387 "uni-star-a-b-star" , 388 ((TestRule) list.get(0)).getIdentifier()); 389 assertEquals("Match missed (1)", "a-b-c" , ((TestRule) list.get(1)).getIdentifier()); 390 391 list = digester.getRules().match(null, "b/c"); 392 assertEquals("Wild ancester match (2)", 1, list.size()); 393 assertEquals("Match missed (2)", "star-b-c-star" , ((TestRule) list.get(0)).getIdentifier()); 394 395 list = digester.getRules().match(null, "a/b/c/d"); 396 assertEquals("Wild ancester match (3)", 2, list.size()); 397 assertEquals("Match missed (3)", "uni-star-a-b-star" , ((TestRule) list.get(0)).getIdentifier()); 398 assertEquals("Match missed (4)", "star-b-c-d" , ((TestRule) list.get(1)).getIdentifier()); 399 400 list = digester.getRules().match(null, "b/b/c/e/d"); 401 assertEquals("Wild ancester match (2)", 1, list.size()); 402 assertEquals("Match missed (5)", "star-b-c-star" , ((TestRule) list.get(0)).getIdentifier()); 403 } 404 405 public void testLongMatch() { 406 407 digester.getRules().clear(); 408 409 digester.addRule("a/b/c/d/*", new TestRule("a-b-c-d-star")); 410 411 List list = digester.getRules().match(null, "a/b/c/d/e"); 412 assertEquals("Long match (1)", 1, list.size()); 413 assertEquals("Match missed (1)", "a-b-c-d-star" , ((TestRule) list.get(0)).getIdentifier()); 414 415 list = digester.getRules().match(null, "a/b/c/d/e/f"); 416 assertEquals("Long match (2)", 1, list.size()); 417 assertEquals("Match missed (2)", "a-b-c-d-star" , ((TestRule) list.get(0)).getIdentifier()); 418 419 list = digester.getRules().match(null, "a/b/c/d/e/f/g"); 420 assertEquals("Long match (3)", 1, list.size()); 421 assertEquals("Match missed (3)", "a-b-c-d-star" , ((TestRule) list.get(0)).getIdentifier()); 422 423 list = digester.getRules().match(null, "a/b/c/d"); 424 assertEquals("Long match (4)", 0, list.size()); 425 } 426 427 public void testInstructors() { 428 digester.getRules().clear(); 429 430 digester.addRule("!instructors/*", new TestRule("instructors")); 431 digester.addRule("!instructor/*", new TestRule("instructor")); 432 433 List list = digester.getRules().match(null, "instructors"); 434 assertEquals("Only expect to match instructors", 1, list.size()); 435 assertEquals("Instructors expected", "instructors" , ((TestRule) list.get(0)).getIdentifier()); 436 437 } 438 439 public void testMiddleInstructors() { 440 digester.getRules().clear(); 441 442 digester.addRule("!instructors/*", new TestRule("instructors")); 443 444 List list = digester.getRules().match(null, "/tosh/instructors/fiddlesticks"); 445 assertEquals("No matches expected", 0, list.size()); 446 447 } 448 } 449 | Popular Tags |