001: // StrutsTestCase - a JUnit extension for testing Struts actions
002: // within the context of the ActionServlet.
003: // Copyright (C) 2002 Deryl Seale
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the Apache Software License as
007: // published by the Apache Software Foundation; either version 1.1
008: // of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: // Apache Software Foundation Licens for more details.
014: //
015: // You may view the full text here: http://www.apache.org/LICENSE.txt
016:
017: package servletunit.struts;
018:
019: import junit.framework.AssertionFailedError;
020: import org.apache.cactus.ServletTestCase;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.struts.Globals;
024: import org.apache.struts.action.ActionForm;
025: import org.apache.struts.action.ActionServlet;
026:
027: import javax.servlet.ServletContext;
028: import javax.servlet.ServletException;
029: import javax.servlet.ServletConfig;
030: import javax.servlet.http.*;
031: import java.util.Enumeration;
032: import java.util.List;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035:
036: /**
037: * CactusStrutsTestCase is an extension of the Cactus ServletTestCase
038: * base class that provides additional methods to aid in testing
039: * Struts Action objects. It uses an in-container approach to run
040: * the servlet container, and tests the execution of Action objects as they
041: * are actually run through the Struts ActionServlet. CactusStrutsTestCase
042: * provides methods that set up the request path, request parameters
043: * for ActionForm subclasses, as well as methods that can verify
044: * that the correct ActionForward was used and that the proper
045: * ActionError messages were supplied.
046: * <br>
047: * <br>
048: * <b>Please note</b> that this class is meant to run in the Cactus
049: * framework, and you must configure your test environment
050: * accordingly. Please see <a href="http://jakarta.apache.org/cactus">http://jakarta.apache.org/cactus</a>
051: * for more details.
052: *
053: */
054: public class CactusStrutsTestCase extends ServletTestCase {
055:
056: protected ActionServlet actionServlet;
057: protected HttpServletRequestWrapper requestWrapper;
058: protected HttpServletResponseWrapper responseWrapper;
059: protected boolean isInitialized = false;
060: protected boolean actionServletIsInitialized = false;
061: protected boolean requestPathIsSet = false;
062: protected String moduleName;
063: protected String servletMapping = "*.do";
064:
065: protected static Log logger = LogFactory
066: .getLog(CactusStrutsTestCase.class);
067:
068: /**
069: * Default constructor.
070: */
071: public CactusStrutsTestCase() {
072: super ();
073: }
074:
075: /**
076: * Constructor that takes test name parameter, for backwards compatibility with older versions on JUnit.
077: */
078: public CactusStrutsTestCase(String testName) {
079: super (testName);
080: }
081:
082: /**
083: * A check that every method should run to ensure that the
084: * base class setUp method has been called.
085: */
086: private void init() {
087: if (!isInitialized) {
088: throw new AssertionFailedError(
089: "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.");
090: }
091: }
092:
093: /**
094: * Sets up the test fixture for this test. This method creates
095: * an instance of the ActionServlet, initializes it to validate
096: * forms and turn off debugging, and clears all other parameters.
097: */
098: protected void setUp() throws Exception {
099: if (logger.isDebugEnabled())
100: logger.debug("Entering");
101: try {
102:
103: if (actionServlet == null)
104: actionServlet = new ActionServlet();
105: requestWrapper = null;
106: responseWrapper = null;
107: ServletContext servletContext = new StrutsServletContextWrapper(
108: this .config.getServletContext());
109: this .config = new StrutsServletConfigWrapper(this .config);
110: ((StrutsServletConfigWrapper) this .config)
111: .setServletContext(servletContext);
112: this .request = new StrutsRequestWrapper(this .request);
113: this .response = new StrutsResponseWrapper(this .response);
114: isInitialized = true;
115: if (logger.isDebugEnabled())
116: logger.debug("Exiting");
117: } catch (Exception e) {
118: if (logger.isDebugEnabled())
119: logger.debug(e);
120: throw new AssertionFailedError(
121: "Error trying to set up test fixture: "
122: + e.getClass() + " - " + e.getMessage());
123: }
124: }
125:
126: /**
127: * Sets the servlet mapping used to map requests to the Struts controller. This is used to restore
128: * proper setting after the test has been executed. By default, the stanard "*.do" mapping will be
129: * restored, so only use this method for non-standard servlet mappings.
130: * @param servletMapping
131: */
132: public void setServletMapping(String servletMapping) {
133: this .servletMapping = servletMapping;
134: }
135:
136: protected void tearDown() throws Exception {
137: if (logger.isTraceEnabled())
138: logger.trace("Entering");
139:
140: // remove all RequestProcessor instances from context
141: // so that web application will function normally.
142: ServletContext context = this .config.getServletContext();
143: List nameList = new ArrayList();
144: Enumeration attributeNames = context.getAttributeNames();
145: while (attributeNames.hasMoreElements()) {
146: String name = (String) attributeNames.nextElement();
147: if (name.startsWith(Globals.REQUEST_PROCESSOR_KEY))
148: nameList.add(name);
149: }
150:
151: // restore the original servlet mapping
152: ServletConfig originalConfig = ((org.apache.cactus.server.ServletConfigWrapper) this .config)
153: .getOriginalConfig();
154: ServletContext originalContext = originalConfig
155: .getServletContext();
156: originalContext.setAttribute(Globals.SERVLET_KEY,
157: servletMapping);
158: ((StrutsServletConfigWrapper) config)
159: .setServletContext(originalContext);
160:
161: for (Iterator iterator = nameList.iterator(); iterator
162: .hasNext();) {
163: context.removeAttribute((String) iterator.next());
164: }
165:
166: if (logger.isTraceEnabled())
167: logger.trace("Exiting");
168: }
169:
170: /**
171: * Returns an HttpServletRequest object that can be used in
172: * this test.
173: */
174: public HttpServletRequest getRequest() {
175: if (logger.isDebugEnabled())
176: logger.debug("Entering");
177: init();
178: if (logger.isDebugEnabled())
179: logger.debug("Exiting");
180: return this .request;
181: }
182:
183: /**
184: * Returns a HttpServletRequestWrapper object that can be used
185: * in this test. Note that if {@link #setRequestWrapper} has not been
186: * called, this method will return an instance of
187: * javax.servlet.http.HttpServletRequestWrapper.
188: */
189: public HttpServletRequestWrapper getRequestWrapper() {
190: if (logger.isDebugEnabled())
191: logger.debug("Entering");
192: init();
193: if (requestWrapper == null) {
194: if (logger.isDebugEnabled())
195: logger.debug("Exiting");
196: return new HttpServletRequestWrapper(this .request);
197: } else {
198: if (logger.isDebugEnabled()) {
199: logger.debug("wrapper class is '"
200: + requestWrapper.getClass() + "'");
201: }
202: if (logger.isDebugEnabled())
203: logger.debug("Exiting");
204: return requestWrapper;
205: }
206: }
207:
208: /**
209: * Set this TestCase to use a given HttpServletRequestWrapper
210: * class when calling Action.execute(). Note that if this
211: * method is not called, then the normal HttpServletRequest
212: * object is used.
213: *
214: * @param wrapper an HttpServletRequestWrapper object to be
215: * used when calling Action.execute().
216: */
217: public void setRequestWrapper(HttpServletRequestWrapper wrapper) {
218: if (logger.isDebugEnabled())
219: logger.debug("Entering - wrapper = " + wrapper);
220: init();
221: if (wrapper == null) {
222: throw new IllegalArgumentException(
223: "wrapper class cannot be null!");
224: } else {
225: if (wrapper.getRequest() == null)
226: wrapper.setRequest(this .request);
227: this .requestWrapper = wrapper;
228: }
229: if (logger.isDebugEnabled())
230: logger.debug("Exiting");
231: }
232:
233: /**
234: * Returns an HttpServletResponse object that can be used in
235: * this test.
236: */
237: public HttpServletResponse getResponse() {
238: if (logger.isDebugEnabled())
239: logger.debug("Entering");
240: init();
241: if (logger.isDebugEnabled())
242: logger.debug("Exiting");
243: return this .response;
244: }
245:
246: /**
247: * Returns an HttpServletResponseWrapper object that can be used in
248: * this test. Note that if {@link #setResponseWrapper} has not been
249: * called, this method will return an instance of
250: * javax.servlet.http.HttpServletResponseWrapper.
251: */
252: public HttpServletResponseWrapper getResponseWrapper() {
253: if (logger.isDebugEnabled())
254: logger.debug("Entering");
255: init();
256: if (responseWrapper == null) {
257: if (logger.isDebugEnabled())
258: logger.debug("Exiting getResponseWrapper()");
259: return new HttpServletResponseWrapper(this .response);
260: } else {
261: if (logger.isDebugEnabled()) {
262: logger.debug("wrapper class is '"
263: + responseWrapper.getClass() + "'");
264: }
265: if (logger.isDebugEnabled())
266: logger.debug("getResponseWrapper()");
267: return responseWrapper;
268: }
269: }
270:
271: /**
272: * Set this TestCase to use a given HttpServletResponseWrapper
273: * class when calling Action.execute(). Note that if this
274: * method is not called, then the normal HttpServletResponse
275: * object is used.
276: *
277: * @param wrapper an HttpServletResponseWrapper object to be
278: * used when calling Action.execute().
279: */
280: public void setResponseWrapper(HttpServletResponseWrapper wrapper) {
281: if (logger.isDebugEnabled())
282: logger.debug("Entering - wrapper = " + wrapper.getClass());
283: init();
284: if (wrapper == null)
285: throw new IllegalArgumentException(
286: "wrapper class cannot be null!");
287: else {
288: if (wrapper.getResponse() == null)
289: wrapper.setResponse(this .response);
290: if (logger.isDebugEnabled())
291: logger.debug("Exiting");
292: this .responseWrapper = wrapper;
293: }
294: }
295:
296: /**
297: * Returns an HttpSession object that can be used in this
298: * test.
299: */
300: public HttpSession getSession() {
301: if (logger.isDebugEnabled())
302: logger.debug("Entering");
303: init();
304: if (logger.isDebugEnabled())
305: logger.debug("Exiting");
306: return this .session;
307: }
308:
309: /**
310: * Adds an HttpServletRequest parameter to be used in setting up the
311: * ActionForm instance to be used in this test. Each parameter added
312: * should correspond to an attribute in the ActionForm instance used
313: * by the Action instance being tested.
314: */
315: public void addRequestParameter(String parameterName,
316: String parameterValue) {
317: if (logger.isDebugEnabled())
318: logger.debug("Entering - paramaterName = " + parameterName
319: + ", parameterValue = " + parameterValue);
320: init();
321: ((StrutsRequestWrapper) this .request).addParameter(
322: parameterName, parameterValue);
323: if (logger.isDebugEnabled())
324: logger.debug("Exiting");
325: }
326:
327: /**
328: * Adds an HttpServletRequest parameter that is an array of String values
329: * to be used in setting up the ActionForm instance to be used in this test.
330: * Each parameter added should correspond to an attribute in the ActionForm
331: * instance used by the Action instance being tested.
332: */
333: public void addRequestParameter(String parameterName,
334: String[] parameterValues) {
335: if (logger.isDebugEnabled())
336: logger.debug("Entering - paramaterName = " + parameterName
337: + ", parameterValue = " + parameterValues);
338: init();
339: ((StrutsRequestWrapper) this .request).addParameter(
340: parameterName, parameterValues);
341: if (logger.isDebugEnabled())
342: logger.debug("Exiting");
343: }
344:
345: /**
346: * Clears all request parameters previously set. NOTE: This will <strong>not</strong> clear
347: * parameters set using Cactus beginXXX methods!
348: */
349: public void clearRequestParameters() {
350: init();
351: ((StrutsRequestWrapper) request).clearRequestParameters();
352: }
353:
354: /**
355: * Sets the request path instructing the ActionServlet to used a
356: * particual ActionMapping.
357: *
358: * @param pathInfo the request path to be processed. This should
359: * correspond to a particular action mapping, as would normally
360: * appear in an HTML or JSP source file.
361: */
362: public void setRequestPathInfo(String pathInfo) {
363: if (logger.isDebugEnabled())
364: logger.debug("Entering - pathInfo = " + pathInfo);
365: init();
366: this .setRequestPathInfo("", pathInfo);
367: if (logger.isDebugEnabled())
368: logger.debug("Exiting");
369: }
370:
371: /**
372: * Sets the request path instructing the ActionServlet to used a
373: * particual ActionMapping. Also sets the ServletPath property
374: * on the request.
375: *
376: * @param moduleName the name of the Struts sub-application with
377: * which this request is associated, or null if it is the default
378: * application.
379: * @param pathInfo the request path to be processed. This should
380: * correspond to a particular action mapping, as would normally
381: * appear in an HTML or JSP source file. If this request is part
382: * of a sub-application, the module name should not appear in the
383: * request path.
384: */
385: public void setRequestPathInfo(String moduleName, String pathInfo) {
386: if (logger.isDebugEnabled())
387: logger.debug("Entering - moduleName = " + moduleName
388: + ", pathInfo = " + pathInfo);
389: init();
390: ((StrutsRequestWrapper) this .request).setPathInfo(Common
391: .stripActionPath(pathInfo));
392: if (moduleName != null) {
393: if (!moduleName.equals("")) {
394: if (!moduleName.startsWith("/"))
395: moduleName = "/" + moduleName;
396: if (!moduleName.endsWith("/"))
397: moduleName = moduleName + "/";
398: }
399: if (logger.isDebugEnabled()) {
400: logger.debug("setting request.ServletPath value = "
401: + moduleName);
402: }
403: ((StrutsRequestWrapper) this .request)
404: .setServletPath(moduleName);
405: this .requestPathIsSet = true;
406: }
407: if (logger.isDebugEnabled())
408: logger.debug("Exiting");
409: }
410:
411: /**
412: * Sets an initialization parameter on the
413: * ActionServlet. Allows you to simulate an init parameter
414: * that would normally have been found in web.xml.
415: * @param key the name of the initialization parameter
416: * @param value the value of the intialization parameter
417: */
418: public void setInitParameter(String key, String value) {
419: if (logger.isDebugEnabled())
420: logger.debug("Entering - key = " + key + ", value = "
421: + value);
422: this .config.setInitParameter(key, value);
423: this .actionServletIsInitialized = false;
424: if (logger.isDebugEnabled())
425: logger.debug("Exiting");
426: }
427:
428: /**
429: * Sets the location of the Struts configuration file for the default module.
430: * This method can take either an absolute path, or a relative path. If an
431: * absolute path is supplied, the configuration file will be loaded from the
432: * underlying filesystem; otherwise, the ServletContext loader will be used.
433: */
434: public void setConfigFile(String pathname) {
435: if (logger.isDebugEnabled())
436: logger.debug("Entering - pathname = " + pathname);
437: init();
438: setConfigFile(null, pathname);
439: if (logger.isDebugEnabled())
440: logger.debug("Exiting");
441: }
442:
443: /**
444: * Sets the struts configuration file for a given sub-application.
445: * This method can take either an absolute path, or a relative path. If an
446: * absolute path is supplied, the configuration file will be loaded from the
447: * underlying filesystem; otherwise, the ServletContext loader will be used.
448: *
449: * @param moduleName the name of the sub-application, or null if this is the default application
450: * @param pathname the location of the configuration file for this sub-application
451: */
452: public void setConfigFile(String moduleName, String pathname) {
453: if (logger.isDebugEnabled())
454: logger.debug("Entering - moduleName = " + moduleName
455: + ", pathname = " + pathname);
456: init();
457: if (moduleName == null)
458: this .config.setInitParameter("config", pathname);
459: else
460: this .config.setInitParameter("config/" + moduleName,
461: pathname);
462: actionServletIsInitialized = false;
463: if (logger.isDebugEnabled())
464: logger.debug("Exiting");
465: }
466:
467: /**
468: * Returns the ActionServlet controller used in this
469: * test.
470: *
471: */
472: public ActionServlet getActionServlet() {
473: if (logger.isDebugEnabled())
474: logger.debug("Entering");
475: init();
476: try {
477: if (!actionServletIsInitialized) {
478: // the RequestProcessor holds on to the ServletContext, so
479: // we need to ensure that it is replaced for each test.
480: ServletContext context = config.getServletContext();
481: String name = "org.apache.struts.action.REQUEST_PROCESSOR";
482:
483: // remove request processor for default module
484: Object obj = context.getAttribute(name);
485: if (obj != null) {
486: config.getServletContext().removeAttribute(name);
487: }
488:
489: // remove request processor for sub-applications, if used.
490: // todo: this seems pretty redundant.. may want to make this cleaner.
491: String moduleName = request.getServletPath() != null ? request
492: .getServletPath()
493: : "";
494:
495: if (moduleName.endsWith("/"))
496: moduleName = moduleName.substring(0, moduleName
497: .lastIndexOf("/"));
498:
499: obj = context.getAttribute(name + moduleName);
500: if (obj != null) {
501: config.getServletContext().removeAttribute(
502: name + moduleName);
503: }
504:
505: this .actionServlet.init(config);
506: actionServletIsInitialized = true;
507: }
508: if (logger.isDebugEnabled())
509: logger.debug("Exiting");
510: return this .actionServlet;
511: } catch (ServletException e) {
512: if (logger.isDebugEnabled())
513: logger.debug("Error in getActionServlet()", e
514: .getRootCause());
515: throw new AssertionFailedError(
516: "Error while initializing ActionServlet: "
517: + e.getMessage());
518: }
519: }
520:
521: /**
522: * Sets the ActionServlet to be used in this test execution. This
523: * method should only be used if you plan to use a customized
524: * version different from that provided in the Struts distribution.
525: */
526: public void setActionServlet(ActionServlet servlet) {
527: if (logger.isDebugEnabled())
528: logger.debug("Entering - servlet = " + servlet);
529: init();
530: if (servlet == null)
531: throw new AssertionFailedError(
532: "Cannot set ActionServlet to null");
533: this .actionServlet = servlet;
534: actionServletIsInitialized = false;
535: if (logger.isDebugEnabled())
536: logger.debug("Exiting");
537: }
538:
539: /**
540: * Executes the Action instance to be tested. This method initializes
541: * the ActionServlet, sets up and optionally validates the ActionForm
542: * bean associated with the Action to be tested, and then calls the
543: * Action.execute() method. Results are stored for further validation.
544: *
545: * @exception AssertionFailedError if there are any execution
546: * errors while calling Action.execute() or ActionForm.validate().
547: *
548: */
549: public void actionPerform() {
550: if (logger.isDebugEnabled())
551: logger.debug("Entering");
552: if (!this .requestPathIsSet) {
553: throw new IllegalStateException(
554: "You must call setRequestPathInfo() prior to calling actionPerform().");
555: }
556: init();
557: try {
558: HttpServletRequest request = this .request;
559: HttpServletResponse response = this .response;
560: // make sure errors are cleared from last test.
561: request.removeAttribute(Globals.ERROR_KEY);
562: request.removeAttribute(Globals.MESSAGE_KEY);
563:
564: if (this .requestWrapper != null)
565: request = this .requestWrapper;
566: if (this .responseWrapper != null)
567: response = this .responseWrapper;
568:
569: ActionServlet actionServlet = this .getActionServlet();
570: actionServlet.doPost(request, response);
571: if (logger.isDebugEnabled())
572: logger.debug("Exiting");
573: } catch (NullPointerException npe) {
574: String message = "A NullPointerException was thrown. This may indicate an error in your ActionForm, or "
575: + "it may indicate that the Struts ActionServlet was unable to find struts config file. "
576: + "TestCase is running from "
577: + System.getProperty("user.dir") + " directory.";
578: throw new ExceptionDuringTestError(message, npe);
579: } catch (Exception e) {
580: throw new ExceptionDuringTestError(
581: "An uncaught exception was thrown during actionExecute()",
582: e);
583: }
584: }
585:
586: /**
587: * Returns the forward sent to RequestDispatcher.
588: */
589: protected String getActualForward() {
590: if (logger.isDebugEnabled())
591: logger.debug("Entering");
592: if (response.containsHeader("Location")) {
593: return Common
594: .stripJSessionID(((StrutsResponseWrapper) response)
595: .getRedirectLocation());
596: } else {
597: String forward = ((StrutsServletContextWrapper) this .actionServlet
598: .getServletContext()).getForward();
599: if (logger.isDebugEnabled()) {
600: logger.debug("actual forward = " + forward);
601: }
602: if (forward == null) {
603: if (logger.isDebugEnabled())
604: logger.debug("Exiting");
605: return null;
606: } else {
607: String temp = Common.stripJSessionID(forward);
608: if (!temp.startsWith("/"))
609: temp = "/" + temp;
610: String strippedForward = request.getContextPath()
611: + temp;
612: if (logger.isDebugEnabled()) {
613: logger
614: .debug("stripped forward and added context path = "
615: + strippedForward);
616: }
617: if (logger.isDebugEnabled())
618: logger.debug("Exiting");
619: return strippedForward;
620: }
621: }
622:
623: }
624:
625: /**
626: * Verifies if the ActionServlet controller used this forward.
627: *
628: * @param forwardName the logical name of a forward, as defined
629: * in the Struts configuration file. This can either refer to a
630: * global forward, or one local to the ActionMapping.
631: *
632: * @exception AssertionFailedError if the ActionServlet controller
633: * used a different forward than <code>forwardName</code> after
634: * executing an Action object.
635: */
636: public void verifyForward(String forwardName)
637: throws AssertionFailedError {
638: if (logger.isDebugEnabled())
639: logger.debug("Entering - forwardName = " + forwardName);
640: init();
641: Common.verifyForwardPath(request.getPathInfo(), forwardName,
642: getActualForward(), false, request, config
643: .getServletContext(), config);
644: if (logger.isDebugEnabled())
645: logger.debug("Exiting");
646: }
647:
648: /**
649: * Verifies if the ActionServlet controller used this actual path
650: * as a forward.
651: *
652: * @param forwardPath an absolute pathname to which the request
653: * is to be forwarded.
654: *
655: * @exception AssertionFailedError if the ActionServlet controller
656: * used a different forward path than <code>forwardPath</code> after
657: * executing an Action object.
658: */
659: public void verifyForwardPath(String forwardPath)
660: throws AssertionFailedError {
661: if (logger.isDebugEnabled())
662: logger.debug("Entering - forwardPath = " + forwardPath);
663: init();
664: String actualForward = getActualForward();
665:
666: if ((actualForward == null) && (forwardPath == null)) {
667: // actions can return null forwards.
668: return;
669: }
670:
671: forwardPath = request.getContextPath() + forwardPath;
672:
673: if (actualForward == null) {
674: if (logger.isDebugEnabled()) {
675: logger
676: .debug("actualForward is null - this usually means it is not mapped properly.");
677: }
678: throw new AssertionFailedError(
679: "Was expecting '"
680: + forwardPath
681: + "' but it appears the Action has tried to return an ActionForward that is not mapped correctly.");
682: }
683: if (logger.isDebugEnabled()) {
684: logger.debug("expected forward = '" + forwardPath
685: + "' - actual forward = '" + actualForward + "'");
686: }
687: if (!(actualForward.equals(forwardPath)))
688: throw new AssertionFailedError("was expecting '"
689: + forwardPath + "' but received '" + actualForward
690: + "'");
691: if (logger.isDebugEnabled())
692: logger.debug("Exiting");
693: }
694:
695: /**
696: * Verifies if the ActionServlet controller forwarded to the defined
697: * input path.
698: *
699: * @exception AssertionFailedError if the ActionServlet controller
700: * used a different forward than the defined input path after
701: * executing an Action object.
702: */
703: public void verifyInputForward() {
704: if (logger.isDebugEnabled())
705: logger.debug("Entering");
706: init();
707: Common.verifyForwardPath(request.getPathInfo(), null,
708: getActualForward(), true, request, config
709: .getServletContext(), config);
710: if (logger.isDebugEnabled())
711: logger.debug("Exiting");
712: }
713:
714: /**
715: * Verifies that the ActionServlet controller used this forward and Tiles definition.
716: *
717: * @param forwardName the logical name of a forward, as defined
718: * in the Struts configuration file. This can either refer to a
719: * global forward, or one local to the ActionMapping.
720: *
721: * @param definitionName the name of a Tiles definition, as defined
722: * in the Tiles configuration file.
723: *
724: * @exception AssertionFailedError if the ActionServlet controller
725: * used a different forward or tiles definition than those given after
726: * executing an Action object.
727: */
728: public void verifyTilesForward(String forwardName,
729: String definitionName) {
730: if (logger.isTraceEnabled())
731: logger.trace("Entering - forwardName=" + forwardName
732: + ", definitionName=" + definitionName);
733: init();
734: Common.verifyTilesForward(request.getPathInfo(), forwardName,
735: definitionName, false, request, config
736: .getServletContext(), config);
737: if (logger.isTraceEnabled())
738: logger.trace("Exiting");
739: }
740:
741: /**
742: * Verifies that the ActionServlet controller forwarded to the defined
743: * input Tiles definition.
744: *
745: * @param definitionName the name of a Tiles definition, as defined
746: * in the Tiles configuration file.
747: *
748: * @exception AssertionFailedError if the ActionServlet controller
749: * used a different forward than the defined input path after
750: * executing an Action object.
751: */
752: public void verifyInputTilesForward(String definitionName) {
753: if (logger.isTraceEnabled())
754: logger.trace("Entering - defintionName=" + definitionName);
755: init();
756: Common.verifyTilesForward(request.getPathInfo(), null,
757: definitionName, true, request, config
758: .getServletContext(), config);
759: if (logger.isTraceEnabled())
760: logger.trace("Exiting");
761: }
762:
763: /**
764: * Verifies if the ActionServlet controller sent these error messages.
765: * There must be an exact match between the provided error messages, and
766: * those sent by the controller, in both name and number.
767: *
768: * @param errorNames a String array containing the error message keys
769: * to be verified, as defined in the application resource properties
770: * file.
771: *
772: * @exception AssertionFailedError if the ActionServlet controller
773: * sent different error messages than those in <code>errorNames</code>
774: * after executing an Action object.
775: */
776:
777: public void verifyActionErrors(String[] errorNames) {
778: if (logger.isDebugEnabled())
779: logger.debug("Entering - errorNames = " + errorNames);
780: init();
781: Common.verifyActionMessages(request, errorNames,
782: Globals.ERROR_KEY, "error");
783: if (logger.isDebugEnabled())
784: logger.debug("Exiting");
785: }
786:
787: /**
788: * Verifies that the ActionServlet controller sent no error messages upon
789: * executing an Action object.
790: *
791: * @exception AssertionFailedError if the ActionServlet controller
792: * sent any error messages after excecuting and Action object.
793: */
794: public void verifyNoActionErrors() {
795: if (logger.isDebugEnabled())
796: logger.debug("Entering");
797: init();
798: Common.verifyNoActionMessages(request, Globals.ERROR_KEY,
799: "error");
800: if (logger.isDebugEnabled())
801: logger.debug("Exiting");
802: }
803:
804: /**
805: * Verifies if the ActionServlet controller sent these action messages.
806: * There must be an exact match between the provided action messages, and
807: * those sent by the controller, in both name and number.
808: *
809: * @param messageNames a String array containing the action message keys
810: * to be verified, as defined in the application resource properties
811: * file.
812: *
813: * @exception AssertionFailedError if the ActionServlet controller
814: * sent different action messages than those in <code>messageNames</code>
815: * after executing an Action object.
816: */
817: public void verifyActionMessages(String[] messageNames) {
818: if (logger.isDebugEnabled())
819: logger.debug("Entering - messageNames = " + messageNames);
820: init();
821: Common.verifyActionMessages(request, messageNames,
822: Globals.MESSAGE_KEY, "action");
823: if (logger.isDebugEnabled())
824: logger.debug("Exiting");
825: }
826:
827: /**
828: * Verifies that the ActionServlet controller sent no action messages upon
829: * executing an Action object.
830: *
831: * @exception AssertionFailedError if the ActionServlet controller
832: * sent any action messages after excecuting and Action object.
833: */
834: public void verifyNoActionMessages() {
835: if (logger.isDebugEnabled())
836: logger.debug("Entering");
837: init();
838: Common.verifyNoActionMessages(request, Globals.MESSAGE_KEY,
839: "action");
840: if (logger.isDebugEnabled())
841: logger.debug("Exiting");
842: }
843:
844: /**
845: * Returns the ActionForm instance stored in either the request or session. Note
846: * that no form will be returned if the Action being tested cleans up the form
847: * instance.
848: *
849: * @ return the ActionForm instance used in this test, or null if it does not exist.
850: */
851: public ActionForm getActionForm() {
852: if (logger.isDebugEnabled())
853: logger.debug("Entering");
854: init();
855: if (logger.isDebugEnabled())
856: logger.debug("Exiting");
857: return Common.getActionForm(request.getPathInfo(), request,
858: config.getServletContext());
859: }
860:
861: /**
862: * Sets an ActionForm instance to be used in this test. The given ActionForm instance
863: * will be stored in the scope specified in the Struts configuration file (ie: request
864: * or session). Note that while this ActionForm instance is passed to the test, Struts
865: * will still control how it is used. In particular, it will call the ActionForm.reset()
866: * method, so if you override this method in your ActionForm subclass, you could potentially
867: * reset attributes in the form passed through this method.
868: *
869: * @param form the ActionForm instance to be used in this test.
870: */
871: public void setActionForm(ActionForm form) {
872: if (logger.isDebugEnabled())
873: logger.debug("Entering - form = " + form);
874: init();
875: // make sure ActionServlet is initialized.
876: Common.setActionForm(form, request, request.getPathInfo(),
877: config.getServletContext());
878: if (logger.isDebugEnabled())
879: logger.debug("Exiting");
880: }
881:
882: /**
883: * Instructs StrutsTestCase to fully process a forward request. By default, StrutsTestCase
884: * stops processing a request as soon as the forward path has been collected, in order to avoid
885: * side effects; calling this method overrides this behavior.
886: * @param flag set to true to fully process forward requests
887: */
888: public void processRequest(boolean flag) {
889: if (logger.isTraceEnabled())
890: logger.trace("Entering - flag=" + flag);
891: ((StrutsServletContextWrapper) this .config.getServletContext())
892: .setProcessRequest(flag);
893: if (logger.isTraceEnabled())
894: logger.trace("Exiting");
895: }
896:
897: }
|