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.dispatcher;
22
23 import java.util.HashMap;
24 import java.util.Locale;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.struts2.StrutsConstants;
30 import org.apache.struts2.StrutsTestCase;
31 import org.springframework.mock.web.MockHttpServletRequest;
32 import org.springframework.mock.web.MockHttpServletResponse;
33 import org.springframework.mock.web.MockServletContext;
34
35 import com.opensymphony.xwork2.config.ConfigurationManager;
36 import com.opensymphony.xwork2.util.LocalizedTextUtil;
37
38 /***
39 * Test case for Dispatcher.
40 *
41 */
42 public class DispatcherTest extends StrutsTestCase {
43
44 public void testDefaultResurceBundlePropertyLoaded() throws Exception {
45 Locale.setDefault(Locale.US);
46
47
48 assertEquals(
49 LocalizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.US),
50 "Error during Action invocation");
51
52
53 assertEquals(
54 LocalizedTextUtil.findDefaultText("struts.messages.error.uploading", Locale.US,
55 new Object[] { "some error messages" }),
56 "Error uploading: some error messages");
57 }
58
59 public void testPrepareSetEncodingProperly() throws Exception {
60 HttpServletRequest req = new MockHttpServletRequest();
61 HttpServletResponse res = new MockHttpServletResponse();
62
63 Dispatcher du = initDispatcher(new HashMap() {{
64 put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
65 }});
66 du.prepare(req, res);
67
68 assertEquals(req.getCharacterEncoding(), "utf-8");
69 }
70
71 public void testPrepareSetEncodingPropertyWithMultipartRequest() throws Exception {
72 MockHttpServletRequest req = new MockHttpServletRequest();
73 MockHttpServletResponse res = new MockHttpServletResponse();
74
75 req.setContentType("multipart/form-data");
76 Dispatcher du = initDispatcher(new HashMap() {{
77 put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
78 }});
79 du.prepare(req, res);
80
81 assertEquals("utf-8", req.getCharacterEncoding());
82 }
83
84 public void testDispatcherListener() throws Exception {
85
86 final DispatcherListenerState state = new DispatcherListenerState();
87
88 Dispatcher.addDispatcherListener(new DispatcherListener() {
89 public void dispatcherDestroyed(Dispatcher du) {
90 state.isDestroyed = true;
91 }
92 public void dispatcherInitialized(Dispatcher du) {
93 state.isInitialized = true;
94 }
95 });
96
97
98 assertFalse(state.isDestroyed);
99 assertFalse(state.isInitialized);
100
101 Dispatcher du = initDispatcher(new HashMap<String, String>() );
102
103 assertTrue(state.isInitialized);
104
105 du.cleanup();
106
107 assertTrue(state.isDestroyed);
108 }
109
110
111 public void testConfigurationManager() {
112 Dispatcher du = null;
113 InternalConfigurationManager configurationManager = new InternalConfigurationManager();
114 try {
115 du = new Dispatcher(new MockServletContext(), new HashMap<String, String>());
116 du.setConfigurationManager(configurationManager);
117
118 du.init();
119
120 Dispatcher.setInstance(du);
121
122 assertFalse(configurationManager.destroyConfiguration);
123
124 du.cleanup();
125
126 assertTrue(configurationManager.destroyConfiguration);
127
128 }
129 finally {
130 du.setInstance(null);
131 }
132 }
133
134 class InternalConfigurationManager extends ConfigurationManager {
135 public boolean destroyConfiguration = false;
136
137 @Override
138 public synchronized void destroyConfiguration() {
139 super.destroyConfiguration();
140 destroyConfiguration = true;
141 }
142 }
143
144
145 class DispatcherListenerState {
146 public boolean isInitialized = false;
147 public boolean isDestroyed = false;
148 }
149 }