Replace expected @Test attributes with AssertJ

Replace JUnit expected @Test attributes with AssertJ calls.
This commit is contained in:
Phillip Webb
2020-09-10 21:33:16 -07:00
committed by Josh Cummings
parent 20baa7d409
commit c502312719
243 changed files with 2115 additions and 1591 deletions

View File

@@ -22,6 +22,8 @@ import org.springframework.security.access.event.AuthenticationCredentialsNotFou
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.util.SimpleMethodInvocation;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link AuthenticationCredentialsNotFoundEvent}.
*
@@ -29,22 +31,24 @@ import org.springframework.security.util.SimpleMethodInvocation;
*/
public class AuthenticationCredentialsNotFoundEventTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testRejectsNulls() {
new AuthenticationCredentialsNotFoundEvent(null, SecurityConfig.createList("TEST"),
new AuthenticationCredentialsNotFoundException("test"));
assertThatIllegalArgumentException().isThrownBy(() -> new AuthenticationCredentialsNotFoundEvent(null,
SecurityConfig.createList("TEST"), new AuthenticationCredentialsNotFoundException("test")));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testRejectsNulls2() {
new AuthenticationCredentialsNotFoundEvent(new SimpleMethodInvocation(), null,
new AuthenticationCredentialsNotFoundException("test"));
assertThatIllegalArgumentException()
.isThrownBy(() -> new AuthenticationCredentialsNotFoundEvent(new SimpleMethodInvocation(), null,
new AuthenticationCredentialsNotFoundException("test")));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testRejectsNulls3() {
new AuthenticationCredentialsNotFoundEvent(new SimpleMethodInvocation(), SecurityConfig.createList("TEST"),
null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new AuthenticationCredentialsNotFoundEvent(new SimpleMethodInvocation(),
SecurityConfig.createList("TEST"), null));
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.util.SimpleMethodInvocation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link AuthorizationFailureEvent}.
@@ -39,24 +40,29 @@ public class AuthorizationFailureEventTests {
private AccessDeniedException exception = new AuthorizationServiceException("error", new Throwable());
@Test(expected = IllegalArgumentException.class)
@Test
public void rejectsNullSecureObject() {
new AuthorizationFailureEvent(null, this.attributes, this.foo, this.exception);
assertThatIllegalArgumentException()
.isThrownBy(() -> new AuthorizationFailureEvent(null, this.attributes, this.foo, this.exception));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void rejectsNullAttributesList() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), null, this.foo, this.exception);
assertThatIllegalArgumentException().isThrownBy(
() -> new AuthorizationFailureEvent(new SimpleMethodInvocation(), null, this.foo, this.exception));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void rejectsNullAuthentication() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), this.attributes, null, this.exception);
assertThatIllegalArgumentException()
.isThrownBy(() -> new AuthorizationFailureEvent(new SimpleMethodInvocation(), this.attributes, null,
this.exception));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void rejectsNullException() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), this.attributes, this.foo, null);
assertThatIllegalArgumentException().isThrownBy(
() -> new AuthorizationFailureEvent(new SimpleMethodInvocation(), this.attributes, this.foo, null));
}
@Test

View File

