001: /*
002: * $Id: MethodConfigurationProviderTest.java 502294 2007-02-01 17:28:00Z niallp $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.config;
022:
023: import org.apache.struts2.dispatcher.ServletDispatcherResult;
024: import org.apache.struts2.dispatcher.Dispatcher;
025: import org.springframework.mock.web.MockServletContext;
026:
027: import com.opensymphony.xwork2.config.Configuration;
028: import com.opensymphony.xwork2.config.ConfigurationManager;
029: import com.opensymphony.xwork2.config.entities.*;
030: import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
031: import com.opensymphony.xwork2.ActionSupport;
032:
033: import junit.framework.TestCase;
034:
035: import java.util.Map;
036: import java.util.HashMap;
037: import java.util.List;
038:
039: /**
040: * MethodConfigurationProviderTest exercises the MethodConfigurationProvider
041: * to confirm that only the expected methods are generated.
042: */
043: public class MethodConfigurationProviderTest extends TestCase {
044:
045: /**
046: * Object under test.
047: */
048: MethodConfigurationProvider provider;
049:
050: /**
051: * Set of packages and ActionConfigs to exercise.
052: */
053: Configuration configuration;
054:
055: /**
056: * Mock dispatcher.
057: */
058: Dispatcher dispatcher;
059:
060: /**
061: * Creates a mock Dispatcher and seeds Configuration.
062: */
063: public void setUp() {
064:
065: InternalConfigurationManager configurationManager = new InternalConfigurationManager();
066: dispatcher = new Dispatcher(new MockServletContext(),
067: new HashMap<String, String>());
068: dispatcher.setConfigurationManager(configurationManager);
069: dispatcher.init();
070: Dispatcher.setInstance(dispatcher);
071:
072: configuration = new DefaultConfiguration();
073: // empty package for the "default" namespace of empty String
074: PackageConfig strutsDefault = new PackageConfig(
075: "struts-default");
076: strutsDefault.addResultTypeConfig(new ResultTypeConfig(
077: "dispatcher", ServletDispatcherResult.class.getName(),
078: "location"));
079: strutsDefault.setDefaultResultType("dispatcher");
080: configuration.addPackageConfig("struts-default", strutsDefault);
081:
082: // custom package with various actions
083: PackageConfig customPackage = new PackageConfig("trick-package");
084: customPackage.setNamespace("/trick");
085: // action that specifies ActionSupport (not empty) but with no methods
086: ActionConfig action = new ActionConfig(null,
087: ActionSupport.class, null, null, null);
088: customPackage.addActionConfig("action", action);
089: // action that species a custom Action with a manual method
090: ActionConfig custom = new ActionConfig(null, Custom.class,
091: null, null, null);
092: customPackage.addActionConfig("custom", custom);
093: // action for manual method, with params, to prove it is not overwritten
094: Map params = new HashMap();
095: params.put("name", "value");
096: ActionConfig manual = new ActionConfig("manual", Custom.class,
097: params, null, null);
098: customPackage.addActionConfig("custom!manual", manual);
099: configuration.addPackageConfig("trick-package", customPackage);
100:
101: provider = new MethodConfigurationProvider();
102: provider.init(configuration);
103: provider.loadPackages();
104: }
105:
106: /**
107: * Provides the "custom-package" configuration.
108: * @return the "custom-package" configuration.
109: */
110: private PackageConfig getCustom() {
111: return configuration.getPackageConfig("trick-package");
112: }
113:
114: /**
115: * Confirms baseline setup works as expected.
116: */
117: public void testSetup() {
118: assertEquals(2, configuration.getPackageConfigs().size());
119: PackageConfig struts = configuration
120: .getPackageConfig("struts-default");
121: assertNotNull(struts);
122: assertTrue("testSetup: Expected struts-default to be empty!",
123: struts.getActionConfigs().size() == 0);
124:
125: PackageConfig custom = getCustom();
126: assertNotNull(custom);
127: assertTrue("testSetup: Expected ActionConfigs to be added!",
128: custom.getActionConfigs().size() > 0);
129: }
130:
131: /**
132: * Confirms that system detects no-argument methods that return Strings
133: * and generates the appropriate ActionConfigs.
134: */
135: public void testQualifyingMethods() {
136:
137: PackageConfig config = getCustom();
138:
139: boolean action = config.getActionConfigs()
140: .containsKey("action");
141: assertTrue("The root action is missing!", action);
142:
143: boolean custom = config.getActionConfigs()
144: .containsKey("custom");
145: assertTrue("The custom action is missing!", custom);
146:
147: boolean action_input = getCustom().getActionConfigs()
148: .containsKey("action!input");
149: assertTrue(
150: "The Action.input method should have an action mapping!",
151: action_input);
152:
153: boolean custom_input = getCustom().getActionConfigs()
154: .containsKey("custom!input");
155: assertTrue(
156: "The Custom.input method should have an action mapping!",
157: custom_input);
158:
159: boolean custom_auto = getCustom().getActionConfigs()
160: .containsKey("custom!auto");
161: assertTrue(
162: "The Custom.auto method should have an action mapping!",
163: custom_auto);
164:
165: boolean custom_gettysburg = getCustom().getActionConfigs()
166: .containsKey("custom!gettysburg");
167: assertTrue(
168: "The Custom.gettysburg method should have an action mapping!",
169: custom_gettysburg);
170: }
171:
172: /**
173: * Confirms system excludes methods that do not return Strings
174: * and no-argument or begin with "getx" or "isX".
175: */
176: public void testExcludedMethods() {
177:
178: PackageConfig custom = getCustom();
179:
180: boolean action_toString = custom.getActionConfigs()
181: .containsKey("action!toString");
182: assertFalse("The toString has an ActionConfig!",
183: action_toString);
184:
185: boolean action_execute = custom.getActionConfigs().containsKey(
186: "action!execute");
187: assertFalse("The execute has an ActionConfig!", action_execute);
188:
189: boolean action_get_method = custom.getActionConfigs()
190: .containsKey("action!getLocale");
191: assertFalse("A 'getX' method has an ActionConfig!",
192: action_get_method);
193:
194: boolean action_is_method = custom.getActionConfigs()
195: .containsKey("custom!isIt");
196: assertFalse("A 'isX' method has an ActionConfig!",
197: action_is_method);
198:
199: boolean void_method = custom.getActionConfigs().containsKey(
200: "action!validate");
201: assertFalse("A void method has an ActionConfig!", void_method);
202:
203: boolean void_with_parameters = custom.getActionConfigs()
204: .containsKey("action!addActionMessage");
205: assertFalse(
206: "A void method with parameters has an ActionConfig!",
207: void_with_parameters);
208:
209: boolean return_method = custom.getActionConfigs().containsKey(
210: "action!hasActionErrors");
211: assertFalse(
212: "A method with a return type other than String has an ActionConfig!",
213: return_method);
214:
215: ActionConfig manual = getCustom().getActionConfigs().get(
216: "custom!manual");
217: Object val = manual.getParams().get("name");
218: assertTrue("The custom.Manual method was generated!", "value"
219: .equals(val.toString()));
220: }
221:
222: /**
223: * Custom is a test Action class.
224: */
225: public class Custom extends ActionSupport {
226:
227: /**
228: * Tests ordinary methods.
229: * @return SUCCESS
230: */
231: public String custom() {
232: return SUCCESS;
233: }
234:
235: /**
236: * Tests JavaBean property.
237: * @return SUCCESS
238: */
239: public boolean isIt() {
240: return true;
241: }
242:
243: /**
244: * Tests manual override.
245: * @return SUCCESS
246: */
247: public String manual() {
248: return SUCCESS;
249: }
250:
251: /**
252: * Tests dynamic configuration.
253: * @return SUCCESS
254: */
255: public String auto() {
256: return SUCCESS;
257: }
258:
259: /**
260: * Tests method that looks like a JavaBean property.
261: * @return SUCCESS
262: */
263: public String gettysburg() {
264: return SUCCESS;
265: }
266: }
267:
268: /**
269: * InternalConfigurationManager is a mock ConfigurationManager.
270: */
271: class InternalConfigurationManager extends ConfigurationManager {
272: public boolean destroyConfiguration = false;
273:
274: @Override
275: public synchronized void destroyConfiguration() {
276: super .destroyConfiguration();
277: destroyConfiguration = true;
278: }
279: }
280:
281: }
|