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.TestCase; 27 import junit.framework.TestSuite; 28 29 30 40 41 public class RulesBaseTestCase extends TestCase { 42 43 44 46 47 50 protected Digester digester = null; 51 52 54 55 60 public RulesBaseTestCase(String name) { 61 62 super(name); 63 64 } 65 66 67 69 70 73 public void setUp() { 74 75 digester = new Digester(); 76 digester.setRules(createMatchingRulesForTest()); 77 78 } 79 80 85 protected Rules createMatchingRulesForTest() { 86 return new RulesBase(); 87 } 88 89 92 public static Test suite() { 93 94 return (new TestSuite(RulesBaseTestCase.class)); 95 96 } 97 98 99 102 public void tearDown() { 103 104 digester = null; 105 106 } 107 108 109 110 112 115 public void testRules() { 116 117 digester.getRules().clear(); 119 120 List list = null; 122 123 assertEquals("Initial rules list is empty", 124 0, digester.getRules().match("a").size()); 125 digester.addSetProperties("a"); 126 assertEquals("Add a matching rule", 127 1, digester.getRules().match(null, "a").size()); 128 digester.addSetProperties("b"); 129 assertEquals("Add a non-matching rule", 130 1, digester.getRules().match(null, "a").size()); 131 digester.addSetProperties("a/b"); 132 assertEquals("Add a non-matching nested rule", 133 1, digester.getRules().match(null, "a").size()); 134 digester.addSetProperties("a/b"); 135 assertEquals("Add a second matching rule", 136 2, digester.getRules().match(null, "a/b").size()); 137 138 digester.getRules().clear(); 140 141 } 142 143 153 public void testRulesBase() { 154 155 digester.getRules().clear(); 157 158 assertEquals("Initial rules list is empty", 159 0, digester.getRules().rules().size()); 160 161 digester.addRule("a/b/c/d", new TestRule("a/b/c/d")); 163 digester.addRule("*/d", new TestRule("*/d")); 164 digester.addRule("*/c/d", new TestRule("*/c/d")); 165 166 assertEquals("Exact match takes precedence 1", 168 1, digester.getRules().match(null, "a/b/c/d").size()); 169 assertEquals("Exact match takes precedence 2", 170 "a/b/c/d", 171 ((TestRule) digester.getRules().match(null, "a/b/c/d").iterator().next()).getIdentifier()); 172 173 assertEquals("Wildcard tail matching rule 1", 175 1, digester.getRules().match(null, "a/b/d").size()); 176 assertEquals("Wildcard tail matching rule 2", 177 "*/d", 178 ((TestRule) digester.getRules().match(null, "a/b/d").iterator().next()).getIdentifier()); 179 180 assertEquals("Longest tail rule 1", 182 1, digester.getRules().match(null, "x/c/d").size()); 183 assertEquals("Longest tail rule 2", 184 "*/c/d", 185 ((TestRule) digester.getRules().match(null, "x/c/d").iterator().next()).getIdentifier()); 186 187 digester.addRule("*/a", new TestRule("*/a")); 190 assertEquals("Wildcard tail matching rule 3", 191 1, 192 digester.getRules().match(null,"a").size()); 193 194 assertEquals("Wildcard tail matching rule 3 (match too much)", 195 0, 196 digester.getRules().match(null,"aa").size()); 197 digester.getRules().clear(); 199 200 } 201 202 205 public void testBasicNamespaceMatching() { 206 207 List list = null; 208 Iterator it = null; 209 210 digester.getRules().clear(); 212 213 assertEquals("Initial rules list is empty", 214 0, digester.getRules().rules().size()); 215 216 digester.addRule("alpha/beta/gamma", new TestRule("No-Namespace")); 218 digester.addRule("alpha/beta/gamma", new TestRule("Euclidean-Namespace", "euclidean")); 219 220 221 list = digester.getRules().rules(); 222 223 list = digester.getRules().match(null, "alpha/beta/gamma"); 225 226 assertEquals("Null namespace match (A)", 2, list.size()); 227 228 it = list.iterator(); 229 assertEquals("Null namespace match (B)", "No-Namespace", ((TestRule) it.next()).getIdentifier()); 230 assertEquals("Null namespace match (C)", "Euclidean-Namespace", ((TestRule) it.next()).getIdentifier()); 231 232 233 234 list = digester.getRules().match("euclidean", "alpha/beta/gamma"); 236 237 assertEquals("Matching namespace match (A)", 2, list.size()); 238 239 it = list.iterator(); 240 assertEquals("Matching namespace match (B)", "No-Namespace", ((TestRule) it.next()).getIdentifier()); 241 assertEquals("Matching namespace match (C)", "Euclidean-Namespace", ((TestRule) it.next()).getIdentifier()); 242 243 244 245 list = digester.getRules().match("hyperbolic", "alpha/beta/gamma"); 247 248 assertEquals("Non matching namespace match (A)", 1, list.size()); 249 250 it = list.iterator(); 251 assertEquals("Non matching namespace match (B)", "No-Namespace", ((TestRule) it.next()).getIdentifier()); 252 253 digester.getRules().clear(); 255 256 } 257 258 261 public void testOrdering() { 262 263 digester.getRules().clear(); 265 266 assertEquals("Initial rules list is empty", 267 0, digester.getRules().rules().size()); 268 269 digester.addRule("alpha/beta/gamma", new TestRule("one")); 271 digester.addRule("alpha/beta/gamma", new TestRule("two")); 272 digester.addRule("alpha/beta/gamma", new TestRule("three")); 273 274 List list = digester.getRules().match(null, "alpha/beta/gamma"); 276 277 assertEquals("Testing ordering mismatch (A)", 3, list.size()); 278 279 Iterator it = list.iterator(); 280 assertEquals("Testing ordering mismatch (B)", "one", ((TestRule) it.next()).getIdentifier()); 281 assertEquals("Testing ordering mismatch (C)", "two", ((TestRule) it.next()).getIdentifier()); 282 assertEquals("Testing ordering mismatch (D)", "three", ((TestRule) it.next()).getIdentifier()); 283 284 digester.getRules().clear(); 286 287 } 288 289 290 public void testTrailingSlash() { 291 digester.getRules().clear(); 293 294 assertEquals("Initial rules list is empty", 295 0, digester.getRules().rules().size()); 296 297 digester.addRule("alpha/beta/gamma/", new TestRule("one")); 299 digester.addRule("alpha/beta/", new TestRule("two")); 300 digester.addRule("beta/gamma/alpha", new TestRule("three")); 301 302 List list = digester.getRules().match(null, "alpha/beta/gamma"); 304 305 assertEquals("Testing number of matches", 1, list.size()); 306 307 Iterator it = list.iterator(); 308 assertEquals("Testing ordering (A)", "one", ((TestRule) it.next()).getIdentifier()); 309 310 digester.getRules().clear(); 312 } 313 } 314 | Popular Tags |