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 org.aopalliance.intercept.MethodInterceptor;
21  import org.aopalliance.intercept.MethodInvocation;
22  import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
23  import org.apache.commons.proxy.util.AbstractTestCase;
24  import org.apache.commons.proxy.util.Echo;
25  import org.apache.commons.proxy.util.EchoImpl;
26  
27  import java.io.Serializable;
28  
29  public class TestMethodInterceptorAdapter extends AbstractTestCase
30  {
31  //**********************************************************************************************************************
32  // Other Methods
33  //**********************************************************************************************************************
34  
35      public void testSerialization()
36      {
37          final Echo proxy = ( Echo ) new JavassistProxyFactory().createInterceptorProxy(new EchoImpl(),
38                  new MethodInterceptorAdapter(new SuffixMethodInterceptor(
39                          " suffix")),
40                  new Class[] {Echo.class});
41          assertSerializable(proxy);
42      }
43      
44      public void testMethodInterception()
45      {
46          final Echo proxy = ( Echo ) new JavassistProxyFactory().createInterceptorProxy(new EchoImpl(),
47                  new MethodInterceptorAdapter(new SuffixMethodInterceptor(
48                          " suffix")),
49                  new Class[] {Echo.class});
50          assertEquals("message suffix", proxy.echoBack("message"));
51      }
52  
53      public void testMethodInvocationImplementation() throws Exception
54      {
55          final InterceptorTester tester = new InterceptorTester();
56          final EchoImpl target = new EchoImpl();
57          final Echo proxy = ( Echo ) new JavassistProxyFactory().createInterceptorProxy(target, new MethodInterceptorAdapter(tester), new Class[] {Echo.class});
58          proxy.echo();
59          assertNotNull(tester.invocation.getArguments());
60          assertEquals(0, tester.invocation.getArguments().length);
61          assertEquals(Echo.class.getMethod("echo", new Class[] {}), tester.invocation.getMethod());
62          assertEquals(Echo.class.getMethod("echo", new Class[] {}), tester.invocation.getStaticPart());
63          assertEquals(target, tester.invocation.getThis());
64          proxy.echoBack("Hello");
65          assertNotNull(tester.invocation.getArguments());
66          assertEquals(1, tester.invocation.getArguments().length);
67          assertEquals("Hello", tester.invocation.getArguments()[0]);
68          assertEquals(Echo.class.getMethod("echoBack", new Class[] {String.class}), tester.invocation.getMethod());
69          assertEquals(Echo.class.getMethod("echoBack", new Class[] {String.class}), tester.invocation.getStaticPart());
70          proxy.echoBack("Hello", "World");
71          assertNotNull(tester.invocation.getArguments());
72          assertEquals(2, tester.invocation.getArguments().length);
73          assertEquals("Hello", tester.invocation.getArguments()[0]);
74          assertEquals("World", tester.invocation.getArguments()[1]);
75          assertEquals(Echo.class.getMethod("echoBack", new Class[] {String.class, String.class}), tester.invocation.getMethod());
76          assertEquals(Echo.class.getMethod("echoBack", new Class[] {String.class, String.class}), tester.invocation.getStaticPart());
77      }
78  
79  //**********************************************************************************************************************
80  // Inner Classes
81  //**********************************************************************************************************************
82  
83      private static class InterceptorTester implements MethodInterceptor
84      {
85          private MethodInvocation invocation;
86  
87          public Object invoke( MethodInvocation methodInvocation ) throws Throwable
88          {
89              this.invocation = methodInvocation;
90              return methodInvocation.proceed();
91          }
92      }
93  
94      private static class SuffixMethodInterceptor implements MethodInterceptor, Serializable
95      {
96          private final String suffix;
97  
98          public SuffixMethodInterceptor( String suffix )
99          {
100             this.suffix = suffix;
101         }
102 
103         public Object invoke( MethodInvocation methodInvocation ) throws Throwable
104         {
105             return methodInvocation.proceed() + suffix;
106         }
107     }
108 }