]> source.dussan.org Git - sonarqube.git/commitdiff
Assertions should not compare an object to itself
authorPhilippe Perrin <philippe.perrin@sonarsource.com>
Tue, 8 Dec 2020 16:54:55 +0000 (17:54 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 9 Dec 2020 20:07:21 +0000 (20:07 +0000)
server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ResultTest.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/BlameLineTest.java
sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleKeyTest.java
sonar-plugin-api/src/test/java/org/sonar/api/server/profile/BuiltInQualityProfilesDefinitionTest.java
sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java

index 6136a33a15c86afa94746d7280edd96edb9593fe..4c1c396105dc09490cebd88aecbbaf824f117485 100644 (file)
@@ -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());
   }
 }
index 622615132aa7b7673e5a842f768eb0fcb7dcbf25..e039caa3790c70927542fed20623912c1d6f4a27 100644 (file)
@@ -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");
   }
-
 }
index 5b0f5159568cfc75c92847acc611c3ed47711274..713716118a67188e713d81f90d57dafc38838ff8 100644 (file)
@@ -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);
   }
 }
index 53c407de4bc320971fb4c97d60549fc21bbbd3be..57274953db295ffa57f3e07c09ecb3529cf693cd 100644 (file)
@@ -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
index 4fcf716522e811764806ad36820e6735d64c72e2..7e3cc53ecd5dbfed21abc1a64cd1ebbc885ae337 100644 (file)
@@ -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},
       {""}
     };