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.util;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.struts2.dispatcher.Dispatcher;
28
29 import com.opensymphony.xwork2.ActionContext;
30 import com.opensymphony.xwork2.util.LocalizedTextUtil;
31 import com.opensymphony.xwork2.util.ValueStack;
32 import com.opensymphony.xwork2.util.ValueStackFactory;
33
34 import javax.servlet.ServletContext;
35
36 /***
37 * Generic test setup methods to be used with any unit testing framework.
38 */
39 public class StrutsTestCaseHelper {
40
41 /***
42 * Sets up the configuration settings, XWork configuration, and
43 * message resources
44 */
45 public static void setUp() throws Exception {
46 LocalizedTextUtil.clearDefaultResourceBundles();
47 }
48
49 public static Dispatcher initDispatcher(ServletContext ctx, Map<String,String> params) {
50 if (params == null) {
51 params = new HashMap<String,String>();
52 }
53 Dispatcher du = new Dispatcher(ctx, params);
54 du.init();
55 Dispatcher.setInstance(du);
56
57
58 ValueStack stack = du.getContainer().getInstance(ValueStackFactory.class).createValueStack();
59 stack.getContext().put(ActionContext.CONTAINER, du.getContainer());
60 ActionContext.setContext(new ActionContext(stack.getContext()));
61
62 return du;
63 }
64
65 public static void tearDown() throws Exception {
66 Dispatcher.setInstance(null);
67 ActionContext.setContext(null);
68 }
69 }