001: /*
002: * $Id: TestConfigurationProvider.java 476106 2006-11-17 10:56:40Z mrdon $
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;
022:
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Properties;
028:
029: import org.apache.struts2.dispatcher.ServletDispatcherResult;
030: import org.apache.struts2.interceptor.TokenInterceptor;
031: import org.apache.struts2.interceptor.TokenSessionStoreInterceptor;
032:
033: import com.opensymphony.xwork2.Action;
034: import com.opensymphony.xwork2.ActionChainResult;
035: import com.opensymphony.xwork2.ActionProxyFactory;
036: import com.opensymphony.xwork2.DefaultActionProxyFactory;
037: import com.opensymphony.xwork2.ObjectFactory;
038: import com.opensymphony.xwork2.config.Configuration;
039: import com.opensymphony.xwork2.config.ConfigurationException;
040: import com.opensymphony.xwork2.config.ConfigurationProvider;
041: import com.opensymphony.xwork2.config.entities.ActionConfig;
042: import com.opensymphony.xwork2.config.entities.InterceptorMapping;
043: import com.opensymphony.xwork2.config.entities.PackageConfig;
044: import com.opensymphony.xwork2.config.entities.ResultConfig;
045: import com.opensymphony.xwork2.inject.ContainerBuilder;
046: import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
047: import com.opensymphony.xwork2.mock.MockResult;
048: import com.opensymphony.xwork2.util.location.LocatableProperties;
049:
050: /**
051: * TestConfigurationProvider provides a simple configuration class without the need for xml files, etc. for simple testing.
052: *
053: */
054: public class TestConfigurationProvider implements ConfigurationProvider {
055:
056: public static final String TEST_ACTION_NAME = "testAction";
057: public static final String EXECUTION_COUNT_ACTION_NAME = "executionCountAction";
058: public static final String TOKEN_ACTION_NAME = "tokenAction";
059: public static final String TOKEN_SESSION_ACTION_NAME = "tokenSessionAction";
060: public static final String TEST_NAMESPACE = "/testNamespace";
061: public static final String TEST_NAMESPACE_ACTION = "testNamespaceAction";
062: private Configuration configuration;
063:
064: /**
065: * Allows the configuration to clean up any resources used
066: */
067: public void destroy() {
068: }
069:
070: public void init(Configuration config) {
071: this .configuration = config;
072: }
073:
074: /**
075: * Initializes the configuration object.
076: */
077: public void loadPackages() {
078: PackageConfig defaultPackageConfig = new PackageConfig("");
079:
080: HashMap results = new HashMap();
081:
082: HashMap successParams = new HashMap();
083: successParams.put("propertyName", "executionCount");
084: successParams.put("expectedValue", "1");
085:
086: ResultConfig successConfig = new ResultConfig(Action.SUCCESS,
087: TestResult.class.getName(), successParams);
088:
089: results.put(Action.SUCCESS, successConfig);
090:
091: List interceptors = new ArrayList();
092:
093: ActionConfig executionCountActionConfig = new ActionConfig(
094: null, ExecutionCountTestAction.class, null, results,
095: interceptors);
096: defaultPackageConfig
097: .addActionConfig(EXECUTION_COUNT_ACTION_NAME,
098: executionCountActionConfig);
099:
100: results = new HashMap();
101:
102: successParams = new HashMap();
103: successParams.put("location", "success.jsp");
104:
105: successConfig = new ResultConfig(Action.SUCCESS,
106: ServletDispatcherResult.class.getName(), successParams);
107:
108: results.put(Action.SUCCESS, successConfig);
109:
110: interceptors.add(new InterceptorMapping("params",
111: new ParametersInterceptor()));
112:
113: ActionConfig testActionConfig = new ActionConfig(null,
114: TestAction.class, null, results, interceptors);
115: defaultPackageConfig.addActionConfig(TEST_ACTION_NAME,
116: testActionConfig);
117:
118: interceptors = new ArrayList();
119: interceptors.add(new InterceptorMapping("token",
120: new TokenInterceptor()));
121:
122: results = new HashMap();
123:
124: ActionConfig tokenActionConfig = new ActionConfig(null,
125: TestAction.class, null, results, interceptors);
126: tokenActionConfig.addResultConfig(new ResultConfig(
127: "invalid.token", MockResult.class.getName()));
128: tokenActionConfig.addResultConfig(new ResultConfig("success",
129: MockResult.class.getName()));
130: defaultPackageConfig.addActionConfig(TOKEN_ACTION_NAME,
131: tokenActionConfig);
132:
133: interceptors = new ArrayList();
134: interceptors.add(new InterceptorMapping("token-session",
135: new TokenSessionStoreInterceptor()));
136:
137: results = new HashMap();
138:
139: successParams = new HashMap();
140: successParams.put("actionName", EXECUTION_COUNT_ACTION_NAME);
141:
142: successConfig = new ResultConfig(Action.SUCCESS,
143: ActionChainResult.class.getName(), successParams);
144:
145: results.put(Action.SUCCESS, successConfig);
146:
147: // empty results for token session unit test
148: results = new HashMap();
149: ActionConfig tokenSessionActionConfig = new ActionConfig(null,
150: TestAction.class, null, results, interceptors);
151: tokenSessionActionConfig.addResultConfig(new ResultConfig(
152: "invalid.token", MockResult.class.getName()));
153: tokenSessionActionConfig.addResultConfig(new ResultConfig(
154: "success", MockResult.class.getName()));
155: defaultPackageConfig.addActionConfig(TOKEN_SESSION_ACTION_NAME,
156: tokenSessionActionConfig);
157:
158: configuration.addPackageConfig("", defaultPackageConfig);
159:
160: Map testActionTagResults = new HashMap();
161: testActionTagResults.put(Action.SUCCESS, new ResultConfig(
162: Action.SUCCESS, TestActionTagResult.class.getName(),
163: new HashMap()));
164: testActionTagResults.put(Action.INPUT, new ResultConfig(
165: Action.INPUT, TestActionTagResult.class.getName(),
166: new HashMap()));
167: ActionConfig testActionTagActionConfig = new ActionConfig(
168: (String) null, TestAction.class, (Map) null,
169: testActionTagResults, new ArrayList());
170: defaultPackageConfig.addActionConfig("testActionTagAction",
171: testActionTagActionConfig);
172:
173: PackageConfig namespacePackageConfig = new PackageConfig(
174: "namespacePackage");
175: namespacePackageConfig.setNamespace(TEST_NAMESPACE);
176: namespacePackageConfig.addParent(defaultPackageConfig);
177:
178: ActionConfig namespaceAction = new ActionConfig(null,
179: TestAction.class, null, null, null);
180: namespacePackageConfig.addActionConfig(TEST_NAMESPACE_ACTION,
181: namespaceAction);
182:
183: configuration.addPackageConfig("namespacePackage",
184: namespacePackageConfig);
185: }
186:
187: /**
188: * Tells whether the ConfigurationProvider should reload its configuration
189: *
190: * @return
191: */
192: public boolean needsReload() {
193: return false;
194: }
195:
196: public void register(ContainerBuilder builder,
197: LocatableProperties props) throws ConfigurationException {
198: if (!builder.contains(ObjectFactory.class)) {
199: builder.factory(ObjectFactory.class);
200: }
201: if (!builder.contains(ActionProxyFactory.class)) {
202: builder.factory(ActionProxyFactory.class,
203: DefaultActionProxyFactory.class);
204: }
205: }
206: }
|