KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > BeanNameAutoProxyCreatorTests


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.autoproxy;
18
19 import java.io.IOException JavaDoc;
20
21 import junit.framework.AssertionFailedError;
22 import junit.framework.TestCase;
23
24 import org.springframework.aop.framework.CountingBeforeAdvice;
25 import org.springframework.aop.framework.Lockable;
26 import org.springframework.aop.framework.LockedException;
27 import org.springframework.aop.framework.TimeStamped;
28 import org.springframework.aop.interceptor.NopInterceptor;
29 import org.springframework.aop.support.AopUtils;
30 import org.springframework.beans.ITestBean;
31 import org.springframework.beans.TestBean;
32 import org.springframework.beans.factory.BeanFactory;
33 import org.springframework.context.support.ClassPathXmlApplicationContext;
34
35 /**
36  * EnterpriseServices test that ources attributes from source-level metadata.
37  *
38  * @author Rod Johnson
39  */

40 public class BeanNameAutoProxyCreatorTests extends TestCase {
41
42     private BeanFactory bf;
43     
44     protected void setUp() throws IOException JavaDoc {
45         // Note that we need an application context, not just a bean factory,
46
// for post processing and hence auto proxying to work
47
this.bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/beanNameAutoProxyCreatorTests.xml");
48     }
49     
50     public void testNoProxy() {
51         TestBean tb = (TestBean) bf.getBean("noproxy");
52         assertFalse(AopUtils.isAopProxy(tb));
53         assertEquals("noproxy", tb.getName());
54     }
55     
56     public void testJdkProxyWithExactNameMatch() {
57         ITestBean tb = (ITestBean) bf.getBean("onlyJdk");
58         jdkAssertions(tb);
59         assertEquals("onlyJdk", tb.getName());
60     }
61     
62     public void testJdkIntroduction() {
63         ITestBean tb = (ITestBean) bf.getBean("introductionUsingJdk");
64         NopInterceptor nop = (NopInterceptor) bf.getBean("introductionNopInterceptor");
65         assertEquals(0, nop.getCount());
66         assertTrue(AopUtils.isJdkDynamicProxy(tb));
67         int age = 5;
68         tb.setAge(age);
69         assertEquals(age, tb.getAge());
70         assertTrue("Introduction was made", tb instanceof TimeStamped);
71         assertEquals(0, ((TimeStamped) tb).getTimeStamp());
72         assertEquals(3, nop.getCount());
73         assertEquals("introductionUsingJdk", tb.getName());
74     
75         ITestBean tb2 = (ITestBean) bf.getBean("second-introductionUsingJdk");
76             
77         // Check two per-instance mixins were distinct
78
Lockable lockable1 = (Lockable) tb;
79         Lockable lockable2 = (Lockable) tb2;
80         assertFalse(lockable1.locked());
81         assertFalse(lockable2.locked());
82         tb.setAge(65);
83         assertEquals(65, tb.getAge());
84         lockable1.lock();
85         assertTrue(lockable1.locked());
86         // Shouldn't affect second
87
assertFalse(lockable2.locked());
88         // Can still mod second object
89
tb2.setAge(12);
90         // But can't mod first
91
try {
92             tb.setAge(6);
93             fail("Mixin should have locked this object");
94         }
95         catch (LockedException ex) {
96             // Ok
97
}
98     }
99     
100     /**
101      * This is a test that reproduces a bug/enhancement
102      * request, while we decide how to address it.
103      * This one is scheduled to be addressed in 1.1.2.
104      */

105     public void testIntroductionOnFactoryBean() {
106         try {
107             BUGtestJdkIntroductionAppliesToCreatedObjectsNotFactoryBean();
108             fail();
109         }
110         catch (AssertionFailedError ex) {
111             System.err.println("****** SPR 337: Autoproxying currently applies to FactoryBeans, not objects they create");
112         }
113     }
114     
115     public void BUGtestJdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
116         ITestBean tb = (ITestBean) bf.getBean("factory-introductionUsingJdk");
117         NopInterceptor nop = (NopInterceptor) bf.getBean("introductionNopInterceptor");
118         assertEquals("NOP should not have done any work yet", 0, nop.getCount());
119         assertTrue(AopUtils.isJdkDynamicProxy(tb));
120         int age = 5;
121         tb.setAge(age);
122         assertEquals(age, tb.getAge());
123         assertTrue("Introduction was made", tb instanceof TimeStamped);
124         assertEquals(0, ((TimeStamped) tb).getTimeStamp());
125         assertEquals(3, nop.getCount());
126         assertEquals("introductionUsingJdk", tb.getName());
127     
128         ITestBean tb2 = (ITestBean) bf.getBean("second-introductionUsingJdk");
129             
130         // Check two per-instance mixins were distinct
131
Lockable lockable1 = (Lockable) tb;
132         Lockable lockable2 = (Lockable) tb2;
133         assertFalse(lockable1.locked());
134         assertFalse(lockable2.locked());
135         tb.setAge(65);
136         assertEquals(65, tb.getAge());
137         lockable1.lock();
138         assertTrue(lockable1.locked());
139         // Shouldn't affect second
140
assertFalse(lockable2.locked());
141         // Can still mod second object
142
tb2.setAge(12);
143         // But can't mod first
144
try {
145             tb.setAge(6);
146             fail("Mixin should have locked this object");
147         }
148         catch (LockedException ex) {
149             // Ok
150
}
151     }
152     
153     public void testJdkProxyWithWildcardMatch() {
154         ITestBean tb = (ITestBean) bf.getBean("jdk1");
155         jdkAssertions(tb);
156         assertEquals("jdk1", tb.getName());
157     }
158     
159     public void testCglibProxyWithWildcardMatch() {
160         TestBean tb = (TestBean) bf.getBean("cglib1");
161         cglibAssertions(tb);
162         assertEquals("cglib1", tb.getName());
163     }
164     
165     private void jdkAssertions(ITestBean tb) {
166         NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
167         assertEquals(0, nop.getCount());
168         assertTrue(AopUtils.isJdkDynamicProxy(tb));
169         int age = 5;
170         tb.setAge(age);
171         assertEquals(age, tb.getAge());
172         assertEquals(2, nop.getCount());
173     }
174     
175     /**
176      * Also has counting before advice.
177      */

178     private void cglibAssertions(TestBean tb) {
179         CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice");
180         NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
181         assertEquals(0, cba.getCalls());
182         assertEquals(0, nop.getCount());
183         assertTrue(AopUtils.isCglibProxy(tb));
184         int age = 5;
185         tb.setAge(age);
186         assertEquals(age, tb.getAge());
187         assertEquals(2, nop.getCount());
188         assertEquals(2, cba.getCalls());
189     }
190 }
191
Popular Tags