Skip to content

Commit ace89e1

Browse files
ThomasVitalejzheaux
authored andcommitted
Make code cleaner in ProviderManagerTests
1 parent 5ce6002 commit ace89e1

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

core/src/test/java/org/springframework/security/authentication/ProviderManagerTests.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@
3535
*
3636
* @author Ben Alex
3737
*/
38-
@SuppressWarnings("unchecked")
3938
public class ProviderManagerTests {
4039

4140
@Test(expected = ProviderNotFoundException.class)
42-
public void authenticationFailsWithUnsupportedToken() throws Exception {
41+
public void authenticationFailsWithUnsupportedToken() {
4342
Authentication token = new AbstractAuthenticationToken(null) {
4443
public Object getCredentials() {
4544
return "";
@@ -55,7 +54,7 @@ public Object getPrincipal() {
5554
}
5655

5756
@Test
58-
public void credentialsAreClearedByDefault() throws Exception {
57+
public void credentialsAreClearedByDefault() {
5958
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
6059
"Test", "Password");
6160
ProviderManager mgr = makeProviderManager();
@@ -71,8 +70,7 @@ public void credentialsAreClearedByDefault() throws Exception {
7170
@Test
7271
public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() {
7372
final Authentication a = mock(Authentication.class);
74-
ProviderManager mgr = new ProviderManager(
75-
Arrays.asList(createProviderWhichReturns(a)));
73+
ProviderManager mgr = new ProviderManager(createProviderWhichReturns(a));
7674
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
7775
mgr.setAuthenticationEventPublisher(publisher);
7876

@@ -122,7 +120,7 @@ public boolean supports(Class<?> authentication) {
122120
}
123121
};
124122

125-
ProviderManager authMgr = new ProviderManager(Arrays.asList(provider));
123+
ProviderManager authMgr = new ProviderManager(provider);
126124

127125
TestingAuthenticationToken request = createAuthenticationToken();
128126
request.setDetails(requestDetails);
@@ -132,8 +130,7 @@ public boolean supports(Class<?> authentication) {
132130
}
133131

134132
@Test
135-
public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider()
136-
throws Exception {
133+
public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() {
137134
Object details = new Object();
138135
ProviderManager authMgr = makeProviderManager();
139136

@@ -149,8 +146,8 @@ public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider()
149146
public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() {
150147
final Authentication authReq = mock(Authentication.class);
151148
ProviderManager mgr = new ProviderManager(
152-
Arrays.asList(createProviderWhichThrows(new BadCredentialsException("",
153-
new Throwable())), createProviderWhichReturns(authReq)));
149+
createProviderWhichThrows(new BadCredentialsException("",
150+
new Throwable())), createProviderWhichReturns(authReq));
154151
assertThat(mgr.authenticate(mock(Authentication.class))).isSameAs(authReq);
155152
}
156153

@@ -185,7 +182,7 @@ public void accountStatusExceptionPreventsCallsToSubsequentProviders() {
185182
}
186183
catch (AccountStatusException expected) {
187184
}
188-
verifyZeroInteractions(otherProvider);
185+
verifyNoInteractions(otherProvider);
189186
}
190187

191188
@Test
@@ -194,7 +191,7 @@ public void parentAuthenticationIsUsedIfProvidersDontAuthenticate() {
194191
Authentication authReq = mock(Authentication.class);
195192
when(parent.authenticate(authReq)).thenReturn(authReq);
196193
ProviderManager mgr = new ProviderManager(
197-
Arrays.asList(mock(AuthenticationProvider.class)), parent);
194+
Collections.singletonList(mock(AuthenticationProvider.class)), parent);
198195
assertThat(mgr.authenticate(authReq)).isSameAs(authReq);
199196
}
200197

@@ -205,14 +202,14 @@ public void parentIsNotCalledIfAccountStatusExceptionIsThrown() {
205202
});
206203
AuthenticationManager parent = mock(AuthenticationManager.class);
207204
ProviderManager mgr = new ProviderManager(
208-
Arrays.asList(iThrowAccountStatusException), parent);
205+
Collections.singletonList(iThrowAccountStatusException), parent);
209206
try {
210207
mgr.authenticate(mock(Authentication.class));
211208
fail("Expected exception");
212209
}
213210
catch (AccountStatusException expected) {
214211
}
215-
verifyZeroInteractions(parent);
212+
verifyNoInteractions(parent);
216213
}
217214

218215
@Test
@@ -225,7 +222,7 @@ public void providerNotFoundFromParentIsIgnored() {
225222
// Set a provider that throws an exception - this is the exception we expect to be
226223
// propagated
227224
ProviderManager mgr = new ProviderManager(
228-
Arrays.asList(createProviderWhichThrows(new BadCredentialsException(""))),
225+
Collections.singletonList(createProviderWhichThrows(new BadCredentialsException(""))),
229226
parent);
230227
mgr.setAuthenticationEventPublisher(publisher);
231228

@@ -242,7 +239,7 @@ public void providerNotFoundFromParentIsIgnored() {
242239
public void authenticationExceptionFromParentOverridesPreviousOnes() {
243240
AuthenticationManager parent = mock(AuthenticationManager.class);
244241
ProviderManager mgr = new ProviderManager(
245-
Arrays.asList(createProviderWhichThrows(new BadCredentialsException(""))),
242+
Collections.singletonList(createProviderWhichThrows(new BadCredentialsException(""))),
246243
parent);
247244
final Authentication authReq = mock(Authentication.class);
248245
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
@@ -262,12 +259,11 @@ public void authenticationExceptionFromParentOverridesPreviousOnes() {
262259
}
263260

264261
@Test
265-
@SuppressWarnings("deprecation")
266262
public void statusExceptionIsPublished() {
267263
AuthenticationManager parent = mock(AuthenticationManager.class);
268264
final LockedException expected = new LockedException("");
269265
ProviderManager mgr = new ProviderManager(
270-
Arrays.asList(createProviderWhichThrows(expected)), parent);
266+
Collections.singletonList(createProviderWhichThrows(expected)), parent);
271267
final Authentication authReq = mock(Authentication.class);
272268
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
273269
mgr.setAuthenticationEventPublisher(publisher);
@@ -303,10 +299,9 @@ public void providerThrowsInternalAuthenticationServiceException() {
303299
@Test
304300
public void authenticateWhenFailsInParentAndPublishesThenChildDoesNotPublish() {
305301
BadCredentialsException badCredentialsExParent = new BadCredentialsException("Bad Credentials in parent");
306-
ProviderManager parentMgr = new ProviderManager(
307-
Collections.singletonList(createProviderWhichThrows(badCredentialsExParent)));
302+
ProviderManager parentMgr = new ProviderManager(createProviderWhichThrows(badCredentialsExParent));
308303
ProviderManager childMgr = new ProviderManager(Collections.singletonList(createProviderWhichThrows(
309-
new BadCredentialsException("Bad Credentials in child"))), parentMgr);
304+
new BadCredentialsException("Bad Credentials in child"))), parentMgr);
310305

311306
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
312307
parentMgr.setAuthenticationEventPublisher(publisher);
@@ -348,17 +343,14 @@ private TestingAuthenticationToken createAuthenticationToken() {
348343
}
349344

350345
private ProviderManager makeProviderManager() {
351-
MockProvider provider1 = new MockProvider();
352-
List<AuthenticationProvider> providers = new ArrayList<>();
353-
providers.add(provider1);
354-
355-
return new ProviderManager(providers);
346+
MockProvider provider = new MockProvider();
347+
return new ProviderManager(provider);
356348
}
357349

358350
// ~ Inner Classes
359351
// ==================================================================================================
360352

361-
private class MockProvider implements AuthenticationProvider {
353+
private static class MockProvider implements AuthenticationProvider {
362354
public Authentication authenticate(Authentication authentication)
363355
throws AuthenticationException {
364356
if (supports(authentication.getClass())) {
@@ -372,7 +364,7 @@ public Authentication authenticate(Authentication authentication)
372364
public boolean supports(Class<?> authentication) {
373365
return TestingAuthenticationToken.class.isAssignableFrom(authentication)
374366
|| UsernamePasswordAuthenticationToken.class
375-
.isAssignableFrom(authentication);
367+
.isAssignableFrom(authentication);
376368
}
377369
}
378370
}

0 commit comments

Comments
 (0)