KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > junit > VergeMVCTestCase


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
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.junit;
8
9
10 import java.util.Map JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12
13 import com.inversoft.junit.URL;
14 import com.inversoft.junit.WebTestCase;
15 import com.inversoft.junit.Request;
16 import com.inversoft.verge.mvc.MVCURLTools;
17 import com.inversoft.verge.mvc.model.MetaData;
18 import com.inversoft.verge.mvc.model.ModelMVCInfo;
19 import com.inversoft.verge.mvc.model.DefaultModelParser;
20 import com.inversoft.verge.mvc.model.actionflow.ActionFlowModelMetaData;
21 import com.inversoft.verge.mvc.model.form.FormMetaData;
22 import com.inversoft.verge.mvc.controller.form.FormURLTools;
23 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowURLTools;
24 import com.inversoft.verge.config.VergeConfigConstants;
25 import com.inversoft.verge.config.servlet.ConfigServlet;
26
27
28 /**
29  * <p>
30  * This class is a test case that contains useful methods
31  * for helping to test Verge MVC implementions.
32  * </p>
33  *
34  * @author Brian Pontarelli
35  */

36 public class VergeMVCTestCase extends WebTestCase {
37
38     /**
39      * Constructs a new <code>VergeMVCTestCase</code>.
40      */

41     public VergeMVCTestCase(String JavaDoc name) {
42         super(name);
43     }
44
45
46     /**
47      * Creates a Form-Based MVC URL that can be set into the Request object inside
48      * the begin methods.
49      *
50      * @param contextPath (Optional) The context root path information
51      * @param form The name of the form
52      * @param action (Optional) The action
53      * @return The URL object
54      */

55     protected URL createFormMVCURL(String JavaDoc contextPath, String JavaDoc form, String JavaDoc action) {
56         return createMVCURL(contextPath, null, FormURLTools.generateURL(form, action));
57     }
58
59     /**
60      * Creates a generic MVC URL that is can be used to call any of the MVC systems
61      * including the Form-Based MVC, ActionFlow system or custom systems.
62      *
63      * @param contextPath (Optional) The context path information
64      * @param systemName (Optional) The name of the system. If null, this method
65      * assumes that all the information that is needed is in the path
66      * info
67      * @param pathInfo The path information
68      * @return The URL
69      */

70     protected URL createMVCURL(String JavaDoc contextPath, String JavaDoc systemName, String JavaDoc pathInfo) {
71         String JavaDoc localPathInfo = pathInfo;
72         if (systemName != null) {
73             localPathInfo = "/" + systemName + pathInfo;
74         }
75
76         return new URL(contextPath, localPathInfo, "http", null, null,
77             MVCURLTools.getURLBeginning());
78     }
79
80     /**
81      * Sets up a local test with the given MVC configuration files. These are put
82      * into the correct context parameter and the ConfigServlet is executed to
83      * load up the configuration.
84      *
85      * @param files The names of the files to load
86      */

87     protected void setup(String JavaDoc files) {
88         if (isLocal()) {
89             getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM, files);
90
91             ConfigServlet cs = new ConfigServlet();
92             try {
93                 cs.init(createConfig("ConfigServlet"));
94             } catch (ServletException JavaDoc se) {
95                 fail(se.toString());
96             }
97         }
98     }
99
100     /**
101      * Adds a Form-Based MVC input parameter that will be parsed by the Verge MVC
102      * in order to set a value on a model bean. This should be used to mock up the
103      * JSP tags
104      *
105      * <pre>
106      * <form:checkbox ...>
107      * <form:radio...>
108      * <form:select ...>
109      * <form:text ...>
110      * <form:textarea ...>
111      * </pre>
112      *
113      * @param request The request to set the input parameter into
114      * @param id The id of the form bean to use (i.e. name)
115      * @param property The property on the form bean that will be set
116      * @param input The name of the input field (same as the HTML attribute
117      * called name)
118      * @param value The value to set into the form bean
119      */

120     protected void addFBMVCInput(Request request, String JavaDoc id, String JavaDoc property,
121             String JavaDoc input, String JavaDoc value) {
122         MetaData md = new FormMetaData(id, property);
123         String JavaDoc system = md.getModelSystem();
124         Map JavaDoc extraParams = md.getExtraParams();
125
126         ModelMVCInfo info = new ModelMVCInfo(input, system, md.getDefinition(),
127             extraParams);
128         request.addParameter(DefaultModelParser.INPUT_PARAMETER, info.encode());
129         request.addParameter(input, value);
130     }
131
132     /**
133      * Adds a Form-Based MVC submit parameter that will be parsed by the Verge MVC
134      * in order to determine which action handler to call. This should be used to
135      * mock up the JSP tags:
136      *
137      * <pre>
138      * <form:a ...>
139      * <form:image ...>
140      * <form:submit ...>
141      * </pre>
142      *
143      * @param request The request to set the input parameter into
144      * @param action The action that the action handler will handle. This is
145      * mapped to an action handler in the configuration file.
146      * @param input The name of the input field (same as the HTML name attribute)
147      * @param value The value of the input field (HTML value attribute)
148      * @param isImage Determines if this submit parameter is an image submit or
149      * not
150      */

