Skip to content

Commit 7f22a34

Browse files
committed
Polish Tests
Issue gh-16444
1 parent 10ed500 commit 7f22a34

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

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

+36-39
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21-
import java.util.Collections;
2221
import java.util.List;
2322

2423
import org.junit.jupiter.api.Test;
@@ -47,7 +46,7 @@
4746
public class ProviderManagerTests {
4847

4948
@Test
50-
public void authenticationFailsWithUnsupportedToken() {
49+
void authenticationFailsWithUnsupportedToken() {
5150
Authentication token = new AbstractAuthenticationToken(null) {
5251
@Override
5352
public Object getCredentials() {
@@ -65,7 +64,7 @@ public Object getPrincipal() {
6564
}
6665

6766
@Test
68-
public void credentialsAreClearedByDefault() {
67+
void credentialsAreClearedByDefault() {
6968
UsernamePasswordAuthenticationToken token = UsernamePasswordAuthenticationToken.unauthenticated("Test",
7069
"Password");
7170
ProviderManager mgr = makeProviderManager();
@@ -78,8 +77,8 @@ public void credentialsAreClearedByDefault() {
7877
}
7978

8079
@Test
81-
public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() {
82-
final Authentication a = mock(Authentication.class);
80+
void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() {
81+
Authentication a = mock(Authentication.class);
8382
ProviderManager mgr = new ProviderManager(createProviderWhichReturns(a));
8483
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
8584
mgr.setAuthenticationEventPublisher(publisher);
@@ -89,8 +88,8 @@ public void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject() {
8988
}
9089

9190
@Test
92-
public void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates() {
93-
final Authentication a = mock(Authentication.class);
91+
void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates() {
92+
Authentication a = mock(Authentication.class);
9493
ProviderManager mgr = new ProviderManager(
9594
Arrays.asList(createProviderWhichReturns(null), createProviderWhichReturns(a)));
9695
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
@@ -101,24 +100,24 @@ public void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthentic
101100
}
102101

103102
@Test
104-
public void testStartupFailsIfProvidersNotSetAsList() {
103+
void testStartupFailsIfProvidersNotSetAsList() {
105104
assertThatIllegalArgumentException().isThrownBy(() -> new ProviderManager((List<AuthenticationProvider>) null));
106105
}
107106

108107
@Test
109-
public void testStartupFailsIfProvidersNotSetAsVarargs() {
108+
void testStartupFailsIfProvidersNotSetAsVarargs() {
110109
assertThatIllegalArgumentException().isThrownBy(() -> new ProviderManager((AuthenticationProvider) null));
111110
}
112111

113112
@Test
114-
public void testStartupFailsIfProvidersContainNullElement() {
113+
void testStartupFailsIfProvidersContainNullElement() {
115114
assertThatIllegalArgumentException()
116115
.isThrownBy(() -> new ProviderManager(Arrays.asList(mock(AuthenticationProvider.class), null)));
117116
}
118117

119118
// gh-8689
120119
@Test
121-
public void constructorWhenUsingListOfThenNoException() {
120+
void constructorWhenUsingListOfThenNoException() {
122121
List<AuthenticationProvider> providers = spy(ArrayList.class);
123122
// List.of(null) in JDK 9 throws a NullPointerException
124123
given(providers.contains(eq(null))).willThrow(NullPointerException.class);
@@ -127,7 +126,7 @@ public void constructorWhenUsingListOfThenNoException() {
127126
}
128127

129128
@Test
130-
public void detailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() {
129+
void detailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() {
131130
Object requestDetails = "(Request Details)";
132131
final Object resultDetails = "(Result Details)";
133132
// A provider which sets the details object
@@ -151,7 +150,7 @@ public boolean supports(Class<?> authentication) {
151150
}
152151

153152
@Test
154-
public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() {
153+
void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() {
155154
Object details = new Object();
156155
ProviderManager authMgr = makeProviderManager();
157156
TestingAuthenticationToken request = createAuthenticationToken();
@@ -162,16 +161,16 @@ public void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() {
162161
}
163162

164163
@Test
165-
public void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() {
166-
final Authentication authReq = mock(Authentication.class);
164+
void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() {
165+
Authentication authReq = mock(Authentication.class);
167166
ProviderManager mgr = new ProviderManager(
168167
createProviderWhichThrows(new BadCredentialsException("", new Throwable())),
169168
createProviderWhichReturns(authReq));
170169
assertThat(mgr.authenticate(mock(Authentication.class))).isSameAs(authReq);
171170
}
172171

173172
@Test
174-
public void authenticationExceptionIsRethrownIfNoLaterProviderAuthenticates() {
173+
void authenticationExceptionIsRethrownIfNoLaterProviderAuthenticates() {
175174
ProviderManager mgr = new ProviderManager(Arrays
176175
.asList(createProviderWhichThrows(new BadCredentialsException("")), createProviderWhichReturns(null)));
177176
assertThatExceptionOfType(BadCredentialsException.class)
@@ -180,7 +179,7 @@ public void authenticationExceptionIsRethrownIfNoLaterProviderAuthenticates() {
180179

181180
// SEC-546
182181
@Test
183-
public void accountStatusExceptionPreventsCallsToSubsequentProviders() {
182+
void accountStatusExceptionPreventsCallsToSubsequentProviders() {
184183
AuthenticationProvider iThrowAccountStatusException = createProviderWhichThrows(new AccountStatusException("") {
185184
});
186185
AuthenticationProvider otherProvider = mock(AuthenticationProvider.class);
@@ -191,48 +190,47 @@ public void accountStatusExceptionPreventsCallsToSubsequentProviders() {
191190
}
192191

193192
@Test
194-
public void parentAuthenticationIsUsedIfProvidersDontAuthenticate() {
193+
void parentAuthenticationIsUsedIfProvidersDontAuthenticate() {
195194
AuthenticationManager parent = mock(AuthenticationManager.class);
196195
Authentication authReq = mock(Authentication.class);
197196
given(parent.authenticate(authReq)).willReturn(authReq);
198-
ProviderManager mgr = new ProviderManager(Collections.singletonList(mock(AuthenticationProvider.class)),
199-
parent);
197+
ProviderManager mgr = new ProviderManager(List.of(mock(AuthenticationProvider.class)), parent);
200198
assertThat(mgr.authenticate(authReq)).isSameAs(authReq);
201199
}
202200

203201
@Test
204-
public void parentIsNotCalledIfAccountStatusExceptionIsThrown() {
202+
void parentIsNotCalledIfAccountStatusExceptionIsThrown() {
205203
AuthenticationProvider iThrowAccountStatusException = createProviderWhichThrows(
206204
new AccountStatusException("", new Throwable()) {
207205
});
208206
AuthenticationManager parent = mock(AuthenticationManager.class);
209-
ProviderManager mgr = new ProviderManager(Collections.singletonList(iThrowAccountStatusException), parent);
207+
ProviderManager mgr = new ProviderManager(List.of(iThrowAccountStatusException), parent);
210208
assertThatExceptionOfType(AccountStatusException.class)
211209
.isThrownBy(() -> mgr.authenticate(mock(Authentication.class)));
212210
verifyNoInteractions(parent);
213211
}
214212

215213
@Test
216-
public void providerNotFoundFromParentIsIgnored() {
214+
void providerNotFoundFromParentIsIgnored() {
217215
final Authentication authReq = mock(Authentication.class);
218216
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
219217
AuthenticationManager parent = mock(AuthenticationManager.class);
220218
given(parent.authenticate(authReq)).willThrow(new ProviderNotFoundException(""));
221219
// Set a provider that throws an exception - this is the exception we expect to be
222220
// propagated
223-
ProviderManager mgr = new ProviderManager(
224-
Collections.singletonList(createProviderWhichThrows(new BadCredentialsException(""))), parent);
221+
ProviderManager mgr = new ProviderManager(List.of(createProviderWhichThrows(new BadCredentialsException(""))),
222+
parent);
225223
mgr.setAuthenticationEventPublisher(publisher);
226224
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> mgr.authenticate(authReq))
227225
.satisfies((ex) -> verify(publisher).publishAuthenticationFailure(ex, authReq));
228226
}
229227

230228
@Test
231-
public void authenticationExceptionFromParentOverridesPreviousOnes() {
229+
void authenticationExceptionFromParentOverridesPreviousOnes() {
232230
AuthenticationManager parent = mock(AuthenticationManager.class);
233-
ProviderManager mgr = new ProviderManager(
234-
Collections.singletonList(createProviderWhichThrows(new BadCredentialsException(""))), parent);
235-
final Authentication authReq = mock(Authentication.class);
231+
ProviderManager mgr = new ProviderManager(List.of(createProviderWhichThrows(new BadCredentialsException(""))),
232+
parent);
233+
Authentication authReq = mock(Authentication.class);
236234
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
237235
mgr.setAuthenticationEventPublisher(publisher);
238236
// Set a provider that throws an exception - this is the exception we expect to be
@@ -244,12 +242,11 @@ public void authenticationExceptionFromParentOverridesPreviousOnes() {
244242
}
245243

246244
@Test
247-
public void statusExceptionIsPublished() {
245+
void statusExceptionIsPublished() {
248246
AuthenticationManager parent = mock(AuthenticationManager.class);
249-
final LockedException expected = new LockedException("");
250-
ProviderManager mgr = new ProviderManager(Collections.singletonList(createProviderWhichThrows(expected)),
251-
parent);
252-
final Authentication authReq = mock(Authentication.class);
247+
LockedException expected = new LockedException("");
248+
ProviderManager mgr = new ProviderManager(List.of(createProviderWhichThrows(expected)), parent);
249+
Authentication authReq = mock(Authentication.class);
253250
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
254251
mgr.setAuthenticationEventPublisher(publisher);
255252
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> mgr.authenticate(authReq));
@@ -258,7 +255,7 @@ public void statusExceptionIsPublished() {
258255

259256
// SEC-2367
260257
@Test
261-
public void providerThrowsInternalAuthenticationServiceException() {
258+
void providerThrowsInternalAuthenticationServiceException() {
262259
InternalAuthenticationServiceException expected = new InternalAuthenticationServiceException("Expected");
263260
ProviderManager mgr = new ProviderManager(Arrays.asList(createProviderWhichThrows(expected),
264261
createProviderWhichThrows(new BadCredentialsException("Oops"))), null);
@@ -269,15 +266,15 @@ public void providerThrowsInternalAuthenticationServiceException() {
269266

270267
// gh-6281
271268
@Test
272-
public void authenticateWhenFailsInParentAndPublishesThenChildDoesNotPublish() {
269+
void authenticateWhenFailsInParentAndPublishesThenChildDoesNotPublish() {
273270
BadCredentialsException badCredentialsExParent = new BadCredentialsException("Bad Credentials in parent");
274271
ProviderManager parentMgr = new ProviderManager(createProviderWhichThrows(badCredentialsExParent));
275-
ProviderManager childMgr = new ProviderManager(Collections.singletonList(
276-
createProviderWhichThrows(new BadCredentialsException("Bad Credentials in child"))), parentMgr);
272+
ProviderManager childMgr = new ProviderManager(
273+
List.of(createProviderWhichThrows(new BadCredentialsException("Bad Credentials in child"))), parentMgr);
277274
AuthenticationEventPublisher publisher = mock(AuthenticationEventPublisher.class);
278275
parentMgr.setAuthenticationEventPublisher(publisher);
279276
childMgr.setAuthenticationEventPublisher(publisher);
280-
final Authentication authReq = mock(Authentication.class);
277+
Authentication authReq = mock(Authentication.class);
281278
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> childMgr.authenticate(authReq))
282279
.isSameAs(badCredentialsExParent);
283280
verify(publisher).publishAuthenticationFailure(badCredentialsExParent, authReq); // Parent

0 commit comments

Comments
 (0)