KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > DelegatingIntroductionInterceptorTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.support;
18
19 import java.io.Serializable JavaDoc;
20
21 import junit.framework.TestCase;
22 import org.aopalliance.intercept.MethodInterceptor;
23 import org.easymock.MockControl;
24
25 import org.springframework.aop.IntroductionAdvisor;
26 import org.springframework.aop.IntroductionInterceptor;
27 import org.springframework.aop.framework.ProxyFactory;
28 import org.springframework.aop.framework.TimeStamped;
29 import org.springframework.aop.interceptor.SerializableNopInterceptor;
30 import org.springframework.beans.INestedTestBean;
31 import org.springframework.beans.ITestBean;
32 import org.springframework.beans.NestedTestBean;
33 import org.springframework.beans.Person;
34 import org.springframework.beans.SerializablePerson;
35 import org.springframework.beans.TestBean;
36 import org.springframework.util.SerializationTestUtils;
37
38 /**
39  * @author Rod Johnson
40  * @since 13.05.2003
41  */

42 public class DelegatingIntroductionInterceptorTests extends TestCase {
43
44     public void testNullTarget() throws Exception JavaDoc {
45         try {
46             IntroductionInterceptor ii = new DelegatingIntroductionInterceptor(null);
47             fail("Shouldn't accept null target");
48         }
49         catch (IllegalArgumentException JavaDoc ex) {
50             // OK
51
}
52     }
53     
54     public void testIntroductionInterceptorWithDelegation() throws Exception JavaDoc {
55         TestBean raw = new TestBean();
56         assertTrue(! (raw instanceof TimeStamped));
57         ProxyFactory factory = new ProxyFactory(raw);
58     
59         MockControl tsControl = MockControl.createControl(TimeStamped.class);
60         TimeStamped ts = (TimeStamped) tsControl.getMock();
61         ts.getTimeStamp();
62         long timestamp = 111L;
63         tsControl.setReturnValue(timestamp, 1);
64         tsControl.replay();
65
66         factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
67         
68         TimeStamped tsp = (TimeStamped) factory.getProxy();
69         assertTrue(tsp.getTimeStamp() == timestamp);
70     
71         tsControl.verify();
72     }
73     
74     public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception JavaDoc {
75         TestBean raw = new TestBean();
76         assertTrue(! (raw instanceof SubTimeStamped));
77         ProxyFactory factory = new ProxyFactory(raw);
78
79         MockControl tsControl = MockControl.createControl(SubTimeStamped.class);
80         SubTimeStamped ts = (SubTimeStamped) tsControl.getMock();
81         ts.getTimeStamp();
82         long timestamp = 111L;
83         tsControl.setReturnValue(timestamp, 1);
84         tsControl.replay();
85
86         factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class));
87
88         SubTimeStamped tsp = (SubTimeStamped) factory.getProxy();
89         assertTrue(tsp.getTimeStamp() == timestamp);
90
91         tsControl.verify();
92     }
93
94     public void testIntroductionInterceptorWithSuperInterface() throws Exception JavaDoc {
95         TestBean raw = new TestBean();
96         assertTrue(! (raw instanceof TimeStamped));
97         ProxyFactory factory = new ProxyFactory(raw);
98
99         MockControl tsControl = MockControl.createControl(SubTimeStamped.class);
100         SubTimeStamped ts = (SubTimeStamped) tsControl.getMock();
101         ts.getTimeStamp();
102         long timestamp = 111L;
103         tsControl.setReturnValue(timestamp, 1);
104         tsControl.replay();
105
106         factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class));
107
108         TimeStamped tsp = (TimeStamped) factory.getProxy();
109         assertTrue(!(tsp instanceof SubTimeStamped));
110         assertTrue(tsp.getTimeStamp() == timestamp);
111
112         tsControl.verify();
113     }
114
115     public void testAutomaticInterfaceRecognitionInDelegate() throws Exception JavaDoc {
116         final long t = 1001L;
117         class Test implements TimeStamped, ITest {
118             public void foo() throws Exception JavaDoc {
119             }
120             public long getTimeStamp() {
121                 return t;
122             }
123         }
124         
125         DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Test());
126         
127         TestBean target = new TestBean();
128         
129         ProxyFactory pf = new ProxyFactory(target);
130         pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
131         
132         //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
133
TimeStamped ts = (TimeStamped) pf.getProxy();
134         
135         assertTrue(ts.getTimeStamp() == t);
136         ((ITest) ts).foo();
137         
138         ((ITestBean) ts).getAge();
139     }
140     
141     
142     public void testAutomaticInterfaceRecognitionInSubclass() throws Exception JavaDoc {
143         final long t = 1001L;
144         class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITest {
145             public void foo() throws Exception JavaDoc {
146             }
147             public long getTimeStamp() {
148                 return t;
149             }
150         }
151         
152         DelegatingIntroductionInterceptor ii = new TestII();
153         
154         TestBean target = new TestBean();
155         
156         ProxyFactory pf = new ProxyFactory(target);
157         IntroductionAdvisor ia = new DefaultIntroductionAdvisor(ii);
158         assertTrue(ia.isPerInstance());
159         pf.addAdvisor(0, ia);
160         
161         //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
162
TimeStamped ts = (TimeStamped) pf.getProxy();
163         
164         assertTrue(ts instanceof TimeStamped);
165         // Shoulnd't proxy framework interfaces
166
assertTrue(!(ts instanceof MethodInterceptor));
167         assertTrue(!(ts instanceof IntroductionInterceptor));
168         
169         assertTrue(ts.getTimeStamp() == t);
170         ((ITest) ts).foo();
171         ((ITestBean) ts).getAge();
172         
173         // Test removal
174
ii.suppressInterface(TimeStamped.class);
175         // Note that we need to construct a new proxy factory,
176
// or suppress the interface on the proxy factory
177
pf = new ProxyFactory(target);
178         pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
179         Object JavaDoc o = pf.getProxy();
180         assertTrue(!(o instanceof TimeStamped));
181     }
182     
183     public void testIntroductionInterceptorDoesntReplaceToString() throws Exception JavaDoc {
184         TestBean raw = new TestBean();
185         assertTrue(! (raw instanceof TimeStamped));
186         ProxyFactory factory = new ProxyFactory(raw);
187     
188         TimeStamped ts = new SerializableTimeStamped(0);
189
190         factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts) {
191             public String JavaDoc toString() {
192                 throw new UnsupportedOperationException JavaDoc("Shouldn't be invoked");
193             }
194         }));
195         
196         TimeStamped tsp = (TimeStamped) factory.getProxy();
197         assertEquals(0, tsp.getTimeStamp());
198     
199         assertEquals(raw.toString(), tsp.toString());
200     }
201     
202     public void testDelegateReturnsThisIsMassagedToReturnProxy() {
203         NestedTestBean target = new NestedTestBean();
204         String JavaDoc company = "Interface21";
205         target.setCompany(company);
206         TestBean delegate = new TestBean() {
207             public ITestBean getSpouse() {
208                 return this;
209             }
210         };
211         ProxyFactory pf = new ProxyFactory(target);
212         pf.addAdvice(new DelegatingIntroductionInterceptor(delegate));
213         INestedTestBean proxy = (INestedTestBean) pf.getProxy();
214         
215         assertEquals(company, proxy.getCompany());
216         ITestBean introduction = (ITestBean) proxy;
217         assertSame("Introduced method returning delegate returns proxy", introduction, introduction.getSpouse());
218         assertTrue("Introduced method returning delegate returns proxy", AopUtils.isAopProxy(introduction.getSpouse()));
219     }
220     
221     public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception JavaDoc {
222         SerializablePerson serializableTarget = new SerializablePerson();
223         String JavaDoc name = "Tony";
224         serializableTarget.setName("Tony");
225         
226         ProxyFactory factory = new ProxyFactory(serializableTarget);
227         factory.addInterface(Person.class);
228         long time = 1000;
229         TimeStamped ts = new SerializableTimeStamped(time);
230     
231         factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
232         factory.addAdvice(new SerializableNopInterceptor());
233         
234         Person p = (Person) factory.getProxy();
235         
236         assertEquals(name, p.getName());
237         assertEquals(time, ((TimeStamped) p).getTimeStamp());
238         
239         Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
240         assertEquals(name, p1.getName());
241         assertEquals(time, ((TimeStamped) p1).getTimeStamp());
242     }
243     
244 // public void testDelegatingIntroductionInterceptorDoesntMakeNonserializableSerializable() throws Exception {
245
// // Target is NOT serialiable
246
// TestBean raw = new TestBean();
247
// ProxyFactory factory = new ProxyFactory(raw);
248
// factory.addInterface(Person.class);
249
// long time = 1000;
250
// TimeStamped ts = new SerializableTimeStamped(time);
251
//
252
// factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
253
// Object proxy = factory.getProxy();
254
//
255
// assertFalse(proxy instanceof Serializable);
256
// }
257

