1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.proxy.interceptor;
19
20 import junit.framework.TestCase;
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.commons.proxy.ProxyUtils;
24 import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
25 import org.apache.commons.proxy.util.Echo;
26 import org.apache.commons.proxy.util.EchoImpl;
27
28 public class TestMethodInterceptorAdapter extends TestCase
29 {
30 public void testMethodInterception()
31 {
32 final Echo proxy = ( Echo ) new JavassistProxyFactory().createInterceptorProxy( new EchoImpl(),
33 new MethodInterceptorAdapter( new SuffixMethodInterceptor(
34 " suffix" ) ),
35 new Class[]{ Echo.class } );
36 assertEquals( "message suffix", proxy.echoBack( "message" ) );
37 }
38
39 public void testMethodInvocationImplementation() throws Exception
40 {
41 final InterceptorTester tester = new InterceptorTester();
42 final EchoImpl target = new EchoImpl();
43 final Echo proxy = ( Echo ) new JavassistProxyFactory().createInterceptorProxy( target, new MethodInterceptorAdapter( tester ), new Class[] { Echo.class } );
44 proxy.echo();
45 assertNotNull( tester.invocation.getArguments() );
46 assertEquals( 0, tester.invocation.getArguments().length );
47 assertEquals( Echo.class.getMethod( "echo", new Class[] {} ), tester.invocation.getMethod() );
48 assertEquals( Echo.class.getMethod( "echo", new Class[] {} ), tester.invocation.getStaticPart() );
49 assertEquals( target, tester.invocation.getThis() );
50 proxy.echoBack( "Hello" );
51 assertNotNull( tester.invocation.getArguments() );
52 assertEquals( 1, tester.invocation.getArguments().length );
53 assertEquals( "Hello", tester.invocation.getArguments()[0] );
54 assertEquals( Echo.class.getMethod( "echoBack", new Class[] { String.class } ), tester.invocation.getMethod() );
55 assertEquals( Echo.class.getMethod( "echoBack", new Class[] { String.class } ), tester.invocation.getStaticPart() );
56 proxy.echoBack( "Hello", "World" );
57 assertNotNull( tester.invocation.getArguments() );
58 assertEquals( 2, tester.invocation.getArguments().length );
59 assertEquals( "Hello", tester.invocation.getArguments()[0] );
60 assertEquals( "World", tester.invocation.getArguments()[1] );
61 assertEquals( Echo.class.getMethod( "echoBack", new Class[] { String.class, String.class } ), tester.invocation.getMethod() );
62 assertEquals( Echo.class.getMethod( "echoBack", new Class[] { String.class, String.class } ), tester.invocation.getStaticPart() );
63 }
64 private static class InterceptorTester implements MethodInterceptor
65 {
66 private MethodInvocation invocation;
67
68 public Object invoke( MethodInvocation methodInvocation ) throws Throwable
69 {
70 this.invocation = methodInvocation;
71 return methodInvocation.proceed();
72 }
73 }
74 private class SuffixMethodInterceptor implements MethodInterceptor
75 {
76 private final String suffix;
77
78 public SuffixMethodInterceptor( String suffix )
79 {
80 this.suffix = suffix;
81 }
82
83 public Object invoke( MethodInvocation methodInvocation ) throws Throwable
84 {
85 return methodInvocation.proceed() + suffix;
86 }
87 }
88 }