35
35
*
36
36
* @author Ben Alex
37
37
*/
38
- @ SuppressWarnings ("unchecked" )
39
38
public class ProviderManagerTests {
40
39
41
40
@ Test (expected = ProviderNotFoundException .class )
42
- public void authenticationFailsWithUnsupportedToken () throws Exception {
41
+ public void authenticationFailsWithUnsupportedToken () {
43
42
Authentication token = new AbstractAuthenticationToken (null ) {
44
43
public Object getCredentials () {
45
44
return "" ;
@@ -55,7 +54,7 @@ public Object getPrincipal() {
55
54
}
56
55
57
56
@ Test
58
- public void credentialsAreClearedByDefault () throws Exception {
57
+ public void credentialsAreClearedByDefault () {
59
58
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken (
60
59
"Test" , "Password" );
61
60
ProviderManager mgr = makeProviderManager ();
@@ -71,8 +70,7 @@ public void credentialsAreClearedByDefault() throws Exception {
71
70
@ Test
72
71
public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject () {
73
72
final Authentication a = mock (Authentication .class );
74
- ProviderManager mgr = new ProviderManager (
75
- Arrays .asList (createProviderWhichReturns (a )));
73
+ ProviderManager mgr = new ProviderManager (createProviderWhichReturns (a ));
76
74
AuthenticationEventPublisher publisher = mock (AuthenticationEventPublisher .class );
77
75
mgr .setAuthenticationEventPublisher (publisher );
78
76
@@ -122,7 +120,7 @@ public boolean supports(Class<?> authentication) {
122
120
}
123
121
};
124
122
125
- ProviderManager authMgr = new ProviderManager (Arrays . asList ( provider ) );
123
+ ProviderManager authMgr = new ProviderManager (provider );
126
124
127
125
TestingAuthenticationToken request = createAuthenticationToken ();
128
126
request .setDetails (requestDetails );
@@ -132,8 +130,7 @@ public boolean supports(Class<?> authentication) {
132
130
}
133
131
134
132
@ Test
135
- public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider ()
136
- throws Exception {
133
+ public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider () {
137
134
Object details = new Object ();
138
135
ProviderManager authMgr = makeProviderManager ();
139
136
@@ -149,8 +146,8 @@ public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider()
149
146
public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates () {
150
147
final Authentication authReq = mock (Authentication .class );
151
148
ProviderManager mgr = new ProviderManager (
152
- Arrays . asList ( createProviderWhichThrows (new BadCredentialsException ("" ,
153
- new Throwable ())), createProviderWhichReturns (authReq ))) ;
149
+ createProviderWhichThrows (new BadCredentialsException ("" ,
150
+ new Throwable ())), createProviderWhichReturns (authReq ));
154
151
assertThat (mgr .authenticate (mock (Authentication .class ))).isSameAs (authReq );
155
152
}
156
153
@@ -185,7 +182,7 @@ public void accountStatusExceptionPreventsCallsToSubsequentProviders() {
185
182
}
186
183
catch (AccountStatusException expected ) {
187
184
}
188
- verifyZeroInteractions (otherProvider );
185
+ verifyNoInteractions (otherProvider );
189
186
}
190
187
191
188
@ Test
@@ -194,7 +191,7 @@ public void parentAuthenticationIsUsedIfProvidersDontAuthenticate() {
194
191
Authentication authReq = mock (Authentication .class );
195
192
when (parent .authenticate (authReq )).thenReturn (authReq );
196
193
ProviderManager mgr = new ProviderManager (
197
- Arrays . asList (mock (AuthenticationProvider .class )), parent );
194
+ Collections . singletonList (mock (AuthenticationProvider .class )), parent );
198
195
assertThat (mgr .authenticate (authReq )).isSameAs (authReq );
199
196
}
200
197
@@ -205,14 +202,14 @@ public void parentIsNotCalledIfAccountStatusExceptionIsThrown() {
205
202
});
206
203
AuthenticationManager parent = mock (AuthenticationManager .class );
207
204
ProviderManager mgr = new ProviderManager (
208
- Arrays . asList (iThrowAccountStatusException ), parent );
205
+ Collections . singletonList (iThrowAccountStatusException ), parent );
209
206
try {
210
207
mgr .authenticate (mock (Authentication .class ));
211
208
fail ("Expected exception" );
212
209
}
213
210
catch (AccountStatusException expected ) {
214
211
}
215
- verifyZeroInteractions (parent );
212
+ verifyNoInteractions (parent );
216
213
}
217
214
218
215
@ Test
@@ -225,7 +222,7 @@ public void providerNotFoundFromParentIsIgnored() {
225
222
// Set a provider that throws an exception - this is the exception we expect to be
226
223
// propagated
227
224
ProviderManager mgr = new ProviderManager (
228
- Arrays . asList (createProviderWhichThrows (new BadCredentialsException ("" ))),
225
+ Collections . singletonList (createProviderWhichThrows (new BadCredentialsException ("" ))),
229
226
parent );
230
227
mgr .setAuthenticationEventPublisher (publisher );
231
228
@@ -242,7 +239,7 @@ public void providerNotFoundFromParentIsIgnored() {
242
239
public void authenticationExceptionFromParentOverridesPreviousOnes () {
243
240
AuthenticationManager parent = mock (AuthenticationManager .class );
244
241
ProviderManager mgr = new ProviderManager (
245
- Arrays . asList (createProviderWhichThrows (new BadCredentialsException ("" ))),
242
+ Collections . singletonList (createProviderWhichThrows (new BadCredentialsException ("" ))),
246
243
parent );
247
244
final Authentication authReq = mock (Authentication .class );
248
245
AuthenticationEventPublisher publisher = mock (AuthenticationEventPublisher .class );
@@ -262,12 +259,11 @@ public void authenticationExceptionFromParentOverridesPreviousOnes() {
262
259
}
263
260
264
261
@ Test
265
- @ SuppressWarnings ("deprecation" )
266
262
public void statusExceptionIsPublished () {
267
263
AuthenticationManager parent = mock (AuthenticationManager .class );
268
264
final LockedException expected = new LockedException ("" );
269
265
ProviderManager mgr = new ProviderManager (
270
- Arrays . asList (createProviderWhichThrows (expected )), parent );
266
+ Collections . singletonList (createProviderWhichThrows (expected )), parent );
271
267
final Authentication authReq = mock (Authentication .class );
272
268
AuthenticationEventPublisher publisher = mock (AuthenticationEventPublisher .class );
273
269
mgr .setAuthenticationEventPublisher (publisher );
@@ -303,10 +299,9 @@ public void providerThrowsInternalAuthenticationServiceException() {
303
299
@ Test
304
300
public void authenticateWhenFailsInParentAndPublishesThenChildDoesNotPublish () {
305
301
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 ));
308
303
ProviderManager childMgr = new ProviderManager (Collections .singletonList (createProviderWhichThrows (
309
- new BadCredentialsException ("Bad Credentials in child" ))), parentMgr );
304
+ new BadCredentialsException ("Bad Credentials in child" ))), parentMgr );
310
305
311
306
AuthenticationEventPublisher publisher = mock (AuthenticationEventPublisher .class );
312
307
parentMgr .setAuthenticationEventPublisher (publisher );
@@ -348,17 +343,14 @@ private TestingAuthenticationToken createAuthenticationToken() {
348
343
}
349
344
350
345
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 );
356
348
}
357
349
358
350
// ~ Inner Classes
359
351
// ==================================================================================================
360
352
361
- private class MockProvider implements AuthenticationProvider {
353
+ private static class MockProvider implements AuthenticationProvider {
362
354
public Authentication authenticate (Authentication authentication )
363
355
throws AuthenticationException {
364
356
if (supports (authentication .getClass ())) {
@@ -372,7 +364,7 @@ public Authentication authenticate(Authentication authentication)
372
364
public boolean supports (Class <?> authentication ) {
373
365
return TestingAuthenticationToken .class .isAssignableFrom (authentication )
374
366
|| UsernamePasswordAuthenticationToken .class
375
- .isAssignableFrom (authentication );
367
+ .isAssignableFrom (authentication );
376
368
}
377
369
}
378
370
}
0 commit comments