KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > struts > MockStrutsTestCase


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 junit.framework.TestCase;
21 import org.apache.commons.digester.Digester;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.struts.Globals;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionServlet;
27 import servletunit.HttpServletRequestSimulator;
28 import servletunit.HttpServletResponseSimulator;
29 import servletunit.ServletConfigSimulator;
30 import servletunit.ServletContextSimulator;
31
32 import javax.servlet.http.*;
33 import java.io.File JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.net.URL JavaDoc;
36
37 /**
38  * MockStrutsTestCase is an extension of the base JUnit testcase that
39  * provides additional methods to aid in testing Struts Action
40  * objects. It uses a mock object approach to simulate a servlet
41  * container, and tests the execution of Action objects as they
42  * are actually run through the Struts ActionServlet. MockStrutsTestCase
43  * provides methods that set up the request path, request parameters
44  * for ActionForm subclasses, as well as methods that can verify
45  * that the correct ActionForward was used and that the proper
46  * ActionError messages were supplied.
47  *
48  *<br><br>
49  *<b>NOTE:</b> By default, the Struts ActionServlet will look for the
50  * file <code>WEB-INF/struts-config.xml</code>, so you must place
51  * the directory that <i>contains</i> WEB-INF in your CLASSPATH. If
52  * you would like to use an alternate configuration file, please see
53  * the setConfigFile() method for details on how this file is located.
54  */

