How to use method of org.easymock.EasyMockSupport class

Best Easymock code snippet using org.easymock.EasyMockSupport.

Run Easymock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1// Copyright (C) 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gerrit.httpd;
16
17import static org.easymock.EasyMock.anyObject;
18import static org.easymock.EasyMock.capture;
19import static org.easymock.EasyMock.eq;
20
21import com.google.gerrit.extensions.registration.DynamicSet;
22import com.google.gerrit.extensions.registration.ReloadableRegistrationHandle;
23import com.google.gerrit.server.plugins.Plugin;
24import com.google.gerrit.util.http.testutil.FakeHttpServletRequest;
25import com.google.gerrit.util.http.testutil.FakeHttpServletResponse;
26import com.google.inject.Key;
27import com.google.inject.util.Providers;
28import javax.servlet.FilterChain;
29import javax.servlet.FilterConfig;
30import javax.servlet.http.HttpServletRequest;
31import javax.servlet.http.HttpServletResponse;
32import org.easymock.Capture;
33import org.easymock.EasyMockSupport;
34import org.easymock.IMocksControl;
35import org.junit.Before;
36import org.junit.Test;
37
38public class AllRequestFilterFilterProxyTest {
39  /**
40   * Set of filters for FilterProxy
41   *
42   * <p>This set is used to as set of filters when fetching an {@link AllRequestFilter.FilterProxy}
43   * instance through {@link #getFilterProxy()}.
44   */
45  private DynamicSet<AllRequestFilter> filters;
46
47  @Before
48  public void setUp() throws Exception {
49    // Force starting each test with an initially empty set of filters.
50    // Filters get added by the tests themselves.
51    filters = new DynamicSet<>();
52  }
53
54  // The wrapping of {@link #getFilterProxy()} and
55  // {@link #addFilter(AllRequestFilter)} into separate methods may seem
56  // overengineered at this point. However, if in the future we decide to not
57  // test the inner class directly, but rather test from the outside using
58  // Guice Injectors, it is now sufficient to change only those two methods,
59  // and we need not mess with the individual tests.
60
61  /**
62   * Obtain a FilterProxy with a known DynamicSet of filters
63   *
64   * <p>The returned {@link AllRequestFilter.FilterProxy} can have new filters added dynamically by
65   * calling {@link #addFilter(AllRequestFilter)}.
66   */
67  private AllRequestFilter.FilterProxy getFilterProxy() {
68    return new AllRequestFilter.FilterProxy(filters);
69  }
70
71  /**
72   * Add a filter to created FilterProxy instances
73   *
74   * <p>This method adds the given filter to all {@link AllRequestFilter.FilterProxy} instances
75   * created by {@link #getFilterProxy()}.
76   */
77  private ReloadableRegistrationHandle<AllRequestFilter> addFilter(AllRequestFilter filter) {
78    Key<AllRequestFilter> key = Key.get(AllRequestFilter.class);
79    return filters.add("gerrit", key, Providers.of(filter));
80  }
81
82  @Test
83  public void noFilters() throws Exception {
84    EasyMockSupport ems = new EasyMockSupport();
85
86    FilterConfig config = ems.createMock(FilterConfig.class);
87    HttpServletRequest req = new FakeHttpServletRequest();
88    HttpServletResponse res = new FakeHttpServletResponse();
89
90    FilterChain chain = ems.createMock(FilterChain.class);
91    chain.doFilter(req, res);
92
93    ems.replayAll();
94
95    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
96
97    filterProxy.init(config);
98    filterProxy.doFilter(req, res, chain);
99    filterProxy.destroy();
100
101    ems.verifyAll();
102  }
103
104  @Test
105  public void singleFilterNoBubbling() throws Exception {
106    EasyMockSupport ems = new EasyMockSupport();
107
108    FilterConfig config = ems.createMock("config", FilterConfig.class);
109    HttpServletRequest req = new FakeHttpServletRequest();
110    HttpServletResponse res = new FakeHttpServletResponse();
111
112    FilterChain chain = ems.createMock("chain", FilterChain.class);
113
114    AllRequestFilter filter = ems.createStrictMock("filter", AllRequestFilter.class);
115    filter.init(config);
116    filter.doFilter(eq(req), eq(res), anyObject(FilterChain.class));
117    filter.destroy();
118
119    ems.replayAll();
120
121    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
122    addFilter(filter);
123
124    filterProxy.init(config);
125    filterProxy.doFilter(req, res, chain);
126    filterProxy.destroy();
127
128    ems.verifyAll();
129  }
130
131  @Test
132  public void singleFilterBubbling() throws Exception {
133    EasyMockSupport ems = new EasyMockSupport();
134
135    FilterConfig config = ems.createMock(FilterConfig.class);
136    HttpServletRequest req = new FakeHttpServletRequest();
137    HttpServletResponse res = new FakeHttpServletResponse();
138
139    IMocksControl mockControl = ems.createStrictControl();
140    FilterChain chain = mockControl.createMock(FilterChain.class);
141
142    Capture<FilterChain> capturedChain = new Capture<>();
143
144    AllRequestFilter filter = mockControl.createMock(AllRequestFilter.class);
145    filter.init(config);
146    filter.doFilter(eq(req), eq(res), capture(capturedChain));
147    chain.doFilter(req, res);
148    filter.destroy();
149
150    ems.replayAll();
151
152    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
153    addFilter(filter);
154
155    filterProxy.init(config);
156    filterProxy.doFilter(req, res, chain);
157    capturedChain.getValue().doFilter(req, res);
158    filterProxy.destroy();
159
160    ems.verifyAll();
161  }
162
163  @Test
164  public void twoFiltersNoBubbling() throws Exception {
165    EasyMockSupport ems = new EasyMockSupport();
166
167    FilterConfig config = ems.createMock(FilterConfig.class);
168    HttpServletRequest req = new FakeHttpServletRequest();
169    HttpServletResponse res = new FakeHttpServletResponse();
170
171    IMocksControl mockControl = ems.createStrictControl();
172    FilterChain chain = mockControl.createMock(FilterChain.class);
173
174    AllRequestFilter filterA = mockControl.createMock(AllRequestFilter.class);
175
176    AllRequestFilter filterB = mockControl.createMock(AllRequestFilter.class);
177    filterA.init(config);
178    filterB.init(config);
179    filterA.doFilter(eq(req), eq(res), anyObject(FilterChain.class));
180    filterA.destroy();
181    filterB.destroy();
182
183    ems.replayAll();
184
185    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
186    addFilter(filterA);
187    addFilter(filterB);
188
189    filterProxy.init(config);
190    filterProxy.doFilter(req, res, chain);
191    filterProxy.destroy();
192
193    ems.verifyAll();
194  }
195
196  @Test
197  public void twoFiltersBubbling() throws Exception {
198    EasyMockSupport ems = new EasyMockSupport();
199
200    FilterConfig config = ems.createMock(FilterConfig.class);
201    HttpServletRequest req = new FakeHttpServletRequest();
202    HttpServletResponse res = new FakeHttpServletResponse();
203
204    IMocksControl mockControl = ems.createStrictControl();
205    FilterChain chain = mockControl.createMock(FilterChain.class);
206
207    Capture<FilterChain> capturedChainA = new Capture<>();
208    Capture<FilterChain> capturedChainB = new Capture<>();
209
210    AllRequestFilter filterA = mockControl.createMock(AllRequestFilter.class);
211    AllRequestFilter filterB = mockControl.createMock(AllRequestFilter.class);
212
213    filterA.init(config);
214    filterB.init(config);
215    filterA.doFilter(eq(req), eq(res), capture(capturedChainA));
216    filterB.doFilter(eq(req), eq(res), capture(capturedChainB));
217    chain.doFilter(req, res);
218    filterA.destroy();
219    filterB.destroy();
220
221    ems.replayAll();
222
223    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
224    addFilter(filterA);
225    addFilter(filterB);
226
227    filterProxy.init(config);
228    filterProxy.doFilter(req, res, chain);
229    capturedChainA.getValue().doFilter(req, res);
230    capturedChainB.getValue().doFilter(req, res);
231    filterProxy.destroy();
232
233    ems.verifyAll();
234  }
235
236  @Test
237  public void postponedLoading() throws Exception {
238    EasyMockSupport ems = new EasyMockSupport();
239
240    FilterConfig config = ems.createMock(FilterConfig.class);
241    HttpServletRequest req1 = new FakeHttpServletRequest();
242    HttpServletRequest req2 = new FakeHttpServletRequest();
243    HttpServletResponse res1 = new FakeHttpServletResponse();
244    HttpServletResponse res2 = new FakeHttpServletResponse();
245
246    IMocksControl mockControl = ems.createStrictControl();
247    FilterChain chain = mockControl.createMock("chain", FilterChain.class);
248
249    Capture<FilterChain> capturedChainA1 = new Capture<>();
250    Capture<FilterChain> capturedChainA2 = new Capture<>();
251    Capture<FilterChain> capturedChainB = new Capture<>();
252
253    AllRequestFilter filterA = mockControl.createMock("filterA", AllRequestFilter.class);
254    AllRequestFilter filterB = mockControl.createMock("filterB", AllRequestFilter.class);
255
256    filterA.init(config);
257    filterA.doFilter(eq(req1), eq(res1), capture(capturedChainA1));
258    chain.doFilter(req1, res1);
259
260    filterA.doFilter(eq(req2), eq(res2), capture(capturedChainA2));
261    filterB.init(config); // <-- This is crucial part. filterB got loaded
262    // after filterProxy's init finished. Nonetheless filterB gets initialized.
263    filterB.doFilter(eq(req2), eq(res2), capture(capturedChainB));
264    chain.doFilter(req2, res2);
265
266    filterA.destroy();
267    filterB.destroy();
268
269    ems.replayAll();
270
271    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
272    addFilter(filterA);
273
274    filterProxy.init(config);
275    filterProxy.doFilter(req1, res1, chain);
276    capturedChainA1.getValue().doFilter(req1, res1);
277
278    addFilter(filterB); // <-- Adds filter after filterProxy's init got called.
279    filterProxy.doFilter(req2, res2, chain);
280    capturedChainA2.getValue().doFilter(req2, res2);
281    capturedChainB.getValue().doFilter(req2, res2);
282
283    filterProxy.destroy();
284
285    ems.verifyAll();
286  }
287
288  @Test
289  public void dynamicUnloading() throws Exception {
290    EasyMockSupport ems = new EasyMockSupport();
291
292    FilterConfig config = ems.createMock(FilterConfig.class);
293    HttpServletRequest req1 = new FakeHttpServletRequest();
294    HttpServletRequest req2 = new FakeHttpServletRequest();
295    HttpServletRequest req3 = new FakeHttpServletRequest();
296    HttpServletResponse res1 = new FakeHttpServletResponse();
297    HttpServletResponse res2 = new FakeHttpServletResponse();
298    HttpServletResponse res3 = new FakeHttpServletResponse();
299
300    Plugin plugin = ems.createMock(Plugin.class);
301
302    IMocksControl mockControl = ems.createStrictControl();
303    FilterChain chain = mockControl.createMock("chain", FilterChain.class);
304
305    Capture<FilterChain> capturedChainA1 = new Capture<>();
306    Capture<FilterChain> capturedChainB1 = new Capture<>();
307    Capture<FilterChain> capturedChainB2 = new Capture<>();
308
309    AllRequestFilter filterA = mockControl.createMock("filterA", AllRequestFilter.class);
310    AllRequestFilter filterB = mockControl.createMock("filterB", AllRequestFilter.class);
311
312    filterA.init(config);
313    filterB.init(config);
314
315    filterA.doFilter(eq(req1), eq(res1), capture(capturedChainA1));
316    filterB.doFilter(eq(req1), eq(res1), capture(capturedChainB1));
317    chain.doFilter(req1, res1);
318
319    filterA.destroy(); // Cleaning up of filterA after it got unloaded
320
321    filterB.doFilter(eq(req2), eq(res2), capture(capturedChainB2));
322    chain.doFilter(req2, res2);
323
324    filterB.destroy(); // Cleaning up of filterA after it got unloaded
325
326    chain.doFilter(req3, res3);
327
328    ems.replayAll();
329
330    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
331    ReloadableRegistrationHandle<AllRequestFilter> handleFilterA = addFilter(filterA);
332    ReloadableRegistrationHandle<AllRequestFilter> handleFilterB = addFilter(filterB);
333
334    filterProxy.init(config);
335
336    // Request #1 with filterA and filterB
337    filterProxy.doFilter(req1, res1, chain);
338    capturedChainA1.getValue().doFilter(req1, res1);
339    capturedChainB1.getValue().doFilter(req1, res1);
340
341    // Unloading filterA
342    handleFilterA.remove();
343    filterProxy.onStopPlugin(plugin);
344
345    // Request #1 only with filterB
346    filterProxy.doFilter(req2, res2, chain);
347    capturedChainA1.getValue().doFilter(req2, res2);
348
349    // Unloading filterB
350    handleFilterB.remove();
351    filterProxy.onStopPlugin(plugin);
352
353    // Request #1 with no additional filters
354    filterProxy.doFilter(req3, res3, chain);
355
356    filterProxy.destroy();
357
358    ems.verifyAll();
359  }
360}
361
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)