From be50cd8ada625c3bb8e4a774172ccea443f58b7f Mon Sep 17 00:00:00 2001 From: Antoine Date: Thu, 13 Apr 2017 17:24:11 +0200 Subject: [PATCH] Polish more AssertJ assertions Issue gh-4770 --- .../acls/domain/ObjectIdentityImplTests.java | 12 ++++++------ .../acls/jdbc/SpringCacheBasedAclCacheTests.java | 8 ++++---- .../access/hierarchicalroles/TestHelperTests.java | 6 +++--- .../provisioning/JdbcUserDetailsManagerTests.java | 4 ++-- .../context/SecurityContextHolderMTTests.java | 10 +++++----- .../openid/OpenIDAuthenticationFilterTests.java | 4 ++-- .../java/samples/gae/security/AppRoleTests.java | 4 ++-- .../j2ee/WebXmlJ2eeDefinedRolesRetrieverTests.java | 14 ++++++-------- .../AbstractRememberMeServicesTests.java | 4 ++-- ...ersistentTokenBasedRememberMeServicesTests.java | 4 ++-- .../TokenBasedRememberMeServicesTests.java | 14 +++++++------- .../web/csrf/CookieCsrfTokenRepositoryTests.java | 4 ++-- .../web/jackson2/SavedCookieMixinTests.java | 4 ++-- .../security/web/util/ThrowableAnalyzerTests.java | 4 ++-- 14 files changed, 47 insertions(+), 49 deletions(-) diff --git a/acl/src/test/java/org/springframework/security/acls/domain/ObjectIdentityImplTests.java b/acl/src/test/java/org/springframework/security/acls/domain/ObjectIdentityImplTests.java index 18bb2fd38a..878db4c959 100644 --- a/acl/src/test/java/org/springframework/security/acls/domain/ObjectIdentityImplTests.java +++ b/acl/src/test/java/org/springframework/security/acls/domain/ObjectIdentityImplTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -137,9 +137,9 @@ public class ObjectIdentityImplTests { String string = "SOME_STRING"; assertThat(string).isNotSameAs(obj); - assertThat(obj.equals(null)).isFalse(); - assertThat(obj.equals("DIFFERENT_OBJECT_TYPE")).isFalse(); - assertThat(obj.equals(new ObjectIdentityImpl(DOMAIN_CLASS, Long.valueOf(2)))).isFalse(); + assertThat(obj).isNotNull(); + assertThat(obj).isNotEqualTo("DIFFERENT_OBJECT_TYPE"); + assertThat(obj).isNotEqualTo(new ObjectIdentityImpl(DOMAIN_CLASS, Long.valueOf(2))); assertThat(obj).isNotEqualTo(new ObjectIdentityImpl( "org.springframework.security.acls.domain.ObjectIdentityImplTests$MockOtherIdDomainObject", Long.valueOf(1))); @@ -151,7 +151,7 @@ public class ObjectIdentityImplTests { public void hashcodeIsDifferentForDifferentJavaTypes() throws Exception { ObjectIdentity obj = new ObjectIdentityImpl(Object.class, Long.valueOf(1)); ObjectIdentity obj2 = new ObjectIdentityImpl(String.class, Long.valueOf(1)); - assertThat(obj.hashCode() == obj2.hashCode()).isFalse(); + assertThat(obj.hashCode()).isNotEqualTo(obj2.hashCode()); } @Test @@ -175,7 +175,7 @@ public class ObjectIdentityImplTests { public void stringAndNumericIdsAreNotEqual() throws Exception { ObjectIdentity obj = new ObjectIdentityImpl(Object.class, "1000"); ObjectIdentity obj2 = new ObjectIdentityImpl(Object.class, Long.valueOf(1000)); - assertThat(obj.equals(obj2)).isFalse(); + assertThat(obj).isNotEqualTo(obj2); } // ~ Inner Classes diff --git a/acl/src/test/java/org/springframework/security/acls/jdbc/SpringCacheBasedAclCacheTests.java b/acl/src/test/java/org/springframework/security/acls/jdbc/SpringCacheBasedAclCacheTests.java index d9675a1028..a9a45d2886 100644 --- a/acl/src/test/java/org/springframework/security/acls/jdbc/SpringCacheBasedAclCacheTests.java +++ b/acl/src/test/java/org/springframework/security/acls/jdbc/SpringCacheBasedAclCacheTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -104,17 +104,17 @@ public class SpringCacheBasedAclCacheTests { // Try to evict an entry that doesn't exist myCache.evictFromCache(Long.valueOf(3)); myCache.evictFromCache(new ObjectIdentityImpl(TARGET_CLASS, Long.valueOf(102))); - assertThat(4).isEqualTo(realCache.size()); + assertThat(realCache).hasSize(4); myCache.evictFromCache(Long.valueOf(1)); - assertThat(2).isEqualTo(realCache.size()); + assertThat(realCache).hasSize(2); // Check the second object inserted assertThat(acl2).isEqualTo(myCache.getFromCache(Long.valueOf(2))); assertThat(acl2).isEqualTo(myCache.getFromCache(identity2)); myCache.evictFromCache(identity2); - assertThat(0).isEqualTo(realCache.size()); + assertThat(realCache).isEmpty(); } @SuppressWarnings("rawtypes") diff --git a/core/src/test/java/org/springframework/security/access/hierarchicalroles/TestHelperTests.java b/core/src/test/java/org/springframework/security/access/hierarchicalroles/TestHelperTests.java index 350fa5ddbd..6e9c08d435 100644 --- a/core/src/test/java/org/springframework/security/access/hierarchicalroles/TestHelperTests.java +++ b/core/src/test/java/org/springframework/security/access/hierarchicalroles/TestHelperTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -181,12 +181,12 @@ public class TestHelperTests { public void testCreateAuthorityList() { List authorities1 = HierarchicalRolesTestHelper .createAuthorityList("ROLE_A"); - assertThat(1).isEqualTo(authorities1.size()); + assertThat(authorities1).hasSize(1); assertThat(authorities1.get(0).getAuthority()).isEqualTo("ROLE_A"); List authorities2 = HierarchicalRolesTestHelper .createAuthorityList("ROLE_A", "ROLE_C"); - assertThat(2).isEqualTo(authorities2.size()); + assertThat(authorities2).hasSize(2); assertThat(authorities2.get(0).getAuthority()).isEqualTo("ROLE_A"); assertThat(authorities2.get(1).getAuthority()).isEqualTo("ROLE_C"); } diff --git a/core/src/test/java/org/springframework/security/provisioning/JdbcUserDetailsManagerTests.java b/core/src/test/java/org/springframework/security/provisioning/JdbcUserDetailsManagerTests.java index c9f4d35f69..782486d0bb 100644 --- a/core/src/test/java/org/springframework/security/provisioning/JdbcUserDetailsManagerTests.java +++ b/core/src/test/java/org/springframework/security/provisioning/JdbcUserDetailsManagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -262,7 +262,7 @@ public class JdbcUserDetailsManagerTests { manager.renameGroup("GROUP_0", "GROUP_X"); assertThat(template.queryForObject("select id from groups where group_name = 'GROUP_X'", - Integer.class)).isEqualTo(0); + Integer.class)).isZero(); } @Test diff --git a/itest/misc/src/integration-test/java/org/springframework/security/context/SecurityContextHolderMTTests.java b/itest/misc/src/integration-test/java/org/springframework/security/context/SecurityContextHolderMTTests.java index 4344a21bdf..20c38ffcd6 100644 --- a/itest/misc/src/integration-test/java/org/springframework/security/context/SecurityContextHolderMTTests.java +++ b/itest/misc/src/integration-test/java/org/springframework/security/context/SecurityContextHolderMTTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,14 +45,14 @@ public class SecurityContextHolderMTTests extends TestCase{ assertThat(new SecurityContextHolder().toString().isTrue() .lastIndexOf("SecurityContextHolder[strategy='org.springframework.security.context.InheritableThreadLocalSecurityContextHolderStrategy'") != -1); loadStartAndWaitForThreads(true, "Main_", NUM_THREADS, false, true); - assertThat(errors).as("Thread errors detected; review log output for details").isEqualTo(0); + assertThat(errors).as("Thread errors detected; review log output for details").isZero(); } public void testSynchronizationGlobal() throws Exception { SecurityContextHolder.clearContext(); SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL); loadStartAndWaitForThreads(true, "Main_", NUM_THREADS, true, false); - assertThat(errors).as("Thread errors detected; review log output for details").isEqualTo(0); + assertThat(errors).as("Thread errors detected; review log output for details").isZero(); } public void testSynchronizationInheritableThreadLocal() @@ -60,14 +60,14 @@ public class SecurityContextHolderMTTests extends TestCase{ SecurityContextHolder.clearContext(); SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL); loadStartAndWaitForThreads(true, "Main_", NUM_THREADS, false, true); - assertThat(errors).as("Thread errors detected; review log output for details").isEqualTo(0); + assertThat(errors).as("Thread errors detected; review log output for details").isZero(); } public void testSynchronizationThreadLocal() throws Exception { SecurityContextHolder.clearContext(); SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_THREADLOCAL); loadStartAndWaitForThreads(true, "Main_", NUM_THREADS, false, false); - assertThat(errors).as("Thread errors detected; review log output for details").isEqualTo(0); + assertThat(errors).as("Thread errors detected; review log output for details").isZero(); } private void startAndRun(Thread[] threads) { diff --git a/openid/src/test/java/org/springframework/security/openid/OpenIDAuthenticationFilterTests.java b/openid/src/test/java/org/springframework/security/openid/OpenIDAuthenticationFilterTests.java index 5421e13b9c..f8fb5b6117 100644 --- a/openid/src/test/java/org/springframework/security/openid/OpenIDAuthenticationFilterTests.java +++ b/openid/src/test/java/org/springframework/security/openid/OpenIDAuthenticationFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -103,7 +103,7 @@ public class OpenIDAuthenticationFilterTests { URI returnTo = new URI(filter.buildReturnToUrl(req)); String query = returnTo.getRawQuery(); assertThat(count(query, '=')).isEqualTo(1); - assertThat(count(query, '&')).isEqualTo(0); + assertThat(count(query, '&')).isZero(); } /** diff --git a/samples/xml/gae/src/test/java/samples/gae/security/AppRoleTests.java b/samples/xml/gae/src/test/java/samples/gae/security/AppRoleTests.java index d26f698f7c..6c9111899a 100644 --- a/samples/xml/gae/src/test/java/samples/gae/security/AppRoleTests.java +++ b/samples/xml/gae/src/test/java/samples/gae/security/AppRoleTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,7 +37,7 @@ public class AppRoleTests { public void bitsAreCorrect() throws Exception { // If this fails, someone has modified the Enum and the Datastore is probably // corrupt! - assertThat(ADMIN.getBit()).isEqualTo(0); + assertThat(ADMIN.getBit()).isZero(); assertThat(NEW_USER.getBit()).isEqualTo(1); assertThat(USER.getBit()).isEqualTo(2); } diff --git a/web/src/test/java/org/springframework/security/web/authentication/preauth/j2ee/WebXmlJ2eeDefinedRolesRetrieverTests.java b/web/src/test/java/org/springframework/security/web/authentication/preauth/j2ee/WebXmlJ2eeDefinedRolesRetrieverTests.java index ebac35d37b..43cf9df5d3 100644 --- a/web/src/test/java/org/springframework/security/web/authentication/preauth/j2ee/WebXmlJ2eeDefinedRolesRetrieverTests.java +++ b/web/src/test/java/org/springframework/security/web/authentication/preauth/j2ee/WebXmlJ2eeDefinedRolesRetrieverTests.java @@ -30,8 +30,8 @@ public class WebXmlJ2eeDefinedRolesRetrieverTests { @Test public void testRole1To4Roles() throws Exception { - List ROLE1TO4_EXPECTED_ROLES = Arrays.asList(new String[] { "Role1", - "Role2", "Role3", "Role4" }); + List ROLE1TO4_EXPECTED_ROLES = Arrays.asList("Role1", + "Role2", "Role3", "Role4"); final Resource webXml = new ClassPathResource("webxml/Role1-4.web.xml"); WebXmlMappableAttributesRetriever rolesRetriever = new WebXmlMappableAttributesRetriever(); @@ -47,11 +47,9 @@ public class WebXmlJ2eeDefinedRolesRetrieverTests { rolesRetriever.afterPropertiesSet(); Set j2eeRoles = rolesRetriever.getMappableAttributes(); - assertThat(j2eeRoles).isNotNull(); - assertThat(j2eeRoles.size()).withFailMessage("J2eeRoles expected size: " + ROLE1TO4_EXPECTED_ROLES.size() - + ", actual size: " + j2eeRoles).hasSize(ROLE1TO4_EXPECTED_ROLES.size()); - assertThat(j2eeRoles).withFailMessage("J2eeRoles expected contents (arbitrary order).isTrue(): " - + ROLE1TO4_EXPECTED_ROLES + ", actual content: " + j2eeRoles).containsAll(ROLE1TO4_EXPECTED_ROLES); + assertThat(j2eeRoles).isNotNull() + .hasSameSizeAs(ROLE1TO4_EXPECTED_ROLES) + .containsAll(ROLE1TO4_EXPECTED_ROLES); } @Test @@ -69,6 +67,6 @@ public class WebXmlJ2eeDefinedRolesRetrieverTests { }); rolesRetriever.afterPropertiesSet(); Set j2eeRoles = rolesRetriever.getMappableAttributes(); - assertThat(j2eeRoles).withFailMessage("actual size: " + j2eeRoles.size() + "J2eeRoles expected size: 0").isEmpty(); + assertThat(j2eeRoles).isEmpty(); } } diff --git a/web/src/test/java/org/springframework/security/web/authentication/rememberme/AbstractRememberMeServicesTests.java b/web/src/test/java/org/springframework/security/web/authentication/rememberme/AbstractRememberMeServicesTests.java index 0bf6b895b5..861d598439 100644 --- a/web/src/test/java/org/springframework/security/web/authentication/rememberme/AbstractRememberMeServicesTests.java +++ b/web/src/test/java/org/springframework/security/web/authentication/rememberme/AbstractRememberMeServicesTests.java @@ -419,7 +419,7 @@ public class AbstractRememberMeServicesTests { Cookie cookie = response.getCookie( AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); - assertThat(cookie.getVersion()).isEqualTo(0); + assertThat(cookie.getVersion()).isZero(); } @Test @@ -451,7 +451,7 @@ public class AbstractRememberMeServicesTests { Cookie returnedCookie = response.getCookie( AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); assertThat(returnedCookie).isNotNull(); - assertThat(returnedCookie.getMaxAge()).isEqualTo(0); + assertThat(returnedCookie.getMaxAge()).isZero(); } // ~ Inner Classes diff --git a/web/src/test/java/org/springframework/security/web/authentication/rememberme/PersistentTokenBasedRememberMeServicesTests.java b/web/src/test/java/org/springframework/security/web/authentication/rememberme/PersistentTokenBasedRememberMeServicesTests.java index 2e7c53abbc..ac9a84d7bf 100644 --- a/web/src/test/java/org/springframework/security/web/authentication/rememberme/PersistentTokenBasedRememberMeServicesTests.java +++ b/web/src/test/java/org/springframework/security/web/authentication/rememberme/PersistentTokenBasedRememberMeServicesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -135,7 +135,7 @@ public class PersistentTokenBasedRememberMeServicesTests { "somepass", "SOME_AUTH")); Cookie returnedCookie = response.getCookie("mycookiename"); assertThat(returnedCookie).isNotNull(); - assertThat(returnedCookie.getMaxAge()).isEqualTo(0); + assertThat(returnedCookie.getMaxAge()).isZero(); // SEC-1280 services.logout(request, response, null); diff --git a/web/src/test/java/org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServicesTests.java b/web/src/test/java/org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServicesTests.java index 574c98789f..136d7197b4 100644 --- a/web/src/test/java/org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServicesTests.java +++ b/web/src/test/java/org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServicesTests.java @@ -136,7 +136,7 @@ public class TokenBasedRememberMeServicesTests { Cookie returnedCookie = response .getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); assertThat(returnedCookie).isNotNull(); - assertThat(returnedCookie.getMaxAge()).isEqualTo(0); + assertThat(returnedCookie.getMaxAge()).isZero(); } @Test @@ -153,7 +153,7 @@ public class TokenBasedRememberMeServicesTests { Cookie returnedCookie = response .getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); assertThat(returnedCookie).isNotNull(); - assertThat(returnedCookie.getMaxAge()).isEqualTo(0); + assertThat(returnedCookie.getMaxAge()).isZero(); } @Test @@ -169,7 +169,7 @@ public class TokenBasedRememberMeServicesTests { Cookie returnedCookie = response .getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); assertThat(returnedCookie).isNotNull(); - assertThat(returnedCookie.getMaxAge()).isEqualTo(0); + assertThat(returnedCookie.getMaxAge()).isZero(); } @Test @@ -190,7 +190,7 @@ public class TokenBasedRememberMeServicesTests { Cookie returnedCookie = response .getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); assertThat(returnedCookie).isNotNull(); - assertThat(returnedCookie.getMaxAge()).isEqualTo(0); + assertThat(returnedCookie.getMaxAge()).isZero(); } @Test @@ -207,7 +207,7 @@ public class TokenBasedRememberMeServicesTests { Cookie returnedCookie = response .getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); assertThat(returnedCookie).isNotNull(); - assertThat(returnedCookie.getMaxAge()).isEqualTo(0); + assertThat(returnedCookie.getMaxAge()).isZero(); } @Test @@ -227,7 +227,7 @@ public class TokenBasedRememberMeServicesTests { Cookie returnedCookie = response .getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); assertThat(returnedCookie).isNotNull(); - assertThat(returnedCookie.getMaxAge()).isEqualTo(0); + assertThat(returnedCookie.getMaxAge()).isZero(); } @Test @@ -270,7 +270,7 @@ public class TokenBasedRememberMeServicesTests { Cookie cookie = response.getCookie(SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY); assertThat(cookie).isNotNull(); - assertThat(cookie.getMaxAge()).isEqualTo(0); + assertThat(cookie.getMaxAge()).isZero(); } @Test diff --git a/web/src/test/java/org/springframework/security/web/csrf/CookieCsrfTokenRepositoryTests.java b/web/src/test/java/org/springframework/security/web/csrf/CookieCsrfTokenRepositoryTests.java index 9a66dafb6e..95307f2570 100644 --- a/web/src/test/java/org/springframework/security/web/csrf/CookieCsrfTokenRepositoryTests.java +++ b/web/src/test/java/org/springframework/security/web/csrf/CookieCsrfTokenRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -106,7 +106,7 @@ public class CookieCsrfTokenRepositoryTests { Cookie tokenCookie = this.response .getCookie(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME); - assertThat(tokenCookie.getMaxAge()).isEqualTo(0); + assertThat(tokenCookie.getMaxAge()).isZero(); assertThat(tokenCookie.getName()) .isEqualTo(CookieCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME); assertThat(tokenCookie.getPath()).isEqualTo(this.request.getContextPath()); diff --git a/web/src/test/java/org/springframework/security/web/jackson2/SavedCookieMixinTests.java b/web/src/test/java/org/springframework/security/web/jackson2/SavedCookieMixinTests.java index 6e23ea1a4e..a477d2f02f 100644 --- a/web/src/test/java/org/springframework/security/web/jackson2/SavedCookieMixinTests.java +++ b/web/src/test/java/org/springframework/security/web/jackson2/SavedCookieMixinTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -97,7 +97,7 @@ public class SavedCookieMixinTests extends AbstractMixinTests { assertThat(savedCookie.getName()).isEqualTo("SESSION"); assertThat(savedCookie.getValue()).isEqualTo("123456789"); assertThat(savedCookie.isSecure()).isEqualTo(false); - assertThat(savedCookie.getVersion()).isEqualTo(0); + assertThat(savedCookie.getVersion()).isZero(); assertThat(savedCookie.getComment()).isNull(); } } diff --git a/web/src/test/java/org/springframework/security/web/util/ThrowableAnalyzerTests.java b/web/src/test/java/org/springframework/security/web/util/ThrowableAnalyzerTests.java index 7994c30e73..55994eaf98 100644 --- a/web/src/test/java/org/springframework/security/web/util/ThrowableAnalyzerTests.java +++ b/web/src/test/java/org/springframework/security/web/util/ThrowableAnalyzerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -168,7 +168,7 @@ public class ThrowableAnalyzerTests { }; assertThat(analyzer.getRegisteredTypes().length).withFailMessage( - "Unexpected number of registered types").isEqualTo(0); + "Unexpected number of registered types").isZero(); Throwable t = this.testTrace[0]; Throwable[] chain = analyzer.determineCauseChain(t);