KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.AssertionFailedError;
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import org.apache.cactus.Request;
27 import org.apache.cactus.WebResponse;
28
29 /**
30  * Unit tests for the {@link TestCaseImplementChecker} class.
31  *
32  * @version $Id: TestTestCaseImplementChecker.java,v 1.1 2004/05/22 11:34:46 vmassol Exp $
33  */

34 public class TestTestCaseImplementChecker extends TestCase
35 {
36     /**
37      * normal test class for {@link #testCheckTestName}
38      */

39     class NormalTest extends TestCase
40     {
41         /**
42          * @param theName the name of the test
43          */

44         public NormalTest(String JavaDoc theName)
45         {
46             super(theName);
47         }
48
49         /**
50          * dummy test entry
51          */

52         public void testDummy()
53         {
54         }
55     }
56
57     /**
58      * illegal test class for {@link #testCheckTestName}
59      */

60     class NoNameTest extends TestCase
61     {
62         /**
63          * @param theName the name of the test
64          */

65         public NoNameTest(String JavaDoc theName)
66         {
67         }
68
69         /**
70          * dummy test entry
71          */

72         public void testDummy()
73         {
74         }
75     }
76
77     /**
78      * declare methods to be used by {@link #testCheckAsBeginMethod} and
79      * {@link #testCheckAsEndMethod}
80      */

81     class MethodHolder
82     {
83         /**
84          */

85         public MethodHolder()
86         {
87         }
88         
89         // ---- Begin Method ----
90

91         /**
92          * @param theRequest a Request
93          */

94         public void beginNormal(Request theRequest)
95         {
96         }
97         
98         /**
99          * @param theRequest a Request
100          * @return a dummy String
101          */

102         public String JavaDoc beginReturnsString(Request theRequest)
103         {
104             return "a string";
105         }
106         
107         /**
108          * @param theRequest a Request
109          */

110         protected void beginProtected(Request theRequest)
111         {
112         }
113         
114         /**
115          * @param theRequest a Request
116          */

117         private void beginPrivate(Request theRequest)
118         {
119         }
120         
121         /**
122          */

123         public void beginNoParam()
124         {
125         }
126         
127         /**
128          * @param theRequest a Request
129          * @param theObject a Object
130          */

131         public void beginWithTwoParams(Request theRequest, Object JavaDoc theObject)
132         {
133         }
134         
135         /**
136          * @param theString a String
137          */

138         public void beginWithStringParam(String JavaDoc theString)
139         {
140         }
141
142         // ---- End Method ----
143

144         /**
145          * @param theResponse a WebResponse
146          */

147         public void endNormal(WebResponse theResponse)
148         {
149         }
150         
151         /**
152          * @param theResponse a WebResponse
153          * @return a dummy String
154          */

155         public String JavaDoc endReturnsString(WebResponse theResponse)
156         {
157             return "a string";
158         }
159         
160         /**
161          * @param theResponse a WebResponse
162          */

163         protected void endProtected(WebResponse theResponse)
164         {
165         }
166         
167         /**
168          * @param theResponse a WebResponse
169          */

170         private void endPrivate(WebResponse theResponse)
171         {
172         }
173         
174         /**
175          */

176         public void endNoParam()
177         {
178         }
179         
180         /**
181          * @param theResponse a WebResponse
182          * @param theObject a Object
183          */

184         public void endWithTwoParams(WebResponse theResponse, Object JavaDoc theObject)
185         {
186         }
187         
188         /**
189          * @param theString a String
190          */

191         public void endWithStringParam(String JavaDoc theString)
192         {
193         }
194     }
195
196     /**
197      * @return a "Should not be here" message
198      */

199     private String JavaDoc shouldNotHere()
200     {
201         return "shold not be here";
202     }
203
204     /**
205      * @param theThrowable a throwable
206      * @return a "Should not be here" message with throwable information
207      */

208     private String JavaDoc shouldNotHere(Throwable JavaDoc theThrowable)
209     {
210         return shouldNotHere() + ": " + theThrowable.getClass().getName()
211             + "[" + theThrowable.getMessage() + "]";
212     }
213
214     /**
215      * @see {TestCaseImplementChecker#checkTestName(junit.framework.Test)}
216      */

217     public void testCheckTestName()
218     {
219         Test test;
220         try
221         {
222             test = new NormalTest("testDummy");
223             TestCaseImplementChecker.checkTestName(test);
224         }
225         catch (Throwable JavaDoc t)
226         {
227             fail(shouldNotHere(t));
228         }
229
230         try
231         {
232             test = new NoNameTest("testDummy");
233             TestCaseImplementChecker.checkTestName(test);
234             fail(shouldNotHere());
235         }
236         catch (TestCaseImplementError e)
237         {
238             assertEquals("No test name found. The test ["
239                 + TestTestCaseImplementChecker.NoNameTest.class.getName()
240                 + "] is not properly implemented.", e.getMessage());
241         }
242         catch (Throwable JavaDoc t)
243         {
244             fail(shouldNotHere(t));
245         }
246     }
247
248     /**
249      * @see {TestCaseImplementChecker#checkAsBeginMethod}
250      */

251     public void testCheckAsBeginMethod()
252     {
253         //--------------------------------------------------------------------
254

255         try
256         {
257             Method JavaDoc method = MethodHolder.class.getMethod(
258                 "beginNormal", new Class JavaDoc[]{Request.class});
259             TestCaseImplementChecker.checkAsBeginMethod(method);
260         }
261         catch (Throwable JavaDoc t)
262         {
263             fail(shouldNotHere(t));
264         }
265
266         //---------------------------------------------------------------------
267

268         try
269         {
270             Method JavaDoc method = MethodHolder.class.getMethod(
271                 "beginReturnsString", new Class JavaDoc[]{Request.class});
272             TestCaseImplementChecker.checkAsBeginMethod(method);
273             fail(shouldNotHere());
274         }
275         catch (AssertionFailedError e)
276         {
277             assertEquals("The method [beginReturnsString] "
278                 + "should return void and not [java.lang.String]",
279                 e.getMessage());
280         }
281         catch (Throwable JavaDoc t)
282         {
283             fail(shouldNotHere(t));
284         }
285
286         //---------------------------------------------------------------------
287

288         try
289         {
290             Method JavaDoc method = MethodHolder.class.getDeclaredMethod(
291                 "beginProtected", new Class JavaDoc[]{Request.class});
292             TestCaseImplementChecker.checkAsBeginMethod(method);
293             fail(shouldNotHere());
294         }
295         catch (AssertionFailedError e)
296         {
297             assertEquals("The method [beginProtected] "
298                 + "should be declared public", e.getMessage());
299         }
300         catch (Throwable JavaDoc t)
301         {
302             fail(shouldNotHere(t));
303         }
304
305         //---------------------------------------------------------------------
306

307         try
308         {
309             Method JavaDoc method = MethodHolder.class.getDeclaredMethod(
310                 "beginPrivate", new Class JavaDoc[]{Request.class});
311             TestCaseImplementChecker.checkAsBeginMethod(method);
312             fail(shouldNotHere());
313         }
314         catch (AssertionFailedError e)
315         {
316             assertEquals("The method [beginPrivate] "
317                 + "should be declared public", e.getMessage());
318         }
319         catch (Throwable JavaDoc t)
320         {
321             fail(shouldNotHere(t));
322         }
323
324         //---------------------------------------------------------------------
325

326         try
327         {
328             Method JavaDoc method = MethodHolder.class.getMethod(
329                 "beginNoParam", new Class JavaDoc[]{});
330             TestCaseImplementChecker.checkAsBeginMethod(method);
331             fail(shouldNotHere());
332         }
333         catch (AssertionFailedError e)
334         {
335             assertEquals("The method [beginNoParam] must have 1 parameter(s), "
336                 + "but 0 parameter(s) were found", e.getMessage());
337         }
338         catch (Throwable JavaDoc t)
339         {
340             fail(shouldNotHere(t));
341         }
342
343         //---------------------------------------------------------------------
344

345         try
346         {
347             Method JavaDoc method = MethodHolder.class.getMethod(
348                 "beginWithTwoParams",
349                 new Class JavaDoc[]{Request.class, Object JavaDoc.class});
350             TestCaseImplementChecker.checkAsBeginMethod(method);
351             fail(shouldNotHere());
352         }
353         catch (AssertionFailedError e)
354         {
355             assertEquals("The method [beginWithTwoParams] "
356                 + "must have 1 parameter(s), "
357                 + "but 2 parameter(s) were found", e.getMessage());
358         }
359         catch (Throwable JavaDoc t)
360         {
361             fail(shouldNotHere(t));
362         }
363
364         //---------------------------------------------------------------------
365
try
366         {
367             Method JavaDoc method = MethodHolder.class.getMethod(
368                 "beginWithStringParam", new Class JavaDoc[]{String JavaDoc.class});
369             TestCaseImplementChecker.checkAsBeginMethod(method);
370             fail(shouldNotHere());
371         }
372         catch (AssertionFailedError e)
373         {
374             assertEquals("The method [beginWithStringParam] "
375                 + "must accept [org.apache.cactus.Request] "
376                 + "as 1st parameter, but found a "
377                 + "[java.lang.String] parameter instead",
378                 e.getMessage());
379         }
380         catch (Throwable JavaDoc t)
381         {
382             fail(shouldNotHere(t));
383         }
384     }
385
386
387     /**
388      * @see {TestCaseImplementChecker#checkAsEndMethod
389      * (java.lang.reflect.Method)}
390      */

391     public void testCheckAsEndMethod()
392     {
393         //---------------------------------------------------------------------
394

395         try
396         {
397             Method JavaDoc method = MethodHolder.class.getMethod(
398                 "endNormal", new Class JavaDoc[]{WebResponse.class});
399             TestCaseImplementChecker.checkAsEndMethod(method);
400         }
401         catch (Throwable JavaDoc t)
402         {
403             fail(shouldNotHere(t));
404         }
405
406         //---------------------------------------------------------------------
407

408         try
409         {
410             Method JavaDoc method = MethodHolder.class.getMethod(
411                 "endReturnsString", new Class JavaDoc[]{WebResponse.class});
412             TestCaseImplementChecker.checkAsEndMethod(method);
413             fail(shouldNotHere());
414         }
415         catch (AssertionFailedError e)
416         {
417             assertEquals("The method [endReturnsString] "
418                 + "should return void and not [java.lang.String]",
419                 e.getMessage());
420         }
421         catch (Throwable JavaDoc t)
422         {
423             fail(shouldNotHere(t));
424         }
425
426         //---------------------------------------------------------------------
427

428         try
429         {
430             Method JavaDoc method = MethodHolder.class.getDeclaredMethod(
431                 "endProtected", new Class JavaDoc[]{WebResponse.class});
432             TestCaseImplementChecker.checkAsEndMethod(method);
433             fail(shouldNotHere());
434         }
435         catch (AssertionFailedError e)
436         {
437             assertEquals("The method [endProtected] "
438                 + "should be declared public", e.getMessage());
439         }
440         catch (Throwable JavaDoc t)
441         {
442             fail(shouldNotHere(t));
443         }
444
445         //---------------------------------------------------------------------
446

447         try
448         {
449             Method JavaDoc method = MethodHolder.class.getDeclaredMethod(
450                 "endPrivate", new Class JavaDoc[]{WebResponse.class});
451             TestCaseImplementChecker.checkAsEndMethod(method);
452             fail(shouldNotHere());
453         }
454         catch (AssertionFailedError e)
455         {
456             assertEquals("The method [endPrivate] "
457                 + "should be declared public", e.getMessage());
458         }
459         catch (Throwable JavaDoc t)
460         {
461             fail(shouldNotHere(t));
462         }
463
464         //---------------------------------------------------------------------
465

466         try
467         {
468             Method JavaDoc method = MethodHolder.class.getMethod(
469                 "endNoParam", new Class JavaDoc[]{});
470             TestCaseImplementChecker.checkAsEndMethod(method);
471             fail(shouldNotHere());
472         }
473         catch (AssertionFailedError e)
474         {
475             assertEquals("The method [endNoParam] must have 1 parameter(s), "
476                 + "but 0 parameter(s) were found", e.getMessage());
477         }
478         catch (Throwable JavaDoc t)
479         {
480             fail(shouldNotHere(t));
481         }
482
483         //---------------------------------------------------------------------
484

485         try
486         {
487             Method JavaDoc method = MethodHolder.class.getMethod(
488                 "endWithTwoParams",
489                 new Class JavaDoc[]{WebResponse.class, Object JavaDoc.class});
490             TestCaseImplementChecker.checkAsEndMethod(method);
491             fail(shouldNotHere());
492         }
493         catch (AssertionFailedError e)
494         {
495             assertEquals("The method [endWithTwoParams] "
496                 + "must have 1 parameter(s), "
497                 + "but 2 parameter(s) were found", e.getMessage());
498         }
499         catch (Throwable JavaDoc t)
500         {
501             fail(shouldNotHere(t));
502         }
503     }
504 }
505
Popular Tags