View Javadoc

1   /*
2    * $Id: Struts1FactoryTest.java 560753 2007-07-29 16:18:28Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.s1;
22  
23  import java.lang.reflect.InvocationTargetException;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.struts.action.ActionErrors;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  import org.apache.struts.action.ActionMessage;
31  import org.apache.struts.config.ActionConfig;
32  import org.apache.struts.config.ExceptionConfig;
33  import org.apache.struts.config.ForwardConfig;
34  import org.apache.struts.config.ModuleConfig;
35  import org.apache.struts2.config.StrutsXmlConfigurationProvider;
36  
37  import com.opensymphony.xwork2.ActionSupport;
38  import com.opensymphony.xwork2.ObjectFactory;
39  import com.opensymphony.xwork2.config.Configuration;
40  import com.opensymphony.xwork2.config.ConfigurationManager;
41  import com.opensymphony.xwork2.config.ConfigurationProvider;
42  import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;
43  import com.opensymphony.xwork2.config.entities.PackageConfig;
44  import com.opensymphony.xwork2.config.entities.ResultConfig;
45  
46  /***
47   * Test of Struts1Factory, which creates Struts 1.x wrappers around XWork config objects.
48   */
49  public class Struts1FactoryTest extends TestCase {
50  
51      private static final String PACKAGE_NAME = "org/apache/struts2/s1";
52      
53      protected Struts1Factory factory = null;
54      protected Configuration config;
55  
56      public Struts1FactoryTest(String name) throws Exception {
57          super(name);
58      }
59  
60  
61      public static void main(String args[]) {
62          junit.textui.TestRunner.run(Struts1FactoryTest.class);
63      }
64  
65      /***
66       * Set up instance variables required by this test case.
67       */
68      public void setUp() {
69          ConfigurationManager manager = new ConfigurationManager();
70          StrutsXmlConfigurationProvider provider = new StrutsXmlConfigurationProvider(PACKAGE_NAME + "/test-struts-factory.xml", true, null);
71          manager.addConfigurationProvider(provider);
72          config = manager.getConfiguration();
73          factory = new Struts1Factory(config);
74      }
75  
76      /***
77       * Test the creation of a Struts 1.x ModuleConfig wrapper around an XWork PackageConfig.
78       * The PackageConfig is loaded from test-struts-factory.xml.
79       */
80      public void testCreateModuleConfig() {
81          ModuleConfig moduleConfig = factory.createModuleConfig(PACKAGE_NAME);
82          assertNotNull(moduleConfig);
83          
84          assertEquals("/"+PACKAGE_NAME, moduleConfig.getPrefix());
85          
86          ActionConfig actionConfig = moduleConfig.findActionConfig("/action1");
87          assertNotNull(actionConfig);
88          assertEquals("/action1", actionConfig.getPath());
89          
90          ActionConfig[] actionConfigs = moduleConfig.findActionConfigs();
91          assertNotNull(actionConfigs);
92          assertEquals(2, actionConfigs.length);
93          
94          ExceptionConfig exceptionConfig = moduleConfig.findExceptionConfig(Exception.class.getName());
95          assertNotNull(exceptionConfig);
96          assertEquals(Exception.class.getName(), exceptionConfig.getType());
97          
98          ExceptionConfig[] exceptionConfigs = moduleConfig.findExceptionConfigs();
99          assertNotNull(exceptionConfigs);
100         assertEquals(1, exceptionConfigs.length);
101         
102         ForwardConfig fwdConfig = moduleConfig.findForwardConfig("globalResult");
103         assertNotNull(fwdConfig);
104         assertEquals("globalResult", fwdConfig.getName());
105         
106         // These methods are currently not implemented -- replace as functionality is added.
107         assertNYI(moduleConfig, "getControllerConfig", null);
108         assertNYI(moduleConfig, "getActionFormBeanClass", null);
109         assertNYI(moduleConfig, "getActionMappingClass", null);
110         assertNYI(moduleConfig, "getActionForwardClass", null);
111         assertNYI(moduleConfig, "findException", Class.class);
112         assertNYI(moduleConfig, "findFormBeanConfig", String.class);
113         assertNYI(moduleConfig, "findFormBeanConfigs", null);
114         assertNYI(moduleConfig, "findMessageResourcesConfig", String.class);
115         assertNYI(moduleConfig, "findMessageResourcesConfigs", null);
116         assertNYI(moduleConfig, "findPlugInConfigs", null);
117     }
118     
119     /***
120      * Test the creation of a Struts 1.x ActionMapping wrapper around an XWork ActionConfig.
121      * The ActionConfig is loaded from test-struts-factory.xml.
122      */
123     public void testCreateActionMapping() {
124         PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME);
125         com.opensymphony.xwork2.config.entities.ActionConfig actionConfig =
126                 (com.opensymphony.xwork2.config.entities.ActionConfig) packageConfig.getActionConfigs().get("action1");
127         ActionMapping mapping = factory.createActionMapping(actionConfig);
128         assertNotNull(mapping);
129 
130         assertNotNull(mapping.findForward("result1"));
131         assertNotNull(mapping.findForwardConfig("result2"));
132 
133         ForwardConfig[] configs = mapping.findForwardConfigs();
134         assertNotNull(configs);
135         assertEquals(2, configs.length);
136 
137         String[] forwards = mapping.findForwards();
138         assertNotNull(forwards);
139         assertEquals(2, forwards.length);
140         
141         ActionForward fwd = mapping.findForward("result1");
142         assertNotNull(fwd);
143         assertEquals("result1", fwd.getName());
144 
145         assertNotNull(mapping.findException(NullPointerException.class));
146         assertNotNull(mapping.findExceptionConfig("java.lang.IllegalStateException"));
147 
148         ExceptionConfig[] exceptionConfigs = mapping.findExceptionConfigs();
149         assertNotNull(exceptionConfigs);
150         assertEquals(3, exceptionConfigs.length);
151         
152         ModuleConfig moduleConfig = mapping.getModuleConfig();
153         assertNotNull(moduleConfig);
154         
155         // For now, the path will be null if the ActionMapping was created on its own (as opposed to from a
156         // WrapperModuleConfig, which knows the path).
157         assertNull(mapping.getPath());
158         
159         // These methods are currently not implemented -- replace as functionality is added.
160         assertNYI(mapping, "getInputForward", null);
161         assertNYI(mapping, "getForward", null);
162         assertNYI(mapping, "getInclude", null);
163         assertNYI(mapping, "getInput", null);
164         assertNYI(mapping, "getMultipartClass", null);
165         assertNYI(mapping, "getName", null);
166         assertNYI(mapping, "getParameter", null);
167         assertNYI(mapping, "getPrefix", null);
168         assertNYI(mapping, "getRoles", null);
169         assertNYI(mapping, "getRoleNames", null);
170         assertNYI(mapping, "getScope", null);
171         assertNYI(mapping, "getSuffix", null);
172         assertNYI(mapping, "getType", null);
173         assertNYI(mapping, "getUnknown", null);
174         assertNYI(mapping, "getValidate", null);
175     }
176 
177     /***
178      * Test the creation of a Struts 1.x ActionForward wrapper around an XWork ResultConfig.
179      * The ResultConfig is loaded from test-struts-factory.xml.
180      */
181     public void testCreateActionForward() {
182         PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME);
183         ResultConfig resultConfig = (ResultConfig) packageConfig.getGlobalResultConfigs().get("globalResult");
184         ActionForward fwd = factory.createActionForward(resultConfig);
185         assertNotNull(fwd);
186         assertEquals("globalResult", fwd.getName());
187         
188         // These methods are currently not implemented -- replace as functionality is added.
189         assertNYI(fwd, "getPath", null);
190         assertNYI(fwd, "getModule", null);
191         assertNYI(fwd, "getRedirect", null);
192     }
193 
194     /***
195      * Test the creation of a Struts 1.x ExceptionConfig wrapper around an XWork ExceptionHandlerConfig.
196      * The ExceptionConfig is loaded from test-struts-factory.xml.
197      */
198     public void testCreateExceptionConfig() {
199         PackageConfig packageConfig = config.getPackageConfig(PACKAGE_NAME);
200         ExceptionMappingConfig cfg = (ExceptionMappingConfig) packageConfig.getGlobalExceptionMappingConfigs().get(0);
201         ExceptionConfig exceptionConfig = factory.createExceptionConfig(cfg);
202         assertNotNull(exceptionConfig);
203         assertEquals(Exception.class.getName(), exceptionConfig.getType());
204 
205         assertNYI(exceptionConfig, "getBundle", null);
206         assertNYI(exceptionConfig, "getHandler", null);
207         assertNYI(exceptionConfig, "getKey", null);
208         assertNYI(exceptionConfig, "getPath", null);
209         assertNYI(exceptionConfig, "getScope", null);
210     }
211 
212     public void testConvertErrors() throws Exception {
213 
214         ActionMessage err1 = new ActionMessage("error1");
215         ActionMessage err2 = new ActionMessage("error2", new Integer(1));
216         ActionErrors errors = new ActionErrors();
217         errors.add(errors.GLOBAL_MESSAGE, err1);
218         errors.add("foo", err2);
219 
220         ActionSupport action = new ActionSupport();
221         factory.convertErrors(errors, action);
222 
223         assertTrue(1 == action.getActionErrors().size());
224         assertTrue(1 == action.getFieldErrors().size());
225     }
226 
227     /***
228      * Assert that the given method throws UnsupportedOperationException.
229      */
230     private void assertNYI(Object o, String methodName, Class argType) {
231         try {
232             Class[] argTypes = argType != null ? new Class[]{argType} : null;
233             
234             Object[] args = null;
235             if (argType != null) {
236                 if (Class.class == argType) {
237                     args = new Object[]{argType};
238                 } else {
239                     args = new Object[]{argType.newInstance()};
240                 }
241             }
242             o.getClass().getMethod(methodName, argTypes).invoke(o, args);
243         } catch (InvocationTargetException e) {
244             Throwable cause = e.getCause();
245             assertEquals(cause.getMessage(), UnsupportedOperationException.class, cause.getClass());
246             
247             // OK -- it's what we expected
248             return;
249         } catch (Exception e) {
250             fail(e.getClass().getName() + ": " + e.getMessage());
251         }
252 
253         fail("Expected UnsupportedOperationException for " + methodName + "() on " + o.getClass().getName());
254     }
255 }