1 /*
2  * Copyright 2002-2007 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.aspectj;
18 
19 import static org.easymock.EasyMock.*;
20 import static org.junit.Assert.assertTrue;
21 
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.springframework.aop.aspectj.AfterReturningAdviceBindingTestAspect.AfterReturningAdviceBindingCollaborator;
25 import org.springframework.aop.framework.Advised;
26 import org.springframework.aop.support.AopUtils;
27 import org.springframework.beans.ITestBean;
28 import org.springframework.beans.TestBean;
29 import org.springframework.context.support.ClassPathXmlApplicationContext;
30 
31 /**
32  * Tests for various parameter binding scenarios with before advice.
33  *
34  * @author Adrian Colyer
35  * @author Rod Johnson
36  * @author Juergen Hoeller
37  * @author Chris Beams
38  */
39 public final class AfterReturningAdviceBindingTests {
40 
41 	private AfterReturningAdviceBindingTestAspect afterAdviceAspect;
42 
43 	private ITestBean testBeanProxy;
44 
45 	private TestBean testBeanTarget;
46 
47 	private AfterReturningAdviceBindingCollaborator mockCollaborator;
48 
49 
setAfterReturningAdviceAspect(AfterReturningAdviceBindingTestAspect anAspect)50 	public void setAfterReturningAdviceAspect(AfterReturningAdviceBindingTestAspect anAspect) {
51 		this.afterAdviceAspect = anAspect;
52 	}
53 
54 	@Before
setUp()55 	public void setUp() throws Exception {
56 		ClassPathXmlApplicationContext ctx =
57 			new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
58 
59 		afterAdviceAspect = (AfterReturningAdviceBindingTestAspect) ctx.getBean("testAspect");
60 
61 		mockCollaborator = createNiceMock(AfterReturningAdviceBindingCollaborator.class);
62 		afterAdviceAspect.setCollaborator(mockCollaborator);
63 
64 		testBeanProxy = (ITestBean) ctx.getBean("testBean");
65 		assertTrue(AopUtils.isAopProxy(testBeanProxy));
66 
67 		// we need the real target too, not just the proxy...
68 		this.testBeanTarget = (TestBean) ((Advised)testBeanProxy).getTargetSource().getTarget();
69 	}
70 
71 
72 	@Test
testOneIntArg()73 	public void testOneIntArg() {
74 		mockCollaborator.oneIntArg(5);
75 		replay(mockCollaborator);
76 		testBeanProxy.setAge(5);
77 		verify(mockCollaborator);
78 	}
79 
80 	@Test
testOneObjectArg()81 	public void testOneObjectArg() {
82 		mockCollaborator.oneObjectArg(this.testBeanProxy);
83 		replay(mockCollaborator);
84 		testBeanProxy.getAge();
85 		verify(mockCollaborator);
86 	}
87 
88 	@Test
testOneIntAndOneObjectArgs()89 	public void testOneIntAndOneObjectArgs() {
90 		mockCollaborator.oneIntAndOneObject(5,this.testBeanProxy);
91 		replay(mockCollaborator);
92 		testBeanProxy.setAge(5);
93 		verify(mockCollaborator);
94 	}
95 
96 	@Test
testNeedsJoinPoint()97 	public void testNeedsJoinPoint() {
98 		mockCollaborator.needsJoinPoint("getAge");
99 		replay(mockCollaborator);
100 		testBeanProxy.getAge();
101 		verify(mockCollaborator);
102 	}
103 
104 	@Test
testNeedsJoinPointStaticPart()105 	public void testNeedsJoinPointStaticPart() {
106 		mockCollaborator.needsJoinPointStaticPart("getAge");
107 		replay(mockCollaborator);
108 		testBeanProxy.getAge();
109 		verify(mockCollaborator);
110 	}
111 
112 	@Test
testReturningString()113 	public void testReturningString() {
114 		mockCollaborator.oneString("adrian");
115 		replay(mockCollaborator);
116 		testBeanProxy.setName("adrian");
117 		testBeanProxy.getName();
118 		verify(mockCollaborator);
119 	}
120 
121 	@Test
testReturningObject()122 	public void testReturningObject() {
123 		mockCollaborator.oneObjectArg(this.testBeanTarget);
124 		replay(mockCollaborator);
125 		testBeanProxy.returnsThis();
126 		verify(mockCollaborator);
127 	}
128 
129 	@Test
testReturningBean()130 	public void testReturningBean() {
131 		mockCollaborator.oneTestBeanArg(this.testBeanTarget);
132 		replay(mockCollaborator);
133 		testBeanProxy.returnsThis();
134 		verify(mockCollaborator);
135 	}
136 
137 	@Test
testReturningBeanArray()138 	public void testReturningBeanArray() {
139 		this.testBeanTarget.setSpouse(new TestBean());
140 		ITestBean[] spouses = (ITestBean[]) this.testBeanTarget.getSpouses();
141 		mockCollaborator.testBeanArrayArg(spouses);
142 		replay(mockCollaborator);
143 		testBeanProxy.getSpouses();
144 		verify(mockCollaborator);
145 	}
146 
147 	@Test
testNoInvokeWhenReturningParameterTypeDoesNotMatch()148 	public void testNoInvokeWhenReturningParameterTypeDoesNotMatch() {
149 		// we need a strict mock for this...
150 		mockCollaborator = createMock(AfterReturningAdviceBindingCollaborator.class);
151 		afterAdviceAspect.setCollaborator(mockCollaborator);
152 
153 		replay(mockCollaborator);
154 		testBeanProxy.setSpouse(this.testBeanProxy);
155 		testBeanProxy.getSpouse();
156 		verify(mockCollaborator);
157 	}
158 
159 	@Test
testReturningByType()160 	public void testReturningByType() {
161 		mockCollaborator.objectMatchNoArgs();
162 		replay(mockCollaborator);
163 		testBeanProxy.returnsThis();
164 		verify(mockCollaborator);
165 	}
166 
167 	@Test
testReturningPrimitive()168 	public void testReturningPrimitive() {
169 		mockCollaborator.oneInt(20);
170 		replay(mockCollaborator);
171 		testBeanProxy.setAge(20);
172 		testBeanProxy.haveBirthday();
173 		verify(mockCollaborator);
174 	}
175 
176 }
177 
178 
179 final class AfterReturningAdviceBindingTestAspect extends AdviceBindingTestAspect {
180 
getCollaborator()181 	private AfterReturningAdviceBindingCollaborator getCollaborator() {
182 		return (AfterReturningAdviceBindingCollaborator) this.collaborator;
183 	}
184 
oneString(String name)185 	public void oneString(String name) {
186 		getCollaborator().oneString(name);
187 	}
188 
oneTestBeanArg(TestBean bean)189 	public void oneTestBeanArg(TestBean bean) {
190 		getCollaborator().oneTestBeanArg(bean);
191 	}
192 
testBeanArrayArg(ITestBean[] beans)193 	public void testBeanArrayArg(ITestBean[] beans) {
194 		getCollaborator().testBeanArrayArg(beans);
195 	}
196 
objectMatchNoArgs()197 	public void objectMatchNoArgs() {
198 		getCollaborator().objectMatchNoArgs();
199 	}
200 
stringMatchNoArgs()201 	public void stringMatchNoArgs() {
202 		getCollaborator().stringMatchNoArgs();
203 	}
204 
oneInt(int result)205 	public void oneInt(int result) {
206 		getCollaborator().oneInt(result);
207 	}
208 
209 	interface AfterReturningAdviceBindingCollaborator extends AdviceBindingCollaborator {
210 
oneString(String s)211 		void oneString(String s);
oneTestBeanArg(TestBean b)212 		void oneTestBeanArg(TestBean b);
testBeanArrayArg(ITestBean[] b)213 		void testBeanArrayArg(ITestBean[] b);
objectMatchNoArgs()214 		void objectMatchNoArgs();
stringMatchNoArgs()215 		void stringMatchNoArgs();
oneInt(int result)216 		void oneInt(int result);
217 	}
218 
219 }
220