@@ -22,6 +22,8 @@ import org.springframework.security.access.event.AuthorizedEvent;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.util.SimpleMethodInvocation;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link AuthorizedEvent}.
*
@@ -29,20 +31,22 @@ import org.springframework.security.util.SimpleMethodInvocation;
*/
public class AuthorizedEventTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testRejectsNulls() {
new AuthorizedEvent(null, SecurityConfig.createList("TEST"),
new UsernamePasswordAuthenticationToken("foo", "bar"));
assertThatIllegalArgumentException().isThrownBy(() -> new AuthorizedEvent(null,
SecurityConfig.createList("TEST"), new UsernamePasswordAuthenticationToken("foo", "bar")));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testRejectsNulls2() {
new AuthorizedEvent(new SimpleMethodInvocation(), null, new UsernamePasswordAuthenticationToken("foo", "bar"));
assertThatIllegalArgumentException().isThrownBy(() -> new AuthorizedEvent(new SimpleMethodInvocation(), null,
new UsernamePasswordAuthenticationToken("foo", "bar")));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testRejectsNulls3() {
new AuthorizedEvent(new SimpleMethodInvocation(), SecurityConfig.createList("TEST"), null);
assertThatIllegalArgumentException().isThrownBy(
() -> new AuthorizedEvent(new SimpleMethodInvocation(), SecurityConfig.createList("TEST"), null));
}
}

View File

@@ -19,6 +19,8 @@ package org.springframework.security.access;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link SecurityConfig}.
@@ -33,19 +35,20 @@ public class SecurityConfigTests {
assertThat(config.hashCode()).isEqualTo("TEST".hashCode());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCannotConstructWithNullAttribute() {
new SecurityConfig(null); // SEC-727
assertThatIllegalArgumentException().isThrownBy(() -> new SecurityConfig(null)); // SEC-727
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testCannotConstructWithEmptyAttribute() {
new SecurityConfig(""); // SEC-727
assertThatIllegalArgumentException().isThrownBy(() -> new SecurityConfig("")); // SEC-727
}
@Test(expected = NoSuchMethodException.class)
@Test
public void testNoArgConstructorDoesntExist() throws Exception {
SecurityConfig.class.getDeclaredConstructor((Class[]) null);
assertThatExceptionOfType(NoSuchMethodException.class)
.isThrownBy(() -> SecurityConfig.class.getDeclaredConstructor((Class[]) null));
}
@Test

View File

@@ -27,6 +27,7 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.security.core.Authentication;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
/**
@@ -57,9 +58,9 @@ public class AbstractSecurityExpressionHandlerTests {
.isEqualTo(true);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setExpressionParserNull() {
this.handler.setExpressionParser(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.handler.setExpressionParser(null));
}
@Test

View File

@@ -37,6 +37,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
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.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
@@ -69,9 +70,9 @@ public class DefaultMethodSecurityExpressionHandlerTests {
SecurityContextHolder.clearContext();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setTrustResolverNull() {
this.handler.setTrustResolver(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.handler.setTrustResolver(null));
}
@Test

View File

@@ -30,6 +30,7 @@ import org.springframework.security.access.prepost.PreInvocationAttribute;
import org.springframework.security.core.Authentication;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link ExpressionBasedPreInvocationAdvice}
@@ -50,21 +51,23 @@ public class ExpressionBasedPreInvocationAdviceTests {
this.expressionBasedPreInvocationAdvice = new ExpressionBasedPreInvocationAdvice();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findFilterTargetNameProvidedButNotMatch() throws Exception {
PreInvocationAttribute attribute = new PreInvocationExpressionAttribute("true", "filterTargetDoesNotMatch",
null);
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class,
"doSomethingCollection", new Class[] { List.class }, new Object[] { new ArrayList<>() });
this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute);
assertThatIllegalArgumentException().isThrownBy(
() -> this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findFilterTargetNameProvidedArrayUnsupported() throws Exception {
PreInvocationAttribute attribute = new PreInvocationExpressionAttribute("true", "param", null);
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class,
"doSomethingArray", new Class[] { String[].class }, new Object[] { new String[0] });
this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute);
assertThatIllegalArgumentException().isThrownBy(
() -> this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute));
}
@Test
@@ -77,12 +80,13 @@ public class ExpressionBasedPreInvocationAdviceTests {
assertThat(result).isTrue();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findFilterTargetNameNotProvidedArrayUnsupported() throws Exception {
PreInvocationAttribute attribute = new PreInvocationExpressionAttribute("true", "", null);
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class,
"doSomethingArray", new Class[] { String[].class }, new Object[] { new String[0] });
this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute);
assertThatIllegalArgumentException().isThrownBy(
() -> this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute));
}
@Test
@@ -95,21 +99,23 @@ public class ExpressionBasedPreInvocationAdviceTests {
assertThat(result).isTrue();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findFilterTargetNameNotProvidedTypeNotSupported() throws Exception {
PreInvocationAttribute attribute = new PreInvocationExpressionAttribute("true", "", null);
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class,
"doSomethingString", new Class[] { String.class }, new Object[] { "param" });
this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute);
assertThatIllegalArgumentException().isThrownBy(
() -> this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void findFilterTargetNameNotProvidedMethodAcceptMoreThenOneArgument() throws Exception {
PreInvocationAttribute attribute = new PreInvocationExpressionAttribute("true", "", null);
MockMethodInvocation methodInvocation = new MockMethodInvocation(new TestClass(), TestClass.class,
"doSomethingTwoArgs", new Class[] { String.class, List.class },
new Object[] { "param", new ArrayList<>() });
this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute);
assertThatIllegalArgumentException().isThrownBy(
() -> this.expressionBasedPreInvocationAdvice.before(this.authentication, methodInvocation, attribute));
}
private class TestClass {

View File

@@ -32,6 +32,7 @@ import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.util.SimpleMethodInvocation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@SuppressWarnings("unchecked")
public class MethodExpressionVoterTests {
@@ -86,28 +87,28 @@ public class MethodExpressionVoterTests {
assertThat(arg).containsExactly("joe", "sam");
}
@Test(expected = IllegalArgumentException.class)
@Test
public void arraysCannotBePrefiltered() throws Exception {
MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingAnArray(),
createArrayArg("sam", "joe"));
this.am.vote(this.joe, mi,
createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'jim')", "someArray", null)));
assertThatIllegalArgumentException().isThrownBy(() -> this.am.vote(this.joe, mi,
createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'jim')", "someArray", null))));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void incorrectFilterTargetNameIsRejected() throws Exception {
MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingACollection(),
createCollectionArg("joe", "bob"));
this.am.vote(this.joe, mi,
createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'joe')", "collcetion", null)));
assertThatIllegalArgumentException().isThrownBy(() -> this.am.vote(this.joe, mi,
createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'joe')", "collcetion", null))));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void nullNamedFilterTargetIsRejected() throws Exception {
MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingACollection(),
new Object[] { null });
this.am.vote(this.joe, mi,
createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'joe')", "collection", null)));
assertThatIllegalArgumentException().isThrownBy(() -> this.am.vote(this.joe, mi,
createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'joe')", "collection", null))));
}
@Test

View File

@@ -26,6 +26,7 @@ import java.util.TreeMap;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link RoleHierarchyUtils}.
@@ -52,42 +53,47 @@ public class RoleHierarchyUtilsTests {
assertThat(roleHierarchy).isEqualTo(expectedRoleHierarchy);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void roleHierarchyFromMapWhenMapNullThenThrowsIllegalArgumentException() {
RoleHierarchyUtils.roleHierarchyFromMap(null);
assertThatIllegalArgumentException().isThrownBy(() -> RoleHierarchyUtils.roleHierarchyFromMap(null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void roleHierarchyFromMapWhenMapEmptyThenThrowsIllegalArgumentException() {
RoleHierarchyUtils.roleHierarchyFromMap(Collections.<String, List<String>>emptyMap());
assertThatIllegalArgumentException().isThrownBy(
() -> RoleHierarchyUtils.roleHierarchyFromMap(Collections.<String, List<String>>emptyMap()));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void roleHierarchyFromMapWhenRoleNullThenThrowsIllegalArgumentException() {
Map<String, List<String>> roleHierarchyMap = new HashMap<>();
roleHierarchyMap.put(null, Arrays.asList("ROLE_B", "ROLE_C"));
RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);
assertThatIllegalArgumentException()
.isThrownBy(() -> RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void roleHierarchyFromMapWhenRoleEmptyThenThrowsIllegalArgumentException() {
Map<String, List<String>> roleHierarchyMap = new HashMap<>();
roleHierarchyMap.put("", Arrays.asList("ROLE_B", "ROLE_C"));
RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);
assertThatIllegalArgumentException()
.isThrownBy(() -> RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void roleHierarchyFromMapWhenImpliedRolesNullThenThrowsIllegalArgumentException() {
Map<String, List<String>> roleHierarchyMap = new HashMap<>();
roleHierarchyMap.put("ROLE_A", null);
RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);
assertThatIllegalArgumentException()
.isThrownBy(() -> RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void roleHierarchyFromMapWhenImpliedRolesEmptyThenThrowsIllegalArgumentException() {
Map<String, List<String>> roleHierarchyMap = new HashMap<>();
roleHierarchyMap.put("ROLE_A", Collections.<String>emptyList());
RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap);
assertThatIllegalArgumentException()
.isThrownBy(() -> RoleHierarchyUtils.roleHierarchyFromMap(roleHierarchyMap));
}
}

View File

@@ -23,6 +23,7 @@ import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.util.SimpleMethodInvocation;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
/**
@@ -33,7 +34,7 @@ import static org.mockito.Mockito.mock;
*/
public class AbstractSecurityInterceptorTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void detectsIfInvocationPassedIncompatibleSecureObject() {
MockSecurityInterceptorWhichOnlySupportsStrings si = new MockSecurityInterceptorWhichOnlySupportsStrings();
si.setRunAsManager(mock(RunAsManager.class));
@@ -41,10 +42,10 @@ public class AbstractSecurityInterceptorTests {
si.setAfterInvocationManager(mock(AfterInvocationManager.class));
si.setAccessDecisionManager(mock(AccessDecisionManager.class));
si.setSecurityMetadataSource(mock(SecurityMetadataSource.class));
si.beforeInvocation(new SimpleMethodInvocation());
assertThatIllegalArgumentException().isThrownBy(() -> si.beforeInvocation(new SimpleMethodInvocation()));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void detectsViolationOfGetSecureObjectClassMethod() throws Exception {
MockSecurityInterceptorReturnsNull si = new MockSecurityInterceptorReturnsNull();
si.setRunAsManager(mock(RunAsManager.class));
@@ -52,7 +53,7 @@ public class AbstractSecurityInterceptorTests {
si.setAfterInvocationManager(mock(AfterInvocationManager.class));
si.setAccessDecisionManager(mock(AccessDecisionManager.class));
si.setSecurityMetadataSource(mock(SecurityMetadataSource.class));
si.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(si::afterPropertiesSet);
}
private class MockSecurityInterceptorReturnsNull extends AbstractSecurityInterceptor {

View File

@@ -26,19 +26,21 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link RunAsImplAuthenticationProvider}.
*/
public class RunAsImplAuthenticationProviderTests {
@Test(expected = BadCredentialsException.class)
@Test
public void testAuthenticationFailDueToWrongKey() {
RunAsUserToken token = new RunAsUserToken("wrong_key", "Test", "Password",
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"), UsernamePasswordAuthenticationToken.class);
RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
provider.setKey("hello_world");
provider.authenticate(token);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
}
@Test
@@ -53,10 +55,10 @@ public class RunAsImplAuthenticationProviderTests {
assertThat(resultCast.getKeyHash()).isEqualTo("my_password".hashCode());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testStartupFailsIfNoKey() throws Exception {
RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
provider.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(provider::afterPropertiesSet);
}
@Test

View File

@@ -48,6 +48,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -124,63 +125,63 @@ public class MethodSecurityInterceptorTests {
assertThat(this.interceptor.getAfterInvocationManager()).isEqualTo(aim);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void missingAccessDecisionManagerIsDetected() throws Exception {
this.interceptor.setAccessDecisionManager(null);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void missingAuthenticationManagerIsDetected() throws Exception {
this.interceptor.setAuthenticationManager(null);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void missingMethodSecurityMetadataSourceIsRejected() throws Exception {
this.interceptor.setSecurityMetadataSource(null);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void missingRunAsManagerIsRejected() throws Exception {
this.interceptor.setRunAsManager(null);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void initializationRejectsSecurityMetadataSourceThatDoesNotSupportMethodInvocation() throws Throwable {
given(this.mds.supports(MethodInvocation.class)).willReturn(false);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void initializationRejectsAccessDecisionManagerThatDoesNotSupportMethodInvocation() throws Exception {
given(this.mds.supports(MethodInvocation.class)).willReturn(true);
given(this.adm.supports(MethodInvocation.class)).willReturn(false);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void intitalizationRejectsRunAsManagerThatDoesNotSupportMethodInvocation() throws Exception {
final RunAsManager ram = mock(RunAsManager.class);
given(ram.supports(MethodInvocation.class)).willReturn(false);
this.interceptor.setRunAsManager(ram);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void intitalizationRejectsAfterInvocationManagerThatDoesNotSupportMethodInvocation() throws Exception {
final AfterInvocationManager aim = mock(AfterInvocationManager.class);
given(aim.supports(MethodInvocation.class)).willReturn(false);
this.interceptor.setAfterInvocationManager(aim);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void initializationFailsIfAccessDecisionManagerRejectsConfigAttributes() throws Exception {
given(this.adm.supports(any(ConfigAttribute.class))).willReturn(false);
this.interceptor.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.afterPropertiesSet());
}
@Test
@@ -219,13 +220,14 @@ public class MethodSecurityInterceptorTests {
assertThat(!this.token.isAuthenticated()).isTrue();
}
@Test(expected = AuthenticationException.class)
@Test
public void callIsntMadeWhenAuthenticationManagerRejectsAuthentication() {
final TestingAuthenticationToken token = new TestingAuthenticationToken("Test", "Password");
SecurityContextHolder.getContext().setAuthentication(token);
mdsReturnsUserRole();
given(this.authman.authenticate(token)).willThrow(new BadCredentialsException("rejected"));
this.advisedTarget.makeLowerCase("HELLO");
assertThatExceptionOfType(AuthenticationException.class)
.isThrownBy(() -> this.advisedTarget.makeLowerCase("HELLO"));
}
@Test
@@ -256,9 +258,9 @@ public class MethodSecurityInterceptorTests {
verify(this.eventPublisher).publishEvent(any(AuthorizationFailureEvent.class));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void rejectsNullSecuredObjects() throws Throwable {
this.interceptor.invoke(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.interceptor.invoke(null));
}
@Test
@@ -299,10 +301,11 @@ public class MethodSecurityInterceptorTests {
assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.token);
}
@Test(expected = AuthenticationCredentialsNotFoundException.class)
@Test
public void emptySecurityContextIsRejected() {
mdsReturnsUserRole();
this.advisedTarget.makeUpperCase("hello");
assertThatExceptionOfType(AuthenticationCredentialsNotFoundException.class)
.isThrownBy(() -> this.advisedTarget.makeUpperCase("hello"));
}
@Test

View File

@@ -30,6 +30,7 @@ import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -89,19 +90,21 @@ public class AffirmativeBasedTests {
this.mgr.decide(this.user, new Object(), this.attrs);
}
@Test(expected = AccessDeniedException.class)
@Test
public void oneDenyVoteTwoAbstainVotesDeniesAccess() {
this.mgr = new AffirmativeBased(
Arrays.<AccessDecisionVoter<? extends Object>>asList(this.deny, this.abstain, this.abstain));
this.mgr.decide(this.user, new Object(), this.attrs);
assertThatExceptionOfType(AccessDeniedException.class)
.isThrownBy(() -> this.mgr.decide(this.user, new Object(), this.attrs));
}
@Test(expected = AccessDeniedException.class)
@Test
public void onlyAbstainVotesDeniesAccessWithDefault() {
this.mgr = new AffirmativeBased(
Arrays.<AccessDecisionVoter<? extends Object>>asList(this.abstain, this.abstain, this.abstain));
assertThat(!this.mgr.isAllowIfAllAbstainDecisions()).isTrue(); // check default
this.mgr.decide(this.user, new Object(), this.attrs);
assertThatExceptionOfType(AccessDeniedException.class)
.isThrownBy(() -> this.mgr.decide(this.user, new Object(), this.attrs));
}
@Test

View File

@@ -28,7 +28,7 @@ import org.springframework.security.access.SecurityConfig;
import org.springframework.security.authentication.TestingAuthenticationToken;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests {@link ConsensusBased}.
@@ -37,14 +37,14 @@ import static org.assertj.core.api.Assertions.fail;
*/
public class ConsensusBasedTests {
@Test(expected = AccessDeniedException.class)
@Test
public void testOneAffirmativeVoteOneDenyVoteOneAbstainVoteDeniesAccessWithoutDefault() {
TestingAuthenticationToken auth = makeTestToken();
ConsensusBased mgr = makeDecisionManager();
mgr.setAllowIfEqualGrantedDeniedDecisions(false);
assertThat(!mgr.isAllowIfEqualGrantedDeniedDecisions()).isTrue(); // check changed
List<ConfigAttribute> config = SecurityConfig.createList("ROLE_1", "DENY_FOR_SURE");
mgr.decide(auth, new Object(), config);
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> mgr.decide(auth, new Object(), config));
}
@Test
@@ -63,20 +63,21 @@ public class ConsensusBasedTests {
mgr.decide(auth, new Object(), SecurityConfig.createList("ROLE_2"));
}
@Test(expected = AccessDeniedException.class)
@Test
public void testOneDenyVoteTwoAbstainVotesDeniesAccess() {
TestingAuthenticationToken auth = makeTestToken();
ConsensusBased mgr = makeDecisionManager();
mgr.decide(auth, new Object(), SecurityConfig.createList("ROLE_WE_DO_NOT_HAVE"));
fail("Should have thrown AccessDeniedException");
assertThatExceptionOfType(AccessDeniedException.class)
.isThrownBy(() -> mgr.decide(auth, new Object(), SecurityConfig.createList("ROLE_WE_DO_NOT_HAVE")));
}
@Test(expected = AccessDeniedException.class)
@Test
public void testThreeAbstainVotesDeniesAccessWithDefault() {
TestingAuthenticationToken auth = makeTestToken();
ConsensusBased mgr = makeDecisionManager();
assertThat(!mgr.isAllowIfAllAbstainDecisions()).isTrue(); // check default
mgr.decide(auth, new Object(), SecurityConfig.createList("IGNORED_BY_ALL"));
assertThatExceptionOfType(AccessDeniedException.class)
.isThrownBy(() -> mgr.decide(auth, new Object(), SecurityConfig.createList("IGNORED_BY_ALL")));
}
@Test

View File

@@ -27,6 +27,7 @@ import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@@ -46,12 +47,13 @@ public class AbstractAuthenticationTokenTests {
this.authorities = AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO");
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void testAuthoritiesAreImmutable() {
MockAuthenticationImpl token = new MockAuthenticationImpl("Test", "Password", this.authorities);
List<GrantedAuthority> gotAuthorities = (List<GrantedAuthority>) token.getAuthorities();
assertThat(gotAuthorities).isNotSameAs(this.authorities);
gotAuthorities.set(0, new SimpleGrantedAuthority("ROLE_SUPER_USER"));
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> gotAuthorities.set(0, new SimpleGrantedAuthority("ROLE_SUPER_USER")));
}
@Test

View File

@@ -36,6 +36,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
@@ -111,12 +113,13 @@ public class DefaultAuthenticationEventPublisherTests {
verify(appPublisher).publishEvent(isA(AuthenticationFailureDisabledEvent.class));
}
@Test(expected = RuntimeException.class)
@Test
public void missingEventClassExceptionCausesException() {
this.publisher = new DefaultAuthenticationEventPublisher();
Properties p = new Properties();
p.put(MockAuthenticationException.class.getName(), "NoSuchClass");
this.publisher.setAdditionalExceptionMappings(p);
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> this.publisher.setAdditionalExceptionMappings(p));
}
@Test
@@ -132,27 +135,27 @@ public class DefaultAuthenticationEventPublisherTests {
verifyZeroInteractions(appPublisher);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void emptyMapCausesException() {
Map<Class<? extends AuthenticationException>, Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>();
this.publisher = new DefaultAuthenticationEventPublisher();
this.publisher.setAdditionalExceptionMappings(mappings);
assertThatIllegalArgumentException().isThrownBy(() -> this.publisher.setAdditionalExceptionMappings(mappings));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void missingExceptionClassCausesException() {
Map<Class<? extends AuthenticationException>, Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>();
mappings.put(null, AuthenticationFailureLockedEvent.class);
this.publisher = new DefaultAuthenticationEventPublisher();
this.publisher.setAdditionalExceptionMappings(mappings);
assertThatIllegalArgumentException().isThrownBy(() -> this.publisher.setAdditionalExceptionMappings(mappings));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void missingEventClassAsMapValueCausesException() {
Map<Class<? extends AuthenticationException>, Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>();
mappings.put(LockedException.class, null);
this.publisher = new DefaultAuthenticationEventPublisher();
this.publisher.setAdditionalExceptionMappings(mappings);
assertThatIllegalArgumentException().isThrownBy(() -> this.publisher.setAdditionalExceptionMappings(mappings));
}
@Test
@@ -168,10 +171,11 @@ public class DefaultAuthenticationEventPublisherTests {
verify(appPublisher).publishEvent(isA(AuthenticationFailureDisabledEvent.class));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void defaultAuthenticationFailureEventClassSetNullThen() {
this.publisher = new DefaultAuthenticationEventPublisher();
this.publisher.setDefaultAuthenticationFailureEvent(null);
assertThatIllegalArgumentException()
.isThrownBy(() -> this.publisher.setDefaultAuthenticationFailureEvent(null));
}
@Test
@@ -185,11 +189,11 @@ public class DefaultAuthenticationEventPublisherTests {
verify(appPublisher).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
}
@Test(expected = RuntimeException.class)
@Test
public void defaultAuthenticationFailureEventMissingAppropriateConstructorThen() {
this.publisher = new DefaultAuthenticationEventPublisher();
this.publisher
.setDefaultAuthenticationFailureEvent(AuthenticationFailureEventWithoutAppropriateConstructor.class);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> this.publisher
.setDefaultAuthenticationFailureEvent(AuthenticationFailureEventWithoutAppropriateConstructor.class));
}
private static final class AuthenticationFailureEventWithoutAppropriateConstructor

View File

@@ -29,6 +29,7 @@ import org.springframework.security.core.AuthenticationException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -45,7 +46,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*/
public class ProviderManagerTests {
@Test(expected = ProviderNotFoundException.class)
@Test
public void authenticationFailsWithUnsupportedToken() {
Authentication token = new AbstractAuthenticationToken(null) {
@Override
@@ -60,7 +61,7 @@ public class ProviderManagerTests {
};
ProviderManager mgr = makeProviderManager();
mgr.setMessageSource(mock(MessageSource.class));
mgr.authenticate(token);
assertThatExceptionOfType(ProviderNotFoundException.class).isThrownBy(() -> mgr.authenticate(token));
}
@Test
@@ -98,19 +99,20 @@ public class ProviderManagerTests {
verify(publisher).publishAuthenticationSuccess(result);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testStartupFailsIfProvidersNotSetAsList() {
new ProviderManager((List<AuthenticationProvider>) null);
assertThatIllegalArgumentException().isThrownBy(() -> new ProviderManager((List<AuthenticationProvider>) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testStartupFailsIfProvidersNotSetAsVarargs() {
new ProviderManager((AuthenticationProvider) null);
assertThatIllegalArgumentException().isThrownBy(() -> new ProviderManager((AuthenticationProvider) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testStartupFailsIfProvidersContainNullElement() {
new ProviderManager(Arrays.asList(mock(AuthenticationProvider.class), null));
assertThatIllegalArgumentException()
.isThrownBy(() -> new ProviderManager(Arrays.asList(mock(AuthenticationProvider.class), null)));
}
// gh-8689

View File

@@ -27,6 +27,7 @@ import reactor.test.StepVerifier;
import org.springframework.security.core.Authentication;
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.BDDMockito.given;
@@ -50,14 +51,14 @@ public class ReactiveAuthenticationManagerAdapterTests {
this.manager = new ReactiveAuthenticationManagerAdapter(this.delegate);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullAuthenticationManager() {
new ReactiveAuthenticationManagerAdapter(null);
assertThatIllegalArgumentException().isThrownBy(() -> new ReactiveAuthenticationManagerAdapter(null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setSchedulerNull() {
this.manager.setScheduler(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.manager.setScheduler(null));
}
@Test

View File

@@ -33,6 +33,7 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
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.BDDMockito.given;
@@ -62,10 +63,10 @@ public class ReactiveUserDetailsServiceAuthenticationManagerTests {
this.password = "pass";
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullUserDetailsService() {
ReactiveUserDetailsService userDetailsService = null;
new UserDetailsRepositoryReactiveAuthenticationManager(userDetailsService);
assertThatIllegalArgumentException()
.isThrownBy(() -> new UserDetailsRepositoryReactiveAuthenticationManager(null));
}
@Test

View File

@@ -165,7 +165,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
verifyZeroInteractions(this.postAuthenticationChecks);
}
@Test(expected = AccountExpiredException.class)
@Test
public void authenticateWhenAccountExpiredThenException() {
this.manager.setPasswordEncoder(this.encoder);
// @formatter:off
@@ -178,10 +178,11 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
given(this.userDetailsService.findByUsername(any())).willReturn(Mono.just(expiredUser));
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(expiredUser,
expiredUser.getPassword());
this.manager.authenticate(token).block();
assertThatExceptionOfType(AccountExpiredException.class)
.isThrownBy(() -> this.manager.authenticate(token).block());
}
@Test(expected = LockedException.class)
@Test
public void authenticateWhenAccountLockedThenException() {
this.manager.setPasswordEncoder(this.encoder);
// @formatter:off
@@ -194,10 +195,10 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
given(this.userDetailsService.findByUsername(any())).willReturn(Mono.just(lockedUser));
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(lockedUser,
lockedUser.getPassword());
this.manager.authenticate(token).block();
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> this.manager.authenticate(token).block());
}
@Test(expected = DisabledException.class)
@Test
public void authenticateWhenAccountDisabledThenException() {
this.manager.setPasswordEncoder(this.encoder);
// @formatter:off
@@ -210,7 +211,7 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
given(this.userDetailsService.findByUsername(any())).willReturn(Mono.just(disabledUser));
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(disabledUser,
disabledUser.getPassword());
this.manager.authenticate(token).block();
assertThatExceptionOfType(DisabledException.class).isThrownBy(() -> this.manager.authenticate(token).block());
}
}

View File

@@ -21,6 +21,7 @@ import org.junit.Test;
import org.springframework.security.core.authority.AuthorityUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
@@ -63,10 +64,11 @@ public class UsernamePasswordAuthenticationTokenTests {
assertThat(AuthorityUtils.authorityListToSet(token.getAuthorities())).contains("ROLE_TWO");
}
@Test(expected = NoSuchMethodException.class)
@Test
public void testNoArgConstructorDoesntExist() throws Exception {
Class<?> clazz = UsernamePasswordAuthenticationToken.class;
clazz.getDeclaredConstructor((Class[]) null);
assertThatExceptionOfType(NoSuchMethodException.class)
.isThrownBy(() -> clazz.getDeclaredConstructor((Class[]) null));
}
}

View File

@@ -101,19 +101,21 @@ public class AnonymousAuthenticationTokenTests {
assertThat(!token.isAuthenticated()).isTrue();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorWhenNullAuthoritiesThenThrowIllegalArgumentException() {
new AnonymousAuthenticationToken("key", "principal", null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new AnonymousAuthenticationToken("key", "principal", null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorWhenEmptyAuthoritiesThenThrowIllegalArgumentException() {
new AnonymousAuthenticationToken("key", "principal", Collections.<GrantedAuthority>emptyList());
assertThatIllegalArgumentException().isThrownBy(
() -> new AnonymousAuthenticationToken("key", "principal", Collections.<GrantedAuthority>emptyList()));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorWhenPrincipalIsEmptyStringThenThrowIllegalArgumentException() {
new AnonymousAuthenticationToken("key", "", ROLES_12);
assertThatIllegalArgumentException().isThrownBy(() -> new AnonymousAuthenticationToken("key", "", ROLES_12));
}
}

View File

@@ -45,6 +45,7 @@ import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
@@ -82,16 +83,16 @@ public class DefaultJaasAuthenticationProviderTests {
ReflectionTestUtils.setField(this.provider, "log", this.log);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void afterPropertiesSetNullConfiguration() throws Exception {
this.provider.setConfiguration(null);
this.provider.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(this.provider::afterPropertiesSet);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void afterPropertiesSetNullAuthorityGranters() throws Exception {
this.provider.setAuthorityGranters(null);
this.provider.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(this.provider::afterPropertiesSet);
}
@Test

View File

@@ -29,6 +29,7 @@ import org.junit.Test;
import org.springframework.security.authentication.jaas.TestLoginModule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link InMemoryConfiguration}.
@@ -55,9 +56,10 @@ public class InMemoryConfigurationTests {
assertThat(new InMemoryConfiguration((AppConfigurationEntry[]) null).getAppConfigurationEntry("name")).isNull();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullMapped() {
new InMemoryConfiguration((Map<String, AppConfigurationEntry[]>) null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new InMemoryConfiguration((Map<String, AppConfigurationEntry[]>) null));
}
@Test
@@ -72,9 +74,9 @@ public class InMemoryConfigurationTests {
.getAppConfigurationEntry("name")).isNull();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullMapNullDefault() {
new InMemoryConfiguration(null, null);
assertThatIllegalArgumentException().isThrownBy(() -> new InMemoryConfiguration(null, null));
}
@Test

View File

@@ -23,6 +23,7 @@ import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
@@ -35,13 +36,14 @@ import static org.mockito.Mockito.mock;
*/
public class RemoteAuthenticationManagerImplTests {
@Test(expected = RemoteAuthenticationException.class)
@Test
public void testFailedAuthenticationReturnsRemoteAuthenticationException() {
RemoteAuthenticationManagerImpl manager = new RemoteAuthenticationManagerImpl();
AuthenticationManager am = mock(AuthenticationManager.class);
given(am.authenticate(any(Authentication.class))).willThrow(new BadCredentialsException(""));
manager.setAuthenticationManager(am);
manager.attemptAuthentication("rod", "password");
assertThatExceptionOfType(RemoteAuthenticationException.class)
.isThrownBy(() -> manager.attemptAuthentication("rod", "password"));
}
@Test

View File

@@ -29,6 +29,7 @@ import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
/**
@@ -120,42 +121,40 @@ public class AuthorityReactiveAuthorizationManagerTests {
assertThat(granted).isFalse();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void hasRoleWhenNullThenException() {
String role = null;
AuthorityReactiveAuthorizationManager.hasRole(role);
assertThatIllegalArgumentException()
.isThrownBy(() -> AuthorityReactiveAuthorizationManager.hasRole((String) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void hasAuthorityWhenNullThenException() {
String authority = null;
AuthorityReactiveAuthorizationManager.hasAuthority(authority);
assertThatIllegalArgumentException()
.isThrownBy(() -> AuthorityReactiveAuthorizationManager.hasAuthority((String) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void hasAnyRoleWhenNullThenException() {
String role = null;
AuthorityReactiveAuthorizationManager.hasAnyRole(role);
assertThatIllegalArgumentException()
.isThrownBy(() -> AuthorityReactiveAuthorizationManager.hasAnyRole((String) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void hasAnyAuthorityWhenNullThenException() {
String authority = null;
AuthorityReactiveAuthorizationManager.hasAnyAuthority(authority);
assertThatIllegalArgumentException()
.isThrownBy(() -> AuthorityReactiveAuthorizationManager.hasAnyAuthority((String) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void hasAnyRoleWhenOneIsNullThenException() {
String role1 = "ROLE_ADMIN";
String role2 = null;
AuthorityReactiveAuthorizationManager.hasAnyRole(role1, role2);
assertThatIllegalArgumentException()
.isThrownBy(() -> AuthorityReactiveAuthorizationManager.hasAnyRole("ROLE_ADMIN", (String) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void hasAnyAuthorityWhenOneIsNullThenException() {
String authority1 = "ADMIN";
String authority2 = null;
AuthorityReactiveAuthorizationManager.hasAnyAuthority(authority1, authority2);
assertThatIllegalArgumentException()
.isThrownBy(() -> AuthorityReactiveAuthorizationManager.hasAnyAuthority("ADMIN", (String) null));
}
}

View File

@@ -27,6 +27,7 @@ import org.junit.Test;
import org.mockito.Mock;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
@@ -57,9 +58,9 @@ public abstract class AbstractDelegatingSecurityContextExecutorServiceTests
}
@Override
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullDelegate() {
new DelegatingSecurityContextExecutorService(null);
assertThatIllegalArgumentException().isThrownBy(() -> new DelegatingSecurityContextExecutorService(null));
}
@Test

View File

@@ -22,6 +22,7 @@ import java.util.concurrent.ScheduledExecutorService;
import org.junit.Test;
import org.mockito.Mock;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.verify;
/**
@@ -42,9 +43,9 @@ public abstract class AbstractDelegatingSecurityContextExecutorTests
private DelegatingSecurityContextExecutor executor;
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullDelegate() {
new DelegatingSecurityContextExecutor(null);
assertThatIllegalArgumentException().isThrownBy(() -> new DelegatingSecurityContextExecutor(null));
}
@Test

View File

@@ -34,6 +34,7 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
@@ -79,24 +80,26 @@ public class DelegatingSecurityContextCallableTests {
SecurityContextHolder.clearContext();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullDelegate() {
new DelegatingSecurityContextCallable<>(null);
assertThatIllegalArgumentException().isThrownBy(() -> new DelegatingSecurityContextCallable<>(null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullDelegateNonNullSecurityContext() {
new DelegatingSecurityContextCallable<>(null, this.securityContext);
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingSecurityContextCallable<>(null, this.securityContext));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullDelegateAndSecurityContext() {
new DelegatingSecurityContextCallable<>(null, null);
assertThatIllegalArgumentException().isThrownBy(() -> new DelegatingSecurityContextCallable<>(null, null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullSecurityContext() {
new DelegatingSecurityContextCallable<>(this.delegate, null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingSecurityContextCallable<>(this.delegate, null));
}
@Test
@@ -109,8 +112,8 @@ public class DelegatingSecurityContextCallableTests {
public void callDefaultSecurityContext() throws Exception {
SecurityContextHolder.setContext(this.securityContext);
this.callable = new DelegatingSecurityContextCallable<>(this.delegate);
SecurityContextHolder.clearContext(); // ensure callable is what sets up the
// SecurityContextHolder
// ensure callable is what sets up the SecurityContextHolder
SecurityContextHolder.clearContext();
assertWrapped(this.callable);
}
@@ -123,22 +126,23 @@ public class DelegatingSecurityContextCallableTests {
assertWrapped(this.callable.call());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void createNullDelegate() {
DelegatingSecurityContextCallable.create(null, this.securityContext);
assertThatIllegalArgumentException()
.isThrownBy(() -> DelegatingSecurityContextCallable.create(null, this.securityContext));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void createNullDelegateAndSecurityContext() {
DelegatingSecurityContextRunnable.create(null, null);
assertThatIllegalArgumentException().isThrownBy(() -> DelegatingSecurityContextRunnable.create(null, null));
}
@Test
public void createNullSecurityContext() throws Exception {
SecurityContextHolder.setContext(this.securityContext);
this.callable = DelegatingSecurityContextCallable.create(this.delegate, null);
SecurityContextHolder.clearContext(); // ensure callable is what sets up the
// SecurityContextHolder
// ensure callable is what sets up the SecurityContextHolder
SecurityContextHolder.clearContext();
assertWrapped(this.callable);
}

View File

@@ -34,6 +34,7 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.verify;
@@ -74,24 +75,26 @@ public class DelegatingSecurityContextRunnableTests {
SecurityContextHolder.clearContext();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullDelegate() {
new DelegatingSecurityContextRunnable(null);
assertThatIllegalArgumentException().isThrownBy(() -> new DelegatingSecurityContextRunnable(null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullDelegateNonNullSecurityContext() {
new DelegatingSecurityContextRunnable(null, this.securityContext);
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingSecurityContextRunnable(null, this.securityContext));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullDelegateAndSecurityContext() {
new DelegatingSecurityContextRunnable(null, null);
assertThatIllegalArgumentException().isThrownBy(() -> new DelegatingSecurityContextRunnable(null, null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullSecurityContext() {
new DelegatingSecurityContextRunnable(this.delegate, null);
assertThatIllegalArgumentException()
.isThrownBy(() -> new DelegatingSecurityContextRunnable(this.delegate, null));
}
@Test
@@ -119,14 +122,15 @@ public class DelegatingSecurityContextRunnableTests {
assertWrapped(this.runnable);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void createNullDelegate() {
DelegatingSecurityContextRunnable.create(null, this.securityContext);
assertThatIllegalArgumentException()
.isThrownBy(() -> DelegatingSecurityContextRunnable.create(null, this.securityContext));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void createNullDelegateAndSecurityContext() {
DelegatingSecurityContextRunnable.create(null, null);
assertThatIllegalArgumentException().isThrownBy(() -> DelegatingSecurityContextRunnable.create(null, null));
}
@Test

View File

@@ -25,6 +25,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.never;
@@ -75,9 +76,9 @@ public class DelegatingApplicationListenerTests {
verify(this.delegate, never()).onApplicationEvent(any(ApplicationEvent.class));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void addNull() {
this.listener.addListener(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.listener.addListener(null));
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Ruud Senden
@@ -35,44 +36,41 @@ import static org.assertj.core.api.Assertions.assertThat;
@SuppressWarnings("unchecked")
public class MapBasedAttributes2GrantedAuthoritiesMapperTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testAfterPropertiesSetNoMap() throws Exception {
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper();
mapper.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(mapper::afterPropertiesSet);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testAfterPropertiesSetEmptyMap() throws Exception {
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper();
mapper.setAttributes2grantedAuthoritiesMap(new HashMap());
mapper.afterPropertiesSet();
assertThatIllegalArgumentException()
.isThrownBy(() -> mapper.setAttributes2grantedAuthoritiesMap(new HashMap()));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testAfterPropertiesSetInvalidKeyTypeMap() throws Exception {
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper();
HashMap m = new HashMap();
m.put(new Object(), "ga1");
mapper.setAttributes2grantedAuthoritiesMap(m);
mapper.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> mapper.setAttributes2grantedAuthoritiesMap(m));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testAfterPropertiesSetInvalidValueTypeMap1() throws Exception {
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper();
HashMap m = new HashMap();
m.put("role1", new Object());
mapper.setAttributes2grantedAuthoritiesMap(m);
mapper.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> mapper.setAttributes2grantedAuthoritiesMap(m));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testAfterPropertiesSetInvalidValueTypeMap2() throws Exception {
MapBasedAttributes2GrantedAuthoritiesMapper mapper = new MapBasedAttributes2GrantedAuthoritiesMapper();
HashMap m = new HashMap();
m.put("role1", new Object[] { new String[] { "ga1", "ga2" }, new Object() });
mapper.setAttributes2grantedAuthoritiesMap(m);
mapper.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> mapper.setAttributes2grantedAuthoritiesMap(m));
}
@Test

View File

@@ -25,18 +25,19 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Luke Taylor
*/
public class SimpleAuthoritiesMapperTests {
@Test(expected = IllegalArgumentException.class)
public void rejectsInvalidCaseConversionFlags() throws Exception {
@Test
public void rejectsInvalidCaseConversionFlags() {
SimpleAuthorityMapper mapper = new SimpleAuthorityMapper();
mapper.setConvertToLowerCase(true);
mapper.setConvertToUpperCase(true);
mapper.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(mapper::afterPropertiesSet);
}
@Test

View File

@@ -21,6 +21,7 @@ import java.util.Date;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link DefaultToken}.
@@ -40,11 +41,11 @@ public class DefaultTokenTests {
assertThat(t2).isEqualTo(t1);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testRejectsNullExtendedInformation() {
String key = "key";
long created = new Date().getTime();
new DefaultToken(key, created, null);
assertThatIllegalArgumentException().isThrownBy(() -> new DefaultToken(key, created, null));
}
@Test

View File

@@ -22,12 +22,12 @@ import java.util.Date;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link KeyBasedPersistenceTokenService}.
*
* @author Ben Alex
*
*/
public class KeyBasedPersistenceTokenServiceTests {
@@ -80,20 +80,22 @@ public class KeyBasedPersistenceTokenServiceTests {
assertThat(result).isEqualTo(token);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testOperationWithMissingKey() {
KeyBasedPersistenceTokenService service = getService();
Token token = new DefaultToken("", new Date().getTime(), "");
service.verifyToken(token.getKey());
assertThatIllegalArgumentException().isThrownBy(() -> {
Token token = new DefaultToken("", new Date().getTime(), "");
service.verifyToken(token.getKey());
});
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testOperationWithTamperedKey() {
KeyBasedPersistenceTokenService service = getService();
Token goodToken = service.allocateToken("");
String fake = goodToken.getKey().toUpperCase();
Token token = new DefaultToken(fake, new Date().getTime(), "");
service.verifyToken(token.getKey());
assertThatIllegalArgumentException().isThrownBy(() -> service.verifyToken(token.getKey()));
}
}

View File

@@ -24,6 +24,7 @@ import org.junit.Test;
import reactor.core.publisher.Mono;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
public class MapReactiveUserDetailsServiceTests {
@@ -35,16 +36,16 @@ public class MapReactiveUserDetailsServiceTests {
// @formatter:on
private MapReactiveUserDetailsService users = new MapReactiveUserDetailsService(Arrays.asList(USER_DETAILS));
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorNullUsers() {
Collection<UserDetails> users = null;
new MapReactiveUserDetailsService(users);
assertThatIllegalArgumentException()
.isThrownBy(() -> new MapReactiveUserDetailsService((Collection<UserDetails>) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructorEmptyUsers() {
Collection<UserDetails> users = Collections.emptyList();
new MapReactiveUserDetailsService(users);
assertThatIllegalArgumentException()
.isThrownBy(() -> new MapReactiveUserDetailsService(Collections.emptyList()));
}
@Test

View File

@@ -27,7 +27,7 @@ import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests {@link EhCacheBasedUserCache}.
@@ -77,11 +77,10 @@ public class EhCacheBasedUserCacheTests {
assertThat(cache.getUserFromCache("UNKNOWN_USER")).isNull();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void startupDetectsMissingCache() throws Exception {
EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
cache.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
assertThatIllegalArgumentException().isThrownBy(cache::afterPropertiesSet);
Ehcache myCache = getCache();
cache.setCache(myCache);
assertThat(cache.getCache()).isEqualTo(myCache);

View File

@@ -27,6 +27,7 @@ import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests
@@ -75,9 +76,9 @@ public class SpringCacheBasedUserCacheTests {
assertThat(cache.getUserFromCache("UNKNOWN_USER")).isNull();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void startupDetectsMissingCache() throws Exception {
new SpringCacheBasedUserCache(null);
assertThatIllegalArgumentException().isThrownBy(() -> new SpringCacheBasedUserCache(null));
}
}

View File

@@ -156,10 +156,10 @@ public class JdbcDaoImplTests {
});
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setMessageSourceWhenNullThenThrowsException() {
JdbcDaoImpl dao = new JdbcDaoImpl();
dao.setMessageSource(null);
assertThatIllegalArgumentException().isThrownBy(() -> dao.setMessageSource(null));
}
@Test

View File

@@ -29,6 +29,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Jitendra Singh
@@ -64,12 +65,13 @@ public class AnonymousAuthenticationTokenMixinTests extends AbstractMixinTests {
assertThat(token.getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER"));
}
@Test(expected = JsonMappingException.class)
@Test
public void deserializeAnonymousAuthenticationTokenWithoutAuthoritiesTest() throws IOException {
String jsonString = "{\"@class\": \"org.springframework.security.authentication.AnonymousAuthenticationToken\", \"details\": null,"
+ "\"principal\": \"user\", \"authenticated\": true, \"keyHash\": " + HASH_KEY.hashCode() + ","
+ "\"authorities\": [\"java.util.ArrayList\", []]}";
this.mapper.readValue(jsonString, AnonymousAuthenticationToken.class);
assertThatExceptionOfType(JsonMappingException.class)
.isThrownBy(() -> this.mapper.readValue(jsonString, AnonymousAuthenticationToken.class));
}
@Test

View File

@@ -30,6 +30,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Jitendra Singh
@@ -48,6 +49,7 @@ public class RememberMeAuthenticationTokenMixinTests extends AbstractMixinTests
+ "\"authorities\": " + SimpleGrantedAuthorityMixinTests.AUTHORITIES_ARRAYLIST_JSON
+ "}";
// @formatter:on
// @formatter:off
private static final String REMEMBERME_AUTH_STRINGPRINCIPAL_JSON = "{"
+ "\"@class\": \"org.springframework.security.authentication.RememberMeAuthenticationToken\","
@@ -58,14 +60,17 @@ public class RememberMeAuthenticationTokenMixinTests extends AbstractMixinTests
+ "\"authorities\": " + SimpleGrantedAuthorityMixinTests.AUTHORITIES_ARRAYLIST_JSON
+ "}";
// @formatter:on
@Test(expected = IllegalArgumentException.class)
@Test
public void testWithNullPrincipal() {
new RememberMeAuthenticationToken("key", null, Collections.<GrantedAuthority>emptyList());
assertThatIllegalArgumentException().isThrownBy(
() -> new RememberMeAuthenticationToken("key", null, Collections.<GrantedAuthority>emptyList()));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testWithNullKey() {
new RememberMeAuthenticationToken(null, "principal", Collections.<GrantedAuthority>emptyList());
assertThatIllegalArgumentException().isThrownBy(
() -> new RememberMeAuthenticationToken(null, "principal", Collections.<GrantedAuthority>emptyList()));
}
@Test

View File

@@ -27,6 +27,7 @@ import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Jitendra Singh
@@ -56,10 +57,11 @@ public class SimpleGrantedAuthorityMixinTests extends AbstractMixinTests {
assertThat(authority.getAuthority()).isNotNull().isEqualTo("ROLE_USER");
}
@Test(expected = JsonMappingException.class)
@Test
public void deserializeGrantedAuthorityWithoutRoleTest() throws IOException {
String json = "{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\"}";
this.mapper.readValue(json, SimpleGrantedAuthority.class);
assertThatExceptionOfType(JsonMappingException.class)
.isThrownBy(() -> this.mapper.readValue(json, SimpleGrantedAuthority.class));
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Jitendra Singh
@@ -67,11 +68,12 @@ public class UserDeserializerTests extends AbstractMixinTests {
JSONAssert.assertEquals(userWithNoAuthoritiesJson(), userJson, true);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void deserializeUserWithNullPasswordEmptyAuthorityTest() throws IOException {
String userJsonWithoutPasswordString = USER_JSON.replace(SimpleGrantedAuthorityMixinTests.AUTHORITIES_SET_JSON,
"[]");
this.mapper.readValue(userJsonWithoutPasswordString, User.class);
assertThatIllegalArgumentException()
.isThrownBy(() -> this.mapper.readValue(userJsonWithoutPasswordString, User.class));
}
@Test
@@ -85,11 +87,11 @@ public class UserDeserializerTests extends AbstractMixinTests {
assertThat(user.isEnabled()).isEqualTo(true);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void deserializeUserWithNoClassIdInAuthoritiesTest() throws Exception {
String userJson = USER_JSON.replace(SimpleGrantedAuthorityMixinTests.AUTHORITIES_SET_JSON,
"[{\"authority\": \"ROLE_USER\"}]");
this.mapper.readValue(userJson, User.class);
assertThatIllegalArgumentException().isThrownBy(() -> this.mapper.readValue(userJson, User.class));
}
@Test

View File

@@ -189,9 +189,10 @@ public class JdbcUserDetailsManagerTests {
assertThat(this.cache.getUserMap().containsKey("joe")).isTrue();
}
@Test(expected = AccessDeniedException.class)
@Test
public void changePasswordFailsForUnauthenticatedUser() {
this.manager.changePassword("password", "newPassword");
assertThatExceptionOfType(AccessDeniedException.class)
.isThrownBy(() -> this.manager.changePassword("password", "newPassword"));
}
@Test

View File

@@ -29,6 +29,7 @@ import org.mockito.MockitoAnnotations;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
@@ -65,9 +66,9 @@ public class DelegatingSecurityContextTaskSchedulerTests {
this.delegatingSecurityContextTaskScheduler = null;
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testSchedulerIsNotNull() {
this.delegatingSecurityContextTaskScheduler = new DelegatingSecurityContextTaskScheduler(null);
assertThatIllegalArgumentException().isThrownBy(() -> new DelegatingSecurityContextTaskScheduler(null));
}
@Test

View File

@@ -25,6 +25,7 @@ import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.security.access.annotation.BusinessServiceImpl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Luke Taylor
@@ -44,9 +45,10 @@ public class MethodInvocationUtilsTests {
assertThat(mi).isNotNull();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void exceptionIsRaisedIfArgInfoOmittedAndMethodNameIsNotUnique() {
MethodInvocationUtils.createFromClass(BusinessServiceImpl.class, "methodReturningAList");
assertThatIllegalArgumentException().isThrownBy(
() -> MethodInvocationUtils.createFromClass(BusinessServiceImpl.class, "methodReturningAList"));
}
@Test