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 import java.util.Date;
29
30 /**
31 * @author James Carman
32 * @since 1.0
33 */
34 public abstract class AbstractSubclassingProxyFactoryTestCase extends AbstractProxyFactoryTestCase
35 {
36 private static final Class[] DATE_ONLY = new Class[]{Date.class};
37 //**********************************************************************************************************************
38 // Constructors
39 //**********************************************************************************************************************
40
41 protected AbstractSubclassingProxyFactoryTestCase(ProxyFactory factory)
42 {
43 super(factory);
44 }
45
46 //**********************************************************************************************************************
47 // Other Methods
48 //**********************************************************************************************************************
49
50 public void testCanProxy()
51 {
52 assertTrue(factory.canProxy(new Class[]{Echo.class}));
53 assertTrue(factory.canProxy(new Class[]{EchoImpl.class}));
54 assertFalse(factory.canProxy(new Class[]{FinalEcho.class}));
55 assertTrue(factory.canProxy(new Class[]{FinalMethodEcho.class, Echo.class}));
56 assertFalse(factory.canProxy(new Class[]{NoDefaultConstructorEcho.class}));
57 assertTrue(factory.canProxy(new Class[]{ProtectedConstructorEcho.class}));
58 assertFalse(factory.canProxy(new Class[]{InvisibleEcho.class}));
59 assertFalse(factory.canProxy(new Class[]{Echo.class, EchoImpl.class, String.class}));
60 }
61
62 public void testDelegatorWithMultipleSuperclasses()
63 {
64 try
65 {
66 factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()),
67 new Class[]{EchoImpl.class, String.class});
68 fail();
69 }
70 catch (ProxyFactoryException e)
71 {
72 }
73 }
74
75 public void testDelegatorWithSuperclass()
76 {
77 final Echo echo = (Echo) factory
78 .createDelegatorProxy(new ConstantProvider(new EchoImpl()), new Class[]{Echo.class, EchoImpl.class});
79 assertTrue(echo instanceof EchoImpl);
80 }
81
82 public void testInterceptorWithMultipleSuperclasses()
83 {
84 try
85 {
86 factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(),
87 new Class[]{EchoImpl.class, String.class});
88 fail();
89 }
90 catch (ProxyFactoryException e)
91 {
92 }
93 }
94
95 public void testInterceptorWithSuperclass()
96 {
97 final Echo echo = (Echo) factory
98 .createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), new Class[]{Echo.class, EchoImpl.class});
99 assertTrue(echo instanceof EchoImpl);
100 }
101
102 public void testInvocationHandlerWithMultipleSuperclasses()
103 {
104 try
105 {
106 factory.createInvokerProxy(new NullInvoker(),
107 new Class[]{EchoImpl.class, String.class});
108 fail();
109 }
110 catch (ProxyFactoryException e)
111 {
112 }
113 }
114
115 public void testInvokerWithSuperclass()
116 {
117 final Echo echo = (Echo) factory
118 .createInvokerProxy(new NullInvoker(), new Class[]{Echo.class, EchoImpl.class});
119 assertTrue(echo instanceof EchoImpl);
120 }
121
122 public void testProxiesWithClashingFinalMethodInSuperclass()
123 {
124 final Class[] proxyClasses = new Class[]{Echo.class, FinalMethodEcho.class};
125 Echo proxy = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), proxyClasses);
126 assertEquals("final", proxy.echoBack("echo"));
127
128 proxy = (Echo) factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), proxyClasses);
129 assertEquals("final", proxy.echoBack("echo"));
130
131 proxy = (Echo) factory.createInvokerProxy(new NullInvoker(), proxyClasses);
132 assertEquals("final", proxy.echoBack("echo"));
133 }
134
135 public void testWithAbstractSuperclass()
136 {
137 final Echo echo = (Echo) factory.createDelegatorProxy(new ConstantProvider(new EchoImpl()), new Class[]{AbstractEcho.class});
138 assertEquals("hello", echo.echoBack("hello"));
139 assertEquals("helloworld", echo.echoBack("hello", "world"));
140 }
141
142 public void testInterceptorEquals()
143 {
144 final EqualsEcho echo = new EqualsEcho("text");
145 final Echo proxy1 = (Echo) factory.createInterceptorProxy(echo,
146 new NoOpMethodInterceptor(), new Class[] { EqualsEcho.class } );
147 final Echo proxy2 = (Echo) factory.createInterceptorProxy(echo,
148 new NoOpMethodInterceptor(), new Class[] { EqualsEcho.class } );
149 assertEquals(proxy1, proxy1);
150 assertFalse(proxy1.equals(proxy2));
151 assertFalse(proxy2.equals(proxy1));
152 }
153
154 public void testInvokerEquals() throws Exception
155 {
156 final Date proxy1 = (Date) factory.createInvokerProxy(new InvokerTester(), DATE_ONLY);
157 final Date proxy2 = (Date) factory.createInvokerProxy(new InvokerTester(), DATE_ONLY);
158 assertEquals(proxy1, proxy1);
159 assertFalse(proxy1.equals(proxy2));
160 assertFalse(proxy2.equals(proxy1));
161 }
162
163 public void testDelegatorEquals() throws Exception
164 {
165 final EqualsEcho echo = new EqualsEcho("text");
166 final Echo proxy1 = (Echo) factory.createDelegatorProxy(new ConstantProvider(echo),
167 new Class[] { EqualsEcho.class });
168 final Echo proxy2 = (Echo) factory.createDelegatorProxy(new ConstantProvider(echo),
169 new Class[] { EqualsEcho.class });
170 assertEquals(proxy1, proxy1);
171 assertFalse(proxy1.equals(proxy2));
172 assertFalse(proxy2.equals(proxy1));
173 }
174
175 //**********************************************************************************************************************
176 // Inner Classes
177 //**********************************************************************************************************************
178
179 public static final class FinalEcho extends EchoImpl
180 {
181 }
182
183 public static class FinalMethodEcho extends EchoImpl
184 {
185 public final String echoBack(String message)
186 {
187 return "final";
188 }
189 }
190
191 public static class EqualsEcho extends EchoImpl
192 {
193 private final String text;
194
195 public EqualsEcho()
196 {
197 this("testing");
198 }
199
200 public EqualsEcho(String text)
201 {
202 this.text = text;
203 }
204 }
205
206 private static class InvisibleEcho extends EchoImpl
207 {
208 }
209
210 public static class NoDefaultConstructorEcho extends EchoImpl
211 {
212 public NoDefaultConstructorEcho(String param)
213 {
214 }
215 }
216
217 public static class ProtectedConstructorEcho extends EchoImpl
218 {
219 protected ProtectedConstructorEcho()
220 {
221 }
222 }
223 }