55 public class MockStrutsTestCase extends TestCase {
56
57     protected ActionServlet actionServlet;
58     protected HttpServletRequestSimulator request;
59     protected HttpServletResponseSimulator response;
60     protected ServletContextSimulator context;
61     protected ServletConfigSimulator config;
62     protected String JavaDoc actionPath;
63     protected boolean isInitialized = false;
64     protected boolean actionServletIsInitialized = false;
65     protected boolean requestPathSet = false;
66
67     /**
68      * The set of public identifiers, and corresponding resource names, for
69      * the versions of the configuration file DTDs that we know about. There
70      * <strong>MUST</strong> be an even number of Strings in this list!
71      */

72     protected String JavaDoc registrations[] = {
73         "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN",
74         "/org/apache/struts/resources/web-app_2_2.dtd",
75         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
76         "/org/apache/struts/resources/web-app_2_3.dtd"
77     };
78
79
80     protected static Log logger = LogFactory.getLog(MockStrutsTestCase.class);
81
82     /**
83      * Default constructor.
84      */

85     public MockStrutsTestCase() {
86         super();
87     }
88
89     /**
90      * Constructor that takes test name parameter, for backwards compatibility with older versions on JUnit.
91      */

92     public MockStrutsTestCase(String JavaDoc testName) {
93         super(testName);
94     }
95
96     /**
97      * A check that every method should run to ensure that the
98      * base class setUp method has been called.
99      */

100     private void init() {
101         if (!isInitialized)
102             throw new AssertionFailedError("You are overriding the setUp() method without calling super.setUp(). You must call the superclass setUp() method in your TestCase subclass to ensure proper initialization.");
103     }
104
105     /**
106      * Sets up the test fixture for this test. This method creates
107      * an instance of the ActionServlet, initializes it to validate
108      * forms and turn off debugging, and creates a mock HttpServletRequest
109      * and HttpServletResponse object to use in this test.
110      */

111     protected void setUp() throws Exception JavaDoc {
112         if (logger.isDebugEnabled())
113             logger.debug("Entering");
114         if (actionServlet == null)
115             actionServlet = new ActionServlet();
116         config = new ServletConfigSimulator();
117         request = new HttpServletRequestSimulator(config.getServletContext());
118         response = new HttpServletResponseSimulator();
119         context = (ServletContextSimulator) config.getServletContext();
120         isInitialized = true;
121         if (logger.isDebugEnabled())
122             logger.debug("Exiting");
123     }
124
125     protected void tearDown() throws Exception JavaDoc {
126         ActionServlet servlet = getActionServlet();
127         servlet.destroy();
128         setActionServlet(servlet);
129     }
130
131     /**
132      * Returns an HttpServletRequest object that can be used in
133      * this test.
134      */

135     public HttpServletRequest getRequest() {
136         if (logger.isDebugEnabled())
137             logger.debug("Entering");
138         init();
139         if (logger.isDebugEnabled())
140             logger.debug("Exiting");
141         return this.request;
142     }
143
144     /**
145      * Clears all request parameters previously set.
146      */

147     public void clearRequestParameters() {
148         if (logger.isTraceEnabled())
149             logger.trace("Entering");
150         this.request.getParameterMap().clear();
151
152         // also, clear out the redirect header if it's there.
153
response.removeHeader("Location");
154         if (logger.isTraceEnabled())
155             logger.trace("Exiting");
156     }
157
158     /**
159      * Returns an HttpServletResponse object that can be used in
160      * this test.
161      */

162     public HttpServletResponse getResponse() {
163         if (logger.isDebugEnabled())
164             logger.debug("Entering");
165         init();
166         if (logger.isDebugEnabled())
167             logger.debug("Exiting");
168         return this.response;
169     }
170
171     /**
172      * Returns the mock HttpServletRequest object used in this test. This allows
173      * access to methods for setting up test preconditions that are otherwise
174      * unavailable through the normal Servlet API.
175      */

176     public HttpServletRequestSimulator getMockRequest() {
177         if (logger.isTraceEnabled())
178             logger.trace("Entering");
179         init();
180         if (logger.isTraceEnabled())
181             logger.trace("Exiting");
182         return this.request;
183     }
184
185     /**
186      * Returns the mock HttpServletResponse object used in this test. This allows
187      * access to methods for setting up test preconditions that are otherwise
188      * unavailable through the normal Servlet API.
189      */

190     public HttpServletResponseSimulator getMockResponse() {
191         if (logger.isTraceEnabled())
192             logger.trace("Entering");
193         init();
194         if (logger.isTraceEnabled())
195             logger.trace("Exiting");
196         return this.response;
197     }
198
199     /**
200      * Returns an HttpSession object that can be used in this
201      * test.
202      */

203     public HttpSession getSession() {
204         if (logger.isDebugEnabled())
205             logger.debug("Entering");
206         init();
207         if (logger.isDebugEnabled())
208             logger.debug("Exiting");
209         return this.request.getSession(true);
210     }
211
212     /**
213      * Returns the ActionServlet controller used in this
214      * test.
215      *
216      */

217     public ActionServlet getActionServlet() {
218         if (logger.isDebugEnabled())
219             logger.debug("Entering");
220         init();
221         try {
222             if (!actionServletIsInitialized) {
223                 if (logger.isDebugEnabled()) {
224                     logger.debug("intializing actionServlet");
225                 }
226                 this.actionServlet.init(config);
227                 actionServletIsInitialized = true;
228             }
229         } catch (Exception JavaDoc e) {
230             logger.error("Error initializing action servlet",e);
231             if(e.getMessage().equals("java.lang.NullPointerException")){
232                 String JavaDoc message = "Error initializing action servlet: Unable to find /WEB-INF/web.xml. "
233                                + "TestCase is running from " + System.getProperty("user.dir") + " directory. "
234                                + "Context directory ";
235                 if(this.context.getContextDirectory()==null){
236                     message += "has not been set. Try calling setContextDirectory() with a relative or absolute path";
237                 }else{
238                     message = message + "is " + this.context.getContextDirectory().getAbsolutePath();
239                 }
240                 message = message + ". /WEB-INF/web.xml must be found under the context directory, "
241                         + "the directory the test case is running from, or in the classpath.";
242                 fail(message);
243             }else{
244                 throw new AssertionFailedError(e.getMessage());
245             }
246         }
247         if (logger.isDebugEnabled())
248             logger.debug("Exiting");
249         return actionServlet;
250     }
251
252     /**
253      * Sets the ActionServlet to be used in this test execution. This
254      * method should only be used if you plan to use a customized
255      * version different from that provided in the Struts distribution.
256      */

257     public void setActionServlet(ActionServlet servlet) {
258         if (logger.isDebugEnabled())
259             logger.debug("Entering - servlet = " + servlet);
260         init();
261         if (servlet == null)
262             throw new AssertionFailedError("Cannot set ActionServlet to null");
263         this.actionServlet = servlet;
264         if (logger.isDebugEnabled())
265             logger.debug("Exiting");
266         actionServletIsInitialized = false;
267     }
268
269     /**
270      * Executes the Action instance to be tested. This method
271      * calls the ActionServlet.doPost() method to execute the
272      * Action instance to be tested, passing along any parameters
273      * set in the HttpServletRequest object. It stores any results
274      * for further validation.
275      *
276      * @exception AssertionFailedError if there are any execution
277      * errors while calling Action.execute()
278      *
279      */

280     public void actionPerform() {
281         if (logger.isDebugEnabled())
282             logger.debug("Entering");
283         if(!this.requestPathSet){
284             throw new IllegalStateException JavaDoc("You must call setRequestPathInfo() prior to calling actionPerform().");
285         }
286         init();
287         HttpServletRequest request = this.request;
288         HttpServletResponse response = this.response;
289         try {
290             this.getActionServlet().doPost(request,response);
291         }catch (NullPointerException JavaDoc npe) {
292                 String JavaDoc message = "A NullPointerException was thrown. This may indicate an error in your ActionForm, or "
293                                + "it may indicate that the Struts ActionServlet was unable to find struts config file. "
294                                + "TestCase is running from " + System.getProperty("user.dir") + " directory. "
295                                + "Context directory ";
296                 if(this.context.getContextDirectory()==null){
297                     message += "has not been set. Try calling setContextDirectory() with a relative or absolute path";
298                 }else{
299                     message = message + "is " + this.context.getContextDirectory().getAbsolutePath();
300                 }
301                 message = message + ". struts config file must be found under the context directory, "
302                         + "the directory the test case is running from, or in the classpath.";
303                 throw new ExceptionDuringTestError(message, npe);
304         }catch(Exception JavaDoc e){
305                 throw new ExceptionDuringTestError("An uncaught exception was thrown during actionExecute()", e);
306         }
307         if (logger.isDebugEnabled())
308             logger.debug("Exiting");
309     }
310
311     /**
312      * Adds an HttpServletRequest parameter to be used in setting up the
313      * ActionForm instance to be used in this test. Each parameter added
314      * should correspond to an attribute in the ActionForm instance used
315      * by the Action instance being tested.
316      */

317     public void addRequestParameter(String JavaDoc parameterName, String JavaDoc parameterValue)
318     {
319         if (logger.isDebugEnabled())
320             logger.debug("Entering - parameterName = " + parameterName + ", parameterValue = " + parameterValue);
321         init();
322         this.request.addParameter(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 JavaDoc parameterName, String JavaDoc[] parameterValues)
334     {
335         if (logger.isDebugEnabled())
336             logger.debug("Entering - parameterName = " + parameterName + ", parameteValue = " + parameterValues);
337         init();
338         this.request.addParameter(parameterName,parameterValues);
339         if (logger.isDebugEnabled())
340             logger.debug("Exiting");
341     }
342
343     /**
344      * Sets the request path instructing the ActionServlet to used a
345      * particual ActionMapping.
346      *
347      * @param pathInfo the request path to be processed. This should
348      * correspond to a particular action mapping, as would normally
349      * appear in an HTML or JSP source file.
350      */

351     public void setRequestPathInfo(String JavaDoc pathInfo) {
352         if (logger.isDebugEnabled())
353             logger.debug("Entering - pathInfo = " + pathInfo);
354         init();
355         this.setRequestPathInfo("",pathInfo);
356         if (logger.isDebugEnabled())
357             logger.debug("Exiting");
358     }
359
360     /**
361      * Sets the request path instructing the ActionServlet to used a
362      * particual ActionMapping. Also sets the ServletPath property
363      * on the request.
364      *
365      * @param moduleName the name of the Struts sub-application with
366      * which this request is associated, or null if it is the default
367      * application.
368      * @param pathInfo the request path to be processed. This should
369      * correspond to a particular action mapping, as would normally
370      * appear in an HTML or JSP source file. If this request is part
371      * of a sub-application, the module name should not appear in the
372      * request path.
373      */

374     public void setRequestPathInfo(String JavaDoc moduleName, String JavaDoc pathInfo) {
375         if (logger.isDebugEnabled())
376             logger.debug("Entering - moduleName = " + moduleName + ", pathInfo = " + pathInfo);
377         init();
378         this.actionPath = Common.stripActionPath(pathInfo);
379         if (moduleName != null) {
380             if (!moduleName.equals("")) {
381                 if (!moduleName.startsWith("/"))
382                     moduleName = "/" + moduleName;
383                 if (!moduleName.endsWith("/"))
384                     moduleName = moduleName + "/";
385             }
386             if (logger.isDebugEnabled()) {
387                 logger.debug("setting request attribute - name = " + Common.INCLUDE_SERVLET_PATH + ", value = " + moduleName);
388             }
389             this.request.setAttribute(Common.INCLUDE_SERVLET_PATH, moduleName);
390         }
391         this.request.setPathInfo(actionPath);
392         this.requestPathSet = true;
393         if (logger.isDebugEnabled())
394             logger.debug("Exiting");
395     }
396
397     /**
398      * Sets an initialization parameter on the
399      * ActionServlet. Allows you to simulate an init parameter
400      * that would normally have been found in web.xml,
401      * but is not available while testing with mock objects.
402      * @param key the name of the initialization parameter
403      * @param value the value of the intialization parameter
404      */

405     public void setInitParameter(String JavaDoc key, String JavaDoc value){
406         if (logger.isDebugEnabled())
407             logger.debug("Entering - key = " + key + ", value = " + value);
408         init();
409         config.setInitParameter(key, value);
410         actionServletIsInitialized = false;
411         if (logger.isDebugEnabled())
412             logger.debug("Exiting");
413     }
414
415     /**
416      * Sets the context directory to be used with the getRealPath() methods in
417      * the ServletContext and HttpServletRequest API.
418      * @param contextDirectory a File object representing the root context directory
419      * for this application.
420      */

421     public void setContextDirectory(File JavaDoc contextDirectory) {
422         if (logger.isDebugEnabled())
423             logger.debug("Entering - contextDirectory = " + contextDirectory);
424         init();
425         context.setContextDirectory(contextDirectory);
426         actionServletIsInitialized = false;
427         if (logger.isDebugEnabled())
428             logger.debug("Exiting");
429     }
430
431     /**
432      * Sets the location of the Struts configuration file for the default module.
433      * This method can take either an absolute path, or a relative path. If an
434      * absolute path is supplied, the configuration file will be loaded from the
435      * underlying filesystem; otherwise, the ServletContext loader will be used.
436      */

437     public void setConfigFile(String JavaDoc pathname) {
438         if (logger.isDebugEnabled())
439             logger.debug("Entering - pathName = " + pathname);
440         init();
441         setConfigFile(null,pathname);
442         if (logger.isDebugEnabled())
443             logger.debug("Exiting");
444     }
445
446     /**
447      * Sets the struts configuration file for a given sub-application. This method
448      * can take either an absolute path, or a relative path. If an absolute path
449      * is supplied, the configuration file will be loaded from the underlying
450      * filesystem; otherwise, the ServletContext loader will be used.
451      *
452      * @param moduleName the name of the sub-application, or null if this is the default application
453      * @param pathname the location of the configuration file for this sub-application
454      */

455     public void setConfigFile(String JavaDoc moduleName, String JavaDoc pathname) {
456         if (logger.isDebugEnabled())
457             logger.debug("Entering - moduleName = " + moduleName + ", pathname =" + pathname);
458         init();
459         if (moduleName == null)
460             this.config.setInitParameter("config",pathname);
461         else
462             this.config.setInitParameter("config/" + moduleName,pathname);
463         actionServletIsInitialized = false;
464         if (logger.isDebugEnabled())
465             logger.debug("Exiting");
466     }
467
468     /**
469      * Sets the location of the web.xml configuration file to be used
470      * to set up the servlet context and configuration for this test.
471      * This method supports both init-param and context-param tags,
472      * setting the ServletConfig and ServletContext appropriately.
473      * This method can take either an absolute path, or a relative path. If an
474      * absolute path is supplied, the configuration file will be loaded from the
475      * underlying filesystem; otherwise, the ServletContext loader will be used.
476      */

477     public void setServletConfigFile(String JavaDoc pathname) {
478         if (logger.isDebugEnabled())
479             logger.debug("Entering - pathname = " + pathname);
480         init();
481
482         // pull in the appropriate parts of the
483
// web.xml file -- first the init-parameters
484
Digester digester = new Digester();
485         digester.push(this.config);
486         digester.setValidating(true);
487         digester.addCallMethod("web-app/servlet/init-param", "setInitParameter", 2);
488         digester.addCallParam("web-app/servlet/init-param/param-name", 0);
489         digester.addCallParam("web-app/servlet/init-param/param-value", 1);
490         try {
491             for (int i = 0; i < registrations.length; i += 2) {
492                 URL JavaDoc url = context.getResource(registrations[i + 1]);
493                 if (url != null)
494                     digester.register(registrations[i], url.toString());
495             }
496             InputStream JavaDoc input = context.getResourceAsStream(pathname);
497             if(input==null)
498                 throw new AssertionFailedError("Invalid pathname: " + pathname);
499             digester.parse(input);
500             input.close();
501         } catch (Exception JavaDoc e) {
502             throw new AssertionFailedError("Received an exception while loading web.xml - " + e.getClass() + " : " + e.getMessage());
503         }
504
505         // now the context parameters..
506
digester = new Digester();
507         digester.setValidating(true);
508         digester.push(this.context);
509         digester.addCallMethod("web-app/context-param", "setInitParameter", 2);
510         digester.addCallParam("web-app/context-param/param-name", 0);
511         digester.addCallParam("web-app/context-param/param-value", 1);
512         try {
513             for (int i = 0; i < registrations.length; i += 2) {
514                 URL JavaDoc url = context.getResource(registrations[i + 1]);
515                 if (url != null)
516                     digester.register(registrations[i], url.toString());
517             }
518             InputStream JavaDoc input = context.getResourceAsStream(pathname);
519             if(input==null)
520                 throw new AssertionFailedError("Invalid pathname: " + pathname);
521             digester.parse(input);
522             input.close();
523         } catch (Exception JavaDoc e) {
524             throw new AssertionFailedError("Received an exception while loading web.xml - " + e.getClass() + " : " + e.getMessage());
525         }
526         actionServletIsInitialized = false;
527         if (logger.isDebugEnabled())
528             logger.debug("Exiting");
529     }
530
531     /**
532      * Returns the forward sent to RequestDispatcher.
533      */

534     protected String JavaDoc getActualForward() {
535         if (logger.isDebugEnabled())
536             logger.debug("Entering");
537         if (response.containsHeader("Location")) {
538             return Common.stripJSessionID(response.getHeader("Location"));
539         } else
540             try {
541                 String JavaDoc strippedForward = request.getContextPath() + Common.stripJSessionID(((ServletContextSimulator) config.getServletContext()).getRequestDispatcherSimulator().getForward());
542                 if (logger.isDebugEnabled()) {
543                     logger.debug("stripped forward and added context path - " + strippedForward);
544                 }
545                 if (logger.isDebugEnabled())
546                     logger.debug("Exiting");
547                 return strippedForward;
548             } catch (NullPointerException JavaDoc npe) {
549                 if (logger.isDebugEnabled()) {
550                     logger.debug("caught NullPointerException - returning null",npe);
551                 }
552                 return null;
553             }
554     }
555
556     /**
557      * Verifies if the ActionServlet controller used this forward.
558      *
559      * @param forwardName the logical name of a forward, as defined
560      * in the Struts configuration file. This can either refer to a
561      * global forward, or one local to the ActionMapping.
562      *
563      * @exception AssertionFailedError if the ActionServlet controller
564      * used a different forward than <code>forwardName</code> after
565      * executing an Action object.
566      */

567     public void verifyForward(String JavaDoc forwardName) throws AssertionFailedError {
568         if (logger.isDebugEnabled())
569             logger.debug("Entering - forwardName = " + forwardName);
570         init();
571         Common.verifyForwardPath(actionPath,forwardName,getActualForward(),false,request,config.getServletContext(),config);
572         if (logger.isDebugEnabled())
573             logger.debug("Exiting");
574     }
575
576     /**
577      * Verifies if the ActionServlet controller used this actual path
578      * as a forward.
579      *
580      * @param forwardPath an absolute pathname to which the request
581      * is to be forwarded.
582      *
583      * @exception AssertionFailedError if the ActionServlet controller
584      * used a different forward path than <code>forwardPath</code> after
585      * executing an Action object.
586      */

587     public void verifyForwardPath(String JavaDoc forwardPath) throws AssertionFailedError {
588         if (logger.isDebugEnabled())
589             logger.debug("Entering - forwardPath = " + forwardPath);
590         init();
591         String JavaDoc actualForward = getActualForward();
592         if ((actualForward == null) && (forwardPath == null)) {
593             // actions can send null forwards, which is fine.
594
return;
595         }
596
597         forwardPath = request.getContextPath() + forwardPath;
598
599         if (actualForward == null) {
600             if (logger.isDebugEnabled()) {
601                 logger.debug("actualForward is null - this usually means it is not mapped properly.");
602             }
603             throw new AssertionFailedError("Was expecting '" + forwardPath + "' but it appears the Action has tried to return an ActionForward that is not mapped correctly.");
604         }
605         if (logger.isDebugEnabled()) {
606             logger.debug("expected forward = '" + forwardPath + "' - actual forward = '" + actualForward + "'");
607         }
608         if (!(actualForward.equals(forwardPath)))
609             throw new AssertionFailedError("was expecting '" + forwardPath + "' but received '" + actualForward + "'");
610         if (logger.isDebugEnabled())
611             logger.debug("Exiting");
612     }
613
614     /**
615      * Verifies if the ActionServlet controller forwarded to the defined
616      * input path.
617      *
618      * @exception AssertionFailedError if the ActionServlet controller
619      * used a different forward than the defined input path after
620      * executing an Action object.
621      */

622     public void verifyInputForward() {
623         if (logger.isDebugEnabled())
624             logger.debug("Entering");
625         init();
626         Common.verifyForwardPath(actionPath,null,getActualForward(),true,request,config.getServletContext(),config);
627         if (logger.isDebugEnabled())
628             logger.debug("Exiting");
629     }
630
631     /**
632      * Verifies that the ActionServlet controller used this forward and Tiles definition.
633      *
634      * @param forwardName the logical name of a forward, as defined
635      * in the Struts configuration file. This can either refer to a
636      * global forward, or one local to the ActionMapping.
637      *
638      * @param definitionName the name of a Tiles definition, as defined
639      * in the Tiles configuration file.
640      *
641      * @exception AssertionFailedError if the ActionServlet controller
642      * used a different forward or tiles definition than those given after
643      * executing an Action object.
644      */

645     public void verifyTilesForward(String JavaDoc forwardName, String JavaDoc definitionName) {
646         if (logger.isTraceEnabled())
647             logger.trace("Entering - forwardName=" + forwardName + ", definitionName=" + definitionName);
648         init();
649         Common.verifyTilesForward(actionPath,forwardName,definitionName,false,request,config.getServletContext(),config);
650         if (logger.isTraceEnabled())
651             logger.trace("Exiting");
652     }
653
654     /**
655      * Verifies that the ActionServlet controller forwarded to the defined
656      * input Tiles definition.
657      *
658      * @param definitionName the name of a Tiles definition, as defined
659      * in the Tiles configuration file.
660      *
661      * @exception AssertionFailedError if the ActionServlet controller
662      * used a different forward than the defined input path after
663      * executing an Action object.
664      */

665     public void verifyInputTilesForward(String JavaDoc definitionName) {
666         if (logger.isTraceEnabled())
667             logger.trace("Entering - definitionName=" + definitionName);
668         init();
669         Common.verifyTilesForward(actionPath,null,definitionName,true,request,config.getServletContext(),config);
670         if (logger.isTraceEnabled())
671             logger.trace("Exiting");
672     }
673
674     /**
675      * Verifies if the ActionServlet controller sent these error messages.
676      * There must be an exact match between the provided error messages, and
677      * those sent by the controller, in both name and number.
678      *
679      * @param errorNames a String array containing the error message keys
680      * to be verified, as defined in the application resource properties
681      * file.
682      *
683      * @exception AssertionFailedError if the ActionServlet controller
684      * sent different error messages than those in <code>errorNames</code>
685      * after executing an Action object.
686      */

687
688     public void verifyActionErrors(String JavaDoc[] errorNames) {
689         if (logger.isDebugEnabled())
690             logger.debug("errorNames = " + errorNames);
691         init();
692         Common.verifyActionMessages(request,errorNames,Globals.ERROR_KEY,"error");
693         if (logger.isDebugEnabled())
694             logger.debug("Exiting");
695     }
696
697
698     /**
699      * Verifies that the ActionServlet controller sent no error messages upon
700      * executing an Action object.
701      *
702      * @exception AssertionFailedError if the ActionServlet controller
703      * sent any error messages after excecuting and Action object.
704      */

705     public void verifyNoActionErrors() {
706         if (logger.isDebugEnabled())
707             logger.debug("Entering");
708         init();
709         Common.verifyNoActionMessages(request,Globals.ERROR_KEY,"error");
710         if (logger.isDebugEnabled())
711             logger.debug("Exiting");
712     }
713
714     /**
715      * Verifies if the ActionServlet controller sent these action messages.
716      * There must be an exact match between the provided action messages, and
717      * those sent by the controller, in both name and number.
718      *
719      * @param messageNames a String array containing the action message keys
720      * to be verified, as defined in the application resource properties
721      * file.
722      *
723      * @exception AssertionFailedError if the ActionServlet controller
724      * sent different action messages than those in <code>messageNames</code>
725      * after executing an Action object.
726      */

727     public void verifyActionMessages(String JavaDoc[] messageNames) {
728         if (logger.isDebugEnabled())
729             logger.debug("Entering - messageNames = " + messageNames);
730         init();
731         Common.verifyActionMessages(request,messageNames,Globals.MESSAGE_KEY,"action");
732         if (logger.isDebugEnabled())
733             logger.debug("Exiting");
734     }
735
736     /**
737      * Verifies that the ActionServlet controller sent no action messages upon
738      * executing an Action object.
739      *
740      * @exception AssertionFailedError if the ActionServlet controller
741      * sent any action messages after excecuting and Action object.
742      */

743     public void verifyNoActionMessages() {
744         if (logger.isDebugEnabled())
745             logger.debug("Entering");
746         init();
747         Common.verifyNoActionMessages(request,Globals.MESSAGE_KEY,"action");
748         if (logger.isDebugEnabled())
749             logger.debug("Exiting");
750     }
751
752     /**
753      * Returns the ActionForm instance stored in either the request or session. Note
754      * that no form will be returned if the Action being tested cleans up the form
755      * instance.
756      *
757      * @ return the ActionForm instance used in this test, or null if it does not exist.
758      */

759     public ActionForm getActionForm() {
760         if (logger.isDebugEnabled())
761             logger.debug("Entering");
762         init();
763         if (logger.isDebugEnabled())
764             logger.debug("Exiting");
765         return Common.getActionForm(actionPath,request,context);
766     }
767
768     /**
769      * Sets an ActionForm instance to be used in this test. The given ActionForm instance
770      * will be stored in the scope specified in the Struts configuration file (ie: request
771      * or session). Note that while this ActionForm instance is passed to the test, Struts
772      * will still control how it is used. In particular, it will call the ActionForm.reset()
773      * method, so if you override this method in your ActionForm subclass, you could potentially
774      * reset attributes in the form passed through this method.
775      *
776      * @param form the ActionForm instance to be used in this test.
777      */

778     public void setActionForm(ActionForm form) {
779         if (logger.isDebugEnabled())
780             logger.debug("Entering - form = " + form);
781         init();
782         // make sure action servlet is intialized
783
getActionServlet();
784         Common.setActionForm(form,request,actionPath,context);
785         if (logger.isDebugEnabled())
786             logger.debug("Exiting");
787     }
788
789
790
791
792 }
793
794
Popular Tags