From 1818385e6fb05570a680fb1f65c9c9a62bce5d2e Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 1 Mar 2024 22:27:16 +0000 Subject: [PATCH] refactor: JUnit5 Best Practices Use this link to re-run the recipe: https://app.moderne.io/recipes/builder/hBA8IygoJ?organizationId=YzQ4NzM2ZmItNmRjOS00ZmU2LWJhOTUtNDQ4NTViYTRhNWFk Co-authored-by: Moderne --- .../permission/DomainPermissionTest.java | 3 +- .../SubjectAwareExecutorServiceTest.java | 5 +- .../shiro/mgt/DefaultSecurityManagerTest.java | 4 +- ...VMSingletonDefaultSecurityManagerTest.java | 5 +- .../shiro/realm/AuthorizingRealmTest.java | 2 +- .../ActiveDirectoryRealmTest.java | 10 ++- .../shiro/realm/jdbc/JDBCRealmTest.java | 19 +++-- .../session/mgt/DelegatingSessionTest.java | 2 +- ...ServiceSessionValidationSchedulerTest.java | 22 ++--- .../shiro/subject/DelegatingSubjectTest.java | 11 +-- .../shiro/util/AntPathMatcherTests.java | 42 +++++----- .../shiro/lang/util/ClassUtilsTest.java | 84 +++++++++---------- .../aspectj/bank/SecureBankServiceTest.java | 20 ++--- .../test/web/jakarta/WebContainerIT.java | 15 ++-- .../cache/ehcache/EhCacheManagerTest.java | 10 +-- .../web/ShiroFilterFactoryBeanTest.java | 2 +- .../web/env/EnvironmentLoaderServiceTest.java | 5 +- .../authz/HttpMethodPermissionFilterTest.java | 25 +++--- .../filter/mgt/SimpleNamedFilterListTest.java | 2 +- .../mgt/DefaultWebSecurityManagerTest.java | 2 +- 20 files changed, 152 insertions(+), 138 deletions(-) diff --git a/core/src/test/java/org/apache/shiro/authz/permission/DomainPermissionTest.java b/core/src/test/java/org/apache/shiro/authz/permission/DomainPermissionTest.java index b5faee8205..4e474c9c17 100644 --- a/core/src/test/java/org/apache/shiro/authz/permission/DomainPermissionTest.java +++ b/core/src/test/java/org/apache/shiro/authz/permission/DomainPermissionTest.java @@ -25,7 +25,6 @@ import java.util.Set; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -44,7 +43,7 @@ void testDefaultConstructor() { p = new DomainPermission(); // Verify domain - assertTrue("domain".equals(p.getDomain())); + assertEquals("domain", p.getDomain()); // Verify actions set = p.getActions(); diff --git a/core/src/test/java/org/apache/shiro/concurrent/SubjectAwareExecutorServiceTest.java b/core/src/test/java/org/apache/shiro/concurrent/SubjectAwareExecutorServiceTest.java index 0f31f35f86..345cd8c3b5 100644 --- a/core/src/test/java/org/apache/shiro/concurrent/SubjectAwareExecutorServiceTest.java +++ b/core/src/test/java/org/apache/shiro/concurrent/SubjectAwareExecutorServiceTest.java @@ -20,7 +20,6 @@ import org.apache.shiro.subject.support.SubjectRunnable; import org.apache.shiro.test.SecurityManagerTestSupport; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -33,6 +32,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.assertNotNull; + /** * Test cases for the {@link SubjectAwareExecutorService} implementation. */ @@ -52,7 +53,7 @@ public void testSubmitRunnable() { executor.submit(testRunnable); SubjectRunnable subjectRunnable = captor.getValue(); - Assertions.assertNotNull(subjectRunnable); + assertNotNull(subjectRunnable); }); } diff --git a/core/src/test/java/org/apache/shiro/mgt/DefaultSecurityManagerTest.java b/core/src/test/java/org/apache/shiro/mgt/DefaultSecurityManagerTest.java index dc26793035..1628eb101d 100644 --- a/core/src/test/java/org/apache/shiro/mgt/DefaultSecurityManagerTest.java +++ b/core/src/test/java/org/apache/shiro/mgt/DefaultSecurityManagerTest.java @@ -80,7 +80,7 @@ void testDefaultConfig() { Session session = subject.getSession(); session.setAttribute("key", "value"); - assertEquals(session.getAttribute("key"), "value"); + assertEquals("value", session.getAttribute("key")); subject.logout(); @@ -137,7 +137,7 @@ void testSubjectReuseAfterLogout() { Serializable firstSessionId = session.getId(); session.setAttribute("key", "value"); - assertEquals(session.getAttribute("key"), "value"); + assertEquals("value", session.getAttribute("key")); subject.logout(); diff --git a/core/src/test/java/org/apache/shiro/mgt/VMSingletonDefaultSecurityManagerTest.java b/core/src/test/java/org/apache/shiro/mgt/VMSingletonDefaultSecurityManagerTest.java index 3e46b60c47..f0c1495455 100644 --- a/core/src/test/java/org/apache/shiro/mgt/VMSingletonDefaultSecurityManagerTest.java +++ b/core/src/test/java/org/apache/shiro/mgt/VMSingletonDefaultSecurityManagerTest.java @@ -29,6 +29,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -62,12 +63,12 @@ void testVMSingleton() { AuthenticationToken token = new UsernamePasswordToken("guest", "guest"); subject.login(token); subject.getSession().setAttribute("key", "value"); - assertTrue(subject.getSession().getAttribute("key").equals("value")); + assertEquals("value", subject.getSession().getAttribute("key")); subject = SecurityUtils.getSubject(); assertTrue(subject.isAuthenticated()); - assertTrue(subject.getSession().getAttribute("key").equals("value")); + assertEquals("value", subject.getSession().getAttribute("key")); } finally { sm.destroy(); //SHIRO-270: diff --git a/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java b/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java index 3f6d4f9cfb..c1cd5f8cf7 100644 --- a/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java +++ b/core/src/test/java/org/apache/shiro/realm/AuthorizingRealmTest.java @@ -254,7 +254,7 @@ public Collection resolvePermissionsInRole(String roleString) { authorizationInfo.addStringPermission("\t"); authorizationInfo.addStringPermission(null); Collection permissions = realm.getPermissions(authorizationInfo); - assertEquals(permissions.size(), 4); + assertEquals(4, permissions.size()); } private void assertArrayEquals(boolean[] expected, boolean[] actual) { diff --git a/core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java b/core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java index f480160cbf..09ee8aaed2 100644 --- a/core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java +++ b/core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java @@ -41,7 +41,6 @@ import org.easymock.Capture; import org.easymock.CaptureType; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -63,7 +62,10 @@ import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.is; + +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; /** * Simple test case for ActiveDirectoryRealm. @@ -108,10 +110,10 @@ void testDefaultConfig() { UsernamePrincipal usernamePrincipal = subject.getPrincipals().oneByType(UsernamePrincipal.class); - assertTrue(usernamePrincipal.getUsername().equals(USERNAME)); + assertEquals(USERNAME, usernamePrincipal.getUsername()); UserIdPrincipal userIdPrincipal = subject.getPrincipals().oneByType(UserIdPrincipal.class); - assertTrue(userIdPrincipal.getUserId() == USER_ID); + assertEquals(USER_ID, userIdPrincipal.getUserId()); assertTrue(realm.hasRole(subject.getPrincipals(), ROLE)); @@ -148,7 +150,7 @@ public void assertExistingUserSuffix(String username, String expectedPrincipalNa try { activeDirectoryRealm.getRoleNamesForUser(username, ldapContext); } catch (NamingException e) { - Assertions.fail("Unexpected NamingException thrown during test"); + fail("Unexpected NamingException thrown during test"); } }); diff --git a/core/src/test/java/org/apache/shiro/realm/jdbc/JDBCRealmTest.java b/core/src/test/java/org/apache/shiro/realm/jdbc/JDBCRealmTest.java index 58e9d31d9f..a3938f3a4e 100644 --- a/core/src/test/java/org/apache/shiro/realm/jdbc/JDBCRealmTest.java +++ b/core/src/test/java/org/apache/shiro/realm/jdbc/JDBCRealmTest.java @@ -36,7 +36,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.TestInfo; import javax.sql.DataSource; @@ -48,6 +47,10 @@ import java.util.HashMap; import java.util.Optional; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + /** * Test case for JDBCRealm. @@ -259,7 +262,7 @@ void testRolePresent() throws Exception { Subject currentUser = builder.buildSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword); currentUser.login(token); - Assertions.assertTrue(currentUser.hasRole(testRole)); + assertTrue(currentUser.hasRole(testRole)); } @Test @@ -273,7 +276,7 @@ void testRoleNotPresent() throws Exception { Subject currentUser = builder.buildSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword); currentUser.login(token); - Assertions.assertFalse(currentUser.hasRole("Game Overall Director")); + assertFalse(currentUser.hasRole("Game Overall Director")); } @Test @@ -288,7 +291,7 @@ void testPermissionPresent() throws Exception { Subject currentUser = builder.buildSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword); currentUser.login(token); - Assertions.assertTrue(currentUser.isPermitted(testPermissionString)); + assertTrue(currentUser.isPermitted(testPermissionString)); } @Test @@ -303,7 +306,7 @@ void testPermissionNotPresent() throws Exception { Subject currentUser = builder.buildSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword); currentUser.login(token); - Assertions.assertFalse(currentUser.isPermitted("testDomain:testTarget:specialAction")); + assertFalse(currentUser.isPermitted("testDomain:testTarget:specialAction")); } /** @@ -357,7 +360,7 @@ protected void createDefaultSchema(String testName, boolean salted) { String password = sha256Hash.toHex(); sql.executeUpdate("insert into users values ('" + username + "', '" + password + "')"); } catch (SQLException ex) { - Assertions.fail("Exception creating test database"); + fail("Exception creating test database"); } finally { JdbcUtils.closeStatement(sql); JdbcUtils.closeConnection(conn); @@ -393,7 +396,7 @@ protected void createSaltColumnSchema(String testName, boolean base64EncodeSalt) "insert into users values ('" + username + "', '" + password + "', '" + maybeBase64EncodedSalt + "')"); } catch (SQLException ex) { - Assertions.fail("Exception creating test database"); + fail("Exception creating test database"); } finally { JdbcUtils.closeStatement(sql); JdbcUtils.closeConnection(conn); @@ -418,7 +421,7 @@ protected void createRolesAndPermissions(DataSource ds) { sql.executeUpdate( "insert into roles_permissions values ('" + testRole + "', '" + testPermissionString + "')"); } catch (SQLException ex) { - Assertions.fail("Exception adding test role and permission"); + fail("Exception adding test role and permission"); } finally { JdbcUtils.closeStatement(sql); JdbcUtils.closeConnection(conn); diff --git a/core/src/test/java/org/apache/shiro/session/mgt/DelegatingSessionTest.java b/core/src/test/java/org/apache/shiro/session/mgt/DelegatingSessionTest.java index 334972835a..15a3284ff0 100644 --- a/core/src/test/java/org/apache/shiro/session/mgt/DelegatingSessionTest.java +++ b/core/src/test/java/org/apache/shiro/session/mgt/DelegatingSessionTest.java @@ -62,7 +62,7 @@ public void sleep(long millis) { @Test void testTimeout() { Serializable origId = session.getId(); - assertEquals(session.getTimeout(), AbstractSessionManager.DEFAULT_GLOBAL_SESSION_TIMEOUT); + assertEquals(AbstractSessionManager.DEFAULT_GLOBAL_SESSION_TIMEOUT, session.getTimeout()); session.touch(); session.setTimeout(100); assertEquals(100, session.getTimeout()); diff --git a/core/src/test/java/org/apache/shiro/session/mgt/ExecutorServiceSessionValidationSchedulerTest.java b/core/src/test/java/org/apache/shiro/session/mgt/ExecutorServiceSessionValidationSchedulerTest.java index 98c482a930..bb2d4b3c49 100644 --- a/core/src/test/java/org/apache/shiro/session/mgt/ExecutorServiceSessionValidationSchedulerTest.java +++ b/core/src/test/java/org/apache/shiro/session/mgt/ExecutorServiceSessionValidationSchedulerTest.java @@ -20,10 +20,12 @@ import org.apache.shiro.session.Session; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + @SuppressWarnings("checkstyle:MagicNumber") public class ExecutorServiceSessionValidationSchedulerTest { @@ -47,8 +49,8 @@ void timeoutSessionValidate() throws InterruptedException { session.setTimeout(2000L); defaultSessionManager.create(session); Thread.sleep(5000L); - Assertions.assertTrue(defaultSessionManager.getActiveSessions().isEmpty()); - Assertions.assertTrue(executorServiceSessionValidationScheduler.isEnabled()); + assertTrue(defaultSessionManager.getActiveSessions().isEmpty()); + assertTrue(executorServiceSessionValidationScheduler.isEnabled()); } @Test @@ -59,19 +61,19 @@ void stopSessionValidate() throws InterruptedException { Thread.sleep(1000L); session.stop(); Thread.sleep(3000L); - Assertions.assertTrue(defaultSessionManager.getActiveSessions().isEmpty()); - Assertions.assertTrue(executorServiceSessionValidationScheduler.isEnabled()); + assertTrue(defaultSessionManager.getActiveSessions().isEmpty()); + assertTrue(executorServiceSessionValidationScheduler.isEnabled()); } @Test void enableSessionValidation() throws InterruptedException { - Assertions.assertTrue(executorServiceSessionValidationScheduler.isEnabled()); + assertTrue(executorServiceSessionValidationScheduler.isEnabled()); executorServiceSessionValidationScheduler.disableSessionValidation(); Thread.sleep(2000L); - Assertions.assertFalse(executorServiceSessionValidationScheduler.isEnabled()); + assertFalse(executorServiceSessionValidationScheduler.isEnabled()); executorServiceSessionValidationScheduler.enableSessionValidation(); Thread.sleep(2000L); - Assertions.assertTrue(executorServiceSessionValidationScheduler.isEnabled()); + assertTrue(executorServiceSessionValidationScheduler.isEnabled()); } @Test @@ -88,8 +90,8 @@ void threadException() throws InterruptedException { Thread.sleep(2000L); session.stop(); Thread.sleep(2000L); - Assertions.assertFalse(defaultSessionManager.getActiveSessions().isEmpty()); - Assertions.assertTrue(executorServiceSessionValidationScheduler.isEnabled()); + assertFalse(defaultSessionManager.getActiveSessions().isEmpty()); + assertTrue(executorServiceSessionValidationScheduler.isEnabled()); } @AfterEach diff --git a/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java b/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java index 6dc41fca64..c9dfcc6ce2 100644 --- a/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java +++ b/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java @@ -39,6 +39,7 @@ import static org.easymock.EasyMock.createNiceMock; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -68,7 +69,7 @@ void testSessionStopThenStart() { Session session = subject.getSession(); session.setAttribute(key, value); - assertTrue(session.getAttribute(key).equals(value)); + assertEquals(session.getAttribute(key), value); Serializable firstSessionId = session.getId(); assertNotNull(firstSessionId); @@ -79,7 +80,7 @@ void testSessionStopThenStart() { assertNull(session.getAttribute(key)); Serializable secondSessionId = session.getId(); assertNotNull(secondSessionId); - assertFalse(firstSessionId.equals(secondSessionId)); + assertNotEquals(firstSessionId, secondSessionId); subject.logout(); @@ -175,7 +176,7 @@ void testRunAs() { //assert we still have the previous (user1) principals: PrincipalCollection previous = subject.getPreviousPrincipals(); assertFalse(previous == null || previous.isEmpty()); - assertTrue(previous.getPrimaryPrincipal().equals("user1")); + assertEquals("user1", previous.getPrimaryPrincipal()); //test the stack functionality: While as user2, run as user3: subject.runAs(new SimplePrincipalCollection("user3", INI_REALM_NAME)); @@ -188,7 +189,7 @@ void testRunAs() { //assert we still have the previous (user2) principals in the stack: previous = subject.getPreviousPrincipals(); assertFalse(previous == null || previous.isEmpty()); - assertTrue(previous.getPrimaryPrincipal().equals("user2")); + assertEquals("user2", previous.getPrimaryPrincipal()); //drop down to user2: subject.releaseRunAs(); @@ -203,7 +204,7 @@ void testRunAs() { //assert we still have the previous (user1) principals: previous = subject.getPreviousPrincipals(); assertFalse(previous == null || previous.isEmpty()); - assertTrue(previous.getPrimaryPrincipal().equals("user1")); + assertEquals("user1", previous.getPrimaryPrincipal()); //drop down to original user1: subject.releaseRunAs(); diff --git a/core/src/test/java/org/apache/shiro/util/AntPathMatcherTests.java b/core/src/test/java/org/apache/shiro/util/AntPathMatcherTests.java index c01154d61f..b065a89cbd 100644 --- a/core/src/test/java/org/apache/shiro/util/AntPathMatcherTests.java +++ b/core/src/test/java/org/apache/shiro/util/AntPathMatcherTests.java @@ -283,27 +283,27 @@ void uniqueDelimiter() { @Test void extractPathWithinPattern() throws Exception { - assertEquals(pathMatcher.extractPathWithinPattern("/docs/commit.html", "/docs/commit.html"), ""); - - assertEquals(pathMatcher.extractPathWithinPattern("/docs/*", "/docs/cvs/commit"), "cvs/commit"); - assertEquals(pathMatcher.extractPathWithinPattern("/docs/cvs/*.html", "/docs/cvs/commit.html"), "commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("/docs/**", "/docs/cvs/commit"), "cvs/commit"); - assertEquals(pathMatcher.extractPathWithinPattern("/docs/**/*.html", "/docs/cvs/commit.html"), "cvs/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("/docs/**/*.html", "/docs/commit.html"), "commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("/*.html", "/commit.html"), "commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("/*.html", "/docs/commit.html"), "docs/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("*.html", "/commit.html"), "/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("*.html", "/docs/commit.html"), "/docs/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("**/*.*", "/docs/commit.html"), "/docs/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("*", "/docs/commit.html"), "/docs/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("**/commit.html", "/docs/cvs/other/commit.html"), "/docs/cvs/other/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("/docs/**/commit.html", "/docs/cvs/other/commit.html"), "cvs/other/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("/docs/**/**/**/**", "/docs/cvs/other/commit.html"), "cvs/other/commit.html"); - - assertEquals(pathMatcher.extractPathWithinPattern("/d?cs/*", "/docs/cvs/commit"), "docs/cvs/commit"); - assertEquals(pathMatcher.extractPathWithinPattern("/docs/c?s/*.html", "/docs/cvs/commit.html"), "cvs/commit.html"); - assertEquals(pathMatcher.extractPathWithinPattern("/d?cs/**", "/docs/cvs/commit"), "docs/cvs/commit"); - assertEquals(pathMatcher.extractPathWithinPattern("/d?cs/**/*.html", "/docs/cvs/commit.html"), "docs/cvs/commit.html"); + assertEquals("", pathMatcher.extractPathWithinPattern("/docs/commit.html", "/docs/commit.html")); + + assertEquals("cvs/commit", pathMatcher.extractPathWithinPattern("/docs/*", "/docs/cvs/commit")); + assertEquals("commit.html", pathMatcher.extractPathWithinPattern("/docs/cvs/*.html", "/docs/cvs/commit.html")); + assertEquals("cvs/commit", pathMatcher.extractPathWithinPattern("/docs/**", "/docs/cvs/commit")); + assertEquals("cvs/commit.html", pathMatcher.extractPathWithinPattern("/docs/**/*.html", "/docs/cvs/commit.html")); + assertEquals("commit.html", pathMatcher.extractPathWithinPattern("/docs/**/*.html", "/docs/commit.html")); + assertEquals("commit.html", pathMatcher.extractPathWithinPattern("/*.html", "/commit.html")); + assertEquals("docs/commit.html", pathMatcher.extractPathWithinPattern("/*.html", "/docs/commit.html")); + assertEquals("/commit.html", pathMatcher.extractPathWithinPattern("*.html", "/commit.html")); + assertEquals("/docs/commit.html", pathMatcher.extractPathWithinPattern("*.html", "/docs/commit.html")); + assertEquals("/docs/commit.html", pathMatcher.extractPathWithinPattern("**/*.*", "/docs/commit.html")); + assertEquals("/docs/commit.html", pathMatcher.extractPathWithinPattern("*", "/docs/commit.html")); + assertEquals("/docs/cvs/other/commit.html", pathMatcher.extractPathWithinPattern("**/commit.html", "/docs/cvs/other/commit.html")); + assertEquals("cvs/other/commit.html", pathMatcher.extractPathWithinPattern("/docs/**/commit.html", "/docs/cvs/other/commit.html")); + assertEquals("cvs/other/commit.html", pathMatcher.extractPathWithinPattern("/docs/**/**/**/**", "/docs/cvs/other/commit.html")); + + assertEquals("docs/cvs/commit", pathMatcher.extractPathWithinPattern("/d?cs/*", "/docs/cvs/commit")); + assertEquals("cvs/commit.html", pathMatcher.extractPathWithinPattern("/docs/c?s/*.html", "/docs/cvs/commit.html")); + assertEquals("docs/cvs/commit", pathMatcher.extractPathWithinPattern("/d?cs/**", "/docs/cvs/commit")); + assertEquals("docs/cvs/commit.html", pathMatcher.extractPathWithinPattern("/d?cs/**/*.html", "/docs/cvs/commit.html")); } @Test diff --git a/lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java b/lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java index 04883a47b4..b9097eefa8 100644 --- a/lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java +++ b/lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java @@ -27,61 +27,61 @@ class ClassUtilsTest { @Test void testGetPrimitiveClasses() throws UnknownClassException { - assertEquals(ClassUtils.forName("boolean"), boolean.class); - assertEquals(ClassUtils.forName("byte"), byte.class); - assertEquals(ClassUtils.forName("char"), char.class); - assertEquals(ClassUtils.forName("short"), short.class); - assertEquals(ClassUtils.forName("int"), int.class); - assertEquals(ClassUtils.forName("long"), long.class); - assertEquals(ClassUtils.forName("float"), float.class); - assertEquals(ClassUtils.forName("double"), double.class); - assertEquals(ClassUtils.forName("void"), void.class); + assertEquals(boolean.class, ClassUtils.forName("boolean")); + assertEquals(byte.class, ClassUtils.forName("byte")); + assertEquals(char.class, ClassUtils.forName("char")); + assertEquals(short.class, ClassUtils.forName("short")); + assertEquals(int.class, ClassUtils.forName("int")); + assertEquals(long.class, ClassUtils.forName("long")); + assertEquals(float.class, ClassUtils.forName("float")); + assertEquals(double.class, ClassUtils.forName("double")); + assertEquals(void.class, ClassUtils.forName("void")); - assertEquals(ClassUtils.forName(boolean.class.getName()), boolean.class); - assertEquals(ClassUtils.forName(byte.class.getName()), byte.class); - assertEquals(ClassUtils.forName(char.class.getName()), char.class); - assertEquals(ClassUtils.forName(short.class.getName()), short.class); - assertEquals(ClassUtils.forName(int.class.getName()), int.class); - assertEquals(ClassUtils.forName(long.class.getName()), long.class); - assertEquals(ClassUtils.forName(float.class.getName()), float.class); - assertEquals(ClassUtils.forName(double.class.getName()), double.class); - assertEquals(ClassUtils.forName(void.class.getName()), void.class); + assertEquals(boolean.class, ClassUtils.forName(boolean.class.getName())); + assertEquals(byte.class, ClassUtils.forName(byte.class.getName())); + assertEquals(char.class, ClassUtils.forName(char.class.getName())); + assertEquals(short.class, ClassUtils.forName(short.class.getName())); + assertEquals(int.class, ClassUtils.forName(int.class.getName())); + assertEquals(long.class, ClassUtils.forName(long.class.getName())); + assertEquals(float.class, ClassUtils.forName(float.class.getName())); + assertEquals(double.class, ClassUtils.forName(double.class.getName())); + assertEquals(void.class, ClassUtils.forName(void.class.getName())); } @Test void testGetPrimitiveArrays() throws UnknownClassException { - assertEquals(ClassUtils.forName("[Z"), boolean[].class); - assertEquals(ClassUtils.forName("[B"), byte[].class); - assertEquals(ClassUtils.forName("[C"), char[].class); - assertEquals(ClassUtils.forName("[S"), short[].class); - assertEquals(ClassUtils.forName("[I"), int[].class); - assertEquals(ClassUtils.forName("[J"), long[].class); - assertEquals(ClassUtils.forName("[F"), float[].class); - assertEquals(ClassUtils.forName("[D"), double[].class); + assertEquals(boolean[].class, ClassUtils.forName("[Z")); + assertEquals(byte[].class, ClassUtils.forName("[B")); + assertEquals(char[].class, ClassUtils.forName("[C")); + assertEquals(short[].class, ClassUtils.forName("[S")); + assertEquals(int[].class, ClassUtils.forName("[I")); + assertEquals(long[].class, ClassUtils.forName("[J")); + assertEquals(float[].class, ClassUtils.forName("[F")); + assertEquals(double[].class, ClassUtils.forName("[D")); - assertEquals(ClassUtils.forName(boolean[].class.getName()), boolean[].class); - assertEquals(ClassUtils.forName(byte[].class.getName()), byte[].class); - assertEquals(ClassUtils.forName(char[].class.getName()), char[].class); - assertEquals(ClassUtils.forName(short[].class.getName()), short[].class); - assertEquals(ClassUtils.forName(int[].class.getName()), int[].class); - assertEquals(ClassUtils.forName(long[].class.getName()), long[].class); - assertEquals(ClassUtils.forName(float[].class.getName()), float[].class); - assertEquals(ClassUtils.forName(double[].class.getName()), double[].class); + assertEquals(boolean[].class, ClassUtils.forName(boolean[].class.getName())); + assertEquals(byte[].class, ClassUtils.forName(byte[].class.getName())); + assertEquals(char[].class, ClassUtils.forName(char[].class.getName())); + assertEquals(short[].class, ClassUtils.forName(short[].class.getName())); + assertEquals(int[].class, ClassUtils.forName(int[].class.getName())); + assertEquals(long[].class, ClassUtils.forName(long[].class.getName())); + assertEquals(float[].class, ClassUtils.forName(float[].class.getName())); + assertEquals(double[].class, ClassUtils.forName(double[].class.getName())); } @Test void testGetClass() { - assertEquals(ClassUtils.forName("java.lang.String"), String.class); - assertEquals(ClassUtils.forName("[Ljava.lang.String;"), String[].class); - assertEquals(ClassUtils.forName(String.class.getName()), String.class); - assertEquals(ClassUtils.forName(String[].class.getName()), String[].class); + assertEquals(String.class, ClassUtils.forName("java.lang.String")); + assertEquals(String[].class, ClassUtils.forName("[Ljava.lang.String;")); + assertEquals(String.class, ClassUtils.forName(String.class.getName())); + assertEquals(String[].class, ClassUtils.forName(String[].class.getName())); - assertEquals(ClassUtils.forName("org.apache.shiro.lang.util.ClassUtilsTest"), ClassUtilsTest.class); - assertEquals(ClassUtils.forName("[Lorg.apache.shiro.lang.util.ClassUtilsTest;"), ClassUtilsTest[].class); - assertEquals(ClassUtils.forName(ClassUtilsTest.class.getName()), ClassUtilsTest.class); - assertEquals(ClassUtils.forName(ClassUtilsTest[].class.getName()), ClassUtilsTest[].class); + assertEquals(ClassUtilsTest.class, ClassUtils.forName("org.apache.shiro.lang.util.ClassUtilsTest")); + assertEquals(ClassUtilsTest[].class, ClassUtils.forName("[Lorg.apache.shiro.lang.util.ClassUtilsTest;")); + assertEquals(ClassUtilsTest.class, ClassUtils.forName(ClassUtilsTest.class.getName())); + assertEquals(ClassUtilsTest[].class, ClassUtils.forName(ClassUtilsTest[].class.getName())); } } diff --git a/samples/aspectj/src/test/java/org/apache/shiro/samples/aspectj/bank/SecureBankServiceTest.java b/samples/aspectj/src/test/java/org/apache/shiro/samples/aspectj/bank/SecureBankServiceTest.java index bf9209ae89..b9bc13b2db 100644 --- a/samples/aspectj/src/test/java/org/apache/shiro/samples/aspectj/bank/SecureBankServiceTest.java +++ b/samples/aspectj/src/test/java/org/apache/shiro/samples/aspectj/bank/SecureBankServiceTest.java @@ -26,13 +26,13 @@ import org.apache.shiro.subject.Subject; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @SuppressWarnings({"checkstyle:MemberName", "checkstyle:MethodName", "checkstyle:MagicNumber"}) @@ -175,7 +175,7 @@ void testCloseAccount_zeroBalance() throws Exception { logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); - Assertions.assertEquals(0, (int) closingBalance); + assertEquals(0, (int) closingBalance); assertAccount("Chris Smith", false, 0, 1, accountId); } @@ -188,7 +188,7 @@ void testCloseAccount_withBalance() throws Exception { logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); - Assertions.assertEquals(385, (int) closingBalance); + assertEquals(385, (int) closingBalance); assertAccount("Gerry Smith", false, 0, 2, accountId); } @@ -201,7 +201,7 @@ void testCloseAccount_alreadyClosed() throws Exception { logoutCurrentSubject(); loginAsSuperviser(); double closingBalance = service.closeAccount(accountId); - Assertions.assertEquals(0, (int) closingBalance); + assertEquals(0, (int) closingBalance); assertAccount("Chris Smith", false, 0, 1, accountId); service.closeAccount(accountId); }); @@ -226,7 +226,7 @@ protected double makeDepositAndValidateAccount(long anAccountId, int anAmount, S double previousBalance = service.getBalanceOf(anAccountId); int previousTxCount = service.getTxHistoryFor(anAccountId).length; double newBalance = service.depositInto(anAccountId, anAmount); - Assertions.assertEquals((int) previousBalance + anAmount, (int) newBalance); + assertEquals((int) previousBalance + anAmount, (int) newBalance); assertAccount(eOwnerName, true, (int) newBalance, 1 + previousTxCount, anAccountId); return newBalance; } @@ -235,7 +235,7 @@ protected double makeWithdrawalAndValidateAccount(long anAccountId, int anAmount double previousBalance = service.getBalanceOf(anAccountId); int previousTxCount = service.getTxHistoryFor(anAccountId).length; double newBalance = service.withdrawFrom(anAccountId, anAmount); - Assertions.assertEquals((int) previousBalance - anAmount, (int) newBalance); + assertEquals((int) previousBalance - anAmount, (int) newBalance); assertAccount(eOwnerName, true, (int) newBalance, 1 + previousTxCount, anAccountId); return newBalance; } @@ -243,10 +243,10 @@ protected double makeWithdrawalAndValidateAccount(long anAccountId, int anAmount public static void assertAccount(String eOwnerName, boolean eIsActive, int eBalance, int eTxLogCount, long actualAccountId) throws Exception { - Assertions.assertEquals(eOwnerName, service.getOwnerOf(actualAccountId)); - Assertions.assertEquals(eIsActive, service.isAccountActive(actualAccountId)); - Assertions.assertEquals(eBalance, (int) service.getBalanceOf(actualAccountId)); - Assertions.assertEquals(eTxLogCount, service.getTxHistoryFor(actualAccountId).length); + assertEquals(eOwnerName, service.getOwnerOf(actualAccountId)); + assertEquals(eIsActive, service.isAccountActive(actualAccountId)); + assertEquals(eBalance, (int) service.getBalanceOf(actualAccountId)); + assertEquals(eTxLogCount, service.getTxHistoryFor(actualAccountId).length); } @RequiresGuest diff --git a/samples/web-jakarta/src/test/java/org/apache/shiro/test/web/jakarta/WebContainerIT.java b/samples/web-jakarta/src/test/java/org/apache/shiro/test/web/jakarta/WebContainerIT.java index aefb748d62..d4cc4da38c 100644 --- a/samples/web-jakarta/src/test/java/org/apache/shiro/test/web/jakarta/WebContainerIT.java +++ b/samples/web-jakarta/src/test/java/org/apache/shiro/test/web/jakarta/WebContainerIT.java @@ -23,7 +23,6 @@ import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.core.Cookie; import jakarta.ws.rs.core.Response; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.net.URI; @@ -31,6 +30,10 @@ import static jakarta.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED; import static jakarta.ws.rs.core.MediaType.TEXT_HTML_TYPE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + public class WebContainerIT extends JakartaAbstractContainerIT { @SuppressWarnings("checkstyle:MagicNumber") @@ -46,27 +49,27 @@ public void logIn() { .get()) { jsessionid = new Cookie("JSESSIONID", loginPage.getMetadata().get("Set-Cookie").get(0).toString().split(";")[0].split("=")[1]); - Assertions.assertTrue(loginPage.readEntity(String.class).contains("loginform")); + assertTrue(loginPage.readEntity(String.class).contains("loginform")); } - Assertions.assertNotNull(jsessionid); + assertNotNull(jsessionid); URI location; try (Response loginAction = client.target(getBaseUri()) .path("/login.jsp") .request(APPLICATION_FORM_URLENCODED) .cookie(jsessionid) .post(Entity.entity("username=root&password=secret&submit=Login", APPLICATION_FORM_URLENCODED))) { - Assertions.assertEquals(302, loginAction.getStatus()); + assertEquals(302, loginAction.getStatus()); location = loginAction.getLocation(); } - Assertions.assertNotNull(location); + assertNotNull(location); final String loggedPage = client.target(getBaseUri()) .path(location.getPath()) .request(APPLICATION_FORM_URLENCODED) .cookie(jsessionid) .get(String.class); - Assertions.assertTrue(loggedPage.contains("Hi root!")); + assertTrue(loggedPage.contains("Hi root!")); } finally { client.close(); } diff --git a/support/ehcache/src/test/java/org/apache/shiro/cache/ehcache/EhCacheManagerTest.java b/support/ehcache/src/test/java/org/apache/shiro/cache/ehcache/EhCacheManagerTest.java index 811f4fd309..a13bbb7d7c 100644 --- a/support/ehcache/src/test/java/org/apache/shiro/cache/ehcache/EhCacheManagerTest.java +++ b/support/ehcache/src/test/java/org/apache/shiro/cache/ehcache/EhCacheManagerTest.java @@ -78,7 +78,7 @@ void testLazyCacheManagerCreationWithoutCallingInit() { cache.put("hello", "world"); String value = cache.get("hello"); assertNotNull(value); - assertEquals(value, "world"); + assertEquals("world", value); } @Test @@ -96,7 +96,7 @@ void testRemove() { cache.put("hello2", "world2"); String value = cache.get("hello"); assertNotNull(value); - assertEquals(value, "world"); + assertEquals("world", value); assertEquals("world2", cache.get("hello2")); assertEquals(2, cache.size()); @@ -123,7 +123,7 @@ void testClear() { cache.put("hello2", "world2"); String value = cache.get("hello"); assertNotNull(value); - assertEquals(value, "world"); + assertEquals("world", value); assertEquals("world2", cache.get("hello2")); assertEquals(2, cache.size()); @@ -148,7 +148,7 @@ void testKeys() { cache.put("hello2", "world2"); String value = cache.get("hello"); assertNotNull(value); - assertEquals(value, "world"); + assertEquals("world", value); assertEquals("world2", cache.get("hello2")); assertEquals(2, cache.size()); @@ -182,7 +182,7 @@ void testValues() { cache.put("hello2", "world2"); String value = cache.get("hello"); assertNotNull(value); - assertEquals(value, "world"); + assertEquals("world", value); assertEquals("world2", cache.get("hello2")); assertEquals(2, cache.size()); diff --git a/support/spring/src/test/java/org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.java b/support/spring/src/test/java/org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.java index 23c504ea59..c2a81c0d0f 100644 --- a/support/spring/src/test/java/org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.java +++ b/support/spring/src/test/java/org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.java @@ -68,7 +68,7 @@ void testFilterDefinition() { DefaultFilterChainManager fcManager = (DefaultFilterChainManager) resolver.getFilterChainManager(); NamedFilterList chain = fcManager.getChain("/test"); assertNotNull(chain); - assertEquals(chain.size(), 3); + assertEquals(3, chain.size()); Filter[] filters = new Filter[chain.size()]; filters = chain.toArray(filters); // global filter diff --git a/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java b/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java index 164d60d509..8f1c4d8443 100644 --- a/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java +++ b/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java @@ -20,7 +20,6 @@ import org.apache.shiro.config.ConfigurationException; import org.easymock.EasyMock; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import javax.servlet.ServletContext; @@ -35,6 +34,8 @@ import static org.hamcrest.Matchers.sameInstance; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.mockito.Mockito.mock; + +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -96,7 +97,7 @@ void multipleServiceTest() throws Exception { try { environmentLoader.createEnvironment(servletContext); - Assertions.fail("Expected ConfigurationException to be thrown"); + fail("Expected ConfigurationException to be thrown"); } catch (ConfigurationException e) { assertThat(e.getMessage(), stringContainsInOrder("zero or exactly one", "shiroEnvironmentClass")); } diff --git a/web/src/test/java/org/apache/shiro/web/filter/authz/HttpMethodPermissionFilterTest.java b/web/src/test/java/org/apache/shiro/web/filter/authz/HttpMethodPermissionFilterTest.java index 71dc7f9ec6..3a3c3d791f 100644 --- a/web/src/test/java/org/apache/shiro/web/filter/authz/HttpMethodPermissionFilterTest.java +++ b/web/src/test/java/org/apache/shiro/web/filter/authz/HttpMethodPermissionFilterTest.java @@ -18,9 +18,10 @@ */ package org.apache.shiro.web.filter.authz; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class HttpMethodPermissionFilterTest { @@ -33,16 +34,16 @@ void testPermissionMapping() { String[] permsBefore = {"foo", "bar"}; String[] permsAfter = filter.buildPermissions(permsBefore, filter.getHttpMethodAction("get")); - Assertions.assertEquals(2, permsAfter.length); - Assertions.assertEquals("foo:read", permsAfter[0]); - Assertions.assertEquals("bar:read", permsAfter[1]); - - Assertions.assertEquals("foo:read", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("head"))[0]); - Assertions.assertEquals("foo:update", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("put"))[0]); - Assertions.assertEquals("foo:create", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("post"))[0]); - Assertions.assertEquals("foo:create", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("mkcol"))[0]); - Assertions.assertEquals("foo:delete", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("delete"))[0]); - Assertions.assertEquals("foo:read", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("options"))[0]); - Assertions.assertEquals("foo:read", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("trace"))[0]); + assertEquals(2, permsAfter.length); + assertEquals("foo:read", permsAfter[0]); + assertEquals("bar:read", permsAfter[1]); + + assertEquals("foo:read", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("head"))[0]); + assertEquals("foo:update", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("put"))[0]); + assertEquals("foo:create", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("post"))[0]); + assertEquals("foo:create", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("mkcol"))[0]); + assertEquals("foo:delete", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("delete"))[0]); + assertEquals("foo:read", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("options"))[0]); + assertEquals("foo:read", filter.buildPermissions(permsBefore, filter.getHttpMethodAction("trace"))[0]); } } diff --git a/web/src/test/java/org/apache/shiro/web/filter/mgt/SimpleNamedFilterListTest.java b/web/src/test/java/org/apache/shiro/web/filter/mgt/SimpleNamedFilterListTest.java index 1b187f3630..312dd46ed8 100644 --- a/web/src/test/java/org/apache/shiro/web/filter/mgt/SimpleNamedFilterListTest.java +++ b/web/src/test/java/org/apache/shiro/web/filter/mgt/SimpleNamedFilterListTest.java @@ -98,7 +98,7 @@ void testListMethods() { list.add(0, singleFilter); assertEquals(2, list.size()); assertTrue(list.get(0) instanceof SslFilter); - assertArrayEquals(list.toArray(), new Object[] {singleFilter, filter}); + assertArrayEquals(new Object[]{singleFilter, filter}, list.toArray()); list.addAll(multipleFilters); assertEquals(4, list.size()); diff --git a/web/src/test/java/org/apache/shiro/web/mgt/DefaultWebSecurityManagerTest.java b/web/src/test/java/org/apache/shiro/web/mgt/DefaultWebSecurityManagerTest.java index ac1196b239..9e5775020d 100644 --- a/web/src/test/java/org/apache/shiro/web/mgt/DefaultWebSecurityManagerTest.java +++ b/web/src/test/java/org/apache/shiro/web/mgt/DefaultWebSecurityManagerTest.java @@ -149,7 +149,7 @@ void testSessionTimeout() { Session session = subject.getSession(); assertEquals(session.getTimeout(), globalTimeout); session.setTimeout(125); - assertEquals(session.getTimeout(), 125); + assertEquals(125, session.getTimeout()); sleep(200); try { session.getTimeout();