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