View Javadoc

1   /*
2    * $Id: ExecuteAndWaitInterceptorTest.java 682411 2008-08-04 14:55:30Z musachy $
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  
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      private ParametersInterceptor parametersInterceptor;
68  
69      public void testOneWait() throws Exception {
70          waitInterceptor.setDelay(0);
71          waitInterceptor.setDelaySleepInterval(0);
72  
73          ActionProxy proxy = buildProxy("action1");
74          String result = proxy.execute();
75          assertEquals("wait", result);
76  
77          Thread.sleep(1000);
78  
79          ActionProxy proxy2 = buildProxy("action1");
80          String result2 = proxy2.execute();
81          assertEquals("success", result2);
82      }
83  
84      public void testTwoWait() throws Exception {
85          waitInterceptor.setDelay(0);
86          waitInterceptor.setDelaySleepInterval(0);
87  
88          ActionProxy proxy = buildProxy("action1");
89          String result = proxy.execute();
90          assertEquals("wait", result);
91  
92          Thread.sleep(300);
93  
94          ActionProxy proxy2 = buildProxy("action1");
95          String result2 = proxy2.execute();
96          assertEquals("wait", result2);
97  
98          Thread.sleep(300);
99  
100         ActionProxy proxy3 = buildProxy("action1");
101         String result3 = proxy3.execute();
102         assertEquals("success", result3);
103     }
104 
105     public void testOneWaitWithDelay() throws Exception {
106         waitInterceptor.setDelay(200);
107         waitInterceptor.setDelaySleepInterval(100);
108 
109         ActionProxy proxy = buildProxy("action1");
110         long before = System.currentTimeMillis();
111         String result = proxy.execute();
112         long after = System.currentTimeMillis();
113         assertEquals("wait", result);
114         assertTrue("delay should be ca. 200 millis", (after - before) >= 190);
115 
116         Thread.sleep(400);
117 
118         ActionProxy proxy2 = buildProxy("action1");
119         String result2 = proxy2.execute();
120         assertEquals("success", result2);
121     }
122 
123     public void testTwoWaitWithDelay() throws Exception {
124         waitInterceptor.setDelay(100);
125         waitInterceptor.setDelaySleepInterval(100);
126 
127         ActionProxy proxy = buildProxy("action1");
128         long before = System.currentTimeMillis();
129         String result = proxy.execute();
130         long after = System.currentTimeMillis();
131         assertEquals("wait", result);
132         assertTrue("delay should be ca. 100 millis", (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("Job done already after 500 so there should not be such long delay", diff <= 1000);
160     }
161 
162     public void testWaitDelayAndJobAlreadyDone2() throws Exception {
163         waitInterceptor.setDelay(1500);
164         waitInterceptor.setDelaySleepInterval(200); // just takes a little longer to find out job is done
165 
166         ActionProxy proxy = buildProxy("action1");
167         long before = System.currentTimeMillis();
168         String result = proxy.execute();
169         long diff = System.currentTimeMillis() - before;
170         assertEquals("success", result);
171         assertTrue("Job done already after 500 so there should not be such long delay", diff <= 1000);
172     }
173 
174     protected ActionProxy buildProxy(String actionName) throws Exception {
175         return actionProxyFactory.createActionProxy("", actionName, null, context);
176     }
177 
178     protected void setUp() throws Exception {
179         loadConfigurationProviders(new WaitConfigurationProvider());
180 
181         session = new HashMap();
182         params = new HashMap();
183         context = new HashMap();
184         context.put(ActionContext.SESSION, session);
185         context.put(ActionContext.PARAMETERS, params);
186 
187         request = new StrutsMockHttpServletRequest();
188         httpSession = new StrutsMockHttpSession();
189         request.setSession(httpSession);
190         request.setParameterMap(params);
191         context.put(ServletActionContext.HTTP_REQUEST, request);
192         container.inject(parametersInterceptor);
193     }
194 
195     protected void tearDown() throws Exception {
196         configurationManager.clearConfigurationProviders();
197         configurationManager.destroyConfiguration();
198         ActionContext.setContext(null);
199     }
200 
201     private class WaitConfigurationProvider implements ConfigurationProvider {
202 
203         Configuration configuration;
204         public void destroy() {
205             waitInterceptor.destroy();
206         }
207 
208         public boolean needsReload() {
209             return false;
210         }
211 
212         public void init(Configuration configuration) throws ConfigurationException {
213             this.configuration = configuration;
214         }
215 
216         public void loadPackages() throws ConfigurationException {
217 
218 
219             // interceptors
220             waitInterceptor = new ExecuteAndWaitInterceptor();
221             parametersInterceptor = new ParametersInterceptor();
222             PackageConfig wait = new PackageConfig.Builder("")
223                 .addActionConfig("action1", new ActionConfig.Builder("", "action1", ExecuteAndWaitDelayAction.class.getName())
224                     .addResultConfig(new ResultConfig.Builder(Action.SUCCESS, MockResult.class.getName()).build())
225                     .addResultConfig(new ResultConfig.Builder(ExecuteAndWaitInterceptor.WAIT, MockResult.class.getName()).build())
226                     .addInterceptor(new InterceptorMapping("params", parametersInterceptor))
227                     .addInterceptor(new InterceptorMapping("execAndWait", waitInterceptor))
228                 .build())
229             .build();
230             configuration.addPackageConfig("", wait);
231         }
232 
233         public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
234             builder.factory(ObjectFactory.class);
235             builder.factory(ActionProxyFactory.class, DefaultActionProxyFactory.class);
236         }
237 
238     }
239 }
240