KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > util > TestCaseImplementChecker


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

20 package org.apache.cactus.internal.util;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.lang.reflect.Modifier JavaDoc;
24
25 import junit.framework.Test;
26
27 import org.apache.cactus.Request;
28
29 /**
30  * Utilities to check TestCase implementation.
31  * @version $Id: TestCaseImplementChecker.java,v 1.1 2004/05/22 11:34:48 vmassol Exp $
32  */

33 public final class TestCaseImplementChecker
34 {
35     /**
36      * Default constructor that requires that
37      * {@link #setConfiguration(Configuration)} be called before the methods
38      * requiring a configuration object.
39      *
40      */

41     private TestCaseImplementChecker()
42     {
43     }
44
45     /**
46      * Check if the Test to run is properly implemented or not.
47      * @param theTest the test to check
48      * @throws TestCaseImplementError if has no name
49      */

50     public static void checkTestName(Test theTest)
51         throws TestCaseImplementError
52     {
53         if (theTest == null)
54         {
55             return;
56         }
57
58         if (JUnitVersionHelper.getTestCaseName(theTest) == null)
59         {
60             throw new TestCaseImplementError("No test name found. The test ["
61                 + theTest.getClass().getName()
62                 + "] is not properly implemented.");
63         }
64     }
65
66     /**
67      * @param theNum the number
68      * @return a numeric expresion of theNum
69      */

70     private static String JavaDoc numeric(int theNum)
71     {
72         switch(theNum)
73         {
74         case 1: return "1st";
75         case 2: return "2nd";
76         case 3: return "3rd";
77         default: return (theNum + "th");
78         }
79     }
80
81     /**
82      * @param theMethod the method to check
83      * @param theType the expected return type
84      * @throws TestCaseImplementError if the return-type is not the one expected
85      */

86     private static void checkReturnType(Method JavaDoc theMethod, Class JavaDoc theType)
87         throws TestCaseImplementError
88     {
89         if (!theMethod.getReturnType().equals(theType))
90         {
91             throw new TestCaseImplementError("The method ["
92                 + theMethod.getName()
93                 + "] should return " + theType
94                 + " and not [" + theMethod.getReturnType().getName()
95                 + "]");
96         }
97     }
98
99     /**
100      * @param theMethod the method to test
101      * @throws TestCaseImplementError if the method is not public
102      */

103     private static void isPublic(Method JavaDoc theMethod)
104         throws TestCaseImplementError
105     {
106         if (!Modifier.isPublic(theMethod.getModifiers()))
107         {
108             throw new TestCaseImplementError("The method ["
109                 + theMethod.getName()
110                 + "] should be declared public");
111         }
112     }
113
114     /**
115      * @param theMethod the method to check
116      * @param theParams the expected parameters for the method
117      * @throws TestCaseImplementError if the number of parameter is not same as
118      * that of expected
119      */

120     private static void checkParameterCount(Method JavaDoc theMethod, Class JavaDoc[] theParams)
121         throws TestCaseImplementError
122     {
123         Class JavaDoc[] parameters = theMethod.getParameterTypes();
124         if (parameters.length != theParams.length)
125         {
126             throw new TestCaseImplementError("The method ["
127                 + theMethod.getName()
128                 + "] must have " + theParams.length + " parameter(s), "
129                 + "but " + parameters.length + " parameter(s) were found");
130         }
131     }
132
133     /**
134      * @param theMethod the method to check
135      * @param theParams the expected parameters for the method
136      * @throws TestCaseImplementError if the number and type of parameter
137      * are not same as those of expected
138      */

139     private static void checkParameterTypes(Method JavaDoc theMethod, Class JavaDoc[] theParams)
140         throws TestCaseImplementError
141     {
142         checkParameterCount(theMethod, theParams);
143
144         Class JavaDoc[] parameters = theMethod.getParameterTypes();
145         for (int i = 0; i < parameters.length; i++)
146         {
147             Class JavaDoc expected = theParams[i];
148             Class JavaDoc actual = parameters[i];
149             if (!expected.isAssignableFrom(actual))
150             {
151                 throw new TestCaseImplementError("The method ["
152                     + theMethod.getName()
153                     + "] must accept [" + expected.getName() + "] as "
154                     + numeric(i + 1)
155                     + " parameter, but found a ["
156                     + actual.getName() + "] parameter instead");
157             }
158         }
159     }
160
161     /**
162      * Check if the method is suitable for a test/begin/end method.
163      * @param theMethod the method to check
164      * @throws TestCaseImplementError if the method is not suitable
165      * for Cactus test method
166      */

167     private static void checkAsCactusMethod(Method JavaDoc theMethod)
168         throws TestCaseImplementError
169     {
170         checkReturnType(theMethod, Void.TYPE);
171         isPublic(theMethod);
172     }
173
174     /**
175      * Check if the method is suitable for a begin method.
176      * Throws <code>AssertionFailedError</code> if at least one of following
177      * conditions is failed:
178      * <ul>
179      * <li>return type of the method is void</li>
180      * <li>the method is public</li>
181      * <li>the method accept a parameter of type <code>Request</code></li>
182      * </ul>
183      * @param theMethod the method to check
184      * @throws TestCaseImplementError if the method is not suitable
185      * for Cactus begin method
186      */

187     public static void checkAsBeginMethod(Method JavaDoc theMethod)
188         throws TestCaseImplementError
189     {
190         checkAsCactusMethod(theMethod);
191         checkParameterTypes(theMethod, new Class JavaDoc[]{Request.class});
192     }
193
194     /**
195      * Check if the method is suitable for a end method.
196      * Throws <code>AssertionFailedError</code> if at least one of following
197      * conditions is failed:
198      * <ul>
199      * <li>return type of the method is void</li>
200      * <li>the method is public</li>
201      * <li>the method accept one parameter</li>
202      * </ul>
203      * @param theMethod the method to check
204      * @throws TestCaseImplementError if the method is not suitable
205      * for Cactus end method
206      */

207     public static void checkAsEndMethod(Method JavaDoc theMethod)
208         throws TestCaseImplementError
209     {
210         checkAsCactusMethod(theMethod);
211         checkParameterCount(theMethod, new Class JavaDoc[]{Object JavaDoc.class});
212     }
213 }
214
Popular Tags