Add SecurityContextHolder#addListener

Closes gh-10032
This commit is contained in:
Hiroshi Shirosaki
2021-06-16 16:53:13 +09:00
committed by Josh Cummings
parent b8d51725c7
commit 6f3e346b76
9 changed files with 247 additions and 8 deletions

View File

@@ -23,6 +23,10 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Tests {@link SecurityContextHolder}.
@@ -58,4 +62,17 @@ public class SecurityContextHolderTests {
assertThatIllegalArgumentException().isThrownBy(() -> SecurityContextHolder.setContext(null));
}
@Test
public void addListenerWhenInvokedThenListenersAreNotified() {
SecurityContextChangedListener one = mock(SecurityContextChangedListener.class);
SecurityContextChangedListener two = mock(SecurityContextChangedListener.class);
SecurityContextHolder.addListener(one);
SecurityContextHolder.addListener(two);
SecurityContext context = SecurityContextHolder.createEmptyContext();
SecurityContextHolder.setContext(context);
SecurityContextHolder.clearContext();
verify(one, times(2)).securityContextChanged(any(SecurityContextChangedEvent.class));
verify(two, times(2)).securityContextChanged(any(SecurityContextChangedEvent.class));
}
}