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.factory;
19  
20  import org.apache.commons.proxy.ProxyFactory;
21  import org.apache.commons.proxy.exception.ProxyFactoryException;
22  import org.apache.commons.proxy.invoker.NullInvoker;
23  import org.apache.commons.proxy.provider.ConstantProvider;
24  import org.apache.commons.proxy.util.AbstractEcho;
25  import org.apache.commons.proxy.util.Echo;
26  import org.apache.commons.proxy.util.EchoImpl;
27  
28  /**
29   * @author James Carman
30   * @since 1.0
31   */
32  public abstract class AbstractSubclassingProxyFactoryTestCase extends AbstractProxyFactoryTestCase
33  {
34      protected AbstractSubclassingProxyFactoryTestCase( ProxyFactory factory )
35      {
36          super( factory );
37      }
38  
39      public void testWithAbstractSuperclass()
40      {
41          final Echo echo = ( Echo )factory.createDelegatorProxy( new ConstantProvider( new EchoImpl() ),  new Class[] { AbstractEcho.class } );
42          assertEquals( "hello", echo.echoBack( "hello" ) );
43          assertEquals( "helloworld", echo.echoBack( "hello", "world" ) );
44      }
45  
46      public void testCanProxy()
47      {
48          assertTrue( factory.canProxy(  new Class[] { Echo.class } ) );
49          assertTrue( factory.canProxy(  new Class[] { EchoImpl.class } ) );
50          assertFalse( factory.canProxy(  new Class[] { FinalEcho.class } ) );
51          assertTrue( factory.canProxy(  new Class[] { FinalMethodEcho.class, Echo.class } ) );
52          assertFalse( factory.canProxy(  new Class[] { NoDefaultConstructorEcho.class } ) );
53          assertTrue( factory.canProxy(  new Class[] { ProtectedConstructorEcho.class } ) );
54          assertFalse( factory.canProxy(  new Class[] { InvisibleEcho.class } ) );
55          assertFalse( factory.canProxy(  new Class[] { Echo.class, EchoImpl.class, String.class } ) );
56      }
57  
58      public void testDelegatorWithSuperclass()
59      {
60          final Echo echo = ( Echo ) factory
61                  .createDelegatorProxy( new ConstantProvider( new EchoImpl() ),  new Class[] { Echo.class, EchoImpl.class } );
62          assertTrue( echo instanceof EchoImpl );
63      }
64  
65      public void testInterceptorWithSuperclass()
66      {
67          final Echo echo = ( Echo ) factory
68                  .createInterceptorProxy( new EchoImpl(), new NoOpMethodInterceptor(),  new Class[] { Echo.class, EchoImpl.class } );
69          assertTrue( echo instanceof EchoImpl );
70      }
71  
72      public void testInvocationHandlerWithSuperclass()
73      {
74          final Echo echo = ( Echo ) factory
75                  .createInvokerProxy( new NullInvoker(),  new Class[] { Echo.class, EchoImpl.class } );
76          assertTrue( echo instanceof EchoImpl );
77      }
78  
79      public void testProxiesWithClashingFinalMethodInSuperclass()
80      {
81          final Class[] proxyClasses = new Class[]{Echo.class, FinalMethodEcho.class};
82          Echo proxy = ( Echo )factory.createDelegatorProxy( new ConstantProvider( new EchoImpl() ), proxyClasses );
83          assertEquals( "final", proxy.echoBack("echo") );
84  
85          proxy = ( Echo )factory.createInterceptorProxy( new EchoImpl(), new NoOpMethodInterceptor(), proxyClasses );
86          assertEquals( "final", proxy.echoBack("echo") );
87  
88          proxy = ( Echo )factory.createInvokerProxy( new NullInvoker(), proxyClasses );
89          assertEquals( "final", proxy.echoBack("echo") );
90      }
91  
92      public void testDelegatorWithMultipleSuperclasses()
93      {
94          try
95          {
96              factory.createDelegatorProxy( new ConstantProvider( new EchoImpl() ),
97                                             new Class[] { EchoImpl.class, String.class } );
98              fail();
99          }
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 }