KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > RegexpMethodPointcutAdvisorIntegrationTests


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.support;
18
19 import junit.framework.TestCase;
20
21 import org.springframework.aop.framework.Advised;
22 import org.springframework.aop.interceptor.NopInterceptor;
23 import org.springframework.aop.interceptor.SerializableNopInterceptor;
24 import org.springframework.beans.ITestBean;
25 import org.springframework.beans.Person;
26 import org.springframework.beans.TestBean;
27 import org.springframework.beans.factory.BeanFactory;
28 import org.springframework.context.support.ClassPathXmlApplicationContext;
29 import org.springframework.util.SerializationTestUtils;
30
31 /**
32  * @author Rod Johnson
33  */

34 public class RegexpMethodPointcutAdvisorIntegrationTests extends TestCase {
35
36     public void testSinglePattern() throws Throwable JavaDoc {
37         BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml");
38         ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
39         // Interceptor behind regexp advisor
40
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
41         assertEquals(0, nop.getCount());
42         
43         int newAge = 12;
44         // Not advised
45
advised.exceptional(null);
46         assertEquals(0, nop.getCount());
47         advised.setAge(newAge);
48         assertEquals(newAge, advised.getAge());
49         // Only setter fired
50
assertEquals(1, nop.getCount());
51     }
52     
53     public void testMultiplePatterns() throws Throwable JavaDoc {
54         BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml");
55         // This is a CGLIB proxy, so we can proxy it to the target class
56
TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised");
57         // Interceptor behind regexp advisor
58
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
59         assertEquals(0, nop.getCount());
60     
61         int newAge = 12;
62         // Not advised
63
advised.exceptional(null);
64         assertEquals(0, nop.getCount());
65         
66         // This is proxied
67
advised.absquatulate();
68         assertEquals(1, nop.getCount());
69         advised.setAge(newAge);
70         assertEquals(newAge, advised.getAge());
71         // Only setter fired
72
assertEquals(2, nop.getCount());
73     }
74     
75     public void testSerialization() throws Throwable JavaDoc {
76         BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml");
77         // This is a CGLIB proxy, so we can proxy it to the target class
78
Person p = (Person) bf.getBean("serializableSettersAdvised");
79         // Interceptor behind regexp advisor
80
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
81         assertEquals(0, nop.getCount());
82     
83         int newAge = 12;
84         // Not advised
85
assertEquals(0, p.getAge());
86         assertEquals(0, nop.getCount());
87         
88         // This is proxied
89
p.setAge(newAge);
90         assertEquals(1, nop.getCount());
91         p.setAge(newAge);
92         assertEquals(newAge, p.getAge());
93         // Only setter fired
94
assertEquals(2, nop.getCount());
95         
96         // Serialize and continue...
97
p = (Person) SerializationTestUtils.serializeAndDeserialize(p);
98         assertEquals(newAge, p.getAge());
99         // Remembers count, but we need to get a new reference to nop...
100
nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
101         assertEquals(2, nop.getCount());
102         assertEquals("serializableSettersAdvised", p.getName());
103         p.setAge(newAge + 1);
104         assertEquals(3, nop.getCount());
105         assertEquals(newAge + 1, p.getAge());
106     }
107
108 }
109
Popular Tags