1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.interceptor;
19
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.servlet.http.HttpSession;
26
27 import org.apache.struts2.ServletActionContext;
28 import org.apache.struts2.StrutsTestCase;
29 import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
30 import org.apache.struts2.views.jsp.StrutsMockHttpSession;
31
32 import com.opensymphony.xwork2.Action;
33 import com.opensymphony.xwork2.ActionContext;
34 import com.opensymphony.xwork2.ActionProxy;
35 import com.opensymphony.xwork2.ActionProxyFactory;
36 import com.opensymphony.xwork2.config.Configuration;
37 import com.opensymphony.xwork2.config.ConfigurationException;
38 import com.opensymphony.xwork2.config.ConfigurationProvider;
39 import com.opensymphony.xwork2.config.entities.ActionConfig;
40 import com.opensymphony.xwork2.config.entities.InterceptorMapping;
41 import com.opensymphony.xwork2.config.entities.PackageConfig;
42 import com.opensymphony.xwork2.config.entities.ResultConfig;
43 import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
44 import com.opensymphony.xwork2.mock.MockResult;
45
46 /***
47 * Test case for ExecuteAndWaitInterceptor.
48 */
49 public class ExecuteAndWaitInterceptorTest extends StrutsTestCase {
50
51 private StrutsMockHttpServletRequest request;
52 private HttpSession httpSession;
53 private Map context;
54 private Map params;
55 private Map session;
56 private ExecuteAndWaitInterceptor waitInterceptor;
57
58 public void _testOneWait() throws Exception {
59 waitInterceptor.setDelay(0);
60 waitInterceptor.setDelaySleepInterval(0);
61
62 ActionProxy proxy = buildProxy("action1");
63 String result = proxy.execute();
64 assertEquals("wait", result);
65
66 Thread.sleep(1000);
67
68 ActionProxy proxy2 = buildProxy("action1");
69 String result2 = proxy2.execute();
70 assertEquals("success", result2);
71 }
72
73 public void _testTwoWait() throws Exception {
74 waitInterceptor.setDelay(0);
75 waitInterceptor.setDelaySleepInterval(0);
76
77 ActionProxy proxy = buildProxy("action1");
78 String result = proxy.execute();
79 assertEquals("wait", result);
80
81 Thread.sleep(300);
82
83 ActionProxy proxy2 = buildProxy("action1");
84 String result2 = proxy2.execute();
85 assertEquals("wait", result2);
86
87 Thread.sleep(300);
88
89 ActionProxy proxy3 = buildProxy("action1");
90 String result3 = proxy3.execute();
91 assertEquals("success", result3);
92 }
93
94 public void _testOneWaitWithDelay() throws Exception {
95 waitInterceptor.setDelay(200);
96 waitInterceptor.setDelaySleepInterval(100);
97
98 ActionProxy proxy = buildProxy("action1");
99 long before = System.currentTimeMillis();
100 String result = proxy.execute();
101 long after = System.currentTimeMillis();
102 assertEquals("wait", result);
103 assertTrue("delay should be ca. 200 millis", (after - before) >= 190);
104
105 Thread.sleep(400);
106
107 ActionProxy proxy2 = buildProxy("action1");
108 String result2 = proxy2.execute();
109 assertEquals("success", result2);
110 }
111
112 public void _testTwoWaitWithDelay() throws Exception {
113 waitInterceptor.setDelay(100);
114 waitInterceptor.setDelaySleepInterval(100);
115
116 ActionProxy proxy = buildProxy("action1");
117 long before = System.currentTimeMillis();
118 String result = proxy.execute();
119 long after = System.currentTimeMillis();
120 assertEquals("wait", result);
121 assertTrue("delay should be ca. 100 millis", (after - before) >= 90);
122
123 Thread.sleep(100);
124
125 ActionProxy proxy2 = buildProxy("action1");
126 long before2 = System.currentTimeMillis();
127 String result2 = proxy2.execute();
128 long after2 = System.currentTimeMillis();
129 assertEquals("wait", result2);
130 assertTrue("there should be no delay", (after2 - before2) < 110);
131
132 Thread.sleep(400);
133
134 ActionProxy proxy3 = buildProxy("action1");
135 String result3 = proxy3.execute();
136 assertEquals("success", result3);
137 }
138
139 public void testWaitDelayAndJobAlreadyDone() throws Exception {
140 waitInterceptor.setDelay(1500);
141 waitInterceptor.setDelaySleepInterval(100);
142
143 ActionProxy proxy = buildProxy("action1");
144 long before = System.currentTimeMillis();
145 String result = proxy.execute();
146 long diff = System.currentTimeMillis() - before;
147 assertEquals("success", result);
148 assertTrue("Job done already after 500 so there should not be such long delay", diff <= 750);
149 }
150
151 public void testWaitDelayAndJobAlreadyDone2() throws Exception {
152 waitInterceptor.setDelay(1500);
153 waitInterceptor.setDelaySleepInterval(200);
154
155 ActionProxy proxy = buildProxy("action1");
156 long before = System.currentTimeMillis();
157 String result = proxy.execute();
158 long diff = System.currentTimeMillis() - before;
159 assertEquals("success", result);
160 assertTrue("Job done already after 500 so there should not be such long delay", diff <= 750);
161 }
162
163 protected ActionProxy buildProxy(String actionName) throws Exception {
164 return ActionProxyFactory.getFactory().createActionProxy(
165 configurationManager.getConfiguration(), "", actionName, context);
166 }
167
168 protected void setUp() throws Exception {
169 configurationManager.clearConfigurationProviders();
170 configurationManager.addConfigurationProvider(new WaitConfigurationProvider());
171 configurationManager.reload();
172
173 session = new HashMap();
174 params = new HashMap();
175 context = new HashMap();
176 context.put(ActionContext.SESSION, session);
177 context.put(ActionContext.PARAMETERS, params);
178
179 request = new StrutsMockHttpServletRequest();
180 httpSession = new StrutsMockHttpSession();
181 request.setSession(httpSession);
182 request.setParameterMap(params);
183 context.put(ServletActionContext.HTTP_REQUEST, request);
184 }
185
186 protected void tearDown() throws Exception {
187 configurationManager.clearConfigurationProviders();
188 configurationManager.destroyConfiguration();
189 ActionContext.setContext(null);
190 }
191
192 private class WaitConfigurationProvider implements ConfigurationProvider {
193
194 public void destroy() {
195 waitInterceptor.destroy();
196 }
197
198 public boolean needsReload() {
199 return false;
200 }
201
202 public void init(Configuration configuration) throws ConfigurationException {
203 PackageConfig wait = new PackageConfig("");
204
205 Map results = new HashMap();
206 results.put(Action.SUCCESS, new ResultConfig(Action.SUCCESS, MockResult.class.getName(), null));
207 results.put(ExecuteAndWaitInterceptor.WAIT, new ResultConfig(ExecuteAndWaitInterceptor.WAIT, MockResult.class.getName(), null));
208
209
210 waitInterceptor = new ExecuteAndWaitInterceptor();
211 List interceptors = new ArrayList();
212 interceptors.add(new InterceptorMapping("params", new ParametersInterceptor()));
213 interceptors.add(new InterceptorMapping("execAndWait", waitInterceptor));
214
215 ActionConfig ac = new ActionConfig(null, ExecuteAndWaitDelayAction.class, null, results, interceptors);
216 wait.addActionConfig("action1", ac);
217
218 configuration.addPackageConfig("", wait);
219 }
220
221 }
222 }
223