KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > model > actionflow > test > ActionFlowModelHandlerTest


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.actionflow.test;
8
9
10 import com.inversoft.junit.WebTestCase;
11 import com.inversoft.util.typeconverter.TypeConversionException;
12 import com.inversoft.verge.mvc.MVCConstants;
13 import com.inversoft.verge.mvc.MVCException;
14 import com.inversoft.verge.mvc.MVCRequest;
15 import com.inversoft.verge.mvc.config.BaseFormConfig;
16 import com.inversoft.verge.mvc.config.BaseValidatorConfig;
17 import com.inversoft.verge.mvc.controller.Action;
18 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigStruct;
19 import com.inversoft.verge.mvc.model.DefaultModelHandler;
20 import com.inversoft.verge.mvc.model.ModelResolution;
21 import com.inversoft.verge.mvc.model.actionflow.ActionFlowModelMetaData;
22 import com.inversoft.verge.mvc.validator.DefaultValidatorParser;
23 import com.inversoft.verge.util.ScopeConstants;
24 import com.inversoft.verge.util.WebBean;
25
26
27 /**
28  * <p>
29  * This class contains the TestCases for the action flow model handler
30  * </p>
31  *
32  * @author Brian Pontarelli
33  * @since 2.0
34  * @version 2.0
35  */

36 public class ActionFlowModelHandlerTest extends WebTestCase {
37
38     /**
39      * Constructs a new <code>ActionFlowModelHandlerTest</code> TestCase instance
40      */

41     public ActionFlowModelHandlerTest(String JavaDoc name) {
42         super(name);
43         setLocal(true);
44     }
45
46
47     /**
48      * Tests the that model handler correctly gets the value.
49      */

50     public void testGet() {
51
52         Bean1 bean1 = new Bean1();
53         request.getSession().setAttribute("test", bean1);
54
55         MVCRequest mvcRequest = new MVCRequest(request, response);
56         BaseFormConfig base = new BaseFormConfig("test");
57
58         try {
59             WebBean webBean = new WebBean("test", ScopeConstants.SESSION_INT, Bean1.class);
60             base.addFormBean(webBean);
61             mvcRequest.setConfiguration(new ActionFlowConfigStruct(base, null));
62
63             DefaultModelHandler mh = new DefaultModelHandler();
64             String JavaDoc definition = "test.string1";
65             ActionFlowModelMetaData md = new ActionFlowModelMetaData(definition);
66
67             Object JavaDoc value;
68             
69             // Test that the session object is changed
70
bean1.setString1("foo");
71             value = mh.getValue(mvcRequest, definition, new ModelResolution(bean1, md), null);
72             assertEquals("Should have a value of foo", "foo", value);
73
74             bean1.setString1("bar");
75             value = mh.getValue(mvcRequest, definition, new ModelResolution(bean1, md), null);
76             assertEquals("Should have a value of bar", "bar", value);
77         } catch (Exception JavaDoc e) {
78             e.printStackTrace();
79             fail(e.toString());
80         }
81     }
82
83     /**
84      * Tests the that model handler correctly gets the value using the view to
85      * setup the MVCRequest
86     public void testGetWithView() {
87
88         FormModelHandler fmh = new FormModelHandler();
89         String definition = "testFormBean.string1";
90         Map map = new HashMap();
91         Object value;
92
93
94         try {
95             FormTagTest viewSetup = new FormTagTest("testForm");
96             setupJspTestCase(viewSetup);
97             viewSetup.testForm();
98
99             // Fetch the form bean for the form used in the FormTagTest TestCase
100             FormConfig config = (FormConfig) FormMVCConfigRegistry.getInstance(request).lookupForm("form1");
101             TestFormBean bean = (TestFormBean) config.getFormBean("testFormBean").getInstance(request);
102             MVCRequest mvcRequest = (MVCRequest) request.getAttribute(MVCConstants.MVC_REQUEST_KEY);
103             FormMetaData md = new FormMetaData(definition);
104
105             bean.setString1("foo");
106             value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean, md), null);
107             assertTrue("Should have a value of foo", value.equals("foo"));
108
109             bean.setString1("bar");
110             value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean, md), map);
111             assertTrue("Should have a value of bar", value.equals("bar"));
112
113             // Test that nulls don't break it
114             value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean, md), null);
115             assertTrue("Should have a value of bar", value.equals("bar"));
116         } catch (MVCException e) {
117             fail(e.toString());
118         } catch (BeanException be) {
119             fail(be.toString());
120         }
121     }
122      */

123
124     /**
125      * Tests the that model handler correctly sets the value
126      */

127     public void testSet() {
128
129         Bean1 bean1 = new Bean1();
130         request.getSession().setAttribute("test", bean1);
131
132         MVCRequest mvcRequest = new MVCRequest(request, response);
133         BaseFormConfig base = new BaseFormConfig("test");
134
135         try {
136             WebBean webBean = new WebBean("test", ScopeConstants.SESSION_INT, Bean1.class);
137             base.addFormBean(webBean);
138             mvcRequest.setConfiguration(new ActionFlowConfigStruct(base, null));
139
140             DefaultModelHandler mh = new DefaultModelHandler();
141             String JavaDoc definition = "test.string1";
142             ActionFlowModelMetaData md = new ActionFlowModelMetaData(definition);
143
144             // Test that the session object is changed
145
bean1.setString1("foo");
146             mh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "bar", null);
147             assertEquals("Should have a value of bar", "bar", bean1.getString1());
148
149             bean1.setString1("bob");
150             mh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "sally", null);
151             assertEquals("Should have a value of sally", "sally", bean1.getString1());
152         } catch (Exception JavaDoc e) {
153             e.printStackTrace();
154             fail(e.toString());
155         }
156     }
157
158     /**
159      * Tests the that model handler correctly sets the value and calls the validator
160      */

