1 7 package com.inversoft.verge.mvc.model.form.test; 8 9 10 import java.util.HashMap ; 11 import java.util.Map ; 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 44 public class FormModelHandlerTest extends JspTestCase { 45 46 49 public FormModelHandlerTest(String name) { 50 super(name); 51 setLocal(true); 52 } 53 54 55 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 definition = "test.string1"; 74 Map map = new HashMap (); 75 Object 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 value = fmh.getValue(mvcRequest, definition, new ModelResolution(bean1, md), null); 88 assertTrue("Should have a value of bar", value.equals("bar")); 89 } catch (Exception e) { 90 e.printStackTrace(); 91 fail(e.toString()); 92 } 93 } 94 95 99 public void testGetWithView() { 100 101 DefaultModelHandler fmh = new DefaultModelHandler(); 102 String definition = "testFormBean.string1"; 103 Map map = new HashMap (); 104 Object value; 105 106 try { 107 FormTagTest viewSetup = new FormTagTest("testForm"); 108 setupJspTestCase(viewSetup); 109 viewSetup.testForm(); 110 111 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 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 138 public void testSet() { 139 140 MVCRequest mvcRequest = new MVCRequest(request, response); 141 DefaultModelHandler fmh = new DefaultModelHandler(); 142 String definition = "test.string1"; 143 Map map = new HashMap (); 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 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 fmh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "bar", null); 164 assertTrue("Should have value of bar", bean1.getString1().equals("bar")); 165 } catch (Exception e) { 166 fail(e.toString()); 167 } 168 } 169 170 173 public void testValidator() { 174 175 MVCRequest mvcRequest = new MVCRequest(request, response); 176 DefaultModelHandler fmh = new DefaultModelHandler(); 177 String definition = "test.integer"; 178 Map map = new HashMap (); 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 210 public void testNoValidator() { 211 212 MVCRequest mvcRequest = new MVCRequest(request, response); 213 DefaultModelHandler fmh = new DefaultModelHandler(); 214 String definition = "test.integer"; 215 Map map = new HashMap (); 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 e) { 232 assertTrue("Should be an MVCException", e instanceof MVCException); 233 assertTrue("Should have cause of TCE", e.getCause() instanceof TypeConversionException); 234 } 235 236 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 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 |