1 20 21 package net.innig.macker.rule; 22 23 import net.innig.macker.structure.*; 24 25 import junit.framework.TestCase; 26 27 public final class MackerRegexTest 28 extends TestCase 29 { 30 public MackerRegexTest(String name) 31 { super(name); } 32 33 protected void setUp() 34 throws Exception 35 { 36 context = new EvaluationContext(new ClassManager(), new RuleSet(RuleSet.getMackerDefaults())); 37 } 38 39 protected void tearDown() 40 throws Exception 41 { 42 } 43 44 private EvaluationContext context; 45 46 private void testMatches(MackerRegex re, String [] positive, String [] negative) 47 throws Exception 48 { 49 for(int n = 0; n < positive.length; n++) 50 assertTrue( 51 "\"" + re + "\" should match \"" + positive[n] + "\"", 52 re.matches(context, positive[n])); 53 54 for(int n = 0; n < negative.length; n++) 55 assertTrue( 56 "\"" + re + "\" shouldn't match \"" + negative[n] + "\"", 57 !re.matches(context, negative[n])); 58 } 59 60 public void test_basic() 61 throws Exception 62 { 63 testMatches( 64 new MackerRegex("java.lang.*"), 65 new String [] { "java.lang.String" }, 66 new String [] { "java.lang.reflect.Method", 67 "java.language.MadeUpClass" }); 68 69 testMatches( 70 new MackerRegex("java.lang.**"), 71 new String [] { "java.lang.String", 72 "java.lang.reflect.Method" }, 73 new String [] { "java.language.MadeUpClass" }); 74 75 testMatches( 76 new MackerRegex("java.lang**"), 77 new String [] { "java.lang.String", 78 "java.lang.reflect.Method", 79 "java.language.MadeUpClass" }, 80 new String [0]); 81 82 testMatches( 83 new MackerRegex("String"), 84 new String [] { "String" }, 85 new String [] { "java.lang.String" }); 86 87 testMatches( 88 new MackerRegex("java.lang.String"), 89 new String [] { "java.lang.String" }, 90 new String [] { "String" }); 91 92 testMatches( 93 new MackerRegex("String"), 94 new String [] { "String" }, 95 new String [] { "java.lang.String", 96 "java.lang.StringBuffer", 97 "java.text.AttributedString" }); 98 testMatches( 99 new MackerRegex("**String*"), 100 new String [] { "String", 101 "java.lang.String", 102 "java.lang.StringBuffer", 103 "java.text.AttributedString" }, 104 new String [0]); 105 106 testMatches( 107 new MackerRegex("**String"), 108 new String [] { "String", 109 "java.lang.String", 110 "java.text.AttributedString" }, 111 new String [] { "java.lang.StringBuffer" }); 112 113 testMatches( 114 new MackerRegex("**.String*"), 115 new String [] { "String", 116 "java.lang.String", 117 "java.lang.StringBuffer" }, 118 new String [] { "java.text.AttributedString" }); 119 120 testMatches( 121 new MackerRegex("a.b"), 122 new String [] { "a.b", 123 "a$b" }, 124 new String [] { "" }); 125 } 126 127 public void test_innerClass() 128 throws Exception 129 { 130 testMatches( 131 new MackerRegex("a.b"), 132 new String [] { "a$b", "a.b" }, 133 new String [0]); 134 135 testMatches( 136 new MackerRegex("a$b"), 137 new String [] { "a$b" }, 138 new String [] { "a.b" }); 139 140 testMatches( 141 new MackerRegex("a/b"), 142 new String [] { "a.b" }, 143 new String [] { "a$b" }); 144 145 testMatches( 146 new MackerRegex("**a*b**"), 147 new String [] { "x.axb", 148 "x$axb", 149 "a$b" }, new String [] { "a.b" }); 151 } 152 153 public void test_variable() 154 throws Exception 155 { 156 MackerRegex re = new MackerRegex("x${var}y"); 157 158 try { 159 re.matches(context, ""); 160 fail("expected exception"); 161 } 162 catch(UndeclaredVariableException uve) 163 { } 164 165 context.setVariableValue("var", ""); 166 testMatches( 167 re, 168 new String [] { "xy" }, 169 new String [] { "xay" }); 170 171 context.setVariableValue("var", ".a"); 172 testMatches( 173 re, 174 new String [] { "x.ay" }, 175 new String [] { "xy", "x.y", "ax.y" }); 176 177 context.setVariableValue("var", ".*"); 178 testMatches( 179 re, 180 new String [] { "x.y", "x.ay" }, 181 new String [] { "x.a.y" }); 182 183 context.setVariableValue("rav", "**"); 184 context.setVariableValue("var", ".${rav}"); 185 testMatches( 186 re, 187 new String [] { "x.y", "x.ay", "x.a.y" }, 188 new String [] { "xy" }); 189 190 context.setVariableValue("rav", "*A*"); 191 testMatches( 192 new MackerRegex("*${rav}*"), 193 new String [] { "Apple" }, 194 new String [] { "granny.smith.Apple", "Apple.computer" }); 195 } 196 197 public void test_illegal() 198 throws Exception 199 { 200 String [] illegal = new String [] 201 { 202 "#", "@", "^", "|", "!", 203 "\n", "\r", "a\nb", "a\rb", 205 "a.", ".", "" }; 207 for(int n = 0; n < illegal.length; n++) 208 try { 209 new MackerRegex(illegal[n]).matches(context, ""); 210 fail("expected exception: \"" + illegal[n] + "\" shouldn't be a legal regex"); 211 } 212 catch(MackerRegexSyntaxException mrse) 213 { } 214 } 215 216 public void test_noParts() 217 throws Exception 218 { 219 String [] illegal = new String [] 220 { 221 "a.b", "a$b", "a/b", 222 ".a", "a.", ".", "" 223 }; 224 for(int n = 0; n < illegal.length; n++) 225 try { 226 new MackerRegex(illegal[n], false).matches(context, ""); 227 fail("expected exception: \"" + illegal[n] + "\" shouldn't be a legal regex in no-parts mode"); 228 } 229 catch(MackerRegexSyntaxException mrse) 230 { } 231 232 assertTrue(new MackerRegex("a*z").matches(context, "abblefrabazz")); 233 } 234 } 235 236 237 238 | Popular Tags |