View Javadoc

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