KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > struts > CactusStrutsTestCase


1 // StrutsTestCase - a JUnit extension for testing Struts actions
2
// within the context of the ActionServlet.
3
// Copyright (C) 2002 Deryl Seale
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the Apache Software License as
7
// published by the Apache Software Foundation; either version 1.1
8
// of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// Apache Software Foundation Licens for more details.
14
//
15
// You may view the full text here: http://www.apache.org/LICENSE.txt
16

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 JavaDoc;
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.ServletConfig JavaDoc;
30 import javax.servlet.http.*;
31 import java.util.Enumeration JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * CactusStrutsTestCase is an extension of the Cactus ServletTestCase
38  * base class that provides additional methods to aid in testing
39  * Struts Action objects. It uses an in-container approach to run
40  * the servlet container, and tests the execution of Action objects as they
41  * are actually run through the Struts ActionServlet. CactusStrutsTestCase
42  * provides methods that set up the request path, request parameters
43  * for ActionForm subclasses, as well as methods that can verify
44  * that the correct ActionForward was used and that the proper
45  * ActionError messages were supplied.
46  * <br>
47  * <br>
48  * <b>Please note</b> that this class is meant to run in the Cactus
49  * framework, and you must configure your test environment
50  * accordingly. Please see <a HREF="http://jakarta.apache.org/cactus">http://jakarta.apache.org/cactus</a>
51  * for more details.
52  *
53  */

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 JavaDoc moduleName;
61     protected String JavaDoc servletMapping = "*.do";
62
63     protected static Log logger = LogFactory.getLog(CactusStrutsTestCase.class);
64
65     /**
66      * Default constructor.
67      */

68     public CactusStrutsTestCase() {
69         super();
70     }
71
72     /**
73      * Constructor that takes test name parameter, for backwards compatibility with older versions on JUnit.
74      */

75     public CactusStrutsTestCase(String JavaDoc testName) {
76         super(testName);
77     }
78
79     /**
80      * A check that every method should run to ensure that the
81      * base class setUp method has been called.
82      */

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     /**
90      * Sets up the test fixture for this test. This method creates
91      * an instance of the ActionServlet, initializes it to validate
92      * forms and turn off debugging, and clears all other parameters.
93      */

