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.aspectj.lang.ProceedingJoinPoint;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.springframework.aop.aspectj.AroundAdviceBindingTestAspect.AroundAdviceBindingCollaborator;
26 import org.springframework.aop.framework.Advised;
27 import org.springframework.aop.support.AopUtils;
28 import org.springframework.beans.ITestBean;
29 import org.springframework.beans.TestBean;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.context.support.ClassPathXmlApplicationContext;
32 
33 /**
34  * Tests for various parameter binding scenarios with before advice.
35  *
36  * @author Adrian Colyer
37  * @author Chris Beams
38  */
39 public class AroundAdviceBindingTests {
40 
41 	private AroundAdviceBindingCollaborator mockCollaborator;
42 
43 	private ITestBean testBeanProxy;
44 
45 	private TestBean testBeanTarget;
46 
47 	protected ApplicationContext ctx;
48 
49 	@Before
onSetUp()50 	public void onSetUp() throws Exception {
51 		ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
52 
53 		AroundAdviceBindingTestAspect  aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect"));
54 
55 		ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean");
56 		assertTrue(AopUtils.isAopProxy(injectedTestBean));
57 
58 		this.testBeanProxy = injectedTestBean;
59 		// we need the real target too, not just the proxy...
60 
61 		this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();
62 
63 		mockCollaborator = createNiceMock(AroundAdviceBindingCollaborator.class);
64 		aroundAdviceAspect.setCollaborator(mockCollaborator);
65 	}
66 
67 	@Test
testOneIntArg()68 	public void testOneIntArg() {
69 		mockCollaborator.oneIntArg(5);
70 		replay(mockCollaborator);
71 		testBeanProxy.setAge(5);
72 		verify(mockCollaborator);
73 	}
74 
75 	@Test
testOneObjectArgBoundToTarget()76 	public void testOneObjectArgBoundToTarget() {
77 		mockCollaborator.oneObjectArg(this.testBeanTarget);
78 		replay(mockCollaborator);
79 		testBeanProxy.getAge();
80 		verify(mockCollaborator);
81 	}
82 
83 	@Test
testOneIntAndOneObjectArgs()84 	public void testOneIntAndOneObjectArgs() {
85 		mockCollaborator.oneIntAndOneObject(5, this.testBeanProxy);
86 		replay(mockCollaborator);
87 		testBeanProxy.setAge(5);
88 		verify(mockCollaborator);
89 	}
90 
91 	@Test
testJustJoinPoint()92 	public void testJustJoinPoint() {
93 		mockCollaborator.justJoinPoint("getAge");
94 		replay(mockCollaborator);
95 		testBeanProxy.getAge();
96 		verify(mockCollaborator);
97 	}
98 
99 }
100 
101 
102 class AroundAdviceBindingTestAspect {
103 
104 	private AroundAdviceBindingCollaborator collaborator = null;
105 
setCollaborator(AroundAdviceBindingCollaborator aCollaborator)106 	public void setCollaborator(AroundAdviceBindingCollaborator aCollaborator) {
107 		this.collaborator = aCollaborator;
108 	}
109 
110 	// "advice" methods
oneIntArg(ProceedingJoinPoint pjp, int age)111 	public void oneIntArg(ProceedingJoinPoint pjp, int age) throws Throwable {
112 		this.collaborator.oneIntArg(age);
113 		pjp.proceed();
114 	}
115 
oneObjectArg(ProceedingJoinPoint pjp, Object bean)116 	public int oneObjectArg(ProceedingJoinPoint pjp, Object bean) throws Throwable {
117 		this.collaborator.oneObjectArg(bean);
118 		return ((Integer) pjp.proceed()).intValue();
119 	}
120 
oneIntAndOneObject(ProceedingJoinPoint pjp, int x , Object o)121 	public void oneIntAndOneObject(ProceedingJoinPoint pjp, int x , Object o) throws Throwable {
122 		this.collaborator.oneIntAndOneObject(x,o);
123 		pjp.proceed();
124 	}
125 
justJoinPoint(ProceedingJoinPoint pjp)126 	public int justJoinPoint(ProceedingJoinPoint pjp) throws Throwable {
127 		this.collaborator.justJoinPoint(pjp.getSignature().getName());
128 		return ((Integer) pjp.proceed()).intValue();
129 	}
130 
131 	/**
132 	 * Collaborator interface that makes it easy to test this aspect
133 	 * is working as expected through mocking.
134 	 */
135 	public interface AroundAdviceBindingCollaborator {
136 
oneIntArg(int x)137 		void oneIntArg(int x);
138 
oneObjectArg(Object o)139 		void oneObjectArg(Object o);
140 
oneIntAndOneObject(int x, Object o)141 		void oneIntAndOneObject(int x, Object o);
142 
justJoinPoint(String s)143 		void justJoinPoint(String s);
144 	}
145 
146 }
147