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.framework;
18  
19  import org.aopalliance.intercept.MethodInterceptor;
20  import org.aopalliance.intercept.MethodInvocation;
21  import org.junit.Test;
22  
23  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
24  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
25  import org.springframework.core.io.Resource;
26  
27  import static org.junit.Assert.*;
28  import static org.springframework.tests.TestResourceUtils.*;
29  
30  /**
31   * @author Juergen Hoeller
32   * @author Chris Beams
33   * @since 03.09.2004
34   */
35  public final class PrototypeTargetTests {
36  
37  	private static final Resource CONTEXT = qualifiedResource(PrototypeTargetTests.class, "context.xml");
38  
39  	@Test
40  	public void testPrototypeProxyWithPrototypeTarget() {
41  		TestBeanImpl.constructionCount = 0;
42  		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
43  		new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
44  		for (int i = 0; i < 10; i++) {
45  			TestBean tb = (TestBean) bf.getBean("testBeanPrototype");
46  			tb.doSomething();
47  		}
48  		TestInterceptor interceptor = (TestInterceptor) bf.getBean("testInterceptor");
49  		assertEquals(10, TestBeanImpl.constructionCount);
50  		assertEquals(10, interceptor.invocationCount);
51  	}
52  
53  	@Test
54  	public void testSingletonProxyWithPrototypeTarget() {
55  		TestBeanImpl.constructionCount = 0;
56  		DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
57  		new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
58  		for (int i = 0; i < 10; i++) {
59  			TestBean tb = (TestBean) bf.getBean("testBeanSingleton");
60  			tb.doSomething();
61  		}
62  		TestInterceptor interceptor = (TestInterceptor) bf.getBean("testInterceptor");
63  		assertEquals(1, TestBeanImpl.constructionCount);
64  		assertEquals(10, interceptor.invocationCount);
65  	}
66  
67  	public static interface TestBean {
68  		public void doSomething();
69  	}
70  
71  
72  	public static class TestBeanImpl implements TestBean {
73  		private static int constructionCount = 0;
74  
75  		public TestBeanImpl() {
76  			constructionCount++;
77  		}
78  
79  		@Override
80  		public void doSomething() {
81  		}
82  	}
83  
84  
85  	public static class TestInterceptor implements MethodInterceptor {
86  		private int invocationCount = 0;
87  
88  		@Override
89  		public Object invoke(MethodInvocation methodInvocation) throws Throwable {
90  			invocationCount++;
91  			return methodInvocation.proceed();
92  		}
93  	}
94  
95  }