1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.webapp.controller.testing;
17
18 import java.io.File;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.jmock.Mock;
23 import org.jmock.MockObjectTestCase;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.web.context.WebApplicationContext;
26 import org.springframework.web.context.support.WebApplicationContextUtils;
27
28 import servletunit.ServletContextSimulator;
29
30 /***
31 * <p>
32 * Base class for Struts testing that mocks Spring ApplicationContext and setups
33 * the paths for struts-config.xml file. <br/>By default sets the webapp
34 * directory to the system property "maven.war.webapp.dir" and ConfigFile to
35 * "WEB-INF/struts/struts-config.xml"
36 * </p>
37 *
38 * <p>
39 * It also allows mock testing with jmock and StrutsTestCase at the same time.
40 * Struts operations are available with the struts() method so in the subclasses
41 * you can call for example
42 *
43 * <pre>
44 * struts().setRequestPathInfo(..);
45 * struts().addRequestParameter(..);
46 * </pre>
47 *
48 * </p>
49 *
50 * @author Carlos Sanchez
51 * @version $Revision: 1.3 $
52 */
53 public class JMockStrutsTestCase extends MockObjectTestCase {
54
55 private static final Log log = LogFactory.getLog(JMockStrutsTestCase.class);
56
57 private static final String CONFIG_FILE = "WEB-INF/struts/struts-config.xml";
58
59 private static final String CONTEXT_DIRECTORY_PROPERTY = "maven.war.webapp.dir";
60
61 private static final String contextDirectory = System
62 .getProperty(CONTEXT_DIRECTORY_PROPERTY);
63
64 private servletunit.struts.MockStrutsTestCase mockStrutsTestCase;
65
66 private Mock mockWebApplicationContext;
67
68 public JMockStrutsTestCase() {
69 super();
70 mockStrutsTestCase = new servletunit.struts.MockStrutsTestCase();
71 }
72
73 public JMockStrutsTestCase(String name) {
74 super(name);
75 mockStrutsTestCase = new servletunit.struts.MockStrutsTestCase(name);
76 }
77
78 /***
79 * @see servletunit.struts.MockStrutsTestCase#setUp()
80 */
81 protected void setUp() throws Exception {
82 super.setUp();
83 struts().setUp();
84
85 mockWebApplicationContext = new Mock(WebApplicationContext.class);
86 WebApplicationContext webApplicationContext = (WebApplicationContext) getMockWebApplicationContext()
87 .proxy();
88
89 if (contextDirectory == null) {
90 if (log.isErrorEnabled()) {
91 log.error(CONTEXT_DIRECTORY_PROPERTY + " property is not set");
92 }
93 fail(CONTEXT_DIRECTORY_PROPERTY + " property is not set");
94 }
95 struts().setContextDirectory(new File(contextDirectory));
96 struts().setConfigFile(CONFIG_FILE);
97
98 ServletContextSimulator servletContext = (ServletContextSimulator) struts()
99 .getActionServlet().getServletContext();
100 servletContext.setAttribute(
101 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
102 webApplicationContext);
103 }
104
105 /***
106 * Get Spring ApplicationContext
107 *
108 * @return
109 */
110 public ApplicationContext getApplicationContext() {
111 return WebApplicationContextUtils
112 .getRequiredWebApplicationContext(struts().getActionServlet()
113 .getServletContext());
114 }
115
116 /***
117 * Get mocked Spring WebApplicationContext
118 *
119 * @return
120 */
121 public Mock getMockWebApplicationContext() {
122 return mockWebApplicationContext;
123 }
124
125 /***
126 * Get the <code>servletunit.struts.MockStrutsTestCase</code> of this test
127 * case
128 *
129 * @return servletunit.struts.MockStrutsTestCase
130 */
131 public servletunit.struts.MockStrutsTestCase struts() {
132 return mockStrutsTestCase;
133 }
134
135 }