View Javadoc

1   /*
2    * $Id: MethodConfigurationProviderTest.java 502294 2007-02-01 17:28:00Z niallp $
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.config;
22  
23  import org.apache.struts2.dispatcher.ServletDispatcherResult;
24  import org.apache.struts2.dispatcher.Dispatcher;
25  import org.springframework.mock.web.MockServletContext;
26  
27  import com.opensymphony.xwork2.config.Configuration;
28  import com.opensymphony.xwork2.config.ConfigurationManager;
29  import com.opensymphony.xwork2.config.entities.*;
30  import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
31  import com.opensymphony.xwork2.ActionSupport;
32  
33  import junit.framework.TestCase;
34  
35  import java.util.Map;
36  import java.util.HashMap;
37  import java.util.List;
38  
39  /***
40   * MethodConfigurationProviderTest exercises the MethodConfigurationProvider
41   * to confirm that only the expected methods are generated.
42   */
43  public class MethodConfigurationProviderTest extends TestCase {
44  
45      /***
46       * Object under test.
47       */
48      MethodConfigurationProvider provider;
49  
50      /***
51       * Set of packages and ActionConfigs to exercise.
52       */
53      Configuration configuration;
54  
55      /***
56       * Mock dispatcher.
57       */
58      Dispatcher dispatcher;
59  
60      /***
61       * Creates a mock Dispatcher and seeds Configuration.
62       */
63      public void setUp() {
64  
65          InternalConfigurationManager configurationManager = new InternalConfigurationManager();
66          dispatcher = new Dispatcher(new MockServletContext(), new HashMap<String, String>());
67          dispatcher.setConfigurationManager(configurationManager);
68          dispatcher.init();
69          Dispatcher.setInstance(dispatcher);
70  
71          configuration = new DefaultConfiguration();
72          // empty package for the "default" namespace of empty String
73          PackageConfig strutsDefault = new PackageConfig("struts-default");
74          strutsDefault.addResultTypeConfig(new ResultTypeConfig("dispatcher", ServletDispatcherResult.class.getName(), "location"));
75          strutsDefault.setDefaultResultType("dispatcher");
76          configuration.addPackageConfig("struts-default", strutsDefault);
77  
78          // custom package with various actions
79          PackageConfig customPackage = new PackageConfig("trick-package");
80          customPackage.setNamespace("/trick");
81          // action that specifies ActionSupport (not empty) but with no methods
82          ActionConfig action = new ActionConfig(null, ActionSupport.class, null, null, null);
83          customPackage.addActionConfig("action",action);
84          // action that species a custom Action with a manual method
85          ActionConfig custom = new ActionConfig(null, Custom.class, null, null, null);
86          customPackage.addActionConfig("custom",custom);
87          // action for manual method, with params, to prove it is not overwritten
88          Map params = new HashMap();
89          params.put("name","value");
90          ActionConfig manual = new ActionConfig("manual", Custom.class, params, null, null);
91          customPackage.addActionConfig("custom!manual",manual);
92          configuration.addPackageConfig("trick-package", customPackage);
93  
94          provider = new MethodConfigurationProvider();
95          provider.init(configuration);
96          provider.loadPackages();
97      }
98  
99      /***
100      * Provides the "custom-package" configuration.
101      * @return the "custom-package" configuration.
102      */
103     private PackageConfig getCustom() {
104         return configuration.getPackageConfig("trick-package");
105     }
106 
107     /***
108      * Confirms baseline setup works as expected.
109      */
110     public void testSetup() {
111         assertEquals(2, configuration.getPackageConfigs().size());
112         PackageConfig struts = configuration.getPackageConfig("struts-default");
113         assertNotNull(struts);
114         assertTrue("testSetup: Expected struts-default to be empty!", struts.getActionConfigs().size() == 0);
115 
116         PackageConfig custom = getCustom();
117         assertNotNull(custom);
118         assertTrue("testSetup: Expected ActionConfigs to be added!", custom.getActionConfigs().size() > 0);
119     }
120 
121     /***
122      * Confirms that system detects no-argument methods that return Strings
123      * and generates the appropriate ActionConfigs.
124      */
125     public void testQualifyingMethods() {
126 
127         PackageConfig config = getCustom();
128 
129         boolean action = config.getActionConfigs().containsKey("action");
130         assertTrue("The root action is missing!",action);
131 
132         boolean custom = config.getActionConfigs().containsKey("custom");
133         assertTrue("The custom action is missing!",custom);
134 
135         boolean action_input = getCustom().getActionConfigs().containsKey("action!input");
136         assertTrue("The Action.input method should have an action mapping!",action_input);
137 
138         boolean custom_input = getCustom().getActionConfigs().containsKey("custom!input");
139         assertTrue("The Custom.input method should have an action mapping!",custom_input);
140 
141         boolean custom_auto = getCustom().getActionConfigs().containsKey("custom!auto");
142         assertTrue("The Custom.auto method should have an action mapping!",custom_auto);
143 
144         boolean custom_gettysburg = getCustom().getActionConfigs().containsKey("custom!gettysburg");
145         assertTrue("The Custom.gettysburg method should have an action mapping!",custom_gettysburg);
146     }
147 
148     /***
149      * Confirms system excludes methods that do not return Strings
150      * and no-argument or begin with "getx" or "isX".
151      */
152     public void testExcludedMethods() {
153 
154         PackageConfig custom = getCustom();
155 
156         boolean action_toString = custom.getActionConfigs().containsKey("action!toString");
157         assertFalse("The toString has an ActionConfig!",action_toString);
158 
159         boolean action_execute = custom.getActionConfigs().containsKey("action!execute");
160         assertFalse("The execute has an ActionConfig!",action_execute);
161 
162         boolean action_get_method = custom.getActionConfigs().containsKey("action!getLocale");
163         assertFalse("A 'getX' method has an ActionConfig!",action_get_method);
164 
165         boolean action_is_method = custom.getActionConfigs().containsKey("custom!isIt");
166         assertFalse("A 'isX' method has an ActionConfig!",action_is_method);
167 
168         boolean void_method = custom.getActionConfigs().containsKey("action!validate");
169         assertFalse("A void method has an ActionConfig!",void_method);
170 
171         boolean void_with_parameters = custom.getActionConfigs().containsKey("action!addActionMessage");
172         assertFalse("A void method with parameters has an ActionConfig!",void_with_parameters);
173 
174         boolean return_method = custom.getActionConfigs().containsKey("action!hasActionErrors");
175         assertFalse("A method with a return type other than String has an ActionConfig!",return_method);
176 
177         ActionConfig manual = getCustom().getActionConfigs().get("custom!manual");
178         Object val = manual.getParams().get("name");
179         assertTrue("The custom.Manual method was generated!","value".equals(val.toString()));
180     }
181 
182     /***
183      * Custom is a test Action class.
184      */
185     public class Custom extends ActionSupport {
186 
187         /***
188          * Tests ordinary methods.
189          * @return SUCCESS
190          */
191         public String custom() {
192             return SUCCESS;
193         }
194 
195         /***
196          * Tests JavaBean property.
197          * @return SUCCESS
198          */
199         public boolean isIt() {
200             return true;
201         }
202 
203         /***
204          * Tests manual override.
205          * @return SUCCESS
206          */
207         public String manual() {
208             return SUCCESS;
209         }
210 
211         /***
212          * Tests dynamic configuration.
213          * @return SUCCESS
214          */
215         public String auto() {
216             return SUCCESS;
217         }
218 
219         /***
220          * Tests method that looks like a JavaBean property.
221          * @return SUCCESS
222          */
223         public String gettysburg() {
224             return SUCCESS;
225         }
226     }
227 
228     /***
229      * InternalConfigurationManager is a mock ConfigurationManager.
230      */
231     class InternalConfigurationManager extends ConfigurationManager {
232     	public boolean destroyConfiguration = false;
233 
234     	@Override
235     	public synchronized void destroyConfiguration() {
236     		super.destroyConfiguration();
237     		destroyConfiguration = true;
238     	}
239     }
240 
241 }