KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.hivemind.Location;
19 import org.apache.hivemind.Resource;
20 import org.apache.hivemind.impl.LocationImpl;
21 import org.apache.hivemind.service.MethodSignature;
22
23 /**
24  * Tests for the {@link org.apache.hivemind.methodmatch.MethodMatcher} class.
25  *
26  * @author Howard Lewis Ship
27  */

28 public class TestMethodMatcher extends AbstractMethodTestCase
29 {
30     private MethodMatcher _m = new MethodMatcher();
31
32     public void testEmpty()
33     {
34         MethodSignature sig = getMethodSignature(this, "equals");
35
36         assertEquals(null, _m.get(sig));
37     }
38
39     public void testNoMatch()
40     {
41         _m.put("equals(java.lang.Object)", "match");
42
43         assertEquals(null, _m.get(getMethodSignature(this, "hashCode")));
44     }
45
46     /** @since 1.1 */
47     public void testNoMatchReturnsDefault()
48     {
49         MethodMatcher m = new MethodMatcher("FRED");
50
51         assertEquals("FRED", m.get(getMethodSignature(this, "hashCode")));
52
53         m.put("zoop", "BARNEY");
54
55         assertEquals("FRED", m.get(getMethodSignature(this, "hashCode")));
56     }
57
58     public void testMatch()
59     {
60         _m.put("equals(java.lang.Object)", "match");
61
62         assertEquals("match", _m.get(getMethodSignature(this, "equals")));
63     }
64
65     public void testMatchPriority()
66     {
67         _m.put("*", "star");
68         _m.put("equals(java.lang.Object)", "exact");
69
70         assertEquals("star", _m.get(getMethodSignature(this, "equals")));
71     }
72
73     public void testParseException()
74     {
75         _m.put("*(", "invalid");
76
77         try
78         {
79             _m.get(getMethodSignature(this, "hashCode"));
80             unreachable();
81         }
82         catch (ApplicationRuntimeException ex)
83         {
84             assertEquals("Method pattern '*(' contains an invalid parameters pattern.", ex
85                     .getMessage());
86         }
87     }
88
89     public void testParseExceptionWithLocation() throws Exception JavaDoc
90     {
91         Resource r = getResource("MethodSubject.class");
92         Location l = new LocationImpl(r, 3);
93
94         MethodSubject s = new MethodSubject();
95         s.setLocation(l);
96
97         _m.put("*(", s);
98
99         try
100         {
101             _m.get(getMethodSignature(this, "hashCode"));
102             unreachable();
103         }
104         catch (ApplicationRuntimeException ex)
105         {
106             String JavaDoc message = ex.getMessage();
107
108             boolean matchesPattern = matches(
109                     message,
110                     "Exception at .*?, line 3: Method pattern '\\*\\(' contains an invalid parameters pattern\\.");
111
112             assertEquals(true, matchesPattern);
113         }
114     }
115 }
Popular Tags