Skip to content

Commit 33df5b0

Browse files
committed
Merged changes from Example 3: Avoid duplicate creation of mocks in ConcurrentSessionFilterTests.java
1 parent ee231a5 commit 33df5b0

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

web/src/test/java/org/springframework/security/web/concurrent/ConcurrentSessionFilterTests.java

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,8 @@ public void doFilterWhenNoSessionThenChainIsContinued() throws Exception {
164164
MockHttpServletRequest request = new MockHttpServletRequest();
165165
MockHttpServletResponse response = new MockHttpServletResponse();
166166
RedirectStrategy redirect = mock(RedirectStrategy.class);
167-
SessionRegistry registry = mock(SessionRegistry.class);
168-
SessionInformation information = new SessionInformation("user", "sessionId",
169-
new Date(System.currentTimeMillis() - 1000));
170-
information.expireNow();
171-
given(registry.getSessionInformation(anyString())).willReturn(information);
172167
String expiredUrl = "/expired";
173-
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry, expiredUrl);
168+
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(mockSessionRegistry(), expiredUrl);
174169
filter.setRedirectStrategy(redirect);
175170
MockFilterChain chain = new MockFilterChain();
176171
filter.doFilter(request, response, chain);
@@ -199,13 +194,8 @@ public void doFilterWhenCustomRedirectStrategyThenCustomRedirectStrategyUsed() t
199194
request.setSession(session);
200195
MockHttpServletResponse response = new MockHttpServletResponse();
201196
RedirectStrategy redirect = mock(RedirectStrategy.class);
202-
SessionRegistry registry = mock(SessionRegistry.class);
203-
SessionInformation information = new SessionInformation("user", "sessionId",
204-
new Date(System.currentTimeMillis() - 1000));
205-
information.expireNow();
206-
given(registry.getSessionInformation(anyString())).willReturn(information);
207197
String expiredUrl = "/expired";
208-
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry, expiredUrl);
198+
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(mockSessionRegistry(), expiredUrl);
209199
filter.setRedirectStrategy(redirect);
210200
filter.doFilter(request, response, new MockFilterChain());
211201
verify(redirect).sendRedirect(request, response, expiredUrl);
@@ -218,13 +208,8 @@ public void doFilterWhenOverrideThenCustomRedirectStrategyUsed() throws Exceptio
218208
request.setSession(session);
219209
MockHttpServletResponse response = new MockHttpServletResponse();
220210
RedirectStrategy redirect = mock(RedirectStrategy.class);
221-
SessionRegistry registry = mock(SessionRegistry.class);
222-
SessionInformation information = new SessionInformation("user", "sessionId",
223-
new Date(System.currentTimeMillis() - 1000));
224-
information.expireNow();
225-
given(registry.getSessionInformation(anyString())).willReturn(information);
226211
final String expiredUrl = "/expired";
227-
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry, expiredUrl + "will-be-overrridden") {
212+
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(mockSessionRegistry(), expiredUrl + "will-be-overrridden") {
228213
@Override
229214
protected String determineExpiredUrl(HttpServletRequest request, SessionInformation info) {
230215
return expiredUrl;
@@ -241,12 +226,7 @@ public void doFilterWhenNoExpiredUrlThenResponseWritten() throws Exception {
241226
MockHttpSession session = new MockHttpSession();
242227
request.setSession(session);
243228
MockHttpServletResponse response = new MockHttpServletResponse();
244-
SessionRegistry registry = mock(SessionRegistry.class);
245-
SessionInformation information = new SessionInformation("user", "sessionId",
246-
new Date(System.currentTimeMillis() - 1000));
247-
information.expireNow();
248-
given(registry.getSessionInformation(anyString())).willReturn(information);
249-
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry);
229+
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(mockSessionRegistry());
250230
filter.doFilter(request, response, new MockFilterChain());
251231
assertThat(response.getContentAsString()).contains(
252232
"This session has been expired (possibly due to multiple concurrent logins being attempted as the same user).");
@@ -259,12 +239,7 @@ public void doFilterWhenCustomLogoutHandlersThenHandlersUsed() throws Exception
259239
MockHttpSession session = new MockHttpSession();
260240
request.setSession(session);
261241
MockHttpServletResponse response = new MockHttpServletResponse();
262-
SessionRegistry registry = mock(SessionRegistry.class);
263-
SessionInformation information = new SessionInformation("user", "sessionId",
264-
new Date(System.currentTimeMillis() - 1000));
265-
information.expireNow();
266-
given(registry.getSessionInformation(anyString())).willReturn(information);
267-
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry);
242+
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(mockSessionRegistry());
268243
filter.setLogoutHandlers(new LogoutHandler[] { handler });
269244
filter.doFilter(request, response, new MockFilterChain());
270245
verify(handler).logout(eq(request), eq(response), any());
@@ -276,12 +251,7 @@ public void doFilterWhenCustomSecurityContextHolderStrategyThenHandlersUsed() th
276251
MockHttpSession session = new MockHttpSession();
277252
request.setSession(session);
278253
MockHttpServletResponse response = new MockHttpServletResponse();
279-
SessionRegistry registry = mock(SessionRegistry.class);
280-
SessionInformation information = new SessionInformation("user", "sessionId",
281-
new Date(System.currentTimeMillis() - 1000));
282-
information.expireNow();
283-
given(registry.getSessionInformation(anyString())).willReturn(information);
284-
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(registry);
254+
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(mockSessionRegistry());
285255
SecurityContextHolderStrategy securityContextHolderStrategy = spy(
286256
new MockSecurityContextHolderStrategy(new TestingAuthenticationToken("user", "password")));
287257
filter.setSecurityContextHolderStrategy(securityContextHolderStrategy);
@@ -300,5 +270,12 @@ public void setLogoutHandlersWhenEmptyThenThrowsException() {
300270
ConcurrentSessionFilter filter = new ConcurrentSessionFilter(new SessionRegistryImpl());
301271
assertThatIllegalArgumentException().isThrownBy(() -> filter.setLogoutHandlers(new LogoutHandler[0]));
302272
}
303-
273+
private SessionRegistry mockSessionRegistry(){
274+
SessionRegistry registry = mock(SessionRegistry.class);
275+
SessionInformation information = new SessionInformation("user", "sessionId",
276+
new Date(System.currentTimeMillis() - 1000));
277+
information.expireNow();
278+
given(registry.getSessionInformation(anyString())).willReturn(information);
279+
return registry;
280+
}
304281
}

0 commit comments

Comments
 (0)