KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > ClientCaller


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal;
8
9
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12
13 import com.inversoft.junit.Request;
14 import com.inversoft.junit.Response;
15 import com.inversoft.junit.WebTestCase;
16
17
18 /**
19  * This class does the reflective calling of the client side
20  * test methods.
21  *
22  * @author Brian Pontarelli
23  * @since 2.0
24  * @version 2.0
25  */

26 public class ClientCaller {
27
28     /**
29      * The prefix of a begin test method.
30      */

31     public static final String JavaDoc BEGIN_METHOD_PREFIX = "begin";
32
33     /**
34      * The prefix of an end test method.
35      */

36     public static final String JavaDoc END_METHOD_PREFIX = "end";
37
38
39     /**
40      * Calls the begin test method on the given WebTestCase with the given
41      * test name and passes it the given request instance.
42      *
43      * @param testCase The WebTestCase instance to call the method on
44      * @param request The Request to pass to the begin method
45      * @throws Throwable If the begin method failed or threw an Exception
46      */

47     public void callBeginMethod(WebTestCase testCase, Request request)
48     throws Throwable JavaDoc {
49
50         // Setup the base name and begin name of the test being run
51
String JavaDoc baseName =
52             testCase.getName().substring(Constants.TEST_METHOD_PREFIX.length());
53         String JavaDoc beginName = BEGIN_METHOD_PREFIX + baseName;
54     
55         try {
56             Class JavaDoc klass = testCase.getClass();
57             Method JavaDoc method = klass.getMethod(beginName, new Class JavaDoc[] {Request.class});
58
59             method.invoke(testCase, new Object JavaDoc[] {request});
60         } catch (InvocationTargetException JavaDoc ite) {
61             ite.fillInStackTrace();
62             throw ite.getTargetException();
63         } catch (IllegalAccessException JavaDoc e) {
64             e.fillInStackTrace();
65             throw e;
66         } catch (NoSuchMethodException JavaDoc nsme) {
67             // Smother because there does not have to be a begin method
68
return;
69         }
70     }
71
72     /**
73      * Calls the end test method on the given WebTestCase with the given
74      * test name and passes it the given response object.
75      *
76      * @param testCase The WebTestCase instance to call the method on
77      * @param response The response to pass to the end method
78      * @throws Throwable If the end method failed or threw an Exception
79      */

80     public void callEndMethod(WebTestCase testCase, Response response)
81     throws Throwable JavaDoc {
82
83         // Setup the base name and begin name of the test being run
84
String JavaDoc baseName =
85             testCase.getName().substring(Constants.TEST_METHOD_PREFIX.length());
86         String JavaDoc endName = END_METHOD_PREFIX + baseName;
87     
88         try {
89             Class JavaDoc klass = testCase.getClass();
90             Method JavaDoc method = klass.getMethod(endName, new Class JavaDoc[] {Response.class});
91
92             method.invoke(testCase, new Object JavaDoc[] {response});
93         } catch (InvocationTargetException JavaDoc ite) {
94             ite.fillInStackTrace();
95             throw ite.getTargetException();
96         } catch (IllegalAccessException JavaDoc e) {
97             e.fillInStackTrace();
98             throw e;
99         } catch (NoSuchMethodException JavaDoc nsme) {
100             // Smother because there does not have to be a end method
101
return;
102         }
103     }
104 }
105
Popular Tags