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