/
NameMatchMethodPointcutTests.java
139 lines (115 loc) · 3.97 KB
/
NameMatchMethodPointcutTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.support;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.tests.aop.interceptor.NopInterceptor;
import org.springframework.tests.aop.interceptor.SerializableNopInterceptor;
import org.springframework.tests.sample.beans.Person;
import org.springframework.tests.sample.beans.SerializablePerson;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Rod Johnson
* @author Chris Beams
*/
public class NameMatchMethodPointcutTests {
protected NameMatchMethodPointcut pc;
protected Person proxied;
protected SerializableNopInterceptor nop;
/**
* Create an empty pointcut, populating instance variables.
*/
@Before
public void setup() {
ProxyFactory pf = new ProxyFactory(new SerializablePerson());
nop = new SerializableNopInterceptor();
pc = new NameMatchMethodPointcut();
pf.addAdvisor(new DefaultPointcutAdvisor(pc, nop));
proxied = (Person) pf.getProxy();
}
@Test
public void testMatchingOnly() {
// Can't do exact matching through isMatch
assertThat(pc.isMatch("echo", "ech*")).isTrue();
assertThat(pc.isMatch("setName", "setN*")).isTrue();
assertThat(pc.isMatch("setName", "set*")).isTrue();
assertThat(pc.isMatch("getName", "set*")).isFalse();
assertThat(pc.isMatch("setName", "set")).isFalse();
assertThat(pc.isMatch("testing", "*ing")).isTrue();
}
@Test
public void testEmpty() throws Throwable {
assertThat(nop.getCount()).isEqualTo(0);
proxied.getName();
proxied.setName("");
proxied.echo(null);
assertThat(nop.getCount()).isEqualTo(0);
}
@Test
public void testMatchOneMethod() throws Throwable {
pc.addMethodName("echo");
pc.addMethodName("set*");
assertThat(nop.getCount()).isEqualTo(0);
proxied.getName();
proxied.getName();
assertThat(nop.getCount()).isEqualTo(0);
proxied.echo(null);
assertThat(nop.getCount()).isEqualTo(1);
proxied.setName("");
assertThat(nop.getCount()).isEqualTo(2);
proxied.setAge(25);
assertThat(proxied.getAge()).isEqualTo(25);
assertThat(nop.getCount()).isEqualTo(3);
}
@Test
public void testSets() throws Throwable {
pc.setMappedNames("set*", "echo");
assertThat(nop.getCount()).isEqualTo(0);
proxied.getName();
proxied.setName("");
assertThat(nop.getCount()).isEqualTo(1);
proxied.echo(null);
assertThat(nop.getCount()).isEqualTo(2);
}
@Test
public void testSerializable() throws Throwable {
testSets();
// Count is now 2
Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied);
NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
p2.getName();
assertThat(nop2.getCount()).isEqualTo(2);
p2.echo(null);
assertThat(nop2.getCount()).isEqualTo(3);
}
@Test
public void testEqualsAndHashCode() {
NameMatchMethodPointcut pc1 = new NameMatchMethodPointcut();
NameMatchMethodPointcut pc2 = new NameMatchMethodPointcut();
String foo = "foo";
assertThat(pc2).isEqualTo(pc1);
assertThat(pc2.hashCode()).isEqualTo(pc1.hashCode());
pc1.setMappedName(foo);
assertThat(pc1.equals(pc2)).isFalse();
assertThat(pc1.hashCode() != pc2.hashCode()).isTrue();
pc2.setMappedName(foo);
assertThat(pc2).isEqualTo(pc1);
assertThat(pc2.hashCode()).isEqualTo(pc1.hashCode());
}
}