1 17 package servletunit.struts; 18 19 import junit.framework.AssertionFailedError; 20 import org.apache.cactus.ServletTestCase; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.struts.Globals; 24 import org.apache.struts.action.ActionForm; 25 import org.apache.struts.action.ActionServlet; 26 27 import javax.servlet.ServletContext ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.http.*; 31 import java.util.Enumeration ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.util.Iterator ; 35 36 54 public class CactusStrutsTestCase extends ServletTestCase { 55 56 protected ActionServlet actionServlet; 57 protected boolean isInitialized = false; 58 protected boolean actionServletIsInitialized = false; 59 protected boolean requestPathIsSet = false; 60 protected String moduleName; 61 protected String servletMapping = "*.do"; 62 63 protected static Log logger = LogFactory.getLog(CactusStrutsTestCase.class); 64 65 68 public CactusStrutsTestCase() { 69 super(); 70 } 71 72 75 public CactusStrutsTestCase(String testName) { 76 super(testName); 77 } 78 79 83 private void init() { 84 if (!isInitialized) { 85 throw new AssertionFailedError("You are overriding the setUp() method without calling super.setUp(). You must call the superclass setUp() and tearDown() methods in your TestCase subclass to ensure proper initialization."); 86 } 87 } 88 89 94 protected void setUp() throws Exception { 95 if (logger.isDebugEnabled()) 96 logger.debug("Entering"); 97 try { 98 99 if (actionServlet == null) 100 actionServlet = new ActionServlet(); 101 ServletContext servletContext = new StrutsServletContextWrapper(this.config.getServletContext()); 102 this.config = new StrutsServletConfigWrapper(this.config); 103 ((StrutsServletConfigWrapper) this.config).setServletContext(servletContext); 104 this.request = new StrutsRequestWrapper(this.request); 105 this.response = new StrutsResponseWrapper(this.response); 106 isInitialized = true; 107 if (logger.isDebugEnabled()) 108 logger.debug("Exiting"); 109 } catch (Exception e) { 110 if (logger.isDebugEnabled()) 111 logger.debug(e); 112 throw new AssertionFailedError("Error trying to set up test fixture: " + e.getClass() + " - " + e.getMessage()); 113 } 114 } 115 116 122 public void setServletMapping(String servletMapping) { 123 this.servletMapping = servletMapping; 124 } 125 126 protected void tearDown () throws Exception { 127 if (logger.isTraceEnabled()) 128 logger.trace("Entering"); 129 130 ServletContext context = this.config.getServletContext(); 133 List nameList = new ArrayList (); 134 Enumeration attributeNames = context.getAttributeNames(); 135 while (attributeNames.hasMoreElements()) { 136 String name = (String ) attributeNames.nextElement(); 137 if (name.startsWith(Globals.REQUEST_PROCESSOR_KEY)) 138 nameList.add(name); 139 } 140 141 ServletConfig originalConfig = ((org.apache.cactus.server.ServletConfigWrapper) this.config).getOriginalConfig(); 143 ServletContext originalContext = originalConfig.getServletContext(); 144 originalContext.setAttribute(Globals.SERVLET_KEY,servletMapping); 145 ((StrutsServletConfigWrapper) config).setServletContext(originalContext); 146 147 148 for (Iterator iterator = nameList.iterator(); iterator.hasNext();) { 149 context.removeAttribute((String ) iterator.next()); 150 } 151 152 if (logger.isTraceEnabled()) 153 logger.trace("Exiting"); 154 } 155 156 160 public HttpServletRequest getRequest() { 161 if (logger.isDebugEnabled()) 162 logger.debug("Entering"); 163 init(); 164 if (logger.isDebugEnabled()) 165 logger.debug("Exiting"); 166 return this.request; 167 } 168 169 173 public HttpServletResponse getResponse() { 174 if (logger.isDebugEnabled()) 175 logger.debug("Entering"); 176 init(); 177 if (logger.isDebugEnabled()) 178 logger.debug("Exiting"); 179 return this.response; 180 } 181 182 186 public HttpSession getSession() { 187 if (logger.isDebugEnabled()) 188 logger.debug("Entering"); 189 init(); 190 if (logger.isDebugEnabled()) 191 logger.debug("Exiting"); 192 return this.session; 193 } 194 195 201 public void addRequestParameter(String parameterName, String parameterValue) 202 { 203 if (logger.isDebugEnabled()) 204 logger.debug("Entering - paramaterName = " + parameterName + ", parameterValue = " + parameterValue); 205 init(); 206 ((StrutsRequestWrapper) this.request).addParameter(parameterName,parameterValue); 207 if (logger.isDebugEnabled()) 208 logger.debug("Exiting"); 209 } 210 211 217 public void addRequestParameter(String parameterName, String [] parameterValues) 218 { 219 if (logger.isDebugEnabled()) 220 logger.debug("Entering - paramaterName = " + parameterName + ", parameterValue = " + parameterValues); 221 init(); 222 ((StrutsRequestWrapper) this.request).addParameter(parameterName,parameterValues); 223 if (logger.isDebugEnabled()) 224 logger.debug("Exiting"); 225 } 226 227 231 public void clearRequestParameters() { 232 init(); 233 ((StrutsRequestWrapper) request).clearRequestParameters(); 234 } 235 236 244 public void setRequestPathInfo(String pathInfo) { 245 if (logger.isDebugEnabled()) 246 logger.debug("Entering - pathInfo = " + pathInfo); 247 init(); 248 this.setRequestPathInfo("",pathInfo); 249 if (logger.isDebugEnabled()) 250 logger.debug("Exiting"); 251 } 252 253 254 268 public void setRequestPathInfo(String moduleName, String pathInfo) { 269 if (logger.isDebugEnabled()) 270 logger.debug("Entering - moduleName = " + moduleName + ", pathInfo = " + pathInfo); 271 init(); 272 ((StrutsRequestWrapper) this.request).setPathInfo(Common.stripActionPath(pathInfo)); 273 if (moduleName != null) { 274 if (!moduleName.equals("")) { 275 if (!moduleName.startsWith("/")) 276 moduleName = "/" + moduleName; 277 if (!moduleName.endsWith("/")) 278 moduleName = moduleName + "/"; 279 } 280 if (logger.isDebugEnabled()) { 281 logger.debug("setting request.ServletPath value = " + moduleName); 282 } 283 ((StrutsRequestWrapper) this.request).setServletPath(moduleName); 284 this.requestPathIsSet = true; 285 } 286 if (logger.isDebugEnabled()) 287 logger.debug("Exiting"); 288 } 289 290 297 public void setInitParameter(String key, String value){ 298 if (logger.isDebugEnabled()) 299 logger.debug("Entering - key = " + key + ", value = " + value); 300 this.config.setInitParameter(key, value); 301 this.actionServletIsInitialized = false; 302 if (logger.isDebugEnabled()) 303 logger.debug("Exiting"); 304 } 305 306 312 public void setConfigFile(String pathname) { 313 if (logger.isDebugEnabled()) 314 logger.debug("Entering - pathname = " + pathname); 315 init(); 316 setConfigFile(null,pathname); 317 if (logger.isDebugEnabled()) 318 logger.debug("Exiting"); 319 } 320 321 330 public void setConfigFile(String moduleName, String pathname) { 331 if (logger.isDebugEnabled()) 332 logger.debug("Entering - moduleName = " + moduleName + ", pathname = " + pathname); 333 init(); 334 if (moduleName == null) 335 this.config.setInitParameter("config",pathname); 336 else 337 this.config.setInitParameter("config/" + moduleName,pathname); 338 actionServletIsInitialized = false; 339 if (logger.isDebugEnabled()) 340 logger.debug("Exiting"); 341 } 342 343 348 public ActionServlet getActionServlet() { 349 if (logger.isDebugEnabled()) 350 logger.debug("Entering"); 351 init(); 352 try { 353 if (!actionServletIsInitialized) { 354 ServletContext context = config.getServletContext(); 357 String name = "org.apache.struts.action.REQUEST_PROCESSOR"; 358 359 Object obj = context.getAttribute(name); 361 if (obj != null) { 362 config.getServletContext().removeAttribute(name); 363 } 364 365 String moduleName = request.getServletPath() != null ? request.getServletPath() : ""; 368 369 if (moduleName.endsWith("/")) 370 moduleName = moduleName.substring(0,moduleName.lastIndexOf("/")); 371 372 obj = context.getAttribute(name + moduleName); 373 if (obj != null) { 374 config.getServletContext().removeAttribute(name + moduleName); 375 } 376 377 this.actionServlet.init(config); 378 actionServletIsInitialized = true; 379 } 380 if (logger.isDebugEnabled()) 381 logger.debug("Exiting"); 382 return this.actionServlet; 383 } catch (ServletException e) { 384 if (logger.isDebugEnabled()) 385 logger.debug("Error in getActionServlet()",e.getRootCause()); 386 throw new AssertionFailedError("Error while initializing ActionServlet: " + e.getMessage()); 387 } 388 } 389 390 395 public void setActionServlet(ActionServlet servlet) { 396 if (logger.isDebugEnabled()) 397 logger.debug("Entering - servlet = " + servlet); 398 init(); 399 if (servlet == null) 400 throw new AssertionFailedError("Cannot set ActionServlet to null"); 401 this.actionServlet = servlet; 402 actionServletIsInitialized = false; 403 if (logger.isDebugEnabled()) 404 logger.debug("Exiting"); 405 } 406 407 417 public void actionPerform() { 418 if (logger.isDebugEnabled()) 419 logger.debug("Entering"); 420 if(!this.requestPathIsSet){ 421 throw new IllegalStateException ("You must call setRequestPathInfo() prior to calling actionPerform()."); 422 } 423 init(); 424 try { 425 HttpServletRequest request = this.request; 426 HttpServletResponse response = this.response; 427 request.removeAttribute(Globals.ERROR_KEY); 429 request.removeAttribute(Globals.MESSAGE_KEY); 430 431 ActionServlet actionServlet = this.getActionServlet(); 432 actionServlet.doPost(request,response); 433 if (logger.isDebugEnabled()) 434 logger.debug("Exiting"); 435 } catch (NullPointerException npe) { 436 String message = "A NullPointerException was thrown. This may indicate an error in your ActionForm, or " 437 + "it may indicate that the Struts ActionServlet was unable to find struts config file. " 438 + "TestCase is running from " + System.getProperty("user.dir") + " directory."; 439 throw new ExceptionDuringTestError(message, npe); 440 } catch(Exception e){ 441 throw new ExceptionDuringTestError("An uncaught exception was thrown during actionExecute()", e); 442 } 443 } 444 445 448 protected String getActualForward() { 449 if (logger.isDebugEnabled()) 450 logger.debug("Entering"); 451 if (response.containsHeader("Location")) { 452 return Common.stripJSessionID(((StrutsResponseWrapper) response).getRedirectLocation()); 453 } else { 454 String forward = ((StrutsServletContextWrapper) this.actionServlet.getServletContext()).getForward(); 455 if (logger.isDebugEnabled()) { 456 logger.debug("actual forward = " + forward); 457 } 458 if (forward == null) { 459 if (logger.isDebugEnabled()) 460 logger.debug("Exiting"); 461 return null; 462 } else { 463 String temp = Common.stripJSessionID(forward); 464 if (!temp.startsWith("/")) 465 temp = "/" + temp; 466 String strippedForward = request.getContextPath() + temp; 467 if (logger.isDebugEnabled()) { 468 logger.debug("stripped forward and added context path = " + strippedForward); 469 } 470 if (logger.isDebugEnabled()) 471 logger.debug("Exiting"); 472 return strippedForward; 473 } 474 } 475 476 } 477 478 489 public void verifyForward(String forwardName) throws AssertionFailedError { 490 if (logger.isDebugEnabled()) 491 logger.debug("Entering - forwardName = " + forwardName); 492 init(); 493 Common.verifyForwardPath(request.getPathInfo(),forwardName,getActualForward(),false,request,config.getServletContext(),config); 494 if (logger.isDebugEnabled()) 495 logger.debug("Exiting"); 496 } 497 498 509 public void verifyForwardPath(String forwardPath) throws AssertionFailedError { 510 if (logger.isDebugEnabled()) 511 logger.debug("Entering - forwardPath = " + forwardPath); 512 init(); 513 String actualForward = getActualForward(); 514 515 if ((actualForward == null) && (forwardPath == null)) { 516 return; 518 } 519 520 forwardPath = request.getContextPath() + forwardPath; 521 522 if (actualForward == null) { 523 if (logger.isDebugEnabled()) { 524 logger.debug("actualForward is null - this usually means it is not mapped properly."); 525 } 526 throw new AssertionFailedError("Was expecting '" + forwardPath + "' but it appears the Action has tried to return an ActionForward that is not mapped correctly."); 527 } 528 if (logger.isDebugEnabled()) { 529 logger.debug("expected forward = '" + forwardPath + "' - actual forward = '" + actualForward + "'"); 530 } 531 if (!(actualForward.equals(forwardPath))) 532 throw new AssertionFailedError("was expecting '" + forwardPath + "' but received '" + actualForward + "'"); 533 if (logger.isDebugEnabled()) 534 logger.debug("Exiting"); 535 } 536 537 545 public void verifyInputForward() { 546 if (logger.isDebugEnabled()) 547 logger.debug("Entering"); 548 init(); 549 Common.verifyForwardPath(request.getPathInfo(),null,getActualForward(),true,request,config.getServletContext(),config); 550 if (logger.isDebugEnabled()) 551 logger.debug("Exiting"); 552 } 553 554 568 public void verifyTilesForward(String forwardName, String definitionName) { 569 if (logger.isTraceEnabled()) 570 logger.trace("Entering - forwardName=" + forwardName + ", definitionName=" + definitionName); 571 init(); 572 Common.verifyTilesForward(request.getPathInfo(),forwardName,definitionName,false,request,config.getServletContext(),config); 573 if (logger.isTraceEnabled()) 574 logger.trace("Exiting"); 575 } 576 577 588 public void verifyInputTilesForward(String definitionName) { 589 if (logger.isTraceEnabled()) 590 logger.trace("Entering - defintionName=" + definitionName); 591 init(); 592 Common.verifyTilesForward(request.getPathInfo(),null,definitionName,true,request,config.getServletContext(),config); 593 if (logger.isTraceEnabled()) 594 logger.trace("Exiting"); 595 } 596 597 610 611 public void verifyActionErrors(String [] errorNames) { 612 if (logger.isDebugEnabled()) 613 logger.debug("Entering - errorNames = " + errorNames); 614 init(); 615 Common.verifyActionMessages(request,errorNames,Globals.ERROR_KEY,"error"); 616 if (logger.isDebugEnabled()) 617 logger.debug("Exiting"); 618 } 619 620 627 public void verifyNoActionErrors() { 628 if (logger.isDebugEnabled()) 629 logger.debug("Entering"); 630 init(); 631 Common.verifyNoActionMessages(request,Globals.ERROR_KEY,"error"); 632 if (logger.isDebugEnabled()) 633 logger.debug("Exiting"); 634 } 635 636 649 public void verifyActionMessages(String [] messageNames) { 650 if (logger.isDebugEnabled()) 651 logger.debug("Entering - messageNames = " + messageNames); 652 init(); 653 Common.verifyActionMessages(request,messageNames,Globals.MESSAGE_KEY,"action"); 654 if (logger.isDebugEnabled()) 655 logger.debug("Exiting"); 656 } 657 658 665 public void verifyNoActionMessages() { 666 if (logger.isDebugEnabled()) 667 logger.debug("Entering"); 668 init(); 669 Common.verifyNoActionMessages(request,Globals.MESSAGE_KEY,"action"); 670 if (logger.isDebugEnabled()) 671 logger.debug("Exiting"); 672 } 673 674 681 public ActionForm getActionForm() { 682 if (logger.isDebugEnabled()) 683 logger.debug("Entering"); 684 init(); 685 if (logger.isDebugEnabled()) 686 logger.debug("Exiting"); 687 return Common.getActionForm(request.getPathInfo(),request,config.getServletContext()); 688 } 689 690 700 public void setActionForm(ActionForm form) { 701 if (logger.isDebugEnabled()) 702 logger.debug("Entering - form = " + form); 703 init(); 704 Common.setActionForm(form,request,request.getPathInfo(),config.getServletContext()); 706 if (logger.isDebugEnabled()) 707 logger.debug("Exiting"); 708 } 709 710 716 public void processRequest(boolean flag) { 717 if (logger.isTraceEnabled()) 718 logger.trace("Entering - flag=" + flag); 719 ((StrutsServletContextWrapper) this.config.getServletContext()).setProcessRequest(flag); 720 if (logger.isTraceEnabled()) 721 logger.trace("Exiting"); 722 } 723 724 } 725 726 | Popular Tags |