KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > model > form > test > FormModelHandlerTest


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.model.form.test;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import com.inversoft.beans.BeanException;
14 import com.inversoft.junit.JspTestCase;
15 import com.inversoft.util.typeconverter.TypeConversionException;
16 import com.inversoft.verge.mvc.MVCConstants;
17 import com.inversoft.verge.mvc.MVCException;
18 import com.inversoft.verge.mvc.MVCRequest;
19 import com.inversoft.verge.mvc.config.BaseFormConfig;
20 import com.inversoft.verge.mvc.controller.Action;
21 import com.inversoft.verge.mvc.controller.form.config.FormConfig;
22 import com.inversoft.verge.mvc.controller.form.config.FormConfigStruct;
23 import com.inversoft.verge.mvc.controller.form.config.FormMVCConfigRegistry;
24 import com.inversoft.verge.mvc.controller.form.config.ValidatorConfig;
25 import com.inversoft.verge.mvc.controller.form.config.test.TestFormBean;
26 import com.inversoft.verge.mvc.model.DefaultModelHandler;
27 import com.inversoft.verge.mvc.model.ModelResolution;
28 import com.inversoft.verge.mvc.model.form.FormMetaData;
29 import com.inversoft.verge.mvc.validator.DefaultValidatorParser;
30 import com.inversoft.verge.mvc.view.jsp.form.test.FormTagTest;
31 import com.inversoft.verge.util.ScopeConstants;
32 import com.inversoft.verge.util.WebBean;
33
34
35 /**
36  * <p>
37  * This class contains the TestCases for the form model handler
38  * </p>
39  *
40  * @author Brian Pontarelli
41  * @since 2.0
42  * @version 2.0
43  */

44 public class FormModelHandlerTest extends JspTestCase {
45
46     /**
47      * Constructs a new <code>FormModelHandlerTest</code> TestCase instance
48      */

49     public FormModelHandlerTest(String JavaDoc name) {
50         super(name);
51         setLocal(true);
52     }
53
54
55     /**
56      * Tests the that model handler correctly gets the value.
57      */

58     public void testGet() {
59
60         Bean1 bean1 = new Bean1();
61         request.setAttribute("test", bean1);
62
63         MVCRequest mvcRequest = new MVCRequest(request, response);
64         BaseFormConfig base = new BaseFormConfig("test");
65         FormConfig config = new FormConfig(base);
66
67         try {
68             WebBean webBean = new WebBean("test", ScopeConstants.REQUEST_INT, Bean1.class);
69             config.addFormBean(webBean);
70             mvcRequest.setConfiguration(new FormConfigStruct(config, null, null));
71
72             DefaultModelHandler fmh = new DefaultModelHandler();
73             String JavaDoc definition = "test.string1";
74             Map JavaDoc map = new HashMap JavaDoc();
75             Object JavaDoc value;
76             FormMetaData md = new FormMetaData(definition);
77
78             bean1.setString1("foo");
79             value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean1, md), null);
80             assertTrue("Should have a value of foo", value.equals("foo"));
81
82             bean1.setString1("bar");
83             value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean1, md), map);
84             assertTrue("Should have a value of bar", value.equals("bar"));
85
86             // Test that nulls don't break it
87
value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean1, md), null);
88             assertTrue("Should have a value of bar", value.equals("bar"));
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91             fail(e.toString());
92         }
93     }
94
95     /**
96      * Tests the that model handler correctly gets the value using the view to
97      * setup the MVCRequest
98      */

99     public void testGetWithView() {
100
101         DefaultModelHandler fmh = new DefaultModelHandler();
102         String JavaDoc definition = "testFormBean.string1";
103         Map JavaDoc map = new HashMap JavaDoc();
104         Object JavaDoc value;
105
106         try {
107             FormTagTest viewSetup = new FormTagTest("testForm");
108             setupJspTestCase(viewSetup);
109             viewSetup.testForm();
110
111             // Fetch the form bean for the form used in the FormTagTest TestCase
112
FormConfig config = (FormConfig) FormMVCConfigRegistry.getInstance(request).lookupForm("form1");
113             TestFormBean bean = (TestFormBean) config.getFormBean("testFormBean").getInstance(request);
114             MVCRequest mvcRequest = (MVCRequest) request.getAttribute(MVCConstants.MVC_REQUEST_KEY);
115             FormMetaData md = new FormMetaData(definition);
116
117             bean.setString1("foo");
118             value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean, md), null);
119             assertTrue("Should have a value of foo", value.equals("foo"));
120
121             bean.setString1("bar");
122             value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean, md), map);
123             assertTrue("Should have a value of bar", value.equals("bar"));
124
125             // Test that nulls don't break it
126
value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean, md), null);
127             assertTrue("Should have a value of bar", value.equals("bar"));
128         } catch (MVCException e) {
129             fail(e.toString());
130         } catch (BeanException be) {
131             fail(be.toString());
132         }
133     }
134
135     /**
136      * Tests the that model handler correctly sets the value
137      */

138     public void testSet() {
139
140         MVCRequest mvcRequest = new MVCRequest(request, response);
141         DefaultModelHandler fmh = new DefaultModelHandler();
142         String JavaDoc definition = "test.string1";
143         Map JavaDoc map = new HashMap JavaDoc();
144
145         try {
146             FormMetaData md = new FormMetaData(definition);
147             BaseFormConfig base = new BaseFormConfig("test");
148             FormConfig config = new FormConfig(base);
149             WebBean webBean = new WebBean("test", ScopeConstants.REQUEST_INT, Bean1.class);
150             Bean1 bean1 = (Bean1) webBean.getInstance(request);
151             config.addFormBean(webBean);
152             mvcRequest.setConfiguration(new FormConfigStruct(config, null, null));
153
154             fmh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "foo", map);
155             //bean1 = (Bean1) request.getAttribute("test");
156
assertTrue("Should have bean1 in request", bean1 != null);
157             assertTrue("Should have value of foo", bean1.getString1().equals("foo"));
158
159             fmh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "bar", map);
160             assertTrue("Should have value of bar", bean1.getString1().equals("bar"));
161
162             // Make sure nulls don't break it
163
fmh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "bar", null);
164             assertTrue("Should have value of bar", bean1.getString1().equals("bar"));
165         } catch (Exception JavaDoc e) {
166             fail(e.toString());
167         }
168     }
169
170     /**
171      * Tests the that model handler correctly sets the value and calls the validator
172      */

173     public void testValidator() {
174
175         MVCRequest mvcRequest = new MVCRequest(request, response);
176         DefaultModelHandler fmh = new DefaultModelHandler();
177         String JavaDoc definition = "test.integer";
178         Map JavaDoc map = new HashMap JavaDoc();
179
180         try {
181             FormMetaData md = new FormMetaData(definition);
182             BaseFormConfig base = new BaseFormConfig("test");
183             FormConfig config = new FormConfig(base);
184             config.addValidatorConfig(new ValidatorConfig(TestValidator.class, null));
185             WebBean webBean = new WebBean("test", ScopeConstants.REQUEST_INT, Bean1.class);
186             Bean1 bean1 = (Bean1) webBean.getInstance(request);
187
188             config.addFormBean(webBean);
189             mvcRequest.setConfiguration(new FormConfigStruct(config, null, null));
190             mvcRequest.setAction(new Action("foo", request, response));
191             mvcRequest.setValidatorParser(new DefaultValidatorParser());
192             mvcRequest.addValidatorHandlerToCall(MVCConstants.FORM_NAME);
193
194             ModelResolution res = new ModelResolution(bean1, md);
195
196             TestValidator.conversion = false;
197             fmh.setValue(mvcRequest, definition, res, "notAnInteger", map);
198             assertTrue("Should have called validator", TestValidator.conversion);
199         } catch (MVCException e) {
200             fail(e.toString());
201         } catch (BeanException be) {
202             fail(be.toString());
203         }
204     }
205
206     /**
207      * Tests the that model handler correctly sets the value and tries to call
208      * the validator, but the validator doesn't exist so it throws an exception
209      */

210     public void testNoValidator() {
211
212         MVCRequest mvcRequest = new MVCRequest(request, response);
213         DefaultModelHandler fmh = new DefaultModelHandler();
214         String JavaDoc definition = "test.integer";
215         Map JavaDoc map = new HashMap JavaDoc();
216
217         try {
218             FormMetaData md = new FormMetaData(definition);
219             BaseFormConfig base = new BaseFormConfig("test");
220             FormConfig config = new FormConfig(base);
221             WebBean webBean = new WebBean("test", ScopeConstants.REQUEST_INT, Bean1.class);
222             Bean1 bean1 = (Bean1) webBean.getInstance(request);
223
224             config.addFormBean(webBean);
225             mvcRequest.setConfiguration(new FormConfigStruct(config, null, null));
226             mvcRequest.setValidatorParser(new DefaultValidatorParser());
227             mvcRequest.addValidatorHandlerToCall(MVCConstants.FORM_NAME);
228
229             fmh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "notAnInteger", map);
230             fail("Should have thrown a conversion exception wrapped in an MVCException");
231         } catch (Exception JavaDoc e) {
232             assertTrue("Should be an MVCException", e instanceof MVCException);
233             assertTrue("Should have cause of TCE", e.getCause() instanceof TypeConversionException);
234         }
235
236         // Do same test but pass in null for map
237
try {
238             FormMetaData md = new FormMetaData(definition);
239             BaseFormConfig base = new BaseFormConfig("test");
240             FormConfig config = new FormConfig(base);
241             WebBean webBean = new WebBean("test", ScopeConstants.REQUEST_INT, Bean1.class);
242             Bean1 bean1 = (Bean1) webBean.getInstance(request);
243
244             config.addFormBean(webBean);
245             mvcRequest.setConfiguration(new FormConfigStruct(config, null, null));
246
247             fmh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "notAnInteger", null);
248             fail("Should have thrown a conversion exception wrapped in an MVCException");
249         } catch (Exception JavaDoc e) {
250             assertTrue("Should be an MVCException", e instanceof MVCException);
251             assertTrue("Should have cause of TCE", e.getCause() instanceof TypeConversionException);
252         }
253     }
254 }
255
Popular Tags