1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import org.apache.struts2.dispatcher.ServletDispatcherResult;
30 import org.apache.struts2.interceptor.TokenInterceptor;
31 import org.apache.struts2.interceptor.TokenSessionStoreInterceptor;
32
33 import com.opensymphony.xwork2.Action;
34 import com.opensymphony.xwork2.ActionChainResult;
35 import com.opensymphony.xwork2.ActionProxyFactory;
36 import com.opensymphony.xwork2.DefaultActionProxyFactory;
37 import com.opensymphony.xwork2.ObjectFactory;
38 import com.opensymphony.xwork2.config.Configuration;
39 import com.opensymphony.xwork2.config.ConfigurationException;
40 import com.opensymphony.xwork2.config.ConfigurationProvider;
41 import com.opensymphony.xwork2.config.entities.ActionConfig;
42 import com.opensymphony.xwork2.config.entities.InterceptorMapping;
43 import com.opensymphony.xwork2.config.entities.PackageConfig;
44 import com.opensymphony.xwork2.config.entities.ResultConfig;
45 import com.opensymphony.xwork2.inject.ContainerBuilder;
46 import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
47 import com.opensymphony.xwork2.mock.MockResult;
48 import com.opensymphony.xwork2.util.location.LocatableProperties;
49
50
51 /***
52 * TestConfigurationProvider provides a simple configuration class without the need for xml files, etc. for simple testing.
53 *
54 */
55 public class TestConfigurationProvider implements ConfigurationProvider {
56
57 public static final String TEST_ACTION_NAME = "testAction";
58 public static final String EXECUTION_COUNT_ACTION_NAME = "executionCountAction";
59 public static final String TOKEN_ACTION_NAME = "tokenAction";
60 public static final String TOKEN_SESSION_ACTION_NAME = "tokenSessionAction";
61 public static final String TEST_NAMESPACE = "/testNamespace";
62 public static final String TEST_NAMESPACE_ACTION = "testNamespaceAction";
63 private Configuration configuration;
64
65
66 /***
67 * Allows the configuration to clean up any resources used
68 */
69 public void destroy() {
70 }
71
72 public void init(Configuration config) {
73 this.configuration = config;
74 }
75
76 /***
77 * Initializes the configuration object.
78 */
79 public void loadPackages() {
80 PackageConfig defaultPackageConfig = new PackageConfig("");
81
82 HashMap results = new HashMap();
83
84 HashMap successParams = new HashMap();
85 successParams.put("propertyName", "executionCount");
86 successParams.put("expectedValue", "1");
87
88 ResultConfig successConfig = new ResultConfig(Action.SUCCESS, TestResult.class.getName(), successParams);
89
90 results.put(Action.SUCCESS, successConfig);
91
92 List interceptors = new ArrayList();
93
94 ActionConfig executionCountActionConfig = new ActionConfig(null, ExecutionCountTestAction.class, null, results, interceptors);
95 defaultPackageConfig.addActionConfig(EXECUTION_COUNT_ACTION_NAME, executionCountActionConfig);
96
97 results = new HashMap();
98
99 successParams = new HashMap();
100 successParams.put("location", "success.jsp");
101
102 successConfig = new ResultConfig(Action.SUCCESS, ServletDispatcherResult.class.getName(), successParams);
103
104 results.put(Action.SUCCESS, successConfig);
105
106 interceptors.add(new InterceptorMapping("params", new ParametersInterceptor()));
107
108 ActionConfig testActionConfig = new ActionConfig(null, TestAction.class, null, results, interceptors);
109 defaultPackageConfig.addActionConfig(TEST_ACTION_NAME, testActionConfig);
110
111 interceptors = new ArrayList();
112 interceptors.add(new InterceptorMapping("token", new TokenInterceptor()));
113
114 results = new HashMap();
115
116 ActionConfig tokenActionConfig = new ActionConfig(null, TestAction.class, null, results, interceptors);
117 tokenActionConfig.addResultConfig(new ResultConfig("invalid.token", MockResult.class.getName()));
118 tokenActionConfig.addResultConfig(new ResultConfig("success", MockResult.class.getName()));
119 defaultPackageConfig.addActionConfig(TOKEN_ACTION_NAME, tokenActionConfig);
120
121 interceptors = new ArrayList();
122 interceptors.add(new InterceptorMapping("token-session", new TokenSessionStoreInterceptor()));
123
124 results = new HashMap();
125
126 successParams = new HashMap();
127 successParams.put("actionName", EXECUTION_COUNT_ACTION_NAME);
128
129 successConfig = new ResultConfig(Action.SUCCESS, ActionChainResult.class.getName(), successParams);
130
131 results.put(Action.SUCCESS, successConfig);
132
133
134 results = new HashMap();
135 ActionConfig tokenSessionActionConfig = new ActionConfig(null, TestAction.class, null, results, interceptors);
136 tokenSessionActionConfig.addResultConfig(new ResultConfig("invalid.token", MockResult.class.getName()));
137 tokenSessionActionConfig.addResultConfig(new ResultConfig("success", MockResult.class.getName()));
138 defaultPackageConfig.addActionConfig(TOKEN_SESSION_ACTION_NAME, tokenSessionActionConfig);
139
140 configuration.addPackageConfig("", defaultPackageConfig);
141
142 Map testActionTagResults = new HashMap();
143 testActionTagResults.put(Action.SUCCESS, new ResultConfig(Action.SUCCESS, TestActionTagResult.class.getName(), new HashMap()));
144 testActionTagResults.put(Action.INPUT, new ResultConfig(Action.INPUT, TestActionTagResult.class.getName(), new HashMap()));
145 ActionConfig testActionTagActionConfig = new ActionConfig((String) null, TestAction.class, (Map) null, testActionTagResults, new ArrayList());
146 defaultPackageConfig.addActionConfig("testActionTagAction", testActionTagActionConfig);
147
148 PackageConfig namespacePackageConfig = new PackageConfig("namespacePackage");
149 namespacePackageConfig.setNamespace(TEST_NAMESPACE);
150 namespacePackageConfig.addParent(defaultPackageConfig);
151
152 ActionConfig namespaceAction = new ActionConfig(null, TestAction.class, null, null, null);
153 namespacePackageConfig.addActionConfig(TEST_NAMESPACE_ACTION, namespaceAction);
154
155 configuration.addPackageConfig("namespacePackage", namespacePackageConfig);
156 }
157
158 /***
159 * Tells whether the ConfigurationProvider should reload its configuration
160 *
161 * @return
162 */
163 public boolean needsReload() {
164 return false;
165 }
166
167 public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
168 if (!builder.contains(ObjectFactory.class)) {
169 builder.factory(ObjectFactory.class);
170 }
171 if (!builder.contains(ActionProxyFactory.class)) {
172 builder.factory(ActionProxyFactory.class, DefaultActionProxyFactory.class);
173 }
174 }
175 }