1 17 18 package org.apache.commons.digester; 19 20 import java.util.List ; 21 22 import junit.framework.TestCase; 23 24 30 31 public class RegexRulesTestCase extends TestCase { 32 33 34 public RegexRulesTestCase(String name) { 35 super(name); 36 } 37 38 39 public void testMatchAll() { 40 RegexRules rules = new RegexRules( 42 new RegexMatcher() { 43 public boolean match(String pathPattern, String rulePattern) { 44 return true; 45 } 46 }); 47 48 rules.add("/a/b/b", new TestRule("alpha")); 49 rules.add("/a/d", new TestRule("beta")); 50 rules.add("/b", new TestRule("gamma")); 51 52 List matches = rules.match("", "x/g/e"); 55 assertEquals("Wrong number of rules returned (1)", 3, matches.size()); 56 assertEquals("Rule Out Of Order (1)", "alpha", ((TestRule) matches.get(0)).getIdentifier()); 57 assertEquals("Rule Out Of Order (2)", "beta", ((TestRule) matches.get(1)).getIdentifier()); 58 assertEquals("Rule Out Of Order (3)", "gamma", ((TestRule) matches.get(2)).getIdentifier()); 59 60 matches = rules.match("", "/a"); 61 assertEquals("Wrong number of rules returned (2)", 3, matches.size()); 62 assertEquals("Rule Out Of Order (4)", "alpha", ((TestRule) matches.get(0)).getIdentifier()); 63 assertEquals("Rule Out Of Order (5)", "beta", ((TestRule) matches.get(1)).getIdentifier()); 64 assertEquals("Rule Out Of Order (6)", "gamma", ((TestRule) matches.get(2)).getIdentifier()); 65 } 66 67 68 public void testMatchNothing() { 69 RegexRules rules = new RegexRules( 71 new RegexMatcher() { 72 public boolean match(String pathPattern, String rulePattern) { 73 return false; 74 } 75 }); 76 77 rules.add("/b/c/f", new TestRule("alpha")); 78 rules.add("/c/f", new TestRule("beta")); 79 rules.add("/b", new TestRule("gamma")); 80 81 List matches = rules.match("", "/b/c"); 84 assertEquals("Wrong number of rules returned (1)", 0, matches.size()); 85 86 matches = rules.match("", "/b/c/f"); 87 assertEquals("Wrong number of rules returned (2)", 0, matches.size()); 88 } 89 90 91 public void testMatchMixed() { 92 RegexRules rules = new RegexRules( 94 new RegexMatcher() { 95 public boolean match(String pathPattern, String rulePattern) { 96 return (rulePattern.equals("/match/me")); 97 } 98 }); 99 100 rules.add("/match", new TestRule("alpha")); 101 rules.add("/match/me", new TestRule("beta")); 102 rules.add("/match", new TestRule("gamma")); 103 104 List matches = rules.match("", "/match"); 107 assertEquals("Wrong number of rules returned (1)", 1, matches.size()); 108 assertEquals("Wrong Rule (1)", "beta", ((TestRule) matches.get(0)).getIdentifier()); 109 110 matches = rules.match("", "/a/match"); 111 assertEquals("Wrong Rule (2)", "beta", ((TestRule) matches.get(0)).getIdentifier()); 112 } 113 114 115 public void testClear() { 116 RegexRules rules = new RegexRules( 118 new RegexMatcher() { 119 public boolean match(String pathPattern, String rulePattern) { 120 return true; 121 } 122 }); 123 124 rules.add("/abba", new TestRule("alpha")); 125 rules.add("/ad/ma", new TestRule("beta")); 126 rules.add("/gamma", new TestRule("gamma")); 127 128 List matches = rules.rules(); 130 assertEquals("Wrong number of rules returned (1)", 3, matches.size()); 131 assertEquals("Rule Out Of Order (1)", "alpha", ((TestRule) matches.get(0)).getIdentifier()); 132 assertEquals("Rule Out Of Order (2)", "beta", ((TestRule) matches.get(1)).getIdentifier()); 133 assertEquals("Rule Out Of Order (3)", "gamma", ((TestRule) matches.get(2)).getIdentifier()); 134 135 matches = rules.match("", "/eggs"); 136 assertEquals("Wrong number of rules returned (2)", 3, matches.size()); 137 assertEquals("Rule Out Of Order (4)", "alpha", ((TestRule) matches.get(0)).getIdentifier()); 138 assertEquals("Rule Out Of Order (5)", "beta", ((TestRule) matches.get(1)).getIdentifier()); 139 assertEquals("Rule Out Of Order (6)", "gamma", ((TestRule) matches.get(2)).getIdentifier()); 140 141 rules.clear(); 142 matches = rules.rules(); 143 assertEquals("Wrong number of rules returned (3)", 0, matches.size()); 144 145 matches = rules.match("", "/eggs"); 146 assertEquals("Wrong number of rules returned (4)", 0, matches.size()); 147 } 148 149 public void testSimpleRegexMatch() { 150 151 SimpleRegexMatcher matcher = new SimpleRegexMatcher(); 152 153 156 157 assertEquals( 158 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/beta/gamma' ", 159 true, 160 matcher.match("/alpha/beta/gamma", "/alpha/beta/gamma")); 161 assertEquals( 162 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/beta/gamma/epsilon' ", 163 false, 164 matcher.match("/alpha/beta/gamma", "/alpha/beta/gamma/epsilon")); 165 assertEquals( 166 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*' ", 167 true, 168 matcher.match("/alpha/beta/gamma", "/alpha/*")); 169 assertEquals( 170 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*/gamma' ", 171 true, 172 matcher.match("/alpha/beta/gamma", "/alpha/*/gamma")); 173 assertEquals( 174 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/*me' ", 175 false, 176 matcher.match("/alpha/beta/gamma", "/alpha/*me")); 177 assertEquals( 178 "Simple Regex Match '/alpha/beta/gamma' to '*/beta/gamma' ", 179 true, 180 matcher.match("/alpha/beta/gamma", "*/beta/gamma")); 181 assertEquals( 182 "Simple Regex Match '/alpha/beta/gamma' to '*/alpha/beta/gamma' ", 183 true, 184 matcher.match("/alpha/beta/gamma", "*/alpha/beta/gamma")); 185 assertEquals( 186 "Simple Regex Match '/alpha/beta/gamma' to '*/bet/gamma' ", 187 false, 188 matcher.match("/alpha/beta/gamma", "*/bet/gamma")); 189 assertEquals( 190 "Simple Regex Match '/alpha/beta/gamma' to 'alph?/beta/gamma' ", 191 true, 192 matcher.match("/alpha/beta/gamma", "/alph?/beta/gamma")); 193 assertEquals( 194 "Simple Regex Match '/alpha/beta/gamma' to '/?lpha/beta/gamma' ", 195 true, 196 matcher.match("/alpha/beta/gamma", "/?lpha/beta/gamma")); 197 assertEquals( 198 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?beta/gamma' ", 199 false, 200 matcher.match("/alpha/beta/gamma", "/alpha/?beta/gamma")); 201 assertEquals( 202 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?eta/*' ", 203 true, 204 matcher.match("/alpha/beta/gamma", "/alpha/?eta/*")); 205 assertEquals( 206 "Simple Regex Match '/alpha/beta/gamma' to '/alpha/?eta/*e' ", 207 false, 208 matcher.match("/alpha/beta/gamma", "/alpha/?eta/*e")); 209 assertEquals( 210 "Simple Regex Match '/alpha/beta/gamma' to '*/?et?/?amma' ", 211 true, 212 matcher.match("/alpha/beta/gamma", "*/?et?/?amma")); 213 assertEquals( 214 "Simple Regex Match '/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon' to " 215 + " '*/beta/gamma/?p*n' ", 216 true, 217 matcher.match("/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon", "*/beta/gamma/?p*n")); 218 assertEquals( 219 "Simple Regex Match '/alpha/beta/gamma/beta/epsilon/beta/gamma/epsilon' to " 220 + " '*/beta/gamma/?p*no' ", 221 false, 222 matcher.match("/alpha/beta/gamma", "*/beta/gamma/?p*no")); 223 } 224 } 225 | Popular Tags |