1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.proxy2;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.util.Date;
25
26 import org.apache.commons.proxy2.exception.ProxyFactoryException;
27 import org.apache.commons.proxy2.invoker.NullInvoker;
28 import org.apache.commons.proxy2.provider.ConstantProvider;
29 import org.apache.commons.proxy2.util.AbstractEcho;
30 import org.apache.commons.proxy2.util.Echo;
31 import org.apache.commons.proxy2.util.EchoImpl;
32 import org.junit.Test;
33
34 @SuppressWarnings("serial")
35 public abstract class AbstractSubclassingProxyFactoryTestCase extends AbstractProxyFactoryTestCase
36 {
37
38
39
40
41 private static final Class<?>[] DATE_ONLY = new Class[] { Date.class };
42
43
44
45
46
47 @Override
48 @Test
49 public void testCanProxy()
50 {
51 assertTrue(factory.canProxy(new Class[] { Echo.class }));
52 assertTrue(factory.canProxy(new Class[] { EchoImpl.class }));
53 assertFalse(factory.canProxy(new Class[] { FinalEcho.class }));
54 assertTrue(factory.canProxy(new Class[] { FinalMethodEcho.class, Echo.class }));
55 assertFalse(factory.canProxy(new Class[] { NoDefaultConstructorEcho.class }));
56 assertTrue(factory.canProxy(new Class[] { ProtectedConstructorEcho.class }));
57 assertFalse(factory.canProxy(new Class[] { InvisibleEcho.class }));
58 assertFalse(factory.canProxy(new Class[] { Echo.class, EchoImpl.class, String.class }));
59 }
60
61 @Override
62 @Test
63 public void testDelegatorEquals() throws Exception
64 {
65 final EqualsEcho echo = new EqualsEcho("text");
66 final Echo proxy1 = factory.createDelegatorProxy(new ConstantProvider<Echo>(echo),
67 new Class[] { EqualsEcho.class });
68 final Echo proxy2 = factory.createDelegatorProxy(new ConstantProvider<Echo>(echo),
69 new Class[] { EqualsEcho.class });
70 assertEquals(proxy1, proxy1);
71 assertFalse(proxy1.equals(proxy2));
72 assertFalse(proxy2.equals(proxy1));
73 }
74
75 @Test(expected = ProxyFactoryException.class)
76 public void testDelegatorWithMultipleSuperclasses()
77 {
78 factory.createDelegatorProxy(new ConstantProvider<EchoImpl>(new EchoImpl()), new Class[] { EchoImpl.class,
79 String.class });
80 }
81
82 @Test
83 public void testDelegatorWithSuperclass()
84 {
85 final Echo echo = factory.createDelegatorProxy(new ConstantProvider<EchoImpl>(new EchoImpl()), new Class[] {
86 Echo.class, EchoImpl.class });
87 assertTrue(echo instanceof EchoImpl);
88 }
89
90 @Override
91 @Test
92 public void testInterceptorEquals()
93 {
94 final EqualsEcho echo = new EqualsEcho("text");
95 final Echo proxy1 = factory.createInterceptorProxy(echo, new NoOpMethodInterceptor(),
96 new Class[] { EqualsEcho.class });
97 final Echo proxy2 = factory.createInterceptorProxy(echo, new NoOpMethodInterceptor(),
98 new Class[] { EqualsEcho.class });
99 assertEquals(proxy1, proxy1);
100 assertFalse(proxy1.equals(proxy2));
101 assertFalse(proxy2.equals(proxy1));
102 }
103
104 @Test(expected = ProxyFactoryException.class)
105 public void testInterceptorWithMultipleSuperclasses()
106 {
107 factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), new Class[] { EchoImpl.class,
108 String.class });
109 }
110
111 @Test
112 public void testInterceptorWithSuperclass()
113 {
114 final Echo echo = factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), new Class[] {
115 Echo.class, EchoImpl.class });
116 assertTrue(echo instanceof EchoImpl);
117 }
118
119 @Test(expected = ProxyFactoryException.class)
120 public void testInvocationHandlerWithMultipleSuperclasses()
121 {
122 factory.createInvokerProxy(new NullInvoker(), new Class[] { EchoImpl.class, String.class });
123 }
124
125 @Override
126 @Test
127 public void testInvokerEquals() throws Exception
128 {
129 final Date proxy1 = factory.createInvokerProxy(new InvokerTester(), DATE_ONLY);
130 final Date proxy2 = factory.createInvokerProxy(new InvokerTester(), DATE_ONLY);
131 assertEquals(proxy1, proxy1);
132 assertFalse(proxy1.equals(proxy2));
133 assertFalse(proxy2.equals(proxy1));
134 }
135
136 @Test
137 public void testInvokerWithSuperclass()
138 {
139 final Echo echo = factory.createInvokerProxy(new NullInvoker(), new Class[] { Echo.class, EchoImpl.class });
140 assertTrue(echo instanceof EchoImpl);
141 }
142
143 @Test
144 public void testProxiesWithClashingFinalMethodInSuperclass()
145 {
146 final Class<?>[] proxyClasses = new Class[] { Echo.class, FinalMethodEcho.class };
147 Echo proxy = factory.createDelegatorProxy(new ConstantProvider<EchoImpl>(new EchoImpl()), proxyClasses);
148 assertEquals("final", proxy.echoBack("echo"));
149
150 proxy = factory.createInterceptorProxy(new EchoImpl(), new NoOpMethodInterceptor(), proxyClasses);
151 assertEquals("final", proxy.echoBack("echo"));
152
153 proxy = factory.createInvokerProxy(new NullInvoker(), proxyClasses);
154 assertEquals("final", proxy.echoBack("echo"));
155 }
156
157 @Test
158 public void testWithAbstractSuperclass()
159 {
160 final Echo echo = factory.createDelegatorProxy(new ConstantProvider<EchoImpl>(new EchoImpl()),
161 new Class[] { AbstractEcho.class });
162 assertEquals("hello", echo.echoBack("hello"));
163 assertEquals("helloworld", echo.echoBack("hello", "world"));
164 }
165
166
167
168
169
170 public static class EqualsEcho extends EchoImpl
171 {
172 @SuppressWarnings("unused")
173 private final String text;
174
175 protected EqualsEcho()
176 {
177 this("testing");
178 }
179
180 public EqualsEcho(String text)
181 {
182 this.text = text;
183 }
184 }
185
186 public static final class FinalEcho extends EchoImpl
187 {
188 }
189
190 public static class FinalMethodEcho extends EchoImpl
191 {
192 @Override
193 public final String echoBack(String message)
194 {
195 return "final";
196 }
197 }
198
199 private static class InvisibleEcho extends EchoImpl
200 {
201 }
202
203 public static class NoDefaultConstructorEcho extends EchoImpl
204 {
205 public NoDefaultConstructorEcho(String param)
206 {
207 }
208 }
209
210 public static class ProtectedConstructorEcho extends EchoImpl
211 {
212 protected ProtectedConstructorEcho()
213 {
214 }
215 }
216 }