KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > methodmatch > TestMethodFilters


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.methodmatch;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.hivemind.service.MethodSignature;
21
22 /**
23  * Tests for various {@link org.apache.hivemind.methodmatch.MethodFilter}s
24  * as well as {@link org.apache.hivemind.methodmatch.MethodPatternParser}.
25  *
26  * @author Howard Lewis Ship
27  */

28 public class TestMethodFilters extends AbstractMethodTestCase
29 {
30     public void testExactNameFilter() throws Exception JavaDoc
31     {
32         MethodFilter f = new ExactNameFilter("hashCode");
33
34         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
35         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
36     }
37
38     public void testNamePrefixFilter() throws Exception JavaDoc
39     {
40         MethodFilter f = new NamePrefixFilter("hash");
41
42         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
43         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
44     }
45
46     public void testNameSuffixFilter() throws Exception JavaDoc
47     {
48         MethodFilter f = new NameSuffixFilter("Code");
49
50         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
51         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
52     }
53
54     public void testInfixNameFilter() throws Exception JavaDoc
55     {
56         MethodFilter f1 = new InfixNameFilter("od");
57
58         assertEquals(true, f1.matchMethod(getMethodSignature(this, "hashCode")));
59         assertEquals(false, f1.matchMethod(getMethodSignature(this, "toString")));
60
61         MethodFilter f2 = new InfixNameFilter("hashCode");
62         assertEquals(true, f2.matchMethod(getMethodSignature(this, "hashCode")));
63         assertEquals(false, f2.matchMethod(getMethodSignature(this, "toString")));
64     }
65
66     public void testParameterCountFilter() throws Exception JavaDoc
67     {
68         MethodFilter f = new ParameterCountFilter(0);
69
70         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
71         assertEquals(true, f.matchMethod(getMethodSignature(this, "toString")));
72         assertEquals(false, f.matchMethod(getMethodSignature(this, "equals")));
73
74         f = new ParameterCountFilter(1);
75
76         assertEquals(false, f.matchMethod(getMethodSignature(this, "hashCode")));
77         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
78         assertEquals(true, f.matchMethod(getMethodSignature(this, "equals")));
79     }
80
81     public void testParameterFilterIntArg()
82     {
83         MethodFilter f = new ParameterFilter(0, "int");
84
85         assertEquals(true, f.matchMethod(getMethodSignature(MethodSubject.class, "intArg")));
86         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "integerArg")));
87     }
88
89     public void testParameterFilterIntegerArg()
90     {
91         MethodFilter f = new ParameterFilter(0, "java.lang.Integer");
92
93         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "intArg")));
94         assertEquals(true, f.matchMethod(getMethodSignature(MethodSubject.class, "integerArg")));
95     }
96
97     public void testParameterFilterArrayArg()
98     {
99         MethodFilter f = new ParameterFilter(0, "java.lang.Integer[]");
100
101         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "intArg")));
102         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "integerArg")));
103         assertEquals(true, f.matchMethod(getMethodSignature(MethodSubject.class, "arrayArg")));
104         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "scalarArrayArg")));
105     }
106
107     public void testParameterFilterScalarArrayArg()
108     {
109         MethodFilter f = new ParameterFilter(0, "int[]");
110
111         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "intArg")));
112         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "integerArg")));
113         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "arrayArg")));
114         assertEquals(true, f.matchMethod(getMethodSignature(MethodSubject.class, "scalarArrayArg")));
115     }
116
117     public void testCompositeFilter()
118     {
119         List JavaDoc l = new ArrayList JavaDoc();
120         l.add(new ExactNameFilter("intArg"));
121
122         CompositeFilter f = new CompositeFilter(l);
123         MethodSignature sig = getMethodSignature(MethodSubject.class, "intArg");
124
125         assertEquals(true, f.matchMethod(sig));
126
127         l.add(new ParameterCountFilter(0));
128
129         assertEquals(false, f.matchMethod(sig));
130     }
131
132     public void testMatchAllFilter()
133     {
134         MethodFilter f = new MatchAllFilter();
135
136         assertEquals(true, f.matchMethod(null));
137     }
138
139 }
140
Popular Tags