1 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 36 public class ActionFlowModelHandlerTest extends WebTestCase { 37 38 41 public ActionFlowModelHandlerTest(String name) { 42 super(name); 43 setLocal(true); 44 } 45 46 47 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 definition = "test.string1"; 65 ActionFlowModelMetaData md = new ActionFlowModelMetaData(definition); 66 67 Object value; 68 69 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 e) { 78 e.printStackTrace(); 79 fail(e.toString()); 80 } 81 } 82 83 123 124 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 definition = "test.string1"; 142 ActionFlowModelMetaData md = new ActionFlowModelMetaData(definition); 143 144 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 e) { 153 e.printStackTrace(); 154 fail(e.toString()); 155 } 156 } 157 158 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 definition = "test.integer"; 181 ActionFlowModelMetaData md = new ActionFlowModelMetaData(definition); 182 TestValidator.conversion = false; 183 184 mh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "NotANumber", null); 186 assertTrue("Should have called validator", TestValidator.conversion); 187 } catch (Exception e) { 188 e.printStackTrace(); 189 fail(e.toString()); 190 } 191 } 192 193 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 definition = "test.integer"; 216 ActionFlowModelMetaData md = new ActionFlowModelMetaData(definition); 217 TestValidator.conversion = false; 218 219 mh.setValue(mvcRequest, definition, new ModelResolution(bean1, md), "NotANumber", null); 221 } catch (Exception 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 |