1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }