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.interceptor;
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 javax.servlet.http.HttpSession;
31
32 import org.apache.struts2.ServletActionContext;
33 import org.apache.struts2.StrutsTestCase;
34 import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
35 import org.apache.struts2.views.jsp.StrutsMockHttpSession;
36
37 import com.opensymphony.xwork2.Action;
38 import com.opensymphony.xwork2.ActionContext;
39 import com.opensymphony.xwork2.ActionProxy;
40 import com.opensymphony.xwork2.ActionProxyFactory;
41 import com.opensymphony.xwork2.DefaultActionProxyFactory;
42 import com.opensymphony.xwork2.ObjectFactory;
43 import com.opensymphony.xwork2.config.Configuration;
44 import com.opensymphony.xwork2.config.ConfigurationException;
45 import com.opensymphony.xwork2.config.ConfigurationManager;
46 import com.opensymphony.xwork2.config.ConfigurationProvider;
47 import com.opensymphony.xwork2.config.entities.ActionConfig;
48 import com.opensymphony.xwork2.config.entities.InterceptorMapping;
49 import com.opensymphony.xwork2.config.entities.PackageConfig;
50 import com.opensymphony.xwork2.config.entities.ResultConfig;
51 import com.opensymphony.xwork2.inject.ContainerBuilder;
52 import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
53 import com.opensymphony.xwork2.mock.MockResult;
54 import com.opensymphony.xwork2.util.location.LocatableProperties;
55
56 /***
57 * Test case for ExecuteAndWaitInterceptor.
58 */
59 public class ExecuteAndWaitInterceptorTest extends StrutsTestCase {
60
61 private StrutsMockHttpServletRequest request;
62 private HttpSession httpSession;
63 private Map context;
64 private Map params;
65 private Map session;
66 private ExecuteAndWaitInterceptor waitInterceptor;
67
68 public void testOneWait() throws Exception {
69 waitInterceptor.setDelay(0);
70 waitInterceptor.setDelaySleepInterval(0);
71
72 ActionProxy proxy = buildProxy("action1");
73 String result = proxy.execute();
74 assertEquals("wait", result);
75
76 Thread.sleep(1000);
77
78 ActionProxy proxy2 = buildProxy("action1");
79 String result2 = proxy2.execute();
80 assertEquals("success", result2);
81 }
82
83 public void testTwoWait() throws Exception {
84 waitInterceptor.setDelay(0);
85 waitInterceptor.setDelaySleepInterval(0);
86
87 ActionProxy proxy = buildProxy("action1");
88 String result = proxy.execute();
89 assertEquals("wait", result);
90
91 Thread.sleep(300);
92
93 ActionProxy proxy2 = buildProxy("action1");
94 String result2 = proxy2.execute();
95 assertEquals("wait", result2);
96
97 Thread.sleep(300);
98
99 ActionProxy proxy3 = buildProxy("action1");
100 String result3 = proxy3.execute();
101 assertEquals("success", result3);
102 }
103
104 public void testOneWaitWithDelay() throws Exception {
105 waitInterceptor.setDelay(200);
106 waitInterceptor.setDelaySleepInterval(100);
107
108 ActionProxy proxy = buildProxy("action1");
109 long before = System.currentTimeMillis();
110 String result = proxy.execute();
111 long after = System.currentTimeMillis();
112 assertEquals("wait", result);
113 assertTrue("delay should be ca. 200 millis", (after - before) >= 190);
114
115 Thread.sleep(400);
116
117 ActionProxy proxy2 = buildProxy("action1");
118 String result2 = proxy2.execute();
119 assertEquals("success", result2);
120 }
121
122 public void testTwoWaitWithDelay() throws Exception {
123 waitInterceptor.setDelay(100);
124 waitInterceptor.setDelaySleepInterval(100);
125
126 ActionProxy proxy = buildProxy("action1");
127 long before = System.currentTimeMillis();
128 String result = proxy.execute();
129 long after = System.currentTimeMillis();
130 assertEquals("wait", result);
131 assertTrue("delay should be ca. 100 millis", (after - before) >= 90);
132
133 Thread.sleep(100);
134
135 ActionProxy proxy2 = buildProxy("action1");
136 long before2 = System.currentTimeMillis();
137 String result2 = proxy2.execute();
138 long after2 = System.currentTimeMillis();
139 assertEquals("wait", result2);
140 assertTrue("there should be no delay", (after2 - before2) < 110);
141
142 Thread.sleep(400);
143
144 ActionProxy proxy3 = buildProxy("action1");
145 String result3 = proxy3.execute();
146 assertEquals("success", result3);
147 }
148
149 public void testWaitDelayAndJobAlreadyDone() throws Exception {
150 waitInterceptor.setDelay(1500);
151 waitInterceptor.setDelaySleepInterval(100);
152
153 ActionProxy proxy = buildProxy("action1");
154 long before = System.currentTimeMillis();
155 String result = proxy.execute();
156 long diff = System.currentTimeMillis() - before;
157 assertEquals("success", result);
158 assertTrue("Job done already after 500 so there should not be such long delay", diff <= 1000);
159 }
160
161 public void testWaitDelayAndJobAlreadyDone2() throws Exception {
162 waitInterceptor.setDelay(1500);
163 waitInterceptor.setDelaySleepInterval(200);
164
165 ActionProxy proxy = buildProxy("action1");
166 long before = System.currentTimeMillis();
167 String result = proxy.execute();
168 long diff = System.currentTimeMillis() - before;
169 assertEquals("success", result);
170 assertTrue("Job done already after 500 so there should not be such long delay", diff <= 1000);
171 }
172
173 protected ActionProxy buildProxy(String actionName) throws Exception {
174 return actionProxyFactory.createActionProxy("", actionName, null, context);
175 }
176
177 protected void setUp() throws Exception {
178 loadConfigurationProviders(new WaitConfigurationProvider());
179
180 session = new HashMap();
181 params = new HashMap();
182 context = new HashMap();
183 context.put(ActionContext.SESSION, session);
184 context.put(ActionContext.PARAMETERS, params);
185
186 request = new StrutsMockHttpServletRequest();
187 httpSession = new StrutsMockHttpSession();
188 request.setSession(httpSession);
189 request.setParameterMap(params);
190 context.put(ServletActionContext.HTTP_REQUEST, request);
191 }
192
193 protected void tearDown() throws Exception {
194 configurationManager.clearConfigurationProviders();
195 configurationManager.destroyConfiguration();
196 ActionContext.setContext(null);
197 }
198
199 private class WaitConfigurationProvider implements ConfigurationProvider {
200
201 Configuration configuration;
202 public void destroy() {
203 waitInterceptor.destroy();
204 }
205
206 public boolean needsReload() {
207 return false;
208 }
209
210 public void init(Configuration configuration) throws ConfigurationException {
211 this.configuration = configuration;
212 }
213
214 public void loadPackages() throws ConfigurationException {
215
216
217
218 waitInterceptor = new ExecuteAndWaitInterceptor();
219
220 PackageConfig wait = new PackageConfig.Builder("")
221 .addActionConfig("action1", new ActionConfig.Builder("", "action1", ExecuteAndWaitDelayAction.class.getName())
222 .addResultConfig(new ResultConfig.Builder(Action.SUCCESS, MockResult.class.getName()).build())
223 .addResultConfig(new ResultConfig.Builder(ExecuteAndWaitInterceptor.WAIT, MockResult.class.getName()).build())
224 .addInterceptor(new InterceptorMapping("params", new ParametersInterceptor()))
225 .addInterceptor(new InterceptorMapping("execAndWait", waitInterceptor))
226 .build())
227 .build();
228 configuration.addPackageConfig("", wait);
229 }
230
231 public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
232 builder.factory(ObjectFactory.class);
233 builder.factory(ActionProxyFactory.class, DefaultActionProxyFactory.class);
234 }
235
236 }
237 }
238