1 package org.apache.struts2.s1;
2
3 import java.lang.reflect.InvocationTargetException;
4
5 import junit.framework.TestCase;
6
7 import org.apache.struts.action.ActionErrors;
8 import org.apache.struts.action.ActionForward;
9 import org.apache.struts.action.ActionMapping;
10 import org.apache.struts.action.ActionMessage;
11 import org.apache.struts.config.ActionConfig;
12 import org.apache.struts.config.ExceptionConfig;
13 import org.apache.struts.config.ForwardConfig;
14 import org.apache.struts.config.ModuleConfig;
15 import org.apache.struts2.config.StrutsXmlConfigurationProvider;
16
17 import com.opensymphony.xwork2.ActionSupport;
18 import com.opensymphony.xwork2.ObjectFactory;
19 import com.opensymphony.xwork2.config.Configuration;
20 import com.opensymphony.xwork2.config.ConfigurationManager;
21 import com.opensymphony.xwork2.config.ConfigurationProvider;
22 import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;
23 import com.opensymphony.xwork2.config.entities.PackageConfig;
24 import com.opensymphony.xwork2.config.entities.ResultConfig;
25
26 /***
27 * Test of Struts1Factory, which creates Struts 1.x wrappers around XWork config objects.
28 */
29 public class Struts1FactoryTest extends TestCase {
30
31 private static final String PACKAGE_NAME = "org/apache/struts2/s1";
32
33 protected Struts1Factory factory = null;
34 protected Configuration config;
35
36 public Struts1FactoryTest(String name) throws Exception {
37 super(name);
38 }
39
40
41 public static void main(String args[]) {
42 junit.textui.TestRunner.run(Struts1FactoryTest.class);
43 }
44
45 /***
46 * Set up instance variables required by this test case.
47 */
48 public void setUp() {
49 ConfigurationManager manager = new ConfigurationManager();
50 StrutsXmlConfigurationProvider provider = new StrutsXmlConfigurationProvider(PACKAGE_NAME + "/test-struts-factory.xml", true, null);
51 manager.addConfigurationProvider(provider);
52 config = manager.getConfiguration();
53 factory = new Struts1Factory(config);
54 }
55
56 /***
57 * Test the creation of a Struts 1.x ModuleConfig wrapper around an XWork PackageConfig.
58 * The PackageConfig is loaded from test-struts-factory.xml.
59 */
60 public void testCreateModuleConfig() {
61 ModuleConfig moduleConfig = factory.createModuleConfig(PACKAGE_NAME);
62 assertNotNull(moduleConfig);
63
64 assertEquals("/"+PACKAGE_NAME, moduleConfig.getPrefix());
65
66 ActionConfig actionConfig = moduleConfig.findActionConfig("/action1");
67 assertNotNull(actionConfig);
68 assertEquals("/action1", actionConfig.getPath());
69
70 ActionConfig[] actionConfigs = moduleConfig.findActionConfigs();
71 assertNotNull(actionConfigs);
72 assertEquals(2, actionConfigs.length);
73
74 ExceptionConfig exceptionConfig = moduleConfig.findExceptionConfig(Exception.class.getName());
75 assertNotNull(exceptionConfig);
76 assertEquals(Exception.class.getName(), exceptionConfig.getType());
77
78 ExceptionConfig[] exceptionConfigs = moduleConfig.findExceptionConfigs();
79 assertNotNull(exceptionConfigs);
80 assertEquals(1, exceptionConfigs.length);
81
82 ForwardConfig fwdConfig = moduleConfig.findForwardConfig("globalResult");
83 assertNotNull(fwdConfig);
84 assertEquals("globalResult", fwdConfig.getName());
85
86
87 assertNYI(moduleConfig, "getControllerConfig", null);
88 assertNYI(moduleConfig, "getActionFormBeanClass", null);
89 assertNYI(moduleConfig, "getActionMappingClass", null);
90 assertNYI(moduleConfig, "getActionForwardClass", null);
91 assertNYI(moduleConfig, "findException", Class.class);
92 assertNYI(moduleConfig, "findFormBeanConfig", String.class);
93 assertNYI(moduleConfig, "findFormBeanConfigs", null);
94 assertNYI(moduleConfig, "findMessageResourcesConfig", String.class);
95 assertNYI(moduleConfig, "findMessageResourcesConfigs", null);
96 assertNYI(moduleConfig, "findPlugInConfigs", null);
97 }
98
99 /***
100 * Test the creation of a Struts 1.x ActionMapping wrapper around an XWork ActionConfig.
101 * The ActionConfig is loaded from test-struts-factory.xml.
102 */
103 public void testCreateActionMapping() {
104 PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME);
105 com.opensymphony.xwork2.config.entities.ActionConfig actionConfig =
106 (com.opensymphony.xwork2.config.entities.ActionConfig) packageConfig.getActionConfigs().get("action1");
107 ActionMapping mapping = factory.createActionMapping(actionConfig);
108 assertNotNull(mapping);
109
110 assertNotNull(mapping.findForward("result1"));
111 assertNotNull(mapping.findForwardConfig("result2"));
112
113 ForwardConfig[] configs = mapping.findForwardConfigs();
114 assertNotNull(configs);
115 assertEquals(2, configs.length);
116
117 String[] forwards = mapping.findForwards();
118 assertNotNull(forwards);
119 assertEquals(2, forwards.length);
120
121 ActionForward fwd = mapping.findForward("result1");
122 assertNotNull(fwd);
123 assertEquals("result1", fwd.getName());
124
125 assertNotNull(mapping.findException(NullPointerException.class));
126 assertNotNull(mapping.findExceptionConfig("java.lang.IllegalStateException"));
127
128 ExceptionConfig[] exceptionConfigs = mapping.findExceptionConfigs();
129 assertNotNull(exceptionConfigs);
130 assertEquals(3, exceptionConfigs.length);
131
132 ModuleConfig moduleConfig = mapping.getModuleConfig();
133 assertNotNull(moduleConfig);
134
135
136
137 assertNull(mapping.getPath());
138
139
140 assertNYI(mapping, "getInputForward", null);
141 assertNYI(mapping, "getForward", null);
142 assertNYI(mapping, "getInclude", null);
143 assertNYI(mapping, "getInput", null);
144 assertNYI(mapping, "getMultipartClass", null);
145 assertNYI(mapping, "getName", null);
146 assertNYI(mapping, "getParameter", null);
147 assertNYI(mapping, "getPrefix", null);
148 assertNYI(mapping, "getRoles", null);
149 assertNYI(mapping, "getRoleNames", null);
150 assertNYI(mapping, "getScope", null);
151 assertNYI(mapping, "getSuffix", null);
152 assertNYI(mapping, "getType", null);
153 assertNYI(mapping, "getUnknown", null);
154 assertNYI(mapping, "getValidate", null);
155 }
156
157 /***
158 * Test the creation of a Struts 1.x ActionForward wrapper around an XWork ResultConfig.
159 * The ResultConfig is loaded from test-struts-factory.xml.
160 */
161 public void testCreateActionForward() {
162 PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME);
163 ResultConfig resultConfig = (ResultConfig) packageConfig.getGlobalResultConfigs().get("globalResult");
164 ActionForward fwd = factory.createActionForward(resultConfig);
165 assertNotNull(fwd);
166 assertEquals("globalResult", fwd.getName());
167
168
169 assertNYI(fwd, "getPath", null);
170 assertNYI(fwd, "getModule", null);
171 assertNYI(fwd, "getRedirect", null);
172 }
173
174 /***
175 * Test the creation of a Struts 1.x ExceptionConfig wrapper around an XWork ExceptionHandlerConfig.
176 * The ExceptionConfig is loaded from test-struts-factory.xml.
177 */
178 public void testCreateExceptionConfig() {
179 PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME);
180 ExceptionMappingConfig cfg = (ExceptionMappingConfig) packageConfig.getGlobalExceptionMappingConfigs().get(0);
181 ExceptionConfig exceptionConfig = factory.createExceptionConfig(cfg);
182 assertNotNull(exceptionConfig);
183 assertEquals(Exception.class.getName(), exceptionConfig.getType());
184
185 assertNYI(exceptionConfig, "getBundle", null);
186 assertNYI(exceptionConfig, "getHandler", null);
187 assertNYI(exceptionConfig, "getKey", null);
188 assertNYI(exceptionConfig, "getPath", null);
189 assertNYI(exceptionConfig, "getScope", null);
190 }
191
192 public void testConvertErrors() throws Exception {
193
194 ActionMessage err1 = new ActionMessage("error1");
195 ActionMessage err2 = new ActionMessage("error2", new Integer(1));
196 ActionErrors errors = new ActionErrors();
197 errors.add(errors.GLOBAL_MESSAGE, err1);
198 errors.add("foo", err2);
199
200 ActionSupport action = new ActionSupport();
201 factory.convertErrors(errors, action);
202
203 assertTrue(1 == action.getActionErrors().size());
204 assertTrue(1 == action.getFieldErrors().size());
205 }
206
207 /***
208 * Assert that the given method throws UnsupportedOperationException.
209 */
210 private void assertNYI(Object o, String methodName, Class argType) {
211 try {
212 Class[] argTypes = argType != null ? new Class[]{argType} : null;
213
214 Object[] args = null;
215 if (argType != null) {
216 if (Class.class == argType) {
217 args = new Object[]{argType};
218 } else {
219 args = new Object[]{argType.newInstance()};
220 }
221 }
222 o.getClass().getMethod(methodName, argTypes).invoke(o, args);
223 } catch (InvocationTargetException e) {
224 Throwable cause = e.getCause();
225 assertEquals(cause.getMessage(), UnsupportedOperationException.class, cause.getClass());
226
227
228 return;
229 } catch (Exception e) {
230 fail(e.getClass().getName() + ": " + e.getMessage());
231 }
232
233 fail("Expected UnsupportedOperationException for " + methodName + "() on " + o.getClass().getName());
234 }
235 }