001: /*
002: * $Id: ExecuteAndWaitInterceptorTest.java 527824 2007-04-12 07:53:39Z hermanns $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.interceptor;
022:
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Properties;
028:
029: import javax.servlet.http.HttpSession;
030:
031: import org.apache.struts2.ServletActionContext;
032: import org.apache.struts2.StrutsTestCase;
033: import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
034: import org.apache.struts2.views.jsp.StrutsMockHttpSession;
035:
036: import com.opensymphony.xwork2.Action;
037: import com.opensymphony.xwork2.ActionContext;
038: import com.opensymphony.xwork2.ActionProxy;
039: import com.opensymphony.xwork2.ActionProxyFactory;
040: import com.opensymphony.xwork2.DefaultActionProxyFactory;
041: import com.opensymphony.xwork2.ObjectFactory;
042: import com.opensymphony.xwork2.config.Configuration;
043: import com.opensymphony.xwork2.config.ConfigurationException;
044: import com.opensymphony.xwork2.config.ConfigurationManager;
045: import com.opensymphony.xwork2.config.ConfigurationProvider;
046: import com.opensymphony.xwork2.config.entities.ActionConfig;
047: import com.opensymphony.xwork2.config.entities.InterceptorMapping;
048: import com.opensymphony.xwork2.config.entities.PackageConfig;
049: import com.opensymphony.xwork2.config.entities.ResultConfig;
050: import com.opensymphony.xwork2.inject.ContainerBuilder;
051: import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
052: import com.opensymphony.xwork2.mock.MockResult;
053: import com.opensymphony.xwork2.util.location.LocatableProperties;
054:
055: /**
056: * Test case for ExecuteAndWaitInterceptor.
057: */
058: public class ExecuteAndWaitInterceptorTest extends StrutsTestCase {
059:
060: private StrutsMockHttpServletRequest request;
061: private HttpSession httpSession;
062: private Map context;
063: private Map params;
064: private Map session;
065: private ExecuteAndWaitInterceptor waitInterceptor;
066:
067: public void testOneWait() throws Exception {
068: waitInterceptor.setDelay(0);
069: waitInterceptor.setDelaySleepInterval(0);
070:
071: ActionProxy proxy = buildProxy("action1");
072: String result = proxy.execute();
073: assertEquals("wait", result);
074:
075: Thread.sleep(1000);
076:
077: ActionProxy proxy2 = buildProxy("action1");
078: String result2 = proxy2.execute();
079: assertEquals("success", result2);
080: }
081:
082: public void testTwoWait() throws Exception {
083: waitInterceptor.setDelay(0);
084: waitInterceptor.setDelaySleepInterval(0);
085:
086: ActionProxy proxy = buildProxy("action1");
087: String result = proxy.execute();
088: assertEquals("wait", result);
089:
090: Thread.sleep(300);
091:
092: ActionProxy proxy2 = buildProxy("action1");
093: String result2 = proxy2.execute();
094: assertEquals("wait", result2);
095:
096: Thread.sleep(300);
097:
098: ActionProxy proxy3 = buildProxy("action1");
099: String result3 = proxy3.execute();
100: assertEquals("success", result3);
101: }
102:
103: public void testOneWaitWithDelay() throws Exception {
104: waitInterceptor.setDelay(200);
105: waitInterceptor.setDelaySleepInterval(100);
106:
107: ActionProxy proxy = buildProxy("action1");
108: long before = System.currentTimeMillis();
109: String result = proxy.execute();
110: long after = System.currentTimeMillis();
111: assertEquals("wait", result);
112: assertTrue("delay should be ca. 200 millis",
113: (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",
132: (after - before) >= 90);
133:
134: Thread.sleep(100);
135:
136: ActionProxy proxy2 = buildProxy("action1");
137: long before2 = System.currentTimeMillis();
138: String result2 = proxy2.execute();
139: long after2 = System.currentTimeMillis();
140: assertEquals("wait", result2);
141: assertTrue("there should be no delay", (after2 - before2) < 110);
142:
143: Thread.sleep(400);
144:
145: ActionProxy proxy3 = buildProxy("action1");
146: String result3 = proxy3.execute();
147: assertEquals("success", result3);
148: }
149:
150: public void testWaitDelayAndJobAlreadyDone() throws Exception {
151: waitInterceptor.setDelay(1500);
152: waitInterceptor.setDelaySleepInterval(100);
153:
154: ActionProxy proxy = buildProxy("action1");
155: long before = System.currentTimeMillis();
156: String result = proxy.execute();
157: long diff = System.currentTimeMillis() - before;
158: assertEquals("success", result);
159: assertTrue(
160: "Job done already after 500 so there should not be such long delay",
161: diff <= 1000);
162: }
163:
164: public void testWaitDelayAndJobAlreadyDone2() throws Exception {
165: waitInterceptor.setDelay(1500);
166: waitInterceptor.setDelaySleepInterval(200); // just takes a little longer to find out job is done
167:
168: ActionProxy proxy = buildProxy("action1");
169: long before = System.currentTimeMillis();
170: String result = proxy.execute();
171: long diff = System.currentTimeMillis() - before;
172: assertEquals("success", result);
173: assertTrue(
174: "Job done already after 500 so there should not be such long delay",
175: diff <= 1000);
176: }
177:
178: protected ActionProxy buildProxy(String actionName)
179: throws Exception {
180: return actionProxyFactory.createActionProxy("", actionName,
181: context);
182: }
183:
184: protected void setUp() throws Exception {
185: loadConfigurationProviders(new WaitConfigurationProvider());
186:
187: session = new HashMap();
188: params = new HashMap();
189: context = new HashMap();
190: context.put(ActionContext.SESSION, session);
191: context.put(ActionContext.PARAMETERS, params);
192:
193: request = new StrutsMockHttpServletRequest();
194: httpSession = new StrutsMockHttpSession();
195: request.setSession(httpSession);
196: request.setParameterMap(params);
197: context.put(ServletActionContext.HTTP_REQUEST, request);
198: }
199:
200: protected void tearDown() throws Exception {
201: configurationManager.clearConfigurationProviders();
202: configurationManager.destroyConfiguration();
203: ActionContext.setContext(null);
204: }
205:
206: private class WaitConfigurationProvider implements
207: ConfigurationProvider {
208:
209: Configuration configuration;
210:
211: public void destroy() {
212: waitInterceptor.destroy();
213: }
214:
215: public boolean needsReload() {
216: return false;
217: }
218:
219: public void init(Configuration configuration)
220: throws ConfigurationException {
221: this .configuration = configuration;
222: }
223:
224: public void loadPackages() throws ConfigurationException {
225: PackageConfig wait = new PackageConfig("");
226:
227: Map results = new HashMap();
228: results.put(Action.SUCCESS, new ResultConfig(
229: Action.SUCCESS, MockResult.class.getName(), null));
230: results.put(ExecuteAndWaitInterceptor.WAIT,
231: new ResultConfig(ExecuteAndWaitInterceptor.WAIT,
232: MockResult.class.getName(), null));
233:
234: // interceptors
235: waitInterceptor = new ExecuteAndWaitInterceptor();
236: List interceptors = new ArrayList();
237: interceptors.add(new InterceptorMapping("params",
238: new ParametersInterceptor()));
239: interceptors.add(new InterceptorMapping("execAndWait",
240: waitInterceptor));
241:
242: ActionConfig ac = new ActionConfig(null,
243: ExecuteAndWaitDelayAction.class, null, results,
244: interceptors);
245: wait.addActionConfig("action1", ac);
246:
247: configuration.addPackageConfig("", wait);
248: }
249:
250: public void register(ContainerBuilder builder,
251: LocatableProperties props)
252: throws ConfigurationException {
253: builder.factory(ObjectFactory.class);
254: builder.factory(ActionProxyFactory.class,
255: DefaultActionProxyFactory.class);
256: }
257:
258: }
259: }
|