KickJava   Java API By Example, From Geeks To Geeks.

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


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.framework.ProxyFactory;
23 import org.springframework.aop.interceptor.NopInterceptor;
24 import org.springframework.aop.interceptor.SerializableNopInterceptor;
25 import org.springframework.beans.Person;
26 import org.springframework.beans.SerializablePerson;
27 import org.springframework.util.SerializationTestUtils;
28
29 /**
30  *
31  * @author Rod Johnson
32  */

33 public class NameMatchMethodPointcutTests extends TestCase {
34     
35     protected NameMatchMethodPointcut pc;
36     
37     protected Person proxied;
38     
39     protected SerializableNopInterceptor nop;
40     
41     public NameMatchMethodPointcutTests(String JavaDoc s) {
42         super(s);
43     }
44     
45     /**
46      * Create an empty pointcut, populating instance variables.
47      * @see junit.framework.TestCase#setUp()
48      */

49     protected void setUp() {
50         ProxyFactory pf = new ProxyFactory(new SerializablePerson());
51         nop = new SerializableNopInterceptor();
52         pc = new NameMatchMethodPointcut();
53         pf.addAdvisor(new DefaultPointcutAdvisor(pc, nop));
54         proxied = (Person) pf.getProxy();
55     }
56     
57     public void testMatchingOnly() {
58         // Can't do exact matching through isMatch
59
assertTrue(pc.isMatch("echo", "ech*"));
60         assertTrue(pc.isMatch("setName", "setN*"));
61         assertTrue(pc.isMatch("setName", "set*"));
62         assertFalse(pc.isMatch("getName", "set*"));
63         assertFalse(pc.isMatch("setName", "set"));
64         assertTrue(pc.isMatch("testing", "*ing"));
65     }
66         
67     public void testEmpty() throws Throwable JavaDoc {
68         assertEquals(0, nop.getCount());
69         proxied.getName();
70         proxied.setName("");
71         proxied.echo(null);
72         assertEquals(0, nop.getCount());
73     }
74     
75     
76     public void testMatchOneMethod() throws Throwable JavaDoc {
77         pc.addMethodName("echo");
78         pc.addMethodName("set*");
79         assertEquals(0, nop.getCount());
80         proxied.getName();
81         proxied.getName();
82         assertEquals(0, nop.getCount());
83         proxied.echo(null);
84         assertEquals(1, nop.getCount());
85         
86         proxied.setName("");
87         assertEquals(2, nop.getCount());
88         proxied.setAge(25);
89         assertEquals(25, proxied.getAge());
90         assertEquals(3, nop.getCount());
91     }
92
93     public void testSets() throws Throwable JavaDoc {
94         pc.setMappedNames(new String JavaDoc[] { "set*", "echo" });
95         assertEquals(0, nop.getCount());
96         proxied.getName();
97         proxied.setName("");
98         assertEquals(1, nop.getCount());
99         proxied.echo(null);
100         assertEquals(2, nop.getCount());
101     }
102     
103     public void testSerializable() throws Throwable JavaDoc {
104         testSets();
105         // Count is now 2
106
Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied);
107         NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
108         p2.getName();
109         assertEquals(2, nop2.getCount());
110         p2.echo(null);
111         assertEquals(3, nop2.getCount());
112     }
113 }
114
Popular Tags