KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > PrototypeTargetTests


1 /*
2  * Copyright 2002-2005 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 junit.framework.TestCase;
20 import org.aopalliance.intercept.MethodInterceptor;
21 import org.aopalliance.intercept.MethodInvocation;
22
23 import org.springframework.beans.factory.xml.XmlBeanFactory;
24 import org.springframework.core.io.ClassPathResource;
25
26 /**
27  * @author Juergen Hoeller
28  * @since 03.09.2004
29  */

30 public class PrototypeTargetTests extends TestCase {
31
32     public void testPrototypeProxyWithPrototypeTarget() {
33         TestBeanImpl.constructionCount = 0;
34         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("prototypeTarget.xml", getClass()));
35         for (int i = 0; i < 10; i++) {
36             TestBean tb = (TestBean) xbf.getBean("testBeanPrototype");
37             tb.doSomething();
38         }
39         TestInterceptor interceptor = (TestInterceptor) xbf.getBean("testInterceptor");
40         assertEquals(10, TestBeanImpl.constructionCount);
41         assertEquals(10, interceptor.invocationCount);
42     }
43
44     public void testSingletonProxyWithPrototypeTarget() {
45         TestBeanImpl.constructionCount = 0;
46         XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("prototypeTarget.xml", getClass()));
47         for (int i = 0; i < 10; i++) {
48             TestBean tb = (TestBean) xbf.getBean("testBeanSingleton");
49             tb.doSomething();
50         }
51         TestInterceptor interceptor = (TestInterceptor) xbf.getBean("testInterceptor");
52         assertEquals(1, TestBeanImpl.constructionCount);
53         assertEquals(10, interceptor.invocationCount);
54     }
55
56
57     public static interface TestBean {
58
59         public void doSomething();
60     }
61
62
63     public static class TestBeanImpl implements TestBean {
64
65         private static int constructionCount = 0;
66
67         public TestBeanImpl() {
68             constructionCount++;
69         }
70
71         public void doSomething() {
72         }
73     }
74
75
76     public static class TestInterceptor implements MethodInterceptor {
77
78         private int invocationCount = 0;
79
80         public Object JavaDoc invoke(MethodInvocation methodInvocation) throws Throwable JavaDoc {
81             invocationCount++;
82             return methodInvocation.proceed();
83         }
84     }
85
86 }
87
Popular Tags