1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.aop.aspectj;
18
19 import org.aspectj.lang.ProceedingJoinPoint;
20 import org.junit.Before;
21 import org.junit.Test;
22
23 import org.springframework.aop.aspectj.AroundAdviceBindingTestAspect.AroundAdviceBindingCollaborator;
24 import org.springframework.aop.framework.Advised;
25 import org.springframework.aop.support.AopUtils;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.context.support.ClassPathXmlApplicationContext;
28 import org.springframework.tests.sample.beans.ITestBean;
29 import org.springframework.tests.sample.beans.TestBean;
30
31 import static org.junit.Assert.*;
32 import static org.mockito.BDDMockito.*;
33
34
35
36
37
38
39
40 public class AroundAdviceBindingTests {
41
42 private AroundAdviceBindingCollaborator mockCollaborator;
43
44 private ITestBean testBeanProxy;
45
46 private TestBean testBeanTarget;
47
48 protected ApplicationContext ctx;
49
50 @Before
51 public void onSetUp() throws Exception {
52 ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
53
54 AroundAdviceBindingTestAspect aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect"));
55
56 ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean");
57 assertTrue(AopUtils.isAopProxy(injectedTestBean));
58
59 this.testBeanProxy = injectedTestBean;
60
61
62 this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();
63
64 mockCollaborator = mock(AroundAdviceBindingCollaborator.class);
65 aroundAdviceAspect.setCollaborator(mockCollaborator);
66 }
67
68 @Test
69 public void testOneIntArg() {
70 testBeanProxy.setAge(5);
71 verify(mockCollaborator).oneIntArg(5);
72 }
73
74 @Test
75 public void testOneObjectArgBoundToTarget() {
76 testBeanProxy.getAge();
77 verify(mockCollaborator).oneObjectArg(this.testBeanTarget);;
78 }
79
80 @Test
81 public void testOneIntAndOneObjectArgs() {
82 testBeanProxy.setAge(5);
83 verify(mockCollaborator).oneIntAndOneObject(5, this.testBeanProxy);;
84 }
85
86 @Test
87 public void testJustJoinPoint() {
88 testBeanProxy.getAge();
89 verify(mockCollaborator).justJoinPoint("getAge");;
90 }
91
92 }
93
94
95 class AroundAdviceBindingTestAspect {
96
97 private AroundAdviceBindingCollaborator collaborator = null;
98
99 public void setCollaborator(AroundAdviceBindingCollaborator aCollaborator) {
100 this.collaborator = aCollaborator;
101 }
102
103
104 public void oneIntArg(ProceedingJoinPoint pjp, int age) throws Throwable {
105 this.collaborator.oneIntArg(age);
106 pjp.proceed();
107 }
108
109 public int oneObjectArg(ProceedingJoinPoint pjp, Object bean) throws Throwable {
110 this.collaborator.oneObjectArg(bean);
111 return ((Integer) pjp.proceed()).intValue();
112 }
113
114 public void oneIntAndOneObject(ProceedingJoinPoint pjp, int x , Object o) throws Throwable {
115 this.collaborator.oneIntAndOneObject(x,o);
116 pjp.proceed();
117 }
118
119 public int justJoinPoint(ProceedingJoinPoint pjp) throws Throwable {
120 this.collaborator.justJoinPoint(pjp.getSignature().getName());
121 return ((Integer) pjp.proceed()).intValue();
122 }
123
124
125
126
127
128 public interface AroundAdviceBindingCollaborator {
129
130 void oneIntArg(int x);
131
132 void oneObjectArg(Object o);
133
134 void oneIntAndOneObject(int x, Object o);
135
136 void justJoinPoint(String s);
137 }
138
139 }