151     protected void addFBMVCSubmit(Request request, String JavaDoc action, String JavaDoc input,
152             String JavaDoc value, boolean isImage) {
153         request.addParameter(FormURLTools.SUBMIT_PARAMETER,
154             FormURLTools.generateSubmitParameter(input, action));
155
156         if (isImage) {
157             input = input + ".x";
158         }
159         request.addParameter(input, value);
160     }
161
162     /**
163      * Generates a URL that will correctly call into a Form-Based MVC form. This
164      * URL is parsed by the MVC system to determine which model beans, action
165      * handlers, validators and result mappings to use. The action parameter needs
166      * to be set separately using the addFBMVCSubmit method. This method should
167      * be used to mock up the JSP tags:
168      *
169      * <pre>
170      * <form:form ...>
171      * </pre>
172      *
173      * @param request The request to set the input parameter into
174      * @param form The name of the form to mock the URL for
175      */

176     protected void addFBMVCForm(Request request, String JavaDoc form) {
177         URL url = new URL(null, FormURLTools.generateURL(form, null), null, null,
178             null, "/mvc");
179         request.setURL(url);
180     }
181
182     /**
183      * Generates a URL that will correctly call into a Form-Based MVC form with
184      * a specific action. This URL is parsed by the MVC system to determine which
185      * form to use and which action handler to call with the given action.
186      *
187      * @param request The request to set the input parameter into
188      * @param form The name of the form to mock the URL for
189      * @param action The action to execute on the given form
190      */

191     protected void addFBMVCURL(Request request, String JavaDoc form, String JavaDoc action) {
192         URL url = new URL(null, FormURLTools.generateURL(form, null), null, null,
193             null, "/mvc");
194         request.setURL(url);
195     }
196
197     /**
198      * Adds an action-flow MVC input parameter that will be parsed by the Verge MVC
199      * in order to set a value on a model bean. This should be used to mock up the
200      * JSP tags
201      *
202      * <pre>
203      * <af:checkbox ...>
204      * <af:radio...>
205      * <af:select ...>
206      * <af:text ...>
207      * <af:textarea ...>
208      * </pre>
209      *
210      * @param request The request to set the input parameter into
211      * @param id The id of the form bean to use (i.e. name)
212      * @param property The property on the form bean that will be set
213      * @param input The name of the input field (same as the HTML attribute
214      * called name)
215      * @param value The value to set into the form bean
216      */

217     protected void addAFInput(Request request, String JavaDoc id, String JavaDoc property,
218             String JavaDoc input, String JavaDoc value) {
219         MetaData md = new ActionFlowModelMetaData(id, property);
220         String JavaDoc system = md.getModelSystem();
221         Map JavaDoc extraParams = md.getExtraParams();
222
223         ModelMVCInfo info = new ModelMVCInfo(input, system, md.getDefinition(),
224             extraParams);
225         request.addParameter(DefaultModelParser.INPUT_PARAMETER, info.encode());
226         request.addParameter(input, value);
227     }
228
229     /**
230      * Adds an action-flow MVC submit parameter that will be parsed by the Verge MVC
231      * in order to determine which action handler to call. This should be used to
232      * mock up the JSP tags:
233      *
234      * <pre>
235      * <af:a ...>
236      * <af:image ...>
237      * <af:submit ...>
238      * </pre>
239      *
240      * @param request The request to set the input parameter into.
241      * @param action The action that the action handler will handle. This is
242      * mapped to an action handler in the configuration file.
243      * @param input The name of the input field (same as the HTML name attribute).
244      * @param value The value of the input field (HTML value attribute).
245      * @param isImage Determines if this submit parameter is an image submit or
246      * not.
247      * @param entry The entry node to use when executing the action-flow.
248      */

249     protected void addAFSubmit(Request request, String JavaDoc action, String JavaDoc input,
250             String JavaDoc value, boolean isImage, String JavaDoc entry) {
251         request.addParameter(ActionFlowURLTools.SUBMIT_PARAMETER,
252             ActionFlowURLTools.generateSubmitParameter(input, action, entry, null));
253
254         if (isImage) {
255             input = input + ".x";
256         }
257         request.addParameter(input, value);
258     }
259
260     /**
261      * Generates a URL that will correctly call into an action-flow MVC form. This
262      * URL is parsed by the MVC system to determine which model beans, action
263      * handlers, validators and result mappings to use. The action parameter needs
264      * to be set separately using the addAFSubmit method. This method should be
265      * used to mock up the JSP tags:
266      *
267      * <pre>
268      * <form:form ...>
269      * </pre>
270      *
271      * @param request The request to set the input parameter into
272      * @param form The name of the form to mock the URL for
273      * @param namespace The namespace the form is in
274      */

275     protected void addAFForm(Request request, String JavaDoc form, String JavaDoc namespace) {
276         URL url = new URL(null,
277             ActionFlowURLTools.generateURL(namespace, form, null, null), null,
278             null, null, "/mvc");
279         request.setURL(url);
280     }
281 }
Popular Tags