1 7 package com.inversoft.verge.mvc.model.web.test; 8 9 10 import java.util.HashMap ; 11 import java.util.Map ; 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 34 public class WebModelHandlerTest extends WebTestCase { 35 36 39 public WebModelHandlerTest(String name) { 40 super(name); 41 setLocal(true); 42 } 43 44 45 48 public void testGet() { 49 50 Bean1 bean1 = new Bean1(); 51 request.getSession().setAttribute("test", bean1); 52 53 WebModelHandler wmh = new WebModelHandler(); 54 String definition = "test.string1"; 55 56 Map params = new HashMap (); 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 value; 65 66 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 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 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 e) { 101 e.printStackTrace(); 102 fail(e.toString()); 103 } 104 } 105 106 109 public void testSet() { 110 111 Bean1 bean1 = new Bean1(); 112 request.getSession().setAttribute("test", bean1); 113 114 WebModelHandler wmh = new WebModelHandler(); 115 String definition = "test.string1"; 116 117 Map params = new HashMap (); 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 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 e) { 142 fail(e.toString()); 143 } 144 } 145 146 149 public void testValidator() { 150 151 Bean1 bean1 = new Bean1(); 152 request.getSession().setAttribute("test", bean1); 153 154 WebModelHandler wmh = new WebModelHandler(); 155 String definition = "test.integer"; 156 157 Map params = new HashMap (); 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 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 e) { 179 e.printStackTrace(); 180 fail(e.toString()); 181 } 182 } 183 184 188 public void testNoValidator() { 189 190 Bean1 bean1 = new Bean1(); 191 request.getSession().setAttribute("test", bean1); 192 193 WebModelHandler wmh = new WebModelHandler(); 194 String definition = "test.integer"; 195 196 Map params = new HashMap (); 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 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 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 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 |