KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.JspFactory JavaDoc;
11
12 import com.inversoft.junit.JspTestCase;
13 import com.inversoft.junit.Request;
14 import com.inversoft.junit.Response;
15 import com.inversoft.junit.WebTestCase;
16 import com.inversoft.junit.internal.http.MockHttpServletRequest;
17 import com.inversoft.junit.internal.http.MockHttpServletResponse;
18 import com.inversoft.junit.internal.http.MockJspFactory;
19 import com.inversoft.junit.internal.http.MockPageContext;
20
21
22 /**
23  * This class is the Mediator implementation that conducts
24  * the local tests.
25  *
26  * @author Brian Pontarelli
27  * @since 2.0
28  * @version 2.0
29  */

30 public class LocalMediator implements Mediator {
31
32     /**
33      * Constructs a new LocalMediator passing it the WebTestCase
34      */

35     public LocalMediator() {
36     }
37
38
39     /**
40      * Conducts the local test
41      *
42      * @param testCase The test case to mediate the test on
43      * @throws Throwable If the test failed or something else went wrong
44      */

45     public void mediate(WebTestCase testCase) throws Throwable JavaDoc {
46
47         // First construct a new Request object and call the begin method
48
// if one exists
49
Request request = new Request();
50         ClientCaller client = new ClientCaller();
51
52         client.callBeginMethod(testCase, request);
53
54         // Using the request, populate the Mock objects, set the WebTestCase
55
// variables and call the LocationCaller to execute the test method
56
setupTestCase(testCase, request);
57
58         LocationCaller location = new LocationCaller();
59         location.callTestMethod(testCase);
60
61         // Using the response that might have been setup from the test method
62
// construct a new Reponse and call the end method
63
MockHttpServletResponse mockResponse = testCase.getResponse();
64         Response response = new Response(request, mockResponse.getStatus(),
65             mockResponse.getText(), null, null, mockResponse.getCookiesMap());
66
67         client.callEndMethod(testCase, response);
68     }
69
70     /**
71      * Sets up the WebTestCase with the Mock objects which are constructed using
72      * the request given
73      */

74     protected void setupTestCase(WebTestCase testCase, Request request) {
75         testCase.setRequest(new MockHttpServletRequest(request));
76         testCase.setResponse(new MockHttpServletResponse());
77         testCase.setContext(testCase.getRequest().getSession().getServletContext());
78
79         // Setup the JSP context if this is a jsp test
80
if (testCase instanceof JspTestCase) {
81             JspTestCase jtc = (JspTestCase) testCase;
82             jtc.pageContext = new MockPageContext(testCase.createConfig(),
83                 testCase.getRequest(), testCase.getResponse());
84             JspFactory.setDefaultFactory(new MockJspFactory());
85         }
86     }
87 }
88
Popular Tags