Skip to content

Commit

Permalink
Merge pull request #1453 from apache/revert-1452-assert-j-best-practices
Browse files Browse the repository at this point in the history
Revert "Refactor: AssertJ best practices by @timtebeek"
  • Loading branch information
lprimak committed May 3, 2024
2 parents bb0078f + b95f118 commit b382553
Show file tree
Hide file tree
Showing 104 changed files with 1,624 additions and 1,551 deletions.
27 changes: 13 additions & 14 deletions core/src/test/java/org/apache/shiro/SecurityUtilsUnwrapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
import static org.apache.shiro.SecurityUtils.getSecurityManager;
import static org.apache.shiro.SecurityUtils.isSecurityManagerTypeOf;
import static org.apache.shiro.SecurityUtils.unwrapSecurityManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -79,73 +78,73 @@ public <SM extends SecurityManager> SM unwrap() {
@Test
void basicUnwrap() {
SecurityManager sm = unwrapSecurityManager(securityManager, SecurityManager.class);
assertThat(sm).isEqualTo(securityManager);
assertEquals(securityManager, sm);
}

@Test
void basicTypeCheck() {
assertThat(isSecurityManagerTypeOf(securityManager, SecurityManager.class)).isTrue();
assertTrue(isSecurityManagerTypeOf(securityManager, SecurityManager.class));
}

@Test
void securityManager() {
try (var threadContext = mockStatic(ThreadContext.class)) {
threadContext.when(ThreadContext::getSecurityManager).thenReturn(defaultSecurityManager);
DefaultSecurityManager dsm = getSecurityManager(DefaultSecurityManager.class);
assertThat(dsm).isEqualTo(defaultSecurityManager);
assertEquals(defaultSecurityManager, dsm);
}
}

@Test
void failedTypeUnwrap() {
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> {
assertThrows(ClassCastException.class, () -> {
SessionsSecurityManager ssm = unwrapSecurityManager(securityManager, SessionsSecurityManager.class);
});
}

@Test
void defaultSecurityManager() {
var dsm = unwrapSecurityManager(defaultSecurityManager, DefaultSecurityManager.class);
assertThat(dsm).isEqualTo(defaultSecurityManager);
assertEquals(defaultSecurityManager, dsm);
when(defaultSecurityManager.createSubject(subjectContext)).thenReturn(subject);
Subject subject = dsm.createSubject(subjectContext);
assertThat(subject).isEqualTo(this.subject);
assertEquals(this.subject, subject);
verify(defaultSecurityManager).createSubject(subjectContext);
verifyNoMoreInteractions(defaultSecurityManager, this.subject, subjectContext);
}

@Test
void invalidCast() {
SecurityManager wrapped = new Wrapped(defaultSecurityManager);
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> {
assertThrows(ClassCastException.class, () -> {
DefaultSecurityManager sm = (DefaultSecurityManager) wrapped;
});
}

@Test
void unwrapOne() {
SecurityManager wrapped = new Wrapped(defaultSecurityManager);
assertThat(unwrapSecurityManager(wrapped, DefaultSecurityManager.class)).isEqualTo(defaultSecurityManager);
assertEquals(defaultSecurityManager, unwrapSecurityManager(wrapped, DefaultSecurityManager.class));
}

@Test
void unwrapTwo() {
SecurityManager wrapped = new Wrapped(new Wrapped(defaultSecurityManager));
assertThat(unwrapSecurityManager(wrapped, DefaultSecurityManager.class)).isEqualTo(defaultSecurityManager);
assertEquals(defaultSecurityManager, unwrapSecurityManager(wrapped, DefaultSecurityManager.class));
}

@Test
void invalidWrap() {
SecurityManager wrapped = new Wrapped(new InvalidWrapped(defaultSecurityManager));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> {
assertThrows(IllegalStateException.class, () -> {
assertEquals(defaultSecurityManager, unwrapSecurityManager(wrapped, DefaultSecurityManager.class));
});
}

@Test
void invalidWrapInverted() {
SecurityManager wrapped = new InvalidWrapped(new Wrapped(defaultSecurityManager));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> {
assertThrows(IllegalStateException.class, () -> {
assertEquals(defaultSecurityManager, unwrapSecurityManager(wrapped, DefaultSecurityManager.class));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
*/
package org.apache.shiro.aop;

import static org.assertj.core.api.Assertions.assertThat;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.lang.reflect.Method;

Expand Down Expand Up @@ -52,7 +53,7 @@ void testAnnotationFoundFromClass() throws SecurityException, NoSuchMethodExcept
expect(methodInvocation.getMethod()).andReturn(method);
expect(methodInvocation.getThis()).andReturn(myFixture);
replay(methodInvocation);
assertThat(annotationResolver.getAnnotation(methodInvocation, RequiresRoles.class)).isNotNull();
assertNotNull(annotationResolver.getAnnotation(methodInvocation, RequiresRoles.class));
}

@Test
Expand All @@ -61,7 +62,7 @@ void testAnnotationFoundFromMethod() throws SecurityException, NoSuchMethodExcep
Method method = MyFixture.class.getDeclaredMethod("operateThat");
expect(methodInvocation.getMethod()).andReturn(method);
replay(methodInvocation);
assertThat(annotationResolver.getAnnotation(methodInvocation, RequiresUser.class)).isNotNull();
assertNotNull(annotationResolver.getAnnotation(methodInvocation, RequiresUser.class));
}

@Test
Expand All @@ -71,7 +72,7 @@ void testNullMethodInvocation() throws SecurityException, NoSuchMethodException
expect(methodInvocation.getMethod()).andReturn(method);
expect(methodInvocation.getThis()).andReturn(null);
replay(methodInvocation);
assertThat(annotationResolver.getAnnotation(methodInvocation, RequiresUser.class)).isNull();
assertNull(annotationResolver.getAnnotation(methodInvocation, RequiresUser.class));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@

import java.net.URI;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @since 0.1
Expand Down Expand Up @@ -96,7 +98,7 @@ protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws Au
*/
@Test
void authenticateWithNullArgument() {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
assertThrows(IllegalArgumentException.class, () -> {
abstractAuthenticator.authenticate(null);
});
}
Expand All @@ -107,7 +109,7 @@ void authenticateWithNullArgument() {
*/
@Test
void throwAuthenticationExceptionIfDoAuthenticateReturnsNull() {
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> {
assertThrows(AuthenticationException.class, () -> {
abstractAuthenticator = createAuthcReturnNull();
abstractAuthenticator.authenticate(newToken());
});
Expand All @@ -121,7 +123,7 @@ void throwAuthenticationExceptionIfDoAuthenticateReturnsNull() {
@Test
void nonNullAuthenticationInfoAfterAuthenticate() {
AuthenticationInfo authcInfo = abstractAuthenticator.authenticate(newToken());
assertThat(authcInfo).isNotNull();
assertNotNull(authcInfo);
}

@Test
Expand Down Expand Up @@ -158,7 +160,7 @@ protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws Au
abstractAuthenticator.authenticate(token);
} catch (AuthenticationException e) {
exceptionThrown = true;
assertThat(ae).isEqualTo(e);
assertEquals(e, ae);
}
verify(mockListener);

Expand All @@ -169,7 +171,7 @@ protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws Au

@Test
void notifyFailureAfterDoAuthenticateThrowsNonAuthenticationException() {
assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> {
assertThrows(AuthenticationException.class, () -> {
abstractAuthenticator = new AbstractAuthenticator() {
protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws AuthenticationException {
throw new IllegalArgumentException("not an AuthenticationException subclass");
Expand Down Expand Up @@ -200,8 +202,8 @@ protected AuthenticationInfo doAuthenticate(AuthenticationToken token) throws Au
}

String logMsg = String.join("\n", listAppender.getMessages());
assertThat(logMsg).contains("WARN");
assertThat(logMsg).contains("java.lang.IllegalArgumentException: " + expectedExceptionMessage);
assertTrue(logMsg.contains("WARN"));
assertTrue(logMsg.contains("java.lang.IllegalArgumentException: " + expectedExceptionMessage));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import java.util.Set;

import org.apache.shiro.subject.PrincipalCollection;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;


/**
* @since 0.9
Expand Down Expand Up @@ -106,7 +106,7 @@ public Iterator iterator() {
aggregate.setPrincipals(principalCollection);
SimpleAuthenticationInfo local = new SimpleAuthenticationInfo("username", "password", "testRealm");
aggregate.merge(local);
assertThat(aggregate.getPrincipals().asList()).hasSize(2);
assertEquals(2, aggregate.getPrincipals().asList().size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

import org.apache.shiro.authc.AuthenticationInfo;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.lang.util.ClassUtils;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @since Jun 10, 2008 4:47:09 PM
*/
Expand All @@ -44,6 +44,6 @@ void testBasic() {
byte[] hashed = hash("password").getBytes();
AuthenticationInfo account = new SimpleAuthenticationInfo("username", hashed, "realmName");
AuthenticationToken token = new UsernamePasswordToken("username", "password");
assertThat(matcher.doCredentialsMatch(token, account)).isTrue();
assertTrue(matcher.doCredentialsMatch(token, account));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;


/**
Expand All @@ -30,7 +30,7 @@ public class AllowAllCredentialsMatcherTest {

@Test
void testBasic() {
assertThat(new AllowAllCredentialsMatcher().doCredentialsMatch(null, null)).isTrue();
assertTrue(new AllowAllCredentialsMatcher().doCredentialsMatch(null, null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for the {@link org.apache.shiro.authc.credential.HashedCredentialsMatcher} class.
Expand All @@ -54,7 +54,7 @@ void testSaltedAuthenticationInfo() {
AuthenticationToken token = new UsernamePasswordToken("username", "password");

//verify the hashed token matches what is in the account:
assertThat(matcher.doCredentialsMatch(token, account)).isTrue();
assertTrue(matcher.doCredentialsMatch(token, account));
}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ public Object getCredentials() {
AuthenticationToken token = new UsernamePasswordToken("username", "password");

//verify the hashed token matches what is in the account:
assertThat(matcher.doCredentialsMatch(token, account)).isTrue();
assertTrue(matcher.doCredentialsMatch(token, account));
}

/**
Expand Down Expand Up @@ -124,6 +124,6 @@ public Object getCredentials() {
AuthenticationToken token = new UsernamePasswordToken("username", "password");

//verify the hashed token matches what is in the account:
assertThat(matcher.doCredentialsMatch(token, account)).isTrue();
assertTrue(matcher.doCredentialsMatch(token, account));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.junit.jupiter.api.Test;
import org.apache.shiro.authc.AuthenticationException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
Expand All @@ -46,7 +46,7 @@ public void setUp() {
@Test
void beforeAllAttempts() {
AuthenticationInfo info = strategy.beforeAllAttempts(null, null);
assertThat(info).isNotNull();
assertNotNull(info);
}

@Test
Expand All @@ -56,7 +56,7 @@ void beforeAttemptSupportingToken() {

@Test
void beforeAttemptRealmDoesntSupportToken() {
assertThatExceptionOfType(UnsupportedTokenException.class).isThrownBy(() -> {
assertThrows(UnsupportedTokenException.class, () -> {
Realm notSupportingRealm = new AuthorizingRealm() {

public boolean supports(AuthenticationToken token) {
Expand Down

0 comments on commit b382553

Please sign in to comment.