View Javadoc
1   /*
2    * Copyright 2002-2013 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 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   * Tests for various parameter binding scenarios with before advice.
36   *
37   * @author Adrian Colyer
38   * @author Chris Beams
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  		// we need the real target too, not just the proxy...
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 	// "advice" methods
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 	 * Collaborator interface that makes it easy to test this aspect
126 	 * is working as expected through mocking.
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 }