258     // Test when target implements the interface: should get interceptor by preference.
259
public void testIntroductionMasksTargetImplementation() throws Exception JavaDoc {
260         final long t = 1001L;
261         class TestII extends DelegatingIntroductionInterceptor implements TimeStamped {
262             public long getTimeStamp() {
263                 return t;
264             }
265         }
266         
267         DelegatingIntroductionInterceptor ii = new TestII();
268     
269         // != t
270
TestBean target = new TargetClass(t + 1);
271     
272         ProxyFactory pf = new ProxyFactory(target);
273         pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
274     
275         TimeStamped ts = (TimeStamped) pf.getProxy();
276         // From introduction interceptor, not target
277
assertTrue(ts.getTimeStamp() == t);
278     }
279
280
281     private static class SerializableTimeStamped implements TimeStamped, Serializable JavaDoc {
282
283         private final long ts;
284
285         public SerializableTimeStamped(long ts) {
286             this.ts = ts;
287         }
288
289         public long getTimeStamp() {
290             return ts;
291         }
292     }
293
294
295     public static class TargetClass extends TestBean implements TimeStamped {
296
297         long t;
298
299         public TargetClass(long t) {
300             this.t = t;
301         }
302
303         public long getTimeStamp() {
304             return t;
305         }
306     }
307
308
309     public interface ITest {
310
311         void foo() throws Exception JavaDoc;
312     }
313
314
315     private static interface SubTimeStamped extends TimeStamped {
316     }
317
318 }
319
Popular Tags