1 package org.appfuse.webapp.action; 2 3 import java.util.MissingResourceException ; 4 import java.util.ResourceBundle ; 5 6 import javax.servlet.ServletContextEvent ; 7 import javax.servlet.ServletContextListener ; 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 34 public abstract class BaseStrutsTestCase extends MockStrutsTestCase { 35 37 protected final Log log = LogFactory.getLog(getClass()); 38 protected User user = null; 39 protected ResourceBundle rb = null; 40 protected WebApplicationContext ctx = null; 41 42 44 public BaseStrutsTestCase(String name) { 45 super(name); 46 String className = this.getClass().getName(); 49 50 try { 51 rb = ResourceBundle.getBundle(className); 52 } catch (MissingResourceException mre) { 53 } 55 } 56 57 59 protected void setUp() throws Exception { 60 super.setUp(); 61 62 String 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 contextListener = new ContextLoaderListener(); 71 ServletContextEvent event = new ServletContextEvent (sc); 72 contextListener.contextInitialized(event); 73 74 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 UserManager userMgr = (UserManager) ctx.getBean("userManager"); 84 user = userMgr.getUserByUsername("tomcat"); 85 86 JavaMailSenderImpl mailSender = (JavaMailSenderImpl) ctx.getBean("mailSender"); 89 mailSender.setPort(2525); 90 mailSender.setHost("localhost"); 91 } 92 93 public void tearDown() throws Exception { 94 super.tearDown(); 95 ctx = null; 96 } 97 } 98 | Popular Tags |