94     protected void setUp() throws Exception JavaDoc {
95         if (logger.isDebugEnabled())
96             logger.debug("Entering");
97         try {
98
99             if (actionServlet == null)
100                 actionServlet = new ActionServlet();
101             ServletContext JavaDoc 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 JavaDoc 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     /**
117      * Sets the servlet mapping used to map requests to the Struts controller. This is used to restore
118      * proper setting after the test has been executed. By default, the stanard "*.do" mapping will be
119      * restored, so only use this method for non-standard servlet mappings.
120      * @param servletMapping
121      */

122     public void setServletMapping(String JavaDoc servletMapping) {
123         this.servletMapping = servletMapping;
124     }
125
126     protected void tearDown () throws Exception JavaDoc {
127         if (logger.isTraceEnabled())
128             logger.trace("Entering");
129
130         // remove all RequestProcessor instances from context
131
// so that web application will function normally.
132
ServletContext JavaDoc context = this.config.getServletContext();
133         List JavaDoc nameList = new ArrayList JavaDoc();
134         Enumeration JavaDoc attributeNames = context.getAttributeNames();
135         while (attributeNames.hasMoreElements()) {
136             String JavaDoc name = (String JavaDoc) attributeNames.nextElement();
137             if (name.startsWith(Globals.REQUEST_PROCESSOR_KEY))
138                 nameList.add(name);
139         }
140
141         // restore the original servlet mapping
142
ServletConfig JavaDoc originalConfig = ((org.apache.cactus.server.ServletConfigWrapper) this.config).getOriginalConfig();
143         ServletContext JavaDoc originalContext = originalConfig.getServletContext();
144         originalContext.setAttribute(Globals.SERVLET_KEY,servletMapping);
145         ((StrutsServletConfigWrapper) config).setServletContext(originalContext);
146
147
148         for (Iterator JavaDoc iterator = nameList.iterator(); iterator.hasNext();) {
149             context.removeAttribute((String JavaDoc) iterator.next());
150         }
151
152         if (logger.isTraceEnabled())
153             logger.trace("Exiting");
154     }
155
156     /**
157      * Returns an HttpServletRequest object that can be used in
158      * this test.
159      */

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     /**
170      * Returns an HttpServletResponse object that can be used in
171      * this test.
172      */

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     /**
183      * Returns an HttpSession object that can be used in this
184      * test.
185      */

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     /**
196      * Adds an HttpServletRequest parameter to be used in setting up the
197      * ActionForm instance to be used in this test. Each parameter added
198      * should correspond to an attribute in the ActionForm instance used
199      * by the Action instance being tested.
200      */

201     public void addRequestParameter(String JavaDoc parameterName, String JavaDoc 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     /**
212      * Adds an HttpServletRequest parameter that is an array of String values
213      * to be used in setting up the ActionForm instance to be used in this test.
214      * Each parameter added should correspond to an attribute in the ActionForm
215      * instance used by the Action instance being tested.
216      */

217     public void addRequestParameter(String JavaDoc parameterName, String JavaDoc[] 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     /**
228      * Clears all request parameters previously set. NOTE: This will <strong>not</strong> clear
229      * parameters set using Cactus beginXXX methods!
230      */

231     public void clearRequestParameters() {
232         init();
233         ((StrutsRequestWrapper) request).clearRequestParameters();
234     }
235
236     /**
237      * Sets the request path instructing the ActionServlet to used a
238      * particual ActionMapping.
239      *
240      * @param pathInfo the request path to be processed. This should
241      * correspond to a particular action mapping, as would normally
242      * appear in an HTML or JSP source file.
243      */

244     public void setRequestPathInfo(String JavaDoc 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     /**
255      * Sets the request path instructing the ActionServlet to used a
256      * particual ActionMapping. Also sets the ServletPath property
257      * on the request.
258      *
259      * @param moduleName the name of the Struts sub-application with
260      * which this request is associated, or null if it is the default
261      * application.
262      * @param pathInfo the request path to be processed. This should
263      * correspond to a particular action mapping, as would normally
264      * appear in an HTML or JSP source file. If this request is part
265      * of a sub-application, the module name should not appear in the
266      * request path.
267      */

268     public void setRequestPathInfo(String JavaDoc moduleName, String JavaDoc 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     /**
291      * Sets an initialization parameter on the
292      * ActionServlet. Allows you to simulate an init parameter
293      * that would normally have been found in web.xml.
294      * @param key the name of the initialization parameter
295      * @param value the value of the intialization parameter
296      */

297     public void setInitParameter(String JavaDoc key, String JavaDoc 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     /**
307      * Sets the location of the Struts configuration file for the default module.
308      * This method can take either an absolute path, or a relative path. If an
309      * absolute path is supplied, the configuration file will be loaded from the
310      * underlying filesystem; otherwise, the ServletContext loader will be used.
311      */

312     public void setConfigFile(String JavaDoc 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     /**
322      * Sets the struts configuration file for a given sub-application.
323      * This method can take either an absolute path, or a relative path. If an
324      * absolute path is supplied, the configuration file will be loaded from the
325      * underlying filesystem; otherwise, the ServletContext loader will be used.
326      *
327      * @param moduleName the name of the sub-application, or null if this is the default application
328      * @param pathname the location of the configuration file for this sub-application
329      */

330     public void setConfigFile(String JavaDoc moduleName, String JavaDoc 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     /**
344      * Returns the ActionServlet controller used in this
345      * test.
346      *
347      */

348     public ActionServlet getActionServlet() {
349         if (logger.isDebugEnabled())
350             logger.debug("Entering");
351         init();
352         try {
353             if (!actionServletIsInitialized) {
354                 // the RequestProcessor holds on to the ServletContext, so
355
// we need to ensure that it is replaced for each test.
356
ServletContext JavaDoc context = config.getServletContext();
357                 String JavaDoc name = "org.apache.struts.action.REQUEST_PROCESSOR";
358
359                 // remove request processor for default module
360
Object JavaDoc obj = context.getAttribute(name);
361                 if (obj != null) {
362                     config.getServletContext().removeAttribute(name);
363                 }
364
365                 // remove request processor for sub-applications, if used.
366
// todo: this seems pretty redundant.. may want to make this cleaner.
367
String JavaDoc 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 JavaDoc 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     /**
391      * Sets the ActionServlet to be used in this test execution. This
392      * method should only be used if you plan to use a customized
393      * version different from that provided in the Struts distribution.
394      */

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     /**
408      * Executes the Action instance to be tested. This method initializes
409      * the ActionServlet, sets up and optionally validates the ActionForm
410      * bean associated with the Action to be tested, and then calls the
411      * Action.execute() method. Results are stored for further validation.
412      *
413      * @exception AssertionFailedError if there are any execution
414      * errors while calling Action.execute() or ActionForm.validate().
415      *
416      */

417     public void actionPerform() {
418         if (logger.isDebugEnabled())
419             logger.debug("Entering");
420         if(!this.requestPathIsSet){
421             throw new IllegalStateException JavaDoc("You must call setRequestPathInfo() prior to calling actionPerform().");
422         }
423         init();
424         try {
425             HttpServletRequest request = this.request;
426             HttpServletResponse response = this.response;
427             // make sure errors are cleared from last test.
428
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 JavaDoc npe) {
436             String JavaDoc 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 JavaDoc e){
441             throw new ExceptionDuringTestError("An uncaught exception was thrown during actionExecute()", e);
442         }
443     }
444
445     /**
446      * Returns the forward sent to RequestDispatcher.
447      */

448     protected String JavaDoc 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 JavaDoc 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 JavaDoc temp = Common.stripJSessionID(forward);
464                 if (!temp.startsWith("/"))
465                     temp = "/" + temp;
466                 String JavaDoc 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     /**
479      * Verifies if the ActionServlet controller used this forward.
480      *
481      * @param forwardName the logical name of a forward, as defined
482      * in the Struts configuration file. This can either refer to a
483      * global forward, or one local to the ActionMapping.
484      *
485      * @exception AssertionFailedError if the ActionServlet controller
486      * used a different forward than <code>forwardName</code> after
487      * executing an Action object.
488      */

489     public void verifyForward(String JavaDoc 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     /**
499      * Verifies if the ActionServlet controller used this actual path
500      * as a forward.
501      *
502      * @param forwardPath an absolute pathname to which the request
503      * is to be forwarded.
504      *
505      * @exception AssertionFailedError if the ActionServlet controller
506      * used a different forward path than <code>forwardPath</code> after
507      * executing an Action object.
508      */

509     public void verifyForwardPath(String JavaDoc forwardPath) throws AssertionFailedError {
510         if (logger.isDebugEnabled())
511             logger.debug("Entering - forwardPath = " + forwardPath);
512         init();
513         String JavaDoc actualForward = getActualForward();
514
515         if ((actualForward == null) && (forwardPath == null)) {
516 // actions can return null forwards.
517
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     /**
538      * Verifies if the ActionServlet controller forwarded to the defined
539      * input path.
540      *
541      * @exception AssertionFailedError if the ActionServlet controller
542      * used a different forward than the defined input path after
543      * executing an Action object.
544      */

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     /**
555      * Verifies that the ActionServlet controller used this forward and Tiles definition.
556      *
557      * @param forwardName the logical name of a forward, as defined
558      * in the Struts configuration file. This can either refer to a
559      * global forward, or one local to the ActionMapping.
560      *
561      * @param definitionName the name of a Tiles definition, as defined
562      * in the Tiles configuration file.
563      *
564      * @exception AssertionFailedError if the ActionServlet controller
565      * used a different forward or tiles definition than those given after
566      * executing an Action object.
567      */

568     public void verifyTilesForward(String JavaDoc forwardName, String JavaDoc 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     /**
578      * Verifies that the ActionServlet controller forwarded to the defined
579      * input Tiles definition.
580      *
581      * @param definitionName the name of a Tiles definition, as defined
582      * in the Tiles configuration file.
583      *
584      * @exception AssertionFailedError if the ActionServlet controller
585      * used a different forward than the defined input path after
586      * executing an Action object.
587      */

588     public void verifyInputTilesForward(String JavaDoc 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     /**
598      * Verifies if the ActionServlet controller sent these error messages.
599      * There must be an exact match between the provided error messages, and
600      * those sent by the controller, in both name and number.
601      *
602      * @param errorNames a String array containing the error message keys
603      * to be verified, as defined in the application resource properties
604      * file.
605      *
606      * @exception AssertionFailedError if the ActionServlet controller
607      * sent different error messages than those in <code>errorNames</code>
608      * after executing an Action object.
609      */

610
611     public void verifyActionErrors(String JavaDoc[] 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     /**
621      * Verifies that the ActionServlet controller sent no error messages upon
622      * executing an Action object.
623      *
624      * @exception AssertionFailedError if the ActionServlet controller
625      * sent any error messages after excecuting and Action object.
626      */

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     /**
637      * Verifies if the ActionServlet controller sent these action messages.
638      * There must be an exact match between the provided action messages, and
639      * those sent by the controller, in both name and number.
640      *
641      * @param messageNames a String array containing the action message keys
642      * to be verified, as defined in the application resource properties
643      * file.
644      *
645      * @exception AssertionFailedError if the ActionServlet controller
646      * sent different action messages than those in <code>messageNames</code>
647      * after executing an Action object.
648      */

649     public void verifyActionMessages(String JavaDoc[] 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     /**
659      * Verifies that the ActionServlet controller sent no action messages upon
660      * executing an Action object.
661      *
662      * @exception AssertionFailedError if the ActionServlet controller
663      * sent any action messages after excecuting and Action object.
664      */

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     /**
675      * Returns the ActionForm instance stored in either the request or session. Note
676      * that no form will be returned if the Action being tested cleans up the form
677      * instance.
678      *
679      * @ return the ActionForm instance used in this test, or null if it does not exist.
680      */

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     /**
691      * Sets an ActionForm instance to be used in this test. The given ActionForm instance
692      * will be stored in the scope specified in the Struts configuration file (ie: request
693      * or session). Note that while this ActionForm instance is passed to the test, Struts
694      * will still control how it is used. In particular, it will call the ActionForm.reset()
695      * method, so if you override this method in your ActionForm subclass, you could potentially
696      * reset attributes in the form passed through this method.
697      *
698      * @param form the ActionForm instance to be used in this test.
699      */

700     public void setActionForm(ActionForm form) {
701         if (logger.isDebugEnabled())
702             logger.debug("Entering - form = " + form);
703         init();
704         // make sure ActionServlet is initialized.
705
Common.setActionForm(form,request,request.getPathInfo(),config.getServletContext());
706         if (logger.isDebugEnabled())
707             logger.debug("Exiting");
708     }
709
710     /**
711      * Instructs StrutsTestCase to fully process a forward request. By default, StrutsTestCase
712      * stops processing a request as soon as the forward path has been collected, in order to avoid
713      * side effects; calling this method overrides this behavior.
714      * @param flag set to true to fully process forward requests
715      */

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