1 7 package com.inversoft.verge.mvc.test; 8 9 10 import javax.servlet.ServletException ; 11 12 import com.inversoft.error.BasicError; 13 import com.inversoft.junit.Request; 14 import com.inversoft.verge.mvc.servlet.MVCServlet; 15 import com.inversoft.verge.mvc.junit.VergeMVCTestCase; 16 import com.inversoft.verge.util.RequestContext; 17 18 19 27 public class FormMVCTest extends VergeMVCTestCase { 28 29 34 public FormMVCTest(String name) { 35 super(name); 36 setLocal(true); 37 } 38 39 40 public void beginFormBasedFailureMapping(Request request) { 41 addFBMVCInput(request, "customer", "firstName", "input1", "Brian"); 42 addFBMVCInput(request, "customer", "lastName", "input2", "Pontarelli"); 43 addFBMVCInput(request, "customer", "age", "input3", "bad"); 44 addFBMVCInput(request, "customer", "address.city", "input4", "Boulder"); 45 addFBMVCInput(request, "customer", "address.state", "input5", "Colorado"); 46 addFBMVCInput(request, "customer", "address.address1", "input6", "4019 Wonderland Hill Ave."); 47 addFBMVCInput(request, "customer", "address.zipcode", "input7", "80304"); 48 addFBMVCSubmit(request, "register", "submit", "37", true); 49 addFBMVCForm(request, "registration"); 50 } 51 52 public void testFormBasedFailureMapping() { 53 setup("src/com/inversoft/verge/mvc/test/test-form-mvc.xml"); 54 55 UserValidator.handled = false; 56 UserValidator.validated = false; 57 UserRegistrationActionHandler.called = false; 58 MVCServlet ms = new MVCServlet(); 59 try { 60 ms.init(createConfig("MVCServlet")); 61 ms.doGet(request, response); 62 } catch (ServletException se) { 63 fail(se.toString()); 64 } 65 66 assertTrue(UserValidator.handled); 67 assertTrue(UserValidator.validated); 68 assertFalse(UserRegistrationActionHandler.called); 69 70 if (isLocal()) { 71 assertEquals("Request should be at /registration.jsp", "/registration.jsp", 72 getRequest().getRequestDispatcher().getURL()); 73 } 74 75 Customer customer = (Customer) request.getSession().getAttribute("customer"); 76 assertEquals("Brian", customer.getFirstName()); 77 assertEquals("Pontarelli", customer.getLastName()); 78 assertEquals("Boulder", customer.getAddress().getCity()); 79 assertEquals("Colorado", customer.getAddress().getState()); 80 assertEquals("4019 Wonderland Hill Ave.", customer.getAddress().getAddress1()); 81 assertEquals("80304", customer.getAddress().getZipcode()); 82 } 83 84 public void beginFormBasedSuccess(Request request) { 85 addFBMVCInput(request, "customer", "firstName", "input1", "Brian"); 86 addFBMVCInput(request, "customer", "lastName", "input2", "Pontarelli"); 87 addFBMVCInput(request, "customer", "age", "input3", "31"); 88 addFBMVCInput(request, "customer", "address.city", "input4", "Boulder"); 89 addFBMVCInput(request, "customer", "address.state", "input5", "Colorado"); 90 addFBMVCInput(request, "customer", "address.address1", "input6", "4019 Wonderland Hill Ave."); 91 addFBMVCInput(request, "customer", "address.zipcode", "input7", "80304"); 92 addFBMVCSubmit(request, "register", "submit", "37", true); 93 addFBMVCForm(request, "registration"); 94 } 95 96 public void testFormBasedSuccess() { 97 setup("src/com/inversoft/verge/mvc/test/test-form-mvc.xml"); 98 99 UserValidator.handled = false; 100 UserValidator.validated = false; 101 UserRegistrationActionHandler.called = false; 102 MVCServlet ms = new MVCServlet(); 103 try { 104 ms.init(createConfig("MVCServlet")); 105 ms.doGet(request, response); 106 } catch (ServletException se) { 107 fail(se.toString()); 108 } 109 110 assertFalse(UserValidator.handled); 111 assertTrue(UserValidator.validated); 112 assertTrue(UserRegistrationActionHandler.called); 113 114 if (isLocal()) { 115 assertEquals("Request should be at /home.jsp", "/home.jsp", 116 getRequest().getRequestDispatcher().getURL()); 117 } 118 119 Customer customer = (Customer) request.getSession().getAttribute("customer"); 120 assertEquals("Brian", customer.getFirstName()); 121 assertEquals("Pontarelli", customer.getLastName()); 122 assertEquals(31, customer.getAge()); 123 assertEquals("Boulder", customer.getAddress().getCity()); 124 assertEquals("Colorado", customer.getAddress().getState()); 125 assertEquals("4019 Wonderland Hill Ave.", customer.getAddress().getAddress1()); 126 assertEquals("80304", customer.getAddress().getZipcode()); 127 } 128 129 public void beginFormBasedValidation(Request request) { 130 addFBMVCInput(request, "customer", "firstName", "input1", "Brian"); 131 addFBMVCInput(request, "customer", "lastName", "input2", "Pontarelli"); 132 addFBMVCInput(request, "customer", "age", "input3", "1"); 133 addFBMVCInput(request, "customer", "phone", "input4", "909-34-2"); 134 addFBMVCSubmit(request, "register", "submit", "37", true); 135 addFBMVCForm(request, "validate"); 136 } 137 138 public void testFormBasedValidation() { 139 setup("src/com/inversoft/verge/mvc/test/test-form-mvc.xml"); 140 141 UserValidator.handled = false; 142 UserValidator.validated = false; 143 UserRegistrationActionHandler.called = false; 144 MVCServlet ms = new MVCServlet(); 145 try { 146 ms.init(createConfig("MVCServlet")); 147 ms.doGet(request, response); 148 } catch (ServletException se) { 149 se.printStackTrace(); 150 fail(se.toString()); 151 } 152 153 assertFalse(UserValidator.handled); 154 assertFalse(UserValidator.validated); 155 assertFalse(UserRegistrationActionHandler.called); 156 157 if (isLocal()) { 158 assertEquals("Request should be at /registration.jsp", "/registration.jsp", 159 getRequest().getRequestDispatcher().getURL()); 160 } 161 162 Customer customer = (Customer) request.getSession().getAttribute("customer"); 163 assertEquals("Brian", customer.getFirstName()); 164 assertEquals("Pontarelli", customer.getLastName()); 165 166 RequestContext context = new RequestContext(request); 167 assertTrue(context.hasErrors()); 168 assertEquals(2, context.getAllErrors().size()); 169 assertEquals("Invalid age", ((BasicError) context.getPropertyErrors("customer.age").get(0)).getMessage()); 170 assertEquals("Invalid phone", ((BasicError) context.getPropertyErrors("customer.phone").get(0)).getMessage()); 171 } 172 173 public void beginFormBasedPrimitiveValidation(Request request) { 174 addFBMVCInput(request, "customer", "age", "input3", "bob"); 175 addFBMVCSubmit(request, "register", "submit", "37", true); 176 addFBMVCForm(request, "validatePrimitive"); 177 } 178 179 public void testFormBasedPrimitiveValidation() { 180 setup("src/com/inversoft/verge/mvc/test/test-form-mvc.xml"); 181 182 UserValidator.handled = false; 183 UserValidator.validated = false; 184 UserRegistrationActionHandler.called = false; 185 MVCServlet ms = new MVCServlet(); 186 try { 187 ms.init(createConfig("MVCServlet")); 188 ms.doGet(request, response); 189 } catch (ServletException se) { 190 se.printStackTrace(); 191 fail(se.toString()); 192 } 193 194 assertFalse(UserValidator.handled); 195 assertFalse(UserValidator.validated); 196 assertFalse(UserRegistrationActionHandler.called); 197 198 if (isLocal()) { 199 assertEquals("Request should be at /registration.jsp", "/registration.jsp", 200 getRequest().getRequestDispatcher().getURL()); 201 } 202 203 RequestContext context = new RequestContext(request); 204 assertTrue(context.hasErrors()); 205 assertEquals(1, context.getAllErrors().size()); 206 assertEquals("Invalid age", 207 ((BasicError) context.getPropertyErrors("customer.age").get(0)).getMessage()); 208 } 209 210 public void beginFormBasedLongTxn(Request request) { 211 addFBMVCSubmit(request, "formBasedLongTxn", "submit", "37", true); 212 addFBMVCForm(request, "longTxn"); 213 } 214 215 public void testFormBasedLongTxn() { 216 if (isLocal()) { 217 setup("src/com/inversoft/verge/mvc/test/test-form-mvc.xml"); 218 219 MVCServlet ms = new MVCServlet(); 220 try { 221 ms.init(createConfig("MVCServlet")); 222 ms.doGet(request, response); 223 } catch (ServletException se) { 224 se.printStackTrace(); 225 fail(se.toString()); 226 } 227 228 assertEquals("Request should be at /longTxnStart.jsp", "/longTxnStart.jsp", 230 getRequest().getRequestDispatcher().getURL()); 231 232 String url = getResponse().getText(); 233 assertTrue("Should contain the result mapping", 234 url.indexOf("/home.jsp") > 1); 235 } 236 } 237 238 public void beginGlobalsWithForm(Request request) { 239 addFBMVCInput(request, "customer", "firstName", "input1", "Brian"); 240 addFBMVCSubmit(request, "globalAction1", "submit", "37", true); 241 addFBMVCForm(request, "noValidation"); 242 } 243 244 public void testGlobalsWithForm() { 245 setup("src/com/inversoft/verge/mvc/test/test-form-mvc.xml"); 246 247 MVCServlet ms = new MVCServlet(); 248 try { 249 ms.init(createConfig("MVCServlet")); 250 ms.doGet(request, response); 251 } catch (ServletException se) { 252 se.printStackTrace(); 253 fail(se.toString()); 254 } 255 256 if (isLocal()) { 258 assertEquals("/global/testMapping1.jsp", 259 getRequest().getRequestDispatcher().getURL()); 260 } 261 } 262 263 public void beginGlobalsWithoutForm(Request request) { 264 addFBMVCURL(request, null, null); 265 addFBMVCSubmit(request, "globalAction1", "submit", "37", true); 266 } 267 268 public void testGlobalsWithoutForm() { 269 setup("src/com/inversoft/verge/mvc/test/test-form-mvc.xml"); 270 271 MVCServlet ms = new MVCServlet(); 272 try { 273 ms.init(createConfig("MVCServlet")); 274 ms.doGet(request, response); 275 } catch (ServletException se) { 276 se.printStackTrace(); 277 fail(se.toString()); 278 } 279 280 if (isLocal()) { 282 assertEquals("/global/testMapping1.jsp", 283 getRequest().getRequestDispatcher().getURL()); 284 } 285 } 286 } | Popular Tags |