1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
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 }