KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.hivemind.ApplicationRuntimeException;
18
19 /**
20  * Tests for {@link org.apache.hivemind.methodmatch.MethodPatternParser}.
21  *
22  * @author Howard Lewis Ship
23  */

24 public class TestMethodPatternParser extends AbstractMethodTestCase
25 {
26     public void testCount()
27     {
28         MethodPatternParser p = new MethodPatternParser();
29         MethodFilter f = p.parseMethodPattern("*(1)");
30
31         assertEquals(false, f.matchMethod(getMethodSignature(this, "hashCode")));
32         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
33         assertEquals(true, f.matchMethod(getMethodSignature(this, "equals")));
34     }
35
36     public void testExactName()
37     {
38         MethodPatternParser p = new MethodPatternParser();
39         MethodFilter f = p.parseMethodPattern("hashCode");
40
41         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
42         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
43     }
44
45     public void testInvalidName()
46     {
47         try
48         {
49             MethodPatternParser p = new MethodPatternParser();
50             p.parseMethodPattern("*foo*bar*()");
51             unreachable();
52         }
53         catch (ApplicationRuntimeException ex)
54         {
55             assertEquals(
56                 "Method pattern '*foo*bar*()' contains an invalid method name pattern.",
57                 ex.getMessage());
58         }
59     }
60
61     public void testInvalidParams()
62     {
63         try
64         {
65             MethodPatternParser p = new MethodPatternParser();
66             p.parseMethodPattern("*bar(");
67             unreachable();
68         }
69         catch (ApplicationRuntimeException ex)
70         {
71             assertEquals(
72                 "Method pattern '*bar(' contains an invalid parameters pattern.",
73                 ex.getMessage());
74         }
75     }
76
77     public void testInvalidParamCount()
78     {
79         try
80         {
81             MethodPatternParser p = new MethodPatternParser();
82             p.parseMethodPattern("*(1x)");
83             unreachable();
84         }
85         catch (ApplicationRuntimeException ex)
86         {
87             assertEquals(
88                 "Method pattern '*(1x)' contains an invalid parameters pattern.",
89                 ex.getMessage());
90         }
91     }
92
93     public void testMatchAll()
94     {
95         MethodPatternParser p = new MethodPatternParser();
96         MethodFilter f = p.parseMethodPattern("*");
97
98         assertEquals(true, f.matchMethod(null));
99     }
100
101     public void testNameMissing()
102     {
103         try
104         {
105             MethodPatternParser p = new MethodPatternParser();
106             p.parseMethodPattern("");
107             unreachable();
108         }
109         catch (ApplicationRuntimeException ex)
110         {
111             assertEquals("Method pattern '' does not contain a method name.", ex.getMessage());
112         }
113     }
114
115     public void testParameterTypes()
116     {
117         MethodPatternParser p = new MethodPatternParser();
118         MethodFilter f = p.parseMethodPattern("*(int,java.lang.String[])");
119
120         assertEquals(false, f.matchMethod(getMethodSignature(MethodSubject.class, "intArg")));
121         assertEquals(true, f.matchMethod(getMethodSignature(MethodSubject.class, "intStringArrayArgs")));
122     }
123
124     public void testPrefix()
125     {
126         MethodPatternParser p = new MethodPatternParser();
127         MethodFilter f = p.parseMethodPattern("hash*");
128
129         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
130         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
131     }
132
133     public void testSubstring()
134     {
135         MethodPatternParser p = new MethodPatternParser();
136         MethodFilter f = p.parseMethodPattern("*od*");
137
138         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
139         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
140     }
141
142     public void testSuffix()
143     {
144         MethodPatternParser p = new MethodPatternParser();
145         MethodFilter f = p.parseMethodPattern("*Code");
146
147         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
148         assertEquals(false, f.matchMethod(getMethodSignature(this, "toString")));
149     }
150
151     public void testEmpty()
152     {
153         MethodPatternParser p = new MethodPatternParser();
154         MethodFilter f = p.parseMethodPattern("*()");
155
156         assertEquals(true, f.matchMethod(getMethodSignature(this, "hashCode")));
157         assertEquals(true, f.matchMethod(getMethodSignature(this, "toString")));
158         assertEquals(false, f.matchMethod(getMethodSignature(this, "equals")));
159     }
160
161 }
162
Popular Tags