1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.aop.aspectj.annotation;
18
19 import org.aspectj.lang.ProceedingJoinPoint;
20 import org.aspectj.lang.annotation.Around;
21 import org.aspectj.lang.annotation.Aspect;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import test.aop.PerThisAspect;
25
26 import org.springframework.util.SerializationTestUtils;
27
28 import static org.junit.Assert.*;
29
30
31
32
33
34
35 public final class AspectProxyFactoryTests {
36
37 @Test(expected=IllegalArgumentException.class)
38 public void testWithNonAspect() {
39 AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
40 proxyFactory.addAspect(TestBean.class);
41 }
42
43 @Test
44 public void testWithSimpleAspect() throws Exception {
45 TestBean bean = new TestBean();
46 bean.setAge(2);
47 AspectJProxyFactory proxyFactory = new AspectJProxyFactory(bean);
48 proxyFactory.addAspect(MultiplyReturnValue.class);
49 ITestBean proxy = proxyFactory.getProxy();
50 assertEquals("Multiplication did not occur", bean.getAge() * 2, proxy.getAge());
51 }
52
53 @Test
54 public void testWithPerThisAspect() throws Exception {
55 TestBean bean1 = new TestBean();
56 TestBean bean2 = new TestBean();
57
58 AspectJProxyFactory pf1 = new AspectJProxyFactory(bean1);
59 pf1.addAspect(PerThisAspect.class);
60
61 AspectJProxyFactory pf2 = new AspectJProxyFactory(bean2);
62 pf2.addAspect(PerThisAspect.class);
63
64 ITestBean proxy1 = pf1.getProxy();
65 ITestBean proxy2 = pf2.getProxy();
66
67 assertEquals(0, proxy1.getAge());
68 assertEquals(1, proxy1.getAge());
69 assertEquals(0, proxy2.getAge());
70 assertEquals(2, proxy1.getAge());
71 }
72
73 @Test(expected=IllegalArgumentException.class)
74 public void testWithInstanceWithNonAspect() throws Exception {
75 AspectJProxyFactory pf = new AspectJProxyFactory();
76 pf.addAspect(new TestBean());
77 }
78
79 @Test
80 @Ignore
81 public void testWithInstance() throws Exception {
82 MultiplyReturnValue aspect = new MultiplyReturnValue();
83 int multiple = 3;
84 aspect.setMultiple(multiple);
85
86 TestBean target = new TestBean();
87 target.setAge(24);
88
89 AspectJProxyFactory proxyFactory = new AspectJProxyFactory(target);
90 proxyFactory.addAspect(aspect);
91
92 ITestBean proxy = proxyFactory.getProxy();
93 assertEquals(target.getAge() * multiple, proxy.getAge());
94
95 ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
96 assertEquals(target.getAge() * multiple, serializedProxy.getAge());
97 }
98
99 @Test(expected=IllegalArgumentException.class)
100 public void testWithNonSingletonAspectInstance() throws Exception {
101 AspectJProxyFactory pf = new AspectJProxyFactory();
102 pf.addAspect(new PerThisAspect());
103 }
104
105
106 public static interface ITestBean {
107
108 int getAge();
109 }
110
111
112 public static class TestBean implements ITestBean {
113
114 private int age;
115
116 @Override
117 public int getAge() {
118 return age;
119 }
120
121 public void setAge(int age) {
122 this.age = age;
123 }
124 }
125
126 }
127
128
129
130
131
132 @Aspect
133 class MultiplyReturnValue {
134
135 private int multiple = 2;
136
137 public int invocations;
138
139 public void setMultiple(int multiple) {
140 this.multiple = multiple;
141 }
142
143 public int getMultiple() {
144 return this.multiple;
145 }
146
147 @Around("execution(int *.getAge())")
148 public Object doubleReturnValue(ProceedingJoinPoint pjp) throws Throwable {
149 ++this.invocations;
150 int result = (Integer) pjp.proceed();
151 return result * this.multiple;
152 }
153
154 }