1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program 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.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.util;
16  
17  import org.figure8.join.control.JoinForm;
18  import org.figure8.join.control.JoinAction;
19  
20  import org.apache.struts.Globals;
21  import org.apache.struts.action.ActionErrors;
22  import org.apache.struts.action.ActionMapping;
23  import org.springframework.mock.web.MockServletContext;
24  import org.springframework.mock.web.MockHttpServletRequest;
25  import junit.framework.TestCase;
26  
27  import javax.servlet.ServletContext;
28  /**
29   * This is an abstract super class for JUnit tests for Join Struts forms.
30   * Subclasses only have to implement the <code>getFormToValidate()</code>.
31   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
32   * @version $Revision: 1.2 $
33   */
34  public abstract class JoinFormTestCase extends TestCase{
35  
36     // Attributes ---------------------------------------------------------------
37  
38     /** A stub for ServletContext */
39     protected MockServletContext context;
40     /** A stub for HttpServletRequest */
41     protected MockHttpServletRequest request;
42  
43     /** The JoinForm instance for testing validation */
44     protected JoinForm form = null;
45  
46  
47     // Public -------------------------------------------------------------------
48  
49     /**
50      * Initialize the stub object we will need for validating the tested
51      * form. This involves creating Http stub objects, resources bundles
52      * used by {@link JoinForm}s and ActionServlet. Lastly, the <code>getFormToValidate()</code>
53      * method is called allowing subclasses to specify the form implementation
54      * they want to setup test for.
55      */
56     public void setUp(){
57        // Create stubs for web objects we need.
58        context = new MockServletContext();
59        request = new MockHttpServletRequest(context);
60        // Create stubs for resource bundles available in JoinForm.
61        MessageResourcesStub guiResources = new MessageResourcesStub("gui");
62        MessageResourcesStub msgResources = new MessageResourcesStub("msg");
63        // Register them into servlet context.
64        context.setAttribute(JoinAction.GUI_KEY, guiResources);
65        context.setAttribute(Globals.MESSAGES_KEY, msgResources);
66        // Create also a stub for ActionServlet.
67        ActionServletStub actionServlet = new ActionServletStub();
68        actionServlet.setServletContext(context);
69        // Last, retrieve form and prepare it for validation.
70        form = getFormToValidate();
71        form.setServlet(actionServlet);
72     }
73  
74     /**
75      * Set the operation the JoinForm will have to validate
76      * @param operation Name of operation.
77      */
78     public void setOperation(String operation){
79        // Create a new request and add corresponding parameter.
80        request = new MockHttpServletRequest(context);
81        request.addParameter(JoinAction.OP_PARAMETER, operation);
82     }
83  
84     /** @return The stub ServletContext used during test */
85     public ServletContext getServletContext(){
86        return context;
87     }
88  
89     /**
90      * Actually call the <code>validate()</code> method onto the
91      * JoinForm retrieved from <code>getFormToValidate()</code> method
92      * implementation.
93      * @return The ActionErrors created during form validation
94      */
95     public ActionErrors validateForm(){
96        return form.validate(new ActionMapping(), request);
97     }
98  
99  
100    // Abstract -----------------------------------------------------------------
101 
102    /**
103     * Subclasses have to implement this method. It should provide base
104     * class the {@link JoinForm} implementation to validate within tests.
105     * @return The JoinForm instance for testing validation
106     */
107    public abstract JoinForm getFormToValidate();
108 }