KickJava   Java API By Example, From Geeks To Geeks.

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


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.Pointcut;
22 import org.springframework.aop.framework.ProxyFactory;
23 import org.springframework.aop.interceptor.NopInterceptor;
24 import org.springframework.beans.ITestBean;
25 import org.springframework.beans.TestBean;
26
27 /**
28  *
29  * @author Rod Johnson
30  */

31 public class ControlFlowPointcutTests extends TestCase {
32     
33     public ControlFlowPointcutTests(String JavaDoc s) {
34         super(s);
35     }
36     
37     public void testMatches() {
38         TestBean target = new TestBean();
39         target.setAge(27);
40         NopInterceptor nop = new NopInterceptor();
41         ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge");
42         ProxyFactory pf = new ProxyFactory(target);
43         ITestBean proxied = (ITestBean) pf.getProxy();
44         pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop));
45         
46         // Not advised, not under One
47
assertEquals(target.getAge(), proxied.getAge());
48         assertEquals(0, nop.getCount());
49         
50         // Will be advised
51
assertEquals(target.getAge(), new One().getAge(proxied));
52         assertEquals(1, nop.getCount());
53         
54         // Won't be advised
55
assertEquals(target.getAge(), new One().nomatch(proxied));
56         assertEquals(1, nop.getCount());
57         assertEquals(3, cflow.getEvaluations());
58     }
59     
60     /**
61      * Check that we can use a cflow pointcut only in conjunction with
62      * a static pointcut: e.g. all setter methods that are invoked under
63      * a particular class. This greatly reduces the number of calls
64      * to the cflow pointcut, meaning that it's not so prohibitively
65      * expensive.
66      */

67     public void testSelectiveApplication() {
68         TestBean target = new TestBean();
69         target.setAge(27);
70         NopInterceptor nop = new NopInterceptor();
71         ControlFlowPointcut cflow = new ControlFlowPointcut(One.class);
72         Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow);
73         ProxyFactory pf = new ProxyFactory(target);
74         ITestBean proxied = (ITestBean) pf.getProxy();
75         pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));
76     
77         // Not advised, not under One
78
target.setAge(16);
79         assertEquals(0, nop.getCount());
80     
81         // Not advised; under One but not a setter
82
assertEquals(16, new One().getAge(proxied));
83         assertEquals(0, nop.getCount());
84     
85         // Won't be advised
86
new One().set(proxied);
87         assertEquals(1, nop.getCount());
88         
89         // We saved most evaluations
90
assertEquals(1, cflow.getEvaluations());
91     }
92     
93     
94     public class One {
95         int getAge(ITestBean proxied) {
96             return proxied.getAge();
97         }
98         int nomatch(ITestBean proxied) {
99             return proxied.getAge();
100         }
101         void set(ITestBean proxied) {
102             proxied.setAge(5);
103         }
104     }
105
106 }
107
Popular Tags