1 18 19 20 package org.apache.struts.mock; 21 22 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 27 import org.apache.struts.Globals; 28 import org.apache.struts.action.ActionFormBean; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.struts.config.ModuleConfig; 32 import org.apache.struts.config.ControllerConfig; 33 import org.apache.struts.config.FormPropertyConfig; 34 import org.apache.struts.config.ForwardConfig; 35 import org.apache.struts.config.ModuleConfigFactory; 36 37 38 39 50 51 public class TestMockBase extends TestCase { 52 53 54 56 57 public TestMockBase(String name) { 58 super(name); 59 } 60 61 62 public static void main(String args[]) { 63 junit.awtui.TestRunner.main 64 (new String [] { TestMockBase.class.getName() } ); 65 } 66 67 68 public static Test suite() { 69 return (new TestSuite(TestMockBase.class)); 70 } 71 72 73 75 76 protected ModuleConfig moduleConfig = null; 77 protected ModuleConfig moduleConfig2 = null; 78 protected ModuleConfig moduleConfig3 = null; 79 protected MockServletConfig config = null; 80 protected MockServletContext context = null; 81 protected MockPageContext page = null; 82 protected MockPrincipal principal = null; 83 protected MockHttpServletRequest request = null; 84 protected MockHttpServletResponse response = null; 85 protected MockHttpSession session = null; 86 87 88 90 91 public void setUp() { 92 93 context = new MockServletContext(); 95 config = new MockServletConfig(context); 96 session = new MockHttpSession(context); 97 request = new MockHttpServletRequest(session); 98 principal = new MockPrincipal("username", 99 new String [] { "admin", "manager" }); 100 request.setUserPrincipal(principal); 101 response = new MockHttpServletResponse(); 102 page = new MockPageContext(config, request, response); 103 104 setUpDefaultApp(); 106 setUpSecondApp(); 107 setUpThirdApp(); 108 109 115 } 116 117 118 protected void setUpDefaultApp() { 119 120 ActionFormBean formBean = null; 121 ActionMapping mapping = null; 122 123 ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); 124 moduleConfig = factoryObject.createModuleConfig(""); 125 126 context.setAttribute(Globals.MODULE_KEY, moduleConfig); 127 128 moduleConfig.addForwardConfig 130 (new ActionForward("external", "http://jakarta.apache.org/", 131 false, false)); 132 133 moduleConfig.addForwardConfig 135 (new ActionForward("foo", "/bar.jsp", false, false)); 136 137 moduleConfig.addForwardConfig 139 (new ActionForward("relative1", "relative.jsp", false, false)); 140 141 moduleConfig.addForwardConfig 143 (new ActionForward("relative2", "relative.jsp", false, true)); 144 145 formBean = new ActionFormBean 147 ("static", 148 "org.apache.struts.mock.MockFormBean"); 149 moduleConfig.addFormBeanConfig(formBean); 150 151 mapping = new ActionMapping(); 153 mapping.setInput("/static.jsp"); 154 mapping.setName("static"); 155 mapping.setPath("/static"); 156 mapping.setScope("request"); 157 mapping.setType("org.apache.struts.mock.MockAction"); 158 moduleConfig.addActionConfig(mapping); 159 160 formBean = new ActionFormBean 162 ("dynamic", 163 "org.apache.struts.action.DynaActionForm"); 164 formBean.addFormPropertyConfig 165 (new FormPropertyConfig("booleanProperty", "boolean", "false")); 166 formBean.addFormPropertyConfig 167 (new FormPropertyConfig("stringProperty", "java.lang.String", 168 null)); 169 moduleConfig.addFormBeanConfig(formBean); 170 171 mapping = new ActionMapping(); 173 mapping.setInput("/dynamic.jsp"); 174 mapping.setName("dynamic"); 175 mapping.setPath("/dynamic"); 176 mapping.setScope("session"); 177 mapping.setType("org.apache.struts.mock.MockAction"); 178 moduleConfig.addActionConfig(mapping); 179 180 formBean = new ActionFormBean 182 ("dynamic0", 183 "org.apache.struts.action.DynaActionForm"); 184 formBean.addFormPropertyConfig 185 (new FormPropertyConfig("booleanProperty", "boolean", "true")); 186 formBean.addFormPropertyConfig 187 (new FormPropertyConfig("stringProperty", "java.lang.String", 188 "String Property")); 189 formBean.addFormPropertyConfig 190 (new FormPropertyConfig("intArray1", "int[]", 191 "{1,2,3}", 4)); formBean.addFormPropertyConfig 193 (new FormPropertyConfig("intArray2", "int[]", 194 null, 5)); formBean.addFormPropertyConfig 196 (new FormPropertyConfig("principal", 197 "org.apache.struts.mock.MockPrincipal", 198 null)); 199 formBean.addFormPropertyConfig 200 (new FormPropertyConfig("stringArray1", "java.lang.String[]", 201 "{aaa,bbb,ccc}", 2)); formBean.addFormPropertyConfig 203 (new FormPropertyConfig("stringArray2", "java.lang.String[]", 204 null, 3)); moduleConfig.addFormBeanConfig(formBean); 206 207 mapping = new ActionMapping(); 209 mapping.setName("dynamic0"); 210 mapping.setPath("/dynamic0"); 211 mapping.setScope("request"); 212 mapping.setType("org.apache.struts.mock.MockAction"); 213 moduleConfig.addActionConfig(mapping); 214 215 mapping = new ActionMapping(); 217 mapping.setPath("/noform"); 218 mapping.setType("org.apache.struts.mock.MockAction"); 219 moduleConfig.addActionConfig(mapping); 220 221 moduleConfig.addForwardConfig 223 (new ForwardConfig("moduleForward", 224 "/module/forward", 225 false, false)); 228 moduleConfig.addForwardConfig 229 (new ForwardConfig("moduleRedirect", 230 "/module/redirect", 231 true, false)); 234 moduleConfig.addForwardConfig 235 (new ForwardConfig("contextForward", 236 "/context/forward", 237 false, true)); 240 moduleConfig.addForwardConfig 241 (new ForwardConfig("contextRedirect", 242 "/context/redirect", 243 true, true)); 246 moduleConfig.addForwardConfig 247 (new ForwardConfig("moduleNoslash", 248 "module/noslash", 249 false, false)); 252 moduleConfig.addForwardConfig 253 (new ForwardConfig("contextNoslash", 254 "context/noslash", 255 false, true)); 258 } 259 260 261 protected void setUpSecondApp() { 262 263 ActionFormBean formBean = null; 264 ActionMapping mapping = null; 265 266 267 ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); 268 moduleConfig2 = factoryObject.createModuleConfig("/2"); 269 270 context.setAttribute(Globals.MODULE_KEY + "/2", moduleConfig2); 271 272 moduleConfig2.addForwardConfig 274 (new ActionForward("external", "http://jakarta.apache.org/", 275 false, false)); 276 277 moduleConfig2.addForwardConfig 279 (new ActionForward("foo", "/baz.jsp", false, false)); 280 281 moduleConfig2.addForwardConfig 283 (new ActionForward("relative1", "relative.jsp", false, false)); 284 285 moduleConfig2.addForwardConfig 287 (new ActionForward("relative2", "relative.jsp", false, true)); 288 289 formBean = new ActionFormBean 291 ("static", 292 "org.apache.struts.mock.MockFormBean"); 293 moduleConfig2.addFormBeanConfig(formBean); 294 295 mapping = new ActionMapping(); 297 mapping.setInput("/static.jsp"); 298 mapping.setName("static"); 299 mapping.setPath("/static"); 300 mapping.setScope("request"); 301 mapping.setType("org.apache.struts.mock.MockAction"); 302 moduleConfig2.addActionConfig(mapping); 303 304 formBean = new ActionFormBean 306 ("dynamic2", 307 "org.apache.struts.action.DynaActionForm"); 308 formBean.addFormPropertyConfig 309 (new FormPropertyConfig("booleanProperty", "boolean", "false")); 310 formBean.addFormPropertyConfig 311 (new FormPropertyConfig("stringProperty", "java.lang.String", 312 null)); 313 moduleConfig2.addFormBeanConfig(formBean); 314 315 mapping = new ActionMapping(); 317 mapping.setInput("/dynamic2.jsp"); 318 mapping.setName("dynamic2"); 319 mapping.setPath("/dynamic2"); 320 mapping.setScope("session"); 321 mapping.setType("org.apache.struts.mock.MockAction"); 322 moduleConfig2.addActionConfig(mapping); 323 324 mapping = new ActionMapping(); 326 mapping.setPath("/noform"); 327 mapping.setType("org.apache.struts.mock.MockAction"); 328 moduleConfig2.addActionConfig(mapping); 329 330 moduleConfig2.addForwardConfig 332 (new ForwardConfig("moduleForward", 333 "/module/forward", 334 false, false)); 337 moduleConfig2.addForwardConfig 338 (new ForwardConfig("moduleRedirect", 339 "/module/redirect", 340 true, false)); 343 moduleConfig2.addForwardConfig 344 (new ForwardConfig("contextForward", 345 "/context/forward", 346 false, true)); 349 moduleConfig2.addForwardConfig 350 (new ForwardConfig("contextRedirect", 351 "/context/redirect", 352 true, true)); 355 moduleConfig2.addForwardConfig 356 (new ForwardConfig("moduleNoslash", 357 "module/noslash", 358 false, false)); 361 moduleConfig2.addForwardConfig 362 (new ForwardConfig("contextNoslash", 363 "context/noslash", 364 false, true)); 367 } 368 369 370 protected void setUpThirdApp() { 372 373 374 ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); 375 moduleConfig3 = factoryObject.createModuleConfig("/3"); 376 377 context.setAttribute(Globals.MODULE_KEY + "/3", moduleConfig3); 378 379 ControllerConfig controller = new ControllerConfig(); 381 moduleConfig3.setControllerConfig(controller); 382 383 controller.setForwardPattern("/forwarding$M$P"); 385 controller.setInputForward(true); 386 controller.setPagePattern("/paging$M$P"); 387 388 moduleConfig3.addForwardConfig 390 (new ForwardConfig("moduleForward", 391 "/module/forward", 392 false, false)); 395 moduleConfig3.addForwardConfig 396 (new ForwardConfig("moduleRedirect", 397 "/module/redirect", 398 true, false)); 401 moduleConfig3.addForwardConfig 402 (new ForwardConfig("contextForward", 403 "/context/forward", 404 false, true)); 407 moduleConfig3.addForwardConfig 408 (new ForwardConfig("contextRedirect", 409 "/context/redirect", 410 true, true)); 413 moduleConfig3.addForwardConfig 414 (new ForwardConfig("moduleNoslash", 415 "module/noslash", 416 false, false)); 419 moduleConfig3.addForwardConfig 420 (new ForwardConfig("contextNoslash", 421 "context/noslash", 422 false, true)); 425 } 426 427 428 public void tearDown() { 429 430 moduleConfig3 = null; 431 moduleConfig2 = null; 432 moduleConfig = null; 433 config = null; 434 context = null; 435 page = null; 436 principal = null; 437 request = null; 438 response = null; 439 session = null; 440 441 } 442 443 444 446 447 public void testUtilBaseEnvironment() { 449 450 assertNotNull("config is present", config); 452 assertNotNull("context is present", context); 453 assertNotNull("page is present", page); 454 assertNotNull("principal is present", principal); 455 assertNotNull("request is present", request); 456 assertNotNull("response is present", response); 457 assertNotNull("session is present", session); 458 assertEquals("page-->config", 459 config, page.getServletConfig()); 460 assertEquals("config-->context", 461 context, config.getServletContext()); 462 assertEquals("page-->context", 463 context, page.getServletContext()); 464 assertEquals("page-->request", 465 request, page.getRequest()); 466 assertEquals("page-->response", 467 response, page.getResponse()); 468 assertEquals("page-->session", 469 session, page.getSession()); 470 assertEquals("request-->principal", 471 principal, request.getUserPrincipal()); 472 assertEquals("request-->session", 473 session, request.getSession()); 474 assertEquals("session-->context", 475 context, session.getServletContext()); 476 477 assertNotNull("moduleConfig is present", moduleConfig); 479 assertEquals("context-->moduleConfig", 480 moduleConfig, 481 context.getAttribute(Globals.MODULE_KEY)); 482 483 assertNotNull("moduleConfig2 is present", moduleConfig2); 485 assertEquals("context-->moduleConfig2", 486 moduleConfig2, 487 context.getAttribute(Globals.MODULE_KEY + "/2")); 488 489 } 490 491 492 } 493 | Popular Tags |