1 16 17 package org.springframework.aop.support; 18 19 import java.io.Serializable ; 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 42 public class DelegatingIntroductionInterceptorTests extends TestCase { 43 44 public void testNullTarget() throws Exception { 45 try { 46 IntroductionInterceptor ii = new DelegatingIntroductionInterceptor(null); 47 fail("Shouldn't accept null target"); 48 } 49 catch (IllegalArgumentException ex) { 50 } 52 } 53 54 public void testIntroductionInterceptorWithDelegation() throws Exception { 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 { 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 { 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 { 116 final long t = 1001L; 117 class Test implements TimeStamped, ITest { 118 public void foo() throws Exception { 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 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 { 143 final long t = 1001L; 144 class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITest { 145 public void foo() throws Exception { 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 TimeStamped ts = (TimeStamped) pf.getProxy(); 163 164 assertTrue(ts instanceof TimeStamped); 165 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 ii.suppressInterface(TimeStamped.class); 175 pf = new ProxyFactory(target); 178 pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii)); 179 Object o = pf.getProxy(); 180 assertTrue(!(o instanceof TimeStamped)); 181 } 182 183 public void testIntroductionInterceptorDoesntReplaceToString() throws Exception { 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 toString() { 192 throw new UnsupportedOperationException ("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 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 { 222 SerializablePerson serializableTarget = new SerializablePerson(); 223 String 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 258 public void testIntroductionMasksTargetImplementation() throws Exception { 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 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 assertTrue(ts.getTimeStamp() == t); 278 } 279 280 281 private static class SerializableTimeStamped implements TimeStamped, Serializable { 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 ; 312 } 313 314 315 private static interface SubTimeStamped extends TimeStamped { 316 } 317 318 } 319 | Popular Tags |