KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > model > web > test > WebModelHandlerTest


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.web.test;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import com.inversoft.junit.WebTestCase;
14 import com.inversoft.util.typeconverter.TypeConversionException;
15 import com.inversoft.verge.mvc.MVCException;
16 import com.inversoft.verge.mvc.MVCRequest;
17 import com.inversoft.verge.mvc.controller.Action;
18 import com.inversoft.verge.mvc.model.ModelResolution;
19 import com.inversoft.verge.mvc.model.web.WebMetaData;
20 import com.inversoft.verge.mvc.model.web.WebModelHandler;
21 import com.inversoft.verge.mvc.validator.DefaultValidatorParser;
22 import com.inversoft.verge.util.ScopeConstants;
23
24
25 /**
26  * <p>
27  * This class contains the TestCases for the web model handler
28  * </p>
29  *
30  * @author Brian Pontarelli
31  * @since 2.0
32  * @version 2.0
33  */

34 public class WebModelHandlerTest extends WebTestCase {
35
36     /**
37      * Constructs a new <code>WebModelHandlerTest</code> TestCase instance
38      */

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

48     public void testGet() {
49
50         Bean1 bean1 = new Bean1();
51         request.getSession().setAttribute("test", bean1);
52
53         WebModelHandler wmh = new WebModelHandler();
54         String JavaDoc definition = "test.string1";
55
56         Map JavaDoc params = new HashMap JavaDoc();
57         params.put(WebMetaData.CLASS_PARAMETER,
58             "com.inversoft.verge.mvc.model.web.test.Bean1");
59         params.put(WebMetaData.SCOPE_PARAMETER,
60             "" + ScopeConstants.SESSION_INT);
61         MVCRequest mvcRequest = new MVCRequest(request, response);
62                 
63         try {
64             Object JavaDoc value;
65             
66             // Test that the session object is changed, which should be the case
67
// since the params is setup that way and the md is null in the resolution
68
bean1.setString1("foo");
69             value = wmh.getValue(mvcRequest, definition, new ModelResolution(bean1, null), params);
70             assertTrue("Should have a value of foo", value.equals("foo"));
71
72             bean1.setString1("bar");
73             value = wmh.getValue(mvcRequest, definition, new ModelResolution(bean1, null), params);
74             assertTrue("Should have a value of bar", value.equals("bar"));
75
76             // Test that the request object is changed, which should be the case
77
// since the params is null and the md is null in the resolution
78
Bean1 bean2 = new Bean1();
79             request.setAttribute("test", bean2);
80             bean2.setString1("foo");
81             value = wmh.getValue(mvcRequest, definition, new ModelResolution(bean1, null), null);
82             assertTrue("Should have a value of foo", value.equals("foo"));
83
84             bean2.setString1("bar");
85             value = wmh.getValue(mvcRequest, definition, new ModelResolution(bean1, null), null);
86             assertTrue("Should have a value of bar", value.equals("bar"));
87
88             // Run the same tests with the meta data in this time
89
bean1.setString1("foo");
90             value = wmh.getValue(mvcRequest, definition,
91                 new ModelResolution(bean1, new WebMetaData(definition, params)),
92                 params);
93             assertTrue("Should have a value of foo", value.equals("foo"));
94
95             bean1.setString1("bar");
96             value = wmh.getValue(mvcRequest, definition,
97                 new ModelResolution(bean1, new WebMetaData(definition, params)),
98                 params);
99             assertTrue("Should have a value of bar", value.equals("bar"));
100         } catch (Exception JavaDoc e) {
101             e.printStackTrace();
102             fail(e.toString());
103         }
104     }
105
106     /**
107      * Tests the that model handler correctly sets the value
108      */

109     public void testSet() {
110
111         Bean1 bean1 = new Bean1();
112         request.getSession().setAttribute("test", bean1);
113
114         WebModelHandler wmh = new WebModelHandler();
115         String JavaDoc definition = "test.string1";
116
117         Map JavaDoc params = new HashMap JavaDoc();
118         params.put(WebMetaData.CLASS_PARAMETER,
119             "com.inversoft.verge.mvc.model.web.test.Bean1");
120         params.put(WebMetaData.SCOPE_PARAMETER,
121             "" + ScopeConstants.SESSION_INT);
122         MVCRequest mvcRequest = new MVCRequest(request, response);
123
124         try {
125             wmh.setValue(mvcRequest, definition, new ModelResolution(bean1, null), "foo", params);
126             assertTrue("Should have value of foo", bean1.getString1().equals("foo"));
127
128             wmh.setValue(mvcRequest, definition, new ModelResolution(bean1, null), "bar", params);
129             assertTrue("Should have value of bar", bean1.getString1().equals("bar"));
130
131             // Do same tests with the meta data in the map
132
wmh.setValue(mvcRequest, definition,
133                     new ModelResolution(bean1, new WebMetaData(definition, params)),
134                     "foo", params);
135             assertTrue("Should have value of foo", bean1.getString1().equals("foo"));
136
137             wmh.setValue(mvcRequest, definition,
138                     new ModelResolution(bean1, new WebMetaData(definition, params)),
139                     "bar", params);
140             assertTrue("Should have value of bar", bean1.getString1().equals("bar"));
141         } catch (Exception JavaDoc e) {
142             fail(e.toString());
143         }
144     }
145
146     /**
147      * Tests the that model handler correctly sets the value and calls the validator
148      */

149     public void testValidator() {
150
151         Bean1 bean1 = new Bean1();
152         request.getSession().setAttribute("test", bean1);
153
154         WebModelHandler wmh = new WebModelHandler();
155         String JavaDoc definition = "test.integer";
156
157         Map JavaDoc params = new HashMap JavaDoc();
158         params.put(WebMetaData.CLASS_PARAMETER,
159             "com.inversoft.verge.mvc.model.web.test.Bean1");
160         params.put(WebMetaData.SCOPE_PARAMETER,
161             "" + ScopeConstants.SESSION_INT);
162         MVCRequest mvcRequest = new MVCRequest(request, response);
163         mvcRequest.addValidator(new TestValidator());
164         mvcRequest.setAction(new Action("foo", request, response));
165         mvcRequest.setValidatorParser(new DefaultValidatorParser());
166
167         try {
168             ModelResolution res = new ModelResolution(bean1, null);
169
170             TestValidator.conversion = false;
171             wmh.setValue(mvcRequest, definition, res, "notAnInteger", params);
172             assertTrue("Should have called validator", TestValidator.conversion);
173
174             // Do same tests with the meta data in the res
175
res.setMetaData(new WebMetaData(definition, params));
176             wmh.setValue(mvcRequest, definition, res, "notAnInteger", params);
177             assertTrue("Should have called validator", TestValidator.conversion);
178         } catch (Exception JavaDoc e) {
179             e.printStackTrace();
180             fail(e.toString());
181         }
182     }
183
184     /**
185      * Tests the that model handler correctly sets the value and tries to call
186      * the validator, but the validator doesn't exist so it throws an exception
187      */

188     public void testNoValidator() {
189
190         Bean1 bean1 = new Bean1();
191         request.getSession().setAttribute("test", bean1);
192
193         WebModelHandler wmh = new WebModelHandler();
194         String JavaDoc definition = "test.integer";
195
196         Map JavaDoc params = new HashMap JavaDoc();
197         params.put(WebMetaData.CLASS_PARAMETER,
198             "com.inversoft.verge.mvc.model.web.test.Bean1");
199         params.put(WebMetaData.SCOPE_PARAMETER,
200             "" + ScopeConstants.SESSION_INT);
201         MVCRequest mvcRequest = new MVCRequest(request, response);
202         mvcRequest.setValidatorParser(new DefaultValidatorParser());
203         
204         try {
205             wmh.setValue(mvcRequest, definition,
206                 new ModelResolution(bean1, null), "notAnInteger", params);
207             fail("Should have thrown a MVCException");
208         } catch (Exception JavaDoc e) {
209             assertTrue("Should be an MVCException", e instanceof MVCException);
210             assertTrue("Should have cause of TCE", e.getCause() instanceof TypeConversionException);
211         }
212
213         try {
214             // Do same tests with the meta data in the res
215
wmh.setValue(mvcRequest, definition,
216                 new ModelResolution(bean1, new WebMetaData(definition, params)),
217                 "notAnInteger", params);
218             fail("Should have thrown a MVCException");
219         } catch (Exception JavaDoc e) {
220             assertTrue("Should be an MVCException", e instanceof MVCException);
221             assertTrue("Should have cause of TCE", e.getCause() instanceof TypeConversionException);
222         }
223     }
224 }
225
Popular Tags