KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > servlet > test > MVCServletTest


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.verge.mvc.servlet.test;
8
9
10 import javax.servlet.ServletException JavaDoc;
11
12 import junit.framework.AssertionFailedError;
13
14 import com.inversoft.junit.Request;
15 import com.inversoft.junit.WebTestCase;
16 import com.inversoft.junit.internal.http.MockServletConfig;
17 import com.inversoft.junit.internal.http.MockServletContext;
18 import com.inversoft.verge.mvc.MVCConstants;
19 import com.inversoft.verge.mvc.servlet.MVCServlet;
20
21
22 /**
23  * This class tests the MVCServlet to make sure that it loads
24  * everything correctly and correctly calls the mediator
25  *
26  * @author Brian Pontarelli
27  */

28 public class MVCServletTest extends WebTestCase {
29
30     /**
31      * Instantiates a new <code>MVCServletTest</code>
32      */

33     public MVCServletTest(String JavaDoc name) {
34         super(name);
35         setLocal(true);
36     }
37
38
39     /**
40      * Tests a successful startup of the MVCServlet without a context parameter
41      */

42     public void testSuccess() {
43
44         MockServletConfig config = createConfig();
45         MVCServlet servlet = new MVCServlet();
46         try {
47             servlet.init(config);
48         } catch (ServletException JavaDoc se) {
49             fail(se.toString());
50         }
51     }
52
53     /**
54      * Tests a successful startup of the MVCServlet with a context parameter
55      */

56     public void testDefault() {
57
58         MockServletConfig config = createConfig();
59         if (isLocal()) {
60             MockServletContext context =
61                 (MockServletContext) config.getServletContext();
62             context.setInitParameter(MVCConstants.CONTEXT_PARAM,
63                 "com.inversoft.verge.mvc.servlet.test.TestMVCMediator");
64         }
65
66         MVCServlet servlet = new MVCServlet();
67         try {
68             servlet.init(config);
69         } catch (ServletException JavaDoc se) {
70             fail(se.toString());
71         }
72     }
73
74     /**
75      * Tests a failed startup of the MVCServlet with an invalid context parameter
76      */

77     public void testFailure() {
78
79         MockServletConfig config = createConfig();
80         if (isLocal()) {
81             MockServletContext context =
82                 (MockServletContext) config.getServletContext();
83             context.setInitParameter(MVCConstants.CONTEXT_PARAM,
84                 "com.inversoft.verge.mvc.servlet.test.NotAClass");
85         }
86
87         try {
88             MVCServlet servlet = new MVCServlet();
89             servlet.init(config);
90             fail("Should have asserted");
91         } catch (AssertionFailedError afe) {
92             // Smother. This should be propogated out to the JUnit framework
93
} catch (Throwable JavaDoc t) {
94             assertTrue("Should be assertion error", t instanceof AssertionError JavaDoc);
95         }
96     }
97
98     /**
99      * Tests a success startup of the MVCServlet and successful call to mediator
100      * with a non default mediator
101      */

102     public void testSuccessCall() {
103
104         MockServletConfig config = createConfig();
105         if (isLocal()) {
106             MockServletContext context =
107                 (MockServletContext) config.getServletContext();
108             context.setInitParameter(MVCConstants.CONTEXT_PARAM,
109                 "com.inversoft.verge.mvc.servlet.test.TestMVCMediator");
110         }
111
112         try {
113             MVCServlet servlet = new MVCServlet();
114             servlet.init(config);
115             servlet.doGet(request, response);
116         } catch (Throwable JavaDoc t) {
117             fail(t.toString());
118         }
119     }
120
121     /**
122      * Setup the request parameter that signals failure
123      */

124     public void beginFailureCall(Request request) {
125         request.addParameter(TestMVCMediator.FAIL, TestMVCMediator.FAIL);
126     }
127
128     /**
129      * Tests a success startup of the MVCServlet and successful call to mediator
130      * with a non default mediator
131      */

132     public void testFailureCall() {
133
134         MockServletConfig config = createConfig();
135         if (isLocal()) {
136             MockServletContext context =
137                 (MockServletContext) config.getServletContext();
138             context.setInitParameter(MVCConstants.CONTEXT_PARAM,
139                 "com.inversoft.verge.mvc.servlet.test.TestMVCMediator");
140         }
141
142         try {
143             MVCServlet servlet = new MVCServlet();
144             servlet.init(config);
145             servlet.doGet(request, response);
146             fail("Should have failed inside the call the TestMVCMediator");
147         } catch (Throwable JavaDoc t) {
148             assertTrue("Should have been a ServletException",
149                 t instanceof ServletException JavaDoc);
150             assertTrue("Should have message of fail",
151                 t.getMessage().equals(TestMVCMediator.FAIL));
152         }
153     }
154 }
155
Popular Tags