161     public void testValidator() {
162
163         Bean1 bean1 = new Bean1();
164         request.getSession().setAttribute("test", bean1);
165
166         MVCRequest mvcRequest = new MVCRequest(request, response);
167         BaseFormConfig base = new BaseFormConfig("test");
168
169         try {
170             WebBean webBean = new WebBean("test", ScopeConstants.SESSION_INT, Bean1.class);
171             base.addFormBean(webBean);
172             base.addValidatorConfig(new BaseValidatorConfig(TestValidator.class, null));
173
174             mvcRequest.setConfiguration(new ActionFlowConfigStruct(base, null));
175             mvcRequest.setAction(new Action("foo", request, response));
176             mvcRequest.setValidatorParser(new DefaultValidatorParser());
177             mvcRequest.addValidatorHandlerToCall(MVCConstants.ACTIONFLOW_NAME);
178
179             DefaultModelHandler mh = new DefaultModelHandler();
180             String JavaDoc definition = "test.integer";
181             ActionFlowModelMetaData md = new ActionFlowModelMetaData(definition);
182             TestValidator.conversion = false;
183
184             // Test that the session object is changed
185
mh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "NotANumber", null);
186             assertTrue("Should have called validator", TestValidator.conversion);
187         } catch (Exception JavaDoc e) {
188             e.printStackTrace();
189             fail(e.toString());
190         }
191     }
192
193     /**
194      * Tests the that model handler correctly sets the value and tries to call
195      * the validator, but the validator doesn't exist so it throws an exception
196      */

197     public void testNoValidator() {
198
199         Bean1 bean1 = new Bean1();
200         request.getSession().setAttribute("test", bean1);
201
202         MVCRequest mvcRequest = new MVCRequest(request, response);
203         BaseFormConfig base = new BaseFormConfig("test");
204
205         try {
206             WebBean webBean = new WebBean("test", ScopeConstants.SESSION_INT, Bean1.class);
207             base.addFormBean(webBean);
208
209             mvcRequest.setConfiguration(new ActionFlowConfigStruct(base, null));
210             mvcRequest.setAction(new Action("foo", request, response));
211             mvcRequest.setValidatorParser(new DefaultValidatorParser());
212             mvcRequest.addValidatorHandlerToCall(MVCConstants.ACTIONFLOW_NAME);
213
214             DefaultModelHandler mh = new DefaultModelHandler();
215             String JavaDoc definition = "test.integer";
216             ActionFlowModelMetaData md = new ActionFlowModelMetaData(definition);
217             TestValidator.conversion = false;
218
219             // Test that the session object is changed
220
mh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "NotANumber", null);
221         } catch (Exception JavaDoc e) {
222             assertTrue("Should be an MVCException", e instanceof MVCException);
223             assertTrue("Should have cause of TCE", e.getCause() instanceof TypeConversionException);
224         }
225     }
226 }
227
Popular Tags