1 package test.crispy; 2 3 import java.lang.reflect.Method; 4 5 import junit.framework.TestCase; 6 import net.sf.crispy.InterceptorFilterContext; 7 import net.sf.crispy.InvocationException; 8 import net.sf.crispy.filter.NamePatternInterceptorFilter; 9 import net.sf.crispy.filter.SimpleNameInterceptorFilter; 10 import test.crispy.example.service.Calculator; 11 import test.crispy.example.service.Echo; 12 13 public class InterceptorFilterTest extends TestCase { 14 15 public void testInterceptorFilterContext () throws Exception { 16 InterceptorFilterContext lvContext = new InterceptorFilterContext (); 17 Object args[] = new Object[] { new Integer(1), new Long(2) }; 18 lvContext.setArgs(args); 19 assertEquals(lvContext.getArgs(), args); 20 21 assertNull(lvContext.getInvocationClass()); 22 Method lvMethodAdd = Calculator.class.getMethod("add", new Class[] {int.class, int.class}); 23 lvContext.setMethod(lvMethodAdd); 24 assertEquals(lvContext.getInvocationClass(), Calculator.class); 25 26 lvContext.setInvocationClass(Echo.class); 27 assertEquals(lvContext.getInvocationClass(), Echo.class); 28 29 } 30 public void testSimpleInterceptorFilterForClass() throws Exception { 31 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(Calculator.class); 32 lvFilter.setNegate(false); 33 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 34 boolean lvAccept = lvFilter.accept(lvContext); 35 assertTrue(lvAccept); 36 37 lvContext = new InterceptorFilterContext(Echo.class); 38 lvAccept = lvFilter.accept(lvContext); 39 assertFalse(lvAccept); 40 } 41 42 public void testSimpleInterceptorFilterForClassNegate() throws Exception { 43 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(Calculator.class); 44 lvFilter.setNegate(true); 45 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 46 boolean lvAccept = lvFilter.accept(lvContext); 47 assertFalse(lvAccept); 48 49 lvContext = new InterceptorFilterContext(Echo.class); 50 lvAccept = lvFilter.accept(lvContext); 51 assertTrue(lvAccept); 52 } 53 54 public void testSimpleInterceptorFilterForClassName() throws Exception { 55 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(Calculator.class.getName(), SimpleNameInterceptorFilter.FILTER_TYPE_CLASS); 56 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 57 boolean lvAccept = lvFilter.accept(lvContext); 58 assertTrue(lvAccept); 59 60 lvContext = new InterceptorFilterContext(Echo.class); 61 lvAccept = lvFilter.accept(lvContext); 62 assertFalse(lvAccept); 63 } 64 65 public void testSimpleInterceptorFilterForClassNameNegate() throws Exception { 66 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(Calculator.class.getName(), SimpleNameInterceptorFilter.FILTER_TYPE_CLASS); 67 lvFilter.setNegate(true); 68 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 69 boolean lvAccept = lvFilter.accept(lvContext); 70 assertFalse(lvAccept); 71 72 lvContext = new InterceptorFilterContext(Echo.class); 73 lvAccept = lvFilter.accept(lvContext); 74 assertTrue(lvAccept); 75 } 76 77 public void testSimpleInterceptorFilterForClassArray() throws Exception { 78 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(new Class[] {Calculator.class, Echo.class}); 79 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 80 boolean lvAccept = lvFilter.accept(lvContext); 81 assertTrue(lvAccept); 82 83 lvContext = new InterceptorFilterContext(Echo.class); 84 lvAccept = lvFilter.accept(lvContext); 85 assertTrue(lvAccept); 86 87 lvContext = new InterceptorFilterContext(Object.class); 88 lvAccept = lvFilter.accept(lvContext); 89 assertFalse(lvAccept); 90 } 91 92 public void testSimpleInterceptorFilterForClassArrayNegate() throws Exception { 93 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(new Class[] {Calculator.class, Echo.class}); 94 lvFilter.setNegate(true); 95 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 96 boolean lvAccept = lvFilter.accept(lvContext); 97 assertFalse(lvAccept); 98 99 lvContext = new InterceptorFilterContext(Echo.class); 100 lvAccept = lvFilter.accept(lvContext); 101 assertFalse(lvAccept); 102 103 lvContext = new InterceptorFilterContext(Object.class); 104 lvAccept = lvFilter.accept(lvContext); 105 assertTrue(lvAccept); 106 } 107 108 public void testSimpleInterceptorFilterForMethod() throws Exception { 109 Method lvMethodAdd = Calculator.class.getMethod("add", new Class[] {int.class, int.class}); 110 Method lvMethodEcho = Echo.class.getMethod("echo", new Class[] { String.class }); 111 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(lvMethodAdd); 112 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 113 lvContext.setMethod(lvMethodAdd); 114 boolean lvAccept = lvFilter.accept(lvContext); 115 assertTrue(lvAccept); 116 117 lvContext = new InterceptorFilterContext(Echo.class); 118 lvContext.setMethod(lvMethodEcho); 119 lvAccept = lvFilter.accept(lvContext); 120 assertFalse(lvAccept); 121 } 122 123 public void testSimpleInterceptorFilterForMethodName() throws Exception { 124 Method lvMethodAdd = Calculator.class.getMethod("add", new Class[] {int.class, int.class}); 125 Method lvMethodEcho = Echo.class.getMethod("echo", new Class[] { String.class }); 126 127 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter("add", SimpleNameInterceptorFilter.FILTER_TYPE_METHOD); 128 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 129 lvContext.setMethod(lvMethodAdd); 130 boolean lvAccept = lvFilter.accept(lvContext); 131 assertTrue(lvAccept); 132 133 lvContext = new InterceptorFilterContext(Echo.class); 134 lvContext.setMethod(lvMethodEcho); 135 lvAccept = lvFilter.accept(lvContext); 136 assertFalse(lvAccept); 137 } 138 139 public void testSimpleInterceptorFilterForMethodArray() throws Exception { 140 Method lvMethodAdd = Calculator.class.getMethod("add", new Class[] {int.class, int.class}); 141 Method lvMethodEcho = Echo.class.getMethod("echo", new Class[] { String.class }); 142 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(new Method[] {lvMethodAdd, lvMethodEcho}); 143 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 144 lvContext.setMethod(lvMethodAdd); 145 boolean lvAccept = lvFilter.accept(lvContext); 146 assertTrue(lvAccept); 147 148 lvContext = new InterceptorFilterContext(Echo.class); 149 lvContext.setMethod(lvMethodEcho); 150 lvAccept = lvFilter.accept(lvContext); 151 assertTrue(lvAccept); 152 153 Method lvMethodSubtract = Calculator.class.getMethod("subtract", new Class[] {int.class, int.class}); 154 lvContext = new InterceptorFilterContext(Echo.class); 155 lvContext.setMethod(lvMethodSubtract); 156 lvAccept = lvFilter.accept(lvContext); 157 assertFalse(lvAccept); 158 } 159 160 public void testSimpleInterceptorFilterForMethodNameArray() throws Exception { 161 Method lvMethodAdd = Calculator.class.getMethod("add", new Class[] {int.class, int.class}); 162 Method lvMethodEcho = Echo.class.getMethod("echo", new Class[] { String.class }); 163 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(new String[] {"add", "echo"}, SimpleNameInterceptorFilter.FILTER_TYPE_METHOD); 164 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 165 lvContext.setMethod(lvMethodAdd); 166 boolean lvAccept = lvFilter.accept(lvContext); 167 assertTrue(lvAccept); 168 169 lvContext = new InterceptorFilterContext(Echo.class); 170 lvContext.setMethod(lvMethodEcho); 171 lvAccept = lvFilter.accept(lvContext); 172 assertTrue(lvAccept); 173 174 Method lvMethodSubtract = Calculator.class.getMethod("subtract", new Class[] {int.class, int.class}); 175 lvContext = new InterceptorFilterContext(Echo.class); 176 lvContext.setMethod(lvMethodSubtract); 177 lvAccept = lvFilter.accept(lvContext); 178 assertFalse(lvAccept); 179 } 180 181 public void testSimpleInterceptorFilterForThrowable() throws Exception { 182 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(new InvocationException("Test-Exception")); 183 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 184 lvContext.setThrowable(new InvocationException("Test-Exception")); 185 boolean lvAccept = lvFilter.accept(lvContext); 186 assertTrue(lvAccept); 187 188 lvContext = new InterceptorFilterContext(Echo.class); 189 lvContext.setThrowable(new NullPointerException("Test-Exception")); 190 lvAccept = lvFilter.accept(lvContext); 191 assertFalse(lvAccept); 192 } 193 194 public void testSimpleInterceptorFilterForThrowableName() throws Exception { 195 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(InvocationException.class.getName(), SimpleNameInterceptorFilter.FILTER_TYPE_THROWABLE); 196 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 197 lvContext.setThrowable(new InvocationException("Test-Exception")); 198 boolean lvAccept = lvFilter.accept(lvContext); 199 assertTrue(lvAccept); 200 201 lvContext = new InterceptorFilterContext(Echo.class); 202 lvContext.setThrowable(new NullPointerException("Test-Exception")); 203 lvAccept = lvFilter.accept(lvContext); 204 assertFalse(lvAccept); 205 } 206 207 public void testSimpleInterceptorFilterForThrowableName2() throws Exception { 208 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(InvocationException.class.getName(), SimpleNameInterceptorFilter.FILTER_TYPE_THROWABLE); 209 InterceptorFilterContext lvContext = new InterceptorFilterContext(); 210 lvContext.setThrowable(new InvocationException("Test-Exception")); 211 boolean lvAccept = lvFilter.accept(lvContext); 212 assertTrue(lvAccept); 213 214 lvContext = new InterceptorFilterContext(Echo.class); 215 lvContext.setThrowable(new NullPointerException("Test-Exception")); 216 lvAccept = lvFilter.accept(lvContext); 217 assertFalse(lvAccept); 218 } 219 220 221 public void testSimpleInterceptorFilterForThrowableArray() throws Exception { 222 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(new Exception [] {new InvocationException("Test-Exception")}); 223 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 224 lvContext.setThrowable(new InvocationException("Test-Exception")); 225 boolean lvAccept = lvFilter.accept(lvContext); 226 assertTrue(lvAccept); 227 228 lvContext = new InterceptorFilterContext(Echo.class); 229 lvContext.setThrowable(new NullPointerException("Test-Exception")); 230 lvAccept = lvFilter.accept(lvContext); 231 assertFalse(lvAccept); 232 } 233 234 public void testSimpleInterceptorFilterForThrowableEqualsNull() throws Exception { 235 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(".*InvocationException", SimpleNameInterceptorFilter.FILTER_TYPE_THROWABLE); 236 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 237 boolean lvAccept = lvFilter.accept(lvContext); 238 assertFalse(lvAccept); 239 } 240 241 242 public void testName() throws Exception { 243 SimpleNameInterceptorFilter lvFilter = new SimpleNameInterceptorFilter(new String[] {"add", "echo"}, -99); 244 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 245 Method lvMethodAdd = Calculator.class.getMethod("add", new Class[] {int.class, int.class}); 246 lvContext.setMethod(lvMethodAdd); 247 boolean lvAccept = lvFilter.accept(lvContext); 248 assertFalse(lvAccept); 249 } 250 251 252 public void testNamePatternInterceptorFilterFilterForClass() throws Exception { 253 NamePatternInterceptorFilter lvFilter = new NamePatternInterceptorFilter(".*C.*", NamePatternInterceptorFilter.FILTER_TYPE_CLASS); 254 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 255 boolean lvAccept = lvFilter.accept(lvContext); 256 assertTrue(lvAccept); 257 258 lvContext = new InterceptorFilterContext(Echo.class); 259 lvAccept = lvFilter.accept(lvContext); 260 assertFalse(lvAccept); 261 } 262 263 public void testNamePatternInterceptorFilterFilterForClassNegate() throws Exception { 264 NamePatternInterceptorFilter lvFilter = new NamePatternInterceptorFilter(".*C.*", NamePatternInterceptorFilter.FILTER_TYPE_CLASS); 265 lvFilter.setNegate(true); 266 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 267 boolean lvAccept = lvFilter.accept(lvContext); 268 assertFalse(lvAccept); 269 270 lvContext = new InterceptorFilterContext(Echo.class); 271 lvAccept = lvFilter.accept(lvContext); 272 assertTrue(lvAccept); 273 } 274 275 public void testNamePatternInterceptorFilterForMethodName() throws Exception { 276 Method lvMethodAdd = Calculator.class.getMethod("add", new Class[] {int.class, int.class}); 277 Method lvMethodEcho = Echo.class.getMethod("echo", new Class[] { String.class }); 278 279 NamePatternInterceptorFilter lvFilter = new NamePatternInterceptorFilter("a.*", NamePatternInterceptorFilter.FILTER_TYPE_METHOD); 280 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 281 lvContext.setMethod(lvMethodAdd); 282 boolean lvAccept = lvFilter.accept(lvContext); 283 assertTrue(lvAccept); 284 285 lvContext = new InterceptorFilterContext(Echo.class); 286 lvContext.setMethod(lvMethodEcho); 287 lvAccept = lvFilter.accept(lvContext); 288 assertFalse(lvAccept); 289 } 290 291 public void testNamePatternInterceptorFilterForThrowableName() throws Exception { 292 NamePatternInterceptorFilter lvFilter = new NamePatternInterceptorFilter(".*InvocationException", NamePatternInterceptorFilter.FILTER_TYPE_THROWABLE); 293 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 294 lvContext.setThrowable(new InvocationException("Test-Exception")); 295 boolean lvAccept = lvFilter.accept(lvContext); 296 assertTrue(lvAccept); 297 298 lvContext = new InterceptorFilterContext(Echo.class); 299 lvContext.setThrowable(new NullPointerException("Test-Exception")); 300 lvAccept = lvFilter.accept(lvContext); 301 assertFalse(lvAccept); 302 } 303 304 public void testNamePatternInterceptorFilterForThrowableEqualsNull() throws Exception { 305 NamePatternInterceptorFilter lvFilter = new NamePatternInterceptorFilter(".*InvocationException", NamePatternInterceptorFilter.FILTER_TYPE_THROWABLE); 306 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 307 boolean lvAccept = lvFilter.accept(lvContext); 308 assertFalse(lvAccept); 309 } 310 311 public void testNamePatternInterceptorFilterNullPatter() throws Exception { 312 NamePatternInterceptorFilter lvFilter = new NamePatternInterceptorFilter(null, NamePatternInterceptorFilter.FILTER_TYPE_THROWABLE); 313 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 314 lvContext.setThrowable(new InvocationException("Test-Exception")); 315 boolean lvAccept = lvFilter.accept(lvContext); 316 assertTrue(lvAccept); 317 318 lvContext = new InterceptorFilterContext(Echo.class); 319 lvContext.setThrowable(new NullPointerException("Test-Exception")); 320 lvAccept = lvFilter.accept(lvContext); 321 assertTrue(lvAccept); 322 } 323 324 public void testNamePatternInterceptorFilterEverFalse() throws Exception { 325 NamePatternInterceptorFilter lvFilter = new NamePatternInterceptorFilter(".*", NamePatternInterceptorFilter.FILTER_TYPE_NO); 326 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 327 lvContext.setThrowable(new InvocationException("Test-Exception")); 328 boolean lvAccept = lvFilter.accept(lvContext); 329 assertFalse(lvAccept); 330 331 lvContext = new InterceptorFilterContext(Echo.class); 332 lvContext.setThrowable(new NullPointerException("Test-Exception")); 333 lvAccept = lvFilter.accept(lvContext); 334 assertFalse(lvAccept); 335 } 336 337 public void testNamePatternInterceptorFilterEverMethodFalse() throws Exception { 338 NamePatternInterceptorFilter lvFilter = new NamePatternInterceptorFilter(".*", NamePatternInterceptorFilter.FILTER_TYPE_METHOD); 339 InterceptorFilterContext lvContext = new InterceptorFilterContext(Calculator.class); 340 boolean lvAccept = lvFilter.accept(lvContext); 341 assertFalse(lvAccept); 342 } 343 344 }