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.factory;
019    
020    import org.apache.commons.proxy.ProxyFactory;
021    import org.apache.commons.proxy.exception.ProxyFactoryException;
022    import org.apache.commons.proxy.invoker.NullInvoker;
023    import org.apache.commons.proxy.provider.ConstantProvider;
024    import org.apache.commons.proxy.util.AbstractEcho;
025    import org.apache.commons.proxy.util.Echo;
026    import org.apache.commons.proxy.util.EchoImpl;
027    
028    /**
029     * @author James Carman
030     * @since 1.0
031     */
032    public abstract class AbstractSubclassingProxyFactoryTestCase extends AbstractProxyFactoryTestCase
033    {
034        protected AbstractSubclassingProxyFactoryTestCase( ProxyFactory factory )
035        {
036            super( factory );
037        }
038    
039        public void testWithAbstractSuperclass()
040        {
041            final Echo echo = ( Echo )factory.createDelegatorProxy( new ConstantProvider( new EchoImpl() ),  new Class[] { AbstractEcho.class } );
042            assertEquals( "hello", echo.echoBack( "hello" ) );
043            assertEquals( "helloworld", echo.echoBack( "hello", "world" ) );
044        }
045    
046        public void testCanProxy()
047        {
048            assertTrue( factory.canProxy(  new Class[] { Echo.class } ) );
049            assertTrue( factory.canProxy(  new Class[] { EchoImpl.class } ) );
050            assertFalse( factory.canProxy(  new Class[] { FinalEcho.class } ) );
051            assertTrue( factory.canProxy(  new Class[] { FinalMethodEcho.class, Echo.class } ) );
052            assertFalse( factory.canProxy(  new Class[] { NoDefaultConstructorEcho.class } ) );
053            assertTrue( factory.canProxy(  new Class[] { ProtectedConstructorEcho.class } ) );
054            assertFalse( factory.canProxy(  new Class[] { InvisibleEcho.class } ) );
055            assertFalse( factory.canProxy(  new Class[] { Echo.class, EchoImpl.class, String.class } ) );
056        }
057    
058        public void testDelegatorWithSuperclass()
059        {
060            final Echo echo = ( Echo ) factory
061                    .createDelegatorProxy( new ConstantProvider( new EchoImpl() ),  new Class[] { Echo.class, EchoImpl.class } );
062            assertTrue( echo instanceof EchoImpl );
063        }
064    
065        public void testInterceptorWithSuperclass()
066        {
067            final Echo echo = ( Echo ) factory
068                    .createInterceptorProxy( new EchoImpl(), new NoOpMethodInterceptor(),  new Class[] { Echo.class, EchoImpl.class } );
069            assertTrue( echo instanceof EchoImpl );
070        }
071    
072        public void testInvocationHandlerWithSuperclass()
073        {
074            final Echo echo = ( Echo ) factory
075                    .createInvokerProxy( new NullInvoker(),  new Class[] { Echo.class, EchoImpl.class } );
076            assertTrue( echo instanceof EchoImpl );
077        }
078    
079        public void testProxiesWithClashingFinalMethodInSuperclass()
080        {
081            final Class[] proxyClasses = new Class[]{Echo.class, FinalMethodEcho.class};
082            Echo proxy = ( Echo )factory.createDelegatorProxy( new ConstantProvider( new EchoImpl() ), proxyClasses );
083            assertEquals( "final", proxy.echoBack("echo") );
084    
085            proxy = ( Echo )factory.createInterceptorProxy( new EchoImpl(), new NoOpMethodInterceptor(), proxyClasses );
086            assertEquals( "final", proxy.echoBack("echo") );
087    
088            proxy = ( Echo )factory.createInvokerProxy( new NullInvoker(), proxyClasses );
089            assertEquals( "final", proxy.echoBack("echo") );
090        }
091    
092        public void testDelegatorWithMultipleSuperclasses()
093        {
094            try
095            {
096                factory.createDelegatorProxy( new ConstantProvider( new EchoImpl() ),
097                                               new Class[] { EchoImpl.class, String.class } );
098                fail();
099            }
100            catch( ProxyFactoryException e )
101            {
102            }
103        }
104    
105        public void testInterceptorWithMultipleSuperclasses()
106        {
107            try
108            {
109                factory.createInterceptorProxy( new EchoImpl(), new NoOpMethodInterceptor(),
110                                                 new Class[] { EchoImpl.class, String.class } );
111                fail();
112            }
113            catch( ProxyFactoryException e )
114            {
115            }
116        }
117    
118        public void testInvocationHandlerWithMultipleSuperclasses()
119        {
120            try
121            {
122                factory.createInvokerProxy( new NullInvoker(),
123                                                       new Class[] { EchoImpl.class, String.class } );
124                fail();
125            }
126            catch( ProxyFactoryException e )
127            {
128            }
129        }
130    
131        public static final class FinalEcho extends EchoImpl
132        {
133        }
134    
135        public static class FinalMethodEcho extends EchoImpl
136        {
137            public final String echoBack( String message )
138            {
139                return "final";
140            }
141        }
142    
143        public static class NoDefaultConstructorEcho extends EchoImpl
144        {
145            public NoDefaultConstructorEcho( String param )
146            {
147            }
148        }
149    
150        public static class ProtectedConstructorEcho extends EchoImpl
151        {
152            protected ProtectedConstructorEcho()
153            {
154            }
155        }
156    
157        private static class InvisibleEcho extends EchoImpl
158        {
159        }
160    }