001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy.interceptor;
019    
020    import junit.framework.TestCase;
021    import org.aopalliance.intercept.MethodInterceptor;
022    import org.aopalliance.intercept.MethodInvocation;
023    import org.apache.commons.proxy.ProxyUtils;
024    import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
025    import org.apache.commons.proxy.util.Echo;
026    import org.apache.commons.proxy.util.EchoImpl;
027    
028    public class TestMethodInterceptorAdapter extends TestCase
029    {
030        public void testMethodInterception()
031        {
032            final Echo proxy = ( Echo ) new JavassistProxyFactory().createInterceptorProxy( new EchoImpl(),
033                                                                                             new MethodInterceptorAdapter( new SuffixMethodInterceptor(
034                                                                                                     " suffix" ) ),
035                                                                                             new Class[]{ Echo.class } );
036            assertEquals( "message suffix", proxy.echoBack( "message" ) );
037        }
038    
039        public void testMethodInvocationImplementation() throws Exception
040        {
041            final InterceptorTester tester = new InterceptorTester();
042            final EchoImpl target = new EchoImpl();
043            final Echo proxy = ( Echo ) new JavassistProxyFactory().createInterceptorProxy( target, new MethodInterceptorAdapter( tester ), new Class[] { Echo.class } );
044            proxy.echo();
045            assertNotNull( tester.invocation.getArguments() );
046            assertEquals( 0, tester.invocation.getArguments().length );
047            assertEquals( Echo.class.getMethod( "echo", new Class[] {} ), tester.invocation.getMethod() );
048            assertEquals( Echo.class.getMethod( "echo", new Class[] {} ), tester.invocation.getStaticPart() );
049            assertEquals( target, tester.invocation.getThis() );
050            proxy.echoBack( "Hello" );
051            assertNotNull( tester.invocation.getArguments() );
052            assertEquals( 1, tester.invocation.getArguments().length );
053            assertEquals( "Hello", tester.invocation.getArguments()[0] );
054            assertEquals( Echo.class.getMethod( "echoBack", new Class[] { String.class } ), tester.invocation.getMethod() );
055            assertEquals( Echo.class.getMethod( "echoBack", new Class[] { String.class } ), tester.invocation.getStaticPart() );
056            proxy.echoBack( "Hello", "World" );
057            assertNotNull( tester.invocation.getArguments() );
058            assertEquals( 2, tester.invocation.getArguments().length );
059            assertEquals( "Hello", tester.invocation.getArguments()[0] );
060            assertEquals( "World", tester.invocation.getArguments()[1] );
061            assertEquals( Echo.class.getMethod( "echoBack", new Class[] { String.class, String.class } ), tester.invocation.getMethod() );
062            assertEquals( Echo.class.getMethod( "echoBack", new Class[] { String.class, String.class } ), tester.invocation.getStaticPart() );
063        }
064        private static class InterceptorTester implements MethodInterceptor
065        {
066            private MethodInvocation invocation;
067    
068            public Object invoke( MethodInvocation methodInvocation ) throws Throwable
069            {
070                this.invocation = methodInvocation;
071                return methodInvocation.proceed();
072            }
073        }
074        private class SuffixMethodInterceptor implements MethodInterceptor
075        {
076            private final String suffix;
077    
078            public SuffixMethodInterceptor( String suffix )
079            {
080                this.suffix = suffix;
081            }
082    
083            public Object invoke( MethodInvocation methodInvocation ) throws Throwable
084            {
085                return methodInvocation.proceed() + suffix;
086            }
087        }
088    }