|
16 | 16 |
|
17 | 17 | package org.springframework.security.provisioning; |
18 | 18 |
|
| 19 | +import java.sql.SQLException; |
19 | 20 | import java.util.Collections; |
20 | 21 | import java.util.HashMap; |
21 | 22 | import java.util.List; |
|
28 | 29 | import org.junit.jupiter.api.Test; |
29 | 30 |
|
30 | 31 | import org.springframework.jdbc.core.JdbcTemplate; |
| 32 | +import org.springframework.jdbc.core.RowMapper; |
31 | 33 | import org.springframework.security.PopulatedDatabase; |
32 | 34 | import org.springframework.security.TestDataSource; |
33 | 35 | import org.springframework.security.access.AccessDeniedException; |
|
48 | 50 | import static org.assertj.core.api.Assertions.assertThat; |
49 | 51 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
50 | 52 | import static org.mockito.ArgumentMatchers.any; |
| 53 | +import static org.mockito.ArgumentMatchers.anyInt; |
51 | 54 | import static org.mockito.BDDMockito.given; |
52 | 55 | import static org.mockito.Mockito.mock; |
53 | 56 | import static org.mockito.Mockito.verify; |
| 57 | +import static org.mockito.Mockito.when; |
54 | 58 |
|
55 | 59 | /** |
56 | 60 | * Tests for {@link JdbcUserDetailsManager} |
57 | 61 | * |
58 | 62 | * @author Luke Taylor |
| 63 | + * @author dae won |
59 | 64 | */ |
60 | 65 | public class JdbcUserDetailsManagerTests { |
61 | 66 |
|
@@ -365,6 +370,24 @@ public void createNewAuthenticationUsesNullPasswordToKeepPassordsSave() { |
365 | 370 | assertThat(updatedAuth.getCredentials()).isNull(); |
366 | 371 | } |
367 | 372 |
|
| 373 | + @Test |
| 374 | + public void setUserDetailsMapperWithNullMapperThrowsException() { |
| 375 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 376 | + .isThrownBy(() -> this.manager.setUserDetailsMapper(null)) |
| 377 | + .withMessage("userDetailsMapper cannot be null"); |
| 378 | + } |
| 379 | + |
| 380 | + @Test |
| 381 | + public void setUserDetailsMapperWithMockMapper() throws SQLException { |
| 382 | + RowMapper<UserDetails> mockMapper = mock(RowMapper.class); |
| 383 | + when(mockMapper.mapRow(any(), anyInt())).thenReturn(joe); |
| 384 | + this.manager.setUserDetailsMapper(mockMapper); |
| 385 | + insertJoe(); |
| 386 | + UserDetails newJoe = this.manager.loadUserByUsername("joe"); |
| 387 | + assertThat(joe).isEqualTo(newJoe); |
| 388 | + verify(mockMapper).mapRow(any(), anyInt()); |
| 389 | + } |
| 390 | + |
368 | 391 | private Authentication authenticateJoe() { |
369 | 392 | UsernamePasswordAuthenticationToken auth = UsernamePasswordAuthenticationToken.authenticated("joe", "password", |
370 | 393 | joe.getAuthorities()); |
|
0 commit comments