KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > action > BaseStrutsTestCase


1 package org.appfuse.webapp.action;
2
3 import java.util.MissingResourceException JavaDoc;
4 import java.util.ResourceBundle JavaDoc;
5
6 import javax.servlet.ServletContextEvent JavaDoc;
7 import javax.servlet.ServletContextListener JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.appfuse.Constants;
12 import org.appfuse.model.User;
13 import org.appfuse.service.UserManager;
14 import org.springframework.mail.javamail.JavaMailSenderImpl;
15 import org.springframework.mock.web.MockServletContext;
16 import org.springframework.util.ClassUtils;
17 import org.springframework.web.context.ContextLoader;
18 import org.springframework.web.context.ContextLoaderListener;
19 import org.springframework.web.context.WebApplicationContext;
20 import org.springframework.web.context.support.WebApplicationContextUtils;
21
22 import servletunit.struts.MockStrutsTestCase;
23
24
25 /**
26  * This class is extended by all ActionTests. It basically
27  * contains common methods that they might use.
28  *
29  * <p>
30  * <a HREF="BaseStrutsTestCase.java.htm"><i>View Source</i></a>
31  *
32  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
33  */

34 public abstract class BaseStrutsTestCase extends MockStrutsTestCase {
35     //~ Instance fields ========================================================
36

37     protected final Log log = LogFactory.getLog(getClass());
38     protected User user = null;
39     protected ResourceBundle JavaDoc rb = null;
40     protected WebApplicationContext ctx = null;
41     
42     //~ Constructors ===========================================================
43

44     public BaseStrutsTestCase(String JavaDoc name) {
45         super(name);
46         // Since a ResourceBundle is not required for each class, just
47
// do a simple check to see if one exists
48
String JavaDoc className = this.getClass().getName();
49
50         try {
51             rb = ResourceBundle.getBundle(className);
52         } catch (MissingResourceException JavaDoc mre) {
53             //log.warn("No resource bundle found for: " + className);
54
}
55     }
56
57     //~ Methods ================================================================
58

59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         
62         // initialize Spring
63
String JavaDoc pkg = ClassUtils.classPackageAsResourcePath(Constants.class);
64         MockServletContext sc = new MockServletContext("");
65         sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
66                 "classpath*:/" + pkg + "/dao/applicationContext-*.xml," +
67                 "classpath*:/" + pkg + "/service/applicationContext-service.xml," +
68                 "classpath*:META-INF/applicationContext-*.xml");
69         
70         ServletContextListener JavaDoc contextListener = new ContextLoaderListener();
71         ServletContextEvent JavaDoc event = new ServletContextEvent JavaDoc(sc);
72         contextListener.contextInitialized(event);
73         
74         // magic bridge to make StrutsTestCase aware of Spring's Context
75
getSession().getServletContext().setAttribute(
76                 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
77                 sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
78         
79         ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(
80                     getSession().getServletContext());
81         
82         // populate the userForm and place into session
83
UserManager userMgr = (UserManager) ctx.getBean("userManager");
84         user = userMgr.getUserByUsername("tomcat");
85
86         // change the port on the mailSender so it doesn't conflict with an
87
// existing SMTP server on localhost
88
JavaMailSenderImpl mailSender = (JavaMailSenderImpl) ctx.getBean("mailSender");
89         mailSender.setPort(2525);
90         mailSender.setHost("localhost");
91     }
92     
93     public void tearDown() throws Exception JavaDoc {
94         super.tearDown();
95         ctx = null;
96     }
97 }
98
Popular Tags