View Javadoc

1   /*
2    * $Id: ExecuteAndWaitInterceptorTest.java 476642 2006-11-18 22:40:18Z mrdon $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.interceptor;
22  
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import javax.servlet.http.HttpSession;
30  
31  import org.apache.struts2.ServletActionContext;
32  import org.apache.struts2.StrutsTestCase;
33  import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
34  import org.apache.struts2.views.jsp.StrutsMockHttpSession;
35  
36  import com.opensymphony.xwork2.Action;
37  import com.opensymphony.xwork2.ActionContext;
38  import com.opensymphony.xwork2.ActionProxy;
39  import com.opensymphony.xwork2.ActionProxyFactory;
40  import com.opensymphony.xwork2.DefaultActionProxyFactory;
41  import com.opensymphony.xwork2.ObjectFactory;
42  import com.opensymphony.xwork2.config.Configuration;
43  import com.opensymphony.xwork2.config.ConfigurationException;
44  import com.opensymphony.xwork2.config.ConfigurationManager;
45  import com.opensymphony.xwork2.config.ConfigurationProvider;
46  import com.opensymphony.xwork2.config.entities.ActionConfig;
47  import com.opensymphony.xwork2.config.entities.InterceptorMapping;
48  import com.opensymphony.xwork2.config.entities.PackageConfig;
49  import com.opensymphony.xwork2.config.entities.ResultConfig;
50  import com.opensymphony.xwork2.inject.ContainerBuilder;
51  import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
52  import com.opensymphony.xwork2.mock.MockResult;
53  import com.opensymphony.xwork2.util.location.LocatableProperties;
54  
55  /***
56   * Test case for ExecuteAndWaitInterceptor.
57   */
58  public class ExecuteAndWaitInterceptorTest extends StrutsTestCase {
59  
60      private StrutsMockHttpServletRequest request;
61      private HttpSession httpSession;
62      private Map context;
63      private Map params;
64      private Map session;
65      private ExecuteAndWaitInterceptor waitInterceptor;
66  
67      public void _testOneWait() throws Exception {
68          waitInterceptor.setDelay(0);
69          waitInterceptor.setDelaySleepInterval(0);
70  
71          ActionProxy proxy = buildProxy("action1");
72          String result = proxy.execute();
73          assertEquals("wait", result);
74  
75          Thread.sleep(1000);
76  
77          ActionProxy proxy2 = buildProxy("action1");
78          String result2 = proxy2.execute();
79          assertEquals("success", result2);
80      }
81  
82      public void _testTwoWait() throws Exception {
83          waitInterceptor.setDelay(0);
84          waitInterceptor.setDelaySleepInterval(0);
85  
86          ActionProxy proxy = buildProxy("action1");
87          String result = proxy.execute();
88          assertEquals("wait", result);
89  
90          Thread.sleep(300);
91  
92          ActionProxy proxy2 = buildProxy("action1");
93          String result2 = proxy2.execute();
94          assertEquals("wait", result2);
95  
96          Thread.sleep(300);
97  
98          ActionProxy proxy3 = buildProxy("action1");
99          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", (after - before) >= 190);
113 
114         Thread.sleep(400);
115 
116         ActionProxy proxy2 = buildProxy("action1");
117         String result2 = proxy2.execute();
118         assertEquals("success", result2);
119     }
120 
121     public void _testTwoWaitWithDelay() throws Exception {
122         waitInterceptor.setDelay(100);
123         waitInterceptor.setDelaySleepInterval(100);
124 
125         ActionProxy proxy = buildProxy("action1");
126         long before = System.currentTimeMillis();
127         String result = proxy.execute();
128         long after = System.currentTimeMillis();
129         assertEquals("wait", result);
130         assertTrue("delay should be ca. 100 millis", (after - before) >= 90);
131 
132         Thread.sleep(100);
133 
134         ActionProxy proxy2 = buildProxy("action1");
135         long before2 = System.currentTimeMillis();
136         String result2 = proxy2.execute();
137         long after2 = System.currentTimeMillis();
138         assertEquals("wait", result2);
139         assertTrue("there should be no delay", (after2 - before2) < 110);
140 
141         Thread.sleep(400);
142 
143         ActionProxy proxy3 = buildProxy("action1");
144         String result3 = proxy3.execute();
145         assertEquals("success", result3);
146     }
147 
148     public void testWaitDelayAndJobAlreadyDone() throws Exception {
149         waitInterceptor.setDelay(1500);
150         waitInterceptor.setDelaySleepInterval(100);
151 
152         ActionProxy proxy = buildProxy("action1");
153         long before = System.currentTimeMillis();
154         String result = proxy.execute();
155         long diff = System.currentTimeMillis() - before;
156         assertEquals("success", result);
157         assertTrue("Job done already after 500 so there should not be such long delay", diff <= 750);
158     }
159 
160     public void testWaitDelayAndJobAlreadyDone2() throws Exception {
161         waitInterceptor.setDelay(1500);
162         waitInterceptor.setDelaySleepInterval(200); // just takes a little longer to find out job is done
163 
164         ActionProxy proxy = buildProxy("action1");
165         long before = System.currentTimeMillis();
166         String result = proxy.execute();
167         long diff = System.currentTimeMillis() - before;
168         assertEquals("success", result);
169         assertTrue("Job done already after 500 so there should not be such long delay", diff <= 750);
170     }
171 
172     protected ActionProxy buildProxy(String actionName) throws Exception {
173         return actionProxyFactory.createActionProxy("", actionName, context);
174     }
175 
176     protected void setUp() throws Exception {
177         loadConfigurationProviders(new WaitConfigurationProvider());
178 
179         session = new HashMap();
180         params = new HashMap();
181         context = new HashMap();
182         context.put(ActionContext.SESSION, session);
183         context.put(ActionContext.PARAMETERS, params);
184 
185         request = new StrutsMockHttpServletRequest();
186         httpSession = new StrutsMockHttpSession();
187         request.setSession(httpSession);
188         request.setParameterMap(params);
189         context.put(ServletActionContext.HTTP_REQUEST, request);
190     }
191 
192     protected void tearDown() throws Exception {
193         configurationManager.clearConfigurationProviders();
194         configurationManager.destroyConfiguration();
195         ActionContext.setContext(null);
196     }
197 
198     private class WaitConfigurationProvider implements ConfigurationProvider {
199 
200         Configuration configuration;
201         public void destroy() {
202             waitInterceptor.destroy();
203         }
204 
205         public boolean needsReload() {
206             return false;
207         }
208         
209         public void init(Configuration configuration) throws ConfigurationException {
210             this.configuration = configuration;
211         }
212 
213         public void loadPackages() throws ConfigurationException {
214             PackageConfig wait = new PackageConfig("");
215 
216             Map results = new HashMap();
217             results.put(Action.SUCCESS, new ResultConfig(Action.SUCCESS, MockResult.class.getName(), null));
218             results.put(ExecuteAndWaitInterceptor.WAIT, new ResultConfig(ExecuteAndWaitInterceptor.WAIT, MockResult.class.getName(), null));
219 
220             // interceptors
221             waitInterceptor = new ExecuteAndWaitInterceptor();
222             List interceptors = new ArrayList();
223             interceptors.add(new InterceptorMapping("params", new ParametersInterceptor()));
224             interceptors.add(new InterceptorMapping("execAndWait", waitInterceptor));
225 
226             ActionConfig ac = new ActionConfig(null, ExecuteAndWaitDelayAction.class, null, results, interceptors);
227             wait.addActionConfig("action1", ac);
228 
229             configuration.addPackageConfig("", wait);
230         }
231 
232         public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
233             builder.factory(ObjectFactory.class);
234             builder.factory(ActionProxyFactory.class, DefaultActionProxyFactory.class);
235         }
236 
237     }
238 }
239