View Javadoc

1   /*
2    * $Id: TestConfigurationProvider.java 451943 2006-10-02 10:10:07Z tmjee $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.struts2.dispatcher.ServletDispatcherResult;
26  import org.apache.struts2.interceptor.TokenInterceptor;
27  import org.apache.struts2.interceptor.TokenSessionStoreInterceptor;
28  
29  import com.opensymphony.xwork2.Action;
30  import com.opensymphony.xwork2.ActionChainResult;
31  import com.opensymphony.xwork2.config.Configuration;
32  import com.opensymphony.xwork2.config.ConfigurationProvider;
33  import com.opensymphony.xwork2.config.entities.ActionConfig;
34  import com.opensymphony.xwork2.config.entities.InterceptorMapping;
35  import com.opensymphony.xwork2.config.entities.PackageConfig;
36  import com.opensymphony.xwork2.config.entities.ResultConfig;
37  import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
38  import com.opensymphony.xwork2.mock.MockResult;
39  
40  
41  /***
42   * TestConfigurationProvider provides a simple configuration class without the need for xml files, etc. for simple testing.
43   *
44   */
45  public class TestConfigurationProvider implements ConfigurationProvider {
46  
47      public static final String TEST_ACTION_NAME = "testAction";
48      public static final String EXECUTION_COUNT_ACTION_NAME = "executionCountAction";
49      public static final String TOKEN_ACTION_NAME = "tokenAction";
50      public static final String TOKEN_SESSION_ACTION_NAME = "tokenSessionAction";
51      public static final String TEST_NAMESPACE = "/testNamespace";
52      public static final String TEST_NAMESPACE_ACTION = "testNamespaceAction";
53  
54  
55      /***
56       * Allows the configuration to clean up any resources used
57       */
58      public void destroy() {
59      }
60  
61      /***
62       * Initializes the configuration object.
63       */
64      public void init(Configuration configurationManager) {
65          PackageConfig defaultPackageConfig = new PackageConfig("");
66  
67          HashMap results = new HashMap();
68  
69          HashMap successParams = new HashMap();
70          successParams.put("propertyName", "executionCount");
71          successParams.put("expectedValue", "1");
72  
73          ResultConfig successConfig = new ResultConfig(Action.SUCCESS, TestResult.class.getName(), successParams);
74  
75          results.put(Action.SUCCESS, successConfig);
76  
77          List interceptors = new ArrayList();
78  
79          ActionConfig executionCountActionConfig = new ActionConfig(null, ExecutionCountTestAction.class, null, results, interceptors);
80          defaultPackageConfig.addActionConfig(EXECUTION_COUNT_ACTION_NAME, executionCountActionConfig);
81  
82          results = new HashMap();
83  
84          successParams = new HashMap();
85          successParams.put("location", "success.jsp");
86  
87          successConfig = new ResultConfig(Action.SUCCESS, ServletDispatcherResult.class.getName(), successParams);
88  
89          results.put(Action.SUCCESS, successConfig);
90  
91          interceptors.add(new InterceptorMapping("params", new ParametersInterceptor()));
92  
93          ActionConfig testActionConfig = new ActionConfig(null, TestAction.class, null, results, interceptors);
94          defaultPackageConfig.addActionConfig(TEST_ACTION_NAME, testActionConfig);
95  
96          interceptors = new ArrayList();
97          interceptors.add(new InterceptorMapping("token", new TokenInterceptor()));
98  
99          results = new HashMap();
100 
101         ActionConfig tokenActionConfig = new ActionConfig(null, TestAction.class, null, results, interceptors);
102         tokenActionConfig.addResultConfig(new ResultConfig("invalid.token", MockResult.class.getName()));
103         tokenActionConfig.addResultConfig(new ResultConfig("success", MockResult.class.getName()));
104         defaultPackageConfig.addActionConfig(TOKEN_ACTION_NAME, tokenActionConfig);
105 
106         interceptors = new ArrayList();
107         interceptors.add(new InterceptorMapping("token-session", new TokenSessionStoreInterceptor()));
108 
109         results = new HashMap();
110 
111         successParams = new HashMap();
112         successParams.put("actionName", EXECUTION_COUNT_ACTION_NAME);
113 
114         successConfig = new ResultConfig(Action.SUCCESS, ActionChainResult.class.getName(), successParams);
115 
116         results.put(Action.SUCCESS, successConfig);
117 
118         // empty results for token session unit test
119         results = new HashMap();
120         ActionConfig tokenSessionActionConfig = new ActionConfig(null, TestAction.class, null, results, interceptors);
121         tokenSessionActionConfig.addResultConfig(new ResultConfig("invalid.token", MockResult.class.getName()));
122         tokenSessionActionConfig.addResultConfig(new ResultConfig("success", MockResult.class.getName()));
123         defaultPackageConfig.addActionConfig(TOKEN_SESSION_ACTION_NAME, tokenSessionActionConfig);
124 
125         configurationManager.addPackageConfig("", defaultPackageConfig);
126 
127         Map testActionTagResults = new HashMap();
128         testActionTagResults.put(Action.SUCCESS, new ResultConfig(Action.SUCCESS, TestActionTagResult.class.getName(), new HashMap()));
129         testActionTagResults.put(Action.INPUT, new ResultConfig(Action.INPUT, TestActionTagResult.class.getName(), new HashMap()));
130         ActionConfig testActionTagActionConfig = new ActionConfig((String) null, TestAction.class, (Map) null, testActionTagResults, new ArrayList());
131         defaultPackageConfig.addActionConfig("testActionTagAction", testActionTagActionConfig);
132 
133         PackageConfig namespacePackageConfig = new PackageConfig("namespacePackage");
134         namespacePackageConfig.setNamespace(TEST_NAMESPACE);
135         namespacePackageConfig.addParent(defaultPackageConfig);
136 
137         ActionConfig namespaceAction = new ActionConfig(null, TestAction.class, null, null, null);
138         namespacePackageConfig.addActionConfig(TEST_NAMESPACE_ACTION, namespaceAction);
139 
140         configurationManager.addPackageConfig("namespacePackage", namespacePackageConfig);
141     }
142 
143     /***
144      * Tells whether the ConfigurationProvider should reload its configuration
145      *
146      * @return
147      */
148     public boolean needsReload() {
149         return false;
150     }
151 }