001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005:
006: package com.opensymphony.webwork.interceptor;
007:
008: import com.opensymphony.webwork.ServletActionContext;
009: import com.opensymphony.webwork.WebWorkTestCase;
010: import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletRequest;
011: import com.opensymphony.webwork.views.jsp.WebWorkMockHttpSession;
012: import com.opensymphony.xwork.*;
013: import com.opensymphony.xwork.mock.MockResult;
014: import com.opensymphony.xwork.config.Configuration;
015: import com.opensymphony.xwork.config.ConfigurationException;
016: import com.opensymphony.xwork.config.ConfigurationManager;
017: import com.opensymphony.xwork.config.ConfigurationProvider;
018: import com.opensymphony.xwork.config.entities.ActionConfig;
019: import com.opensymphony.xwork.config.entities.PackageConfig;
020: import com.opensymphony.xwork.config.entities.ResultConfig;
021: import com.opensymphony.xwork.config.entities.InterceptorMapping;
022: import com.opensymphony.xwork.interceptor.ParametersInterceptor;
023:
024: import javax.servlet.http.HttpSession;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: /**
031: * Test case for ExecuteAndWaitInterceptor.
032: *
033: * @author Claus Ibsen
034: */
035: public class ExecuteAndWaitInterceptorTest extends WebWorkTestCase {
036:
037: private WebWorkMockHttpServletRequest request;
038: private HttpSession httpSession;
039: private Map context;
040: private Map params;
041: private Map session;
042: private ExecuteAndWaitInterceptor waitInterceptor;
043:
044: public void testOneWait() throws Exception {
045: waitInterceptor.setDelay(0);
046: waitInterceptor.setDelaySleepInterval(0);
047:
048: ActionProxy proxy = buildProxy("action1");
049: String result = proxy.execute();
050: assertEquals("wait", result);
051:
052: Thread.sleep(1000);
053:
054: ActionProxy proxy2 = buildProxy("action1");
055: String result2 = proxy2.execute();
056: assertEquals("success", result2);
057: }
058:
059: public void testTwoWait() throws Exception {
060: waitInterceptor.setDelay(0);
061: waitInterceptor.setDelaySleepInterval(0);
062:
063: ActionProxy proxy = buildProxy("action1");
064: String result = proxy.execute();
065: assertEquals("wait", result);
066:
067: Thread.sleep(300);
068:
069: ActionProxy proxy2 = buildProxy("action1");
070: String result2 = proxy2.execute();
071: assertEquals("wait", result2);
072:
073: Thread.sleep(300);
074:
075: ActionProxy proxy3 = buildProxy("action1");
076: String result3 = proxy3.execute();
077: assertEquals("success", result3);
078: }
079:
080: public void testOneWaitWithDelay() throws Exception {
081: waitInterceptor.setDelay(200);
082: waitInterceptor.setDelaySleepInterval(100);
083:
084: ActionProxy proxy = buildProxy("action1");
085: long before = System.currentTimeMillis();
086: String result = proxy.execute();
087: long after = System.currentTimeMillis();
088: assertEquals("wait", result);
089: assertTrue("delay should be ca. 200 millis",
090: (after - before) >= 190);
091:
092: Thread.sleep(400);
093:
094: ActionProxy proxy2 = buildProxy("action1");
095: String result2 = proxy2.execute();
096: assertEquals("success", result2);
097: }
098:
099: public void testTwoWaitWithDelay() throws Exception {
100: waitInterceptor.setDelay(100);
101: waitInterceptor.setDelaySleepInterval(100);
102:
103: ActionProxy proxy = buildProxy("action1");
104: long before = System.currentTimeMillis();
105: String result = proxy.execute();
106: long after = System.currentTimeMillis();
107: assertEquals("wait", result);
108: assertTrue("delay should be ca. 100 millis",
109: (after - before) >= 90);
110:
111: Thread.sleep(100);
112:
113: ActionProxy proxy2 = buildProxy("action1");
114: long before2 = System.currentTimeMillis();
115: String result2 = proxy2.execute();
116: long after2 = System.currentTimeMillis();
117: assertEquals("wait", result2);
118: assertTrue("there should be no delay", (after2 - before2) < 110);
119:
120: Thread.sleep(400);
121:
122: ActionProxy proxy3 = buildProxy("action1");
123: String result3 = proxy3.execute();
124: assertEquals("success", result3);
125: }
126:
127: public void testWaitDelayAndJobAlreadyDone() throws Exception {
128: waitInterceptor.setDelay(1500);
129: waitInterceptor.setDelaySleepInterval(100);
130:
131: ActionProxy proxy = buildProxy("action1");
132: long before = System.currentTimeMillis();
133: String result = proxy.execute();
134: long diff = System.currentTimeMillis() - before;
135: assertEquals("success", result);
136: assertTrue(
137: "Job done already after 500 so there should not be such long delay",
138: diff <= 750);
139: }
140:
141: public void testWaitDelayAndJobAlreadyDone2() throws Exception {
142: waitInterceptor.setDelay(1500);
143: waitInterceptor.setDelaySleepInterval(200); // just takes a little longer to find out job is done
144:
145: ActionProxy proxy = buildProxy("action1");
146: long before = System.currentTimeMillis();
147: String result = proxy.execute();
148: long diff = System.currentTimeMillis() - before;
149: assertEquals("success", result);
150: assertTrue(
151: "Job done already after 500 so there should not be such long delay",
152: diff <= 750);
153: }
154:
155: protected ActionProxy buildProxy(String actionName)
156: throws Exception {
157: return ActionProxyFactory.getFactory().createActionProxy("",
158: actionName, context);
159: }
160:
161: protected void setUp() throws Exception {
162: ConfigurationManager.clearConfigurationProviders();
163: ConfigurationManager
164: .addConfigurationProvider(new WaitConfigurationProvider());
165: ConfigurationManager.getConfiguration().reload();
166:
167: session = new HashMap();
168: params = new HashMap();
169: context = new HashMap();
170: context.put(ActionContext.SESSION, session);
171: context.put(ActionContext.PARAMETERS, params);
172:
173: request = new WebWorkMockHttpServletRequest();
174: httpSession = new WebWorkMockHttpSession();
175: request.setSession(httpSession);
176: request.setParameterMap(params);
177: context.put(ServletActionContext.HTTP_REQUEST, request);
178: }
179:
180: protected void tearDown() throws Exception {
181: ConfigurationManager.clearConfigurationProviders();
182: ConfigurationManager.destroyConfiguration();
183: ActionContext.setContext(null);
184: }
185:
186: private class WaitConfigurationProvider implements
187: ConfigurationProvider {
188:
189: public void destroy() {
190: waitInterceptor.destroy();
191: }
192:
193: public boolean needsReload() {
194: return false;
195: }
196:
197: public void init(Configuration configuration)
198: throws ConfigurationException {
199: PackageConfig wait = new PackageConfig();
200:
201: Map results = new HashMap();
202: results.put(Action.SUCCESS, new ResultConfig(
203: Action.SUCCESS, MockResult.class.getName(), null));
204: results.put(ExecuteAndWaitInterceptor.WAIT,
205: new ResultConfig(ExecuteAndWaitInterceptor.WAIT,
206: MockResult.class.getName(), null));
207:
208: // interceptors
209: waitInterceptor = new ExecuteAndWaitInterceptor();
210: List interceptors = new ArrayList();
211: interceptors.add(new InterceptorMapping("params",
212: new ParametersInterceptor()));
213: interceptors.add(new InterceptorMapping("execAndWait",
214: waitInterceptor));
215:
216: ActionConfig ac = new ActionConfig(null,
217: ExecuteAndWaitDelayAction.class, null, results,
218: interceptors);
219: wait.addActionConfig("action1", ac);
220:
221: configuration.addPackageConfig("wait", wait);
222: }
223:
224: }
225: }
|