From 5380a8080eb0d52981739fc3fe5459c66b204f11 Mon Sep 17 00:00:00 2001 From: Philippe Perrin Date: Tue, 8 Dec 2020 17:54:55 +0100 Subject: [PATCH] Assertions should not compare an object to itself --- .../org/sonar/server/issue/ResultTest.java | 53 +++++++++++-------- .../sonar/api/batch/scm/BlameLineTest.java | 32 +++++++---- .../java/org/sonar/api/rule/RuleKeyTest.java | 4 -- .../BuiltInQualityProfilesDefinitionTest.java | 29 +++++----- .../api/server/rule/RulesDefinitionTest.java | 18 +++---- 5 files changed, 76 insertions(+), 60 deletions(-) diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ResultTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ResultTest.java index 6136a33a15c..4c1c396105d 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ResultTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ResultTest.java @@ -64,36 +64,43 @@ public class ResultTest { assertThat(message.l10nParams()[0]).isEqualTo("10"); } + @Test - public void test_text_message() { + public void test_toString() { String errorMessage = "the error"; - Result.Message txtMessage = Result.Message.of(errorMessage); - Result.Message sameMessage = Result.Message.of(errorMessage); - Result.Message otherMessage = Result.Message.of("other"); - - assertThat(txtMessage.toString()).contains(errorMessage); - assertThat(txtMessage).isEqualTo(txtMessage); - assertThat(txtMessage).isEqualTo(sameMessage); - assertThat(txtMessage.hashCode()).isEqualTo(txtMessage.hashCode()); - assertThat(txtMessage.hashCode()).isEqualTo(sameMessage.hashCode()); - assertThat(txtMessage).isNotEqualTo(otherMessage); + Result.Message txtMsg = Result.Message.of(errorMessage); + assertThat(txtMsg.toString()).contains(errorMessage); + assertThat(txtMsg.toString()).isNotEqualTo(errorMessage); + + Result.Message msg = Result.Message.ofL10n("issue.error.123", "10"); + assertThat(msg.toString()).contains("issue.error.123").contains("10"); + assertThat(msg.toString()).isNotEqualTo("issue.error.123"); } @Test - public void test_l10n_message() { + public void test_equals_and_hashCode() { + String errorMessage = "the error"; + Result.Message txtMsg = Result.Message.of(errorMessage); + Result.Message sameTxtMsg = Result.Message.of(errorMessage); + Result.Message otherTxtMessage = Result.Message.of("other"); + + assertThat(txtMsg) + .isEqualTo(txtMsg) + .isEqualTo(sameTxtMsg) + .isNotEqualTo(otherTxtMessage); + Result.Message msg = Result.Message.ofL10n("issue.error.123", "10"); Result.Message sameMsg = Result.Message.ofL10n("issue.error.123", "10"); - Result.Message msg2 = Result.Message.ofL10n("issue.error.123", "200"); - Result.Message msg3 = Result.Message.ofL10n("issue.error.50"); + Result.Message otherMsg1 = Result.Message.ofL10n("issue.error.123", "200"); + Result.Message otherMsg2 = Result.Message.ofL10n("issue.error.50"); - assertThat(msg.toString()).contains("issue.error.123").contains("10"); - assertThat(msg).isEqualTo(msg); - assertThat(msg).isEqualTo(sameMsg); - assertThat(msg.hashCode()).isEqualTo(msg.hashCode()); - assertThat(msg.hashCode()).isEqualTo(sameMsg.hashCode()); - - assertThat(msg).isNotEqualTo(msg2); - assertThat(msg).isNotEqualTo(msg3); - assertThat(msg.text()).isNotEqualTo("issue.error.123"); + assertThat(msg) + .isEqualTo(msg) + .isEqualTo(sameMsg) + .isNotEqualTo(otherMsg1) + .isNotEqualTo(otherMsg2) + .hasSameHashCodeAs(msg) + .hasSameHashCodeAs(sameMsg); + assertThat(msg.hashCode()).isNotEqualTo(otherMsg1.hashCode()); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java index 622615132aa..e039caa3790 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java @@ -19,6 +19,7 @@ */ package org.sonar.api.batch.scm; +import java.util.Calendar; import org.junit.Test; import java.util.Date; @@ -29,23 +30,35 @@ import static org.assertj.core.api.Assertions.assertThat; public class BlameLineTest { @Test - public void testEqualBlameLine() { + public void testBlameLine() { Date date = new Date(); BlameLine line1 = new BlameLine().date(date).revision("1").author("foo"); - BlameLine line1b = new BlameLine().date(date).revision("1").author("foo"); - BlameLine line2 = new BlameLine().date(null).revision("2").author("foo2"); assertThat(line1.author()).isEqualTo("foo"); assertThat(line1.date()).isEqualTo(date); assertThat(line1.revision()).isEqualTo("1"); + assertThat(line1.toString()).isNotEqualTo("foo"); + assertThat(line1.toString()).contains("revision=1,author=foo"); + } - Assertions.assertEquals(line1, line1); - assertThat(line1).isNotEqualTo(null); - assertThat(line1).isEqualTo(line1b); - assertThat(line1.hashCode()).isEqualTo(line1b.hashCode()); - assertThat(line1).isNotEqualTo(line2); + @Test + public void test_equals_and_hashCode() { + Date date = new Date(); + BlameLine line1 = new BlameLine().date(date).revision("1").author("foo"); + BlameLine line2 = new BlameLine().date(date).revision("1").author("foo"); + BlameLine line3 = new BlameLine().date(null).revision("1").author("bar"); + BlameLine line4 = new BlameLine().date(date).revision("2").author("foo"); + BlameLine line5 = new BlameLine().date(date).revision("1").author("bar"); - assertThat(line1.toString()).contains("revision=1,author=foo"); + assertThat(line1) + .isEqualTo(line1) + .isNotEqualTo(null) + .isEqualTo(line2) + .isNotEqualTo(line3) + .hasSameHashCodeAs(line2); + assertThat(line1.hashCode()).isNotEqualTo(line3.hashCode()); + assertThat(line1.hashCode()).isNotEqualTo(line4.hashCode()); + assertThat(line1.hashCode()).isNotEqualTo(line5.hashCode()); } @Test @@ -58,5 +71,4 @@ public class BlameLineTest { assertThat(line2.author()).isNull(); assertThat(line3.author()).isEqualTo("foo3"); } - } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java index 5b0f5159568..713716118a6 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java @@ -129,9 +129,5 @@ public class RuleKeyTest { assertThat(ab).isGreaterThan(aa); assertThat(aa).isLessThan(ab); - assertThat(aa).isNotEqualTo(ab); - assertThat(ab).isNotEqualTo(aa); - assertThat(aa).isEqualTo(aa); - assertThat(ab).isEqualTo(ab); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java index 53c407de4bc..57274953db2 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java @@ -66,26 +66,31 @@ public class BuiltInQualityProfilesDefinitionTest { profile2.done(); NewBuiltInQualityProfile profile3 = c.createBuiltInQualityProfile("Foo1", "xoo2"); profile3.done(); - Assertions.assertEquals(profile1, profile1); - assertThat(profile1).isNotEqualTo(null); - assertThat(profile1.name()).isNotEqualTo("Foo"); + + assertThat(profile1).isNotNull(); assertThat(profile1).isNotEqualTo(profile2); assertThat(profile1).isNotEqualTo(profile3); + assertThat(profile1.hashCode()).isNotEqualTo(profile2.hashCode()); - assertThat(profile1.toString()).isEqualTo("NewBuiltInQualityProfile{name='Foo1', language='xoo', default='false'}"); - assertThat(rule.toString()).isEqualTo("[repository=repo, key=rule]"); + + assertThat(profile1.name()).isNotEqualTo("Foo"); + assertThat(profile1.toString()).hasToString("NewBuiltInQualityProfile{name='Foo1', language='xoo', default='false'}"); + assertThat(rule.toString()).hasToString("[repository=repo, key=rule]"); }); + BuiltInQualityProfile profile1 = profiles.get("xoo").get("Foo1"); BuiltInQualityProfile profile2 = profiles.get("xoo").get("Foo2"); BuiltInQualityProfile profile3 = profiles.get("xoo2").get("Foo1"); - assertThat(profile1).isEqualTo(profile1); - assertThat(profile1).isNotEqualTo(null); - assertThat(profile1.name()).isNotEqualTo("Foo"); - assertThat(profile1).isNotEqualTo(profile2); - assertThat(profile1).isNotEqualTo(profile3); + + assertThat(profile1) + .isNotNull() + .isNotEqualTo(profile2) + .isNotEqualTo(profile3); assertThat(profile1.hashCode()).isNotEqualTo(profile2.hashCode()); - assertThat(profile1.toString()).isEqualTo("BuiltInQualityProfile{name='Foo1', language='xoo', default='false'}"); - assertThat(profile1.rule(RuleKey.of("repo", "rule")).toString()).isEqualTo("[repository=repo, key=rule]"); + + assertThat(profile1.name()).isNotEqualTo("Foo"); + assertThat(profile1.toString()).hasToString("BuiltInQualityProfile{name='Foo1', language='xoo', default='false'}"); + assertThat(profile1.rule(RuleKey.of("repo", "rule")).toString()).hasToString("[repository=repo, key=rule]"); } @Test diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java index 4fcf716522e..7e3cc53ecd5 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java @@ -46,6 +46,7 @@ import org.sonar.api.utils.log.LogTester; import org.sonar.api.impl.server.RulesDefinitionContext; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.useDefaultDateFormatsOnly; import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -83,10 +84,6 @@ public class RulesDefinitionTest { assertThat(checkstyle.name()).isEqualTo("checkstyle"); assertThat(checkstyle.rules()).isEmpty(); assertThat(context.repository("unknown")).isNull(); - - // test equals() and hashCode() - assertThat(findbugs).isEqualTo(findbugs).isNotEqualTo(checkstyle).isNotEqualTo("findbugs").isNotEqualTo(null); - assertThat(findbugs.hashCode()).isEqualTo(findbugs.hashCode()); } @Test @@ -120,6 +117,7 @@ public class RulesDefinitionTest { assertThat(repo.isExternal()).isFalse(); RulesDefinition.Rule rule = repo.rule("NPE"); + assertThat(rule).isNotNull(); assertThat(rule.scope()).isEqualTo(RuleScope.ALL); assertThat(rule.key()).isEqualTo("NPE"); assertThat(rule.name()).isEqualTo("Detect NPE"); @@ -132,7 +130,7 @@ public class RulesDefinitionTest { assertThat(rule.internalKey()).isEqualTo("/something"); assertThat(rule.template()).isFalse(); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); - assertThat(rule.toString()).isEqualTo("[repository=findbugs, key=NPE]"); + assertThat(rule.toString()).hasToString("[repository=findbugs, key=NPE]"); assertThat(rule.repository()).isSameAs(repo); RulesDefinition.Rule otherRule = repo.rule("ABC"); @@ -202,6 +200,7 @@ public class RulesDefinitionTest { assertThat(repo.isExternal()).isTrue(); RulesDefinition.Rule rule = repo.rule("NPE"); + assertThat(rule).isNotNull(); assertThat(rule.scope()).isEqualTo(RuleScope.ALL); assertThat(rule.key()).isEqualTo("NPE"); assertThat(rule.name()).isEqualTo("Detect NPE"); @@ -214,7 +213,7 @@ public class RulesDefinitionTest { assertThat(rule.internalKey()).isEqualTo("/something"); assertThat(rule.template()).isFalse(); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); - assertThat(rule.toString()).isEqualTo("[repository=external_eslint, key=NPE]"); + assertThat(rule.toString()).hasToString("[repository=external_eslint, key=NPE]"); assertThat(rule.repository()).isSameAs(repo); RulesDefinition.Rule otherRule = repo.rule("ABC"); @@ -234,6 +233,7 @@ public class RulesDefinitionTest { assertThat(rule.params()).hasSize(2); RulesDefinition.Param level = rule.param("level"); + assertThat(level).isNotNull(); assertThat(level.key()).isEqualTo("level"); assertThat(level.name()).isEqualTo("Level"); assertThat(level.description()).isEqualTo("The level"); @@ -245,10 +245,6 @@ public class RulesDefinitionTest { assertThat(effort.description()).isNull(); assertThat(effort.defaultValue()).isNull(); assertThat(effort.type()).isEqualTo(RuleParamType.STRING); - - // test equals() and hashCode() - assertThat(level).isEqualTo(level).isNotEqualTo(effort).isNotEqualTo("level").isNotEqualTo(null); - assertThat(level.hashCode()).isEqualTo(level.hashCode()); } @Test @@ -296,7 +292,7 @@ public class RulesDefinitionTest { @DataProvider public static Object[][] nullOrEmpty() { - return new Object[][] { + return new Object[][]{ {null}, {""} }; -- 2.39.5