]> source.dussan.org Git - sonarqube.git/commitdiff
[NO-JIRA] Fix reported code smells & bugs
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>
Wed, 26 Oct 2022 13:16:57 +0000 (15:16 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 28 Oct 2022 20:03:23 +0000 (20:03 +0000)
15 files changed:
server/sonar-ce/src/main/java/org/sonar/ce/analysis/cache/cleaning/package-info.java [new file with mode: 0644]
server/sonar-webserver-core/src/main/java/org/sonar/server/rule/SingleDeprecatedRuleKey.java
server/sonar-webserver-core/src/test/java/org/sonar/server/rule/SingleDeprecatedRuleKeyTest.java
sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java
sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java
sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java
sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java
sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java
sonar-duplications/src/test/java/org/sonar/duplications/detector/suffixtree/TextSetTest.java [new file with mode: 0644]
sonar-duplications/src/test/java/org/sonar/duplications/index/CloneGroupTest.java [new file with mode: 0644]
sonar-duplications/src/test/java/org/sonar/duplications/index/ClonePartTest.java [new file with mode: 0644]
sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java
sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java
sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java

diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/analysis/cache/cleaning/package-info.java b/server/sonar-ce/src/main/java/org/sonar/ce/analysis/cache/cleaning/package-info.java
new file mode 100644 (file)
index 0000000..89412a4
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.ce.analysis.cache.cleaning;
+
+import javax.annotation.ParametersAreNonnullByDefault;
index 56dcc37dfe66b34a5d2c6d2567fa15a7dbf2a95a..e5655148c000df72a4a01573af9d7d95b5d1761d 100644 (file)
@@ -106,7 +106,7 @@ class SingleDeprecatedRuleKey {
     if (this == o) {
       return true;
     }
-    if (!(o instanceof SingleDeprecatedRuleKey)) {
+    if (o == null || getClass() != o.getClass()) {
       return false;
     }
     SingleDeprecatedRuleKey that = (SingleDeprecatedRuleKey) o;
index 587c88b35ccd5bd7b50c921f4b3d16cf7ce3cbab..182b52d273ec272d61d177aaad995886c8984d0f 100644 (file)
@@ -119,6 +119,7 @@ public class SingleDeprecatedRuleKeyTest {
     assertThat(singleDeprecatedRuleKey1)
       .isNotNull()
       .isNotEqualTo("")
+      .isNotEqualTo(null)
       .isNotEqualTo(singleDeprecatedRuleKey2);
     assertThat(singleDeprecatedRuleKey2).isNotEqualTo(singleDeprecatedRuleKey1);
 
index 01c1bd268ecf2b82cde6d340f6ad1fa08226bd65..16764bae35bff082a2e378e1145756e65b72b0b8 100644 (file)
@@ -89,7 +89,7 @@ public final class TextSet extends AbstractText {
 
     @Override
     public boolean equals(Object obj) {
-      return (obj instanceof Terminator) && (((Terminator) obj).stringNumber == stringNumber);
+      return obj != null && getClass() == obj.getClass() && ((Terminator) obj).stringNumber == stringNumber;
     }
 
     @Override
index bfc6897e0fd39983451e92dcb23cd73fdffe8461..402682247b26c0f9801098e9289b3c245a25c530 100644 (file)
@@ -131,7 +131,7 @@ public class CloneGroup {
    */
   @Override
   public boolean equals(Object object) {
-    if (!(object instanceof CloneGroup)) {
+    if (object == null || getClass() != object.getClass()) {
       return false;
     }
     CloneGroup another = (CloneGroup) object;
index daae5d9ace96d39594acf7bcd5b1aedc14ab8dd9..204b07effc0aea6e92a39f7b67200cdbe3dc5438 100644 (file)
@@ -60,7 +60,7 @@ public class ClonePart {
 
   @Override
   public boolean equals(Object obj) {
-    if (obj instanceof ClonePart) {
+    if (obj != null && getClass() == obj.getClass()) {
       ClonePart another = (ClonePart) obj;
       return another.resourceId.equals(resourceId)
         && another.startLine == startLine
index bb0b6c37b406ae4d581b8cfeaf70fd38220f765b..d92aebf2de953d30f3817deea911357e030c4433 100644 (file)
@@ -79,7 +79,7 @@ public class Statement {
 
   @Override
   public boolean equals(Object obj) {
-    if (!(obj instanceof Statement)) {
+    if (obj == null || getClass() != obj.getClass()) {
       return false;
     }
     Statement other = (Statement) obj;
index 6f2f3d00e051d3c88c6005c9feb7bfa8841feb22..58a080588d67d829546cefb275287e2ae0e8446f 100644 (file)
@@ -50,7 +50,7 @@ public class Token {
 
   @Override
   public boolean equals(Object object) {
-    if (object instanceof Token) {
+    if (object != null && getClass() == object.getClass()) {
       Token anotherToken = (Token) object;
       return anotherToken.line == line && anotherToken.column == column && anotherToken.value.equals(value);
     }
diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/detector/suffixtree/TextSetTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/detector/suffixtree/TextSetTest.java
new file mode 100644 (file)
index 0000000..1c19aac
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.duplications.detector.suffixtree;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class TextSetTest {
+
+  @Test
+  public void test_terminator_equals() {
+    TextSet.Terminator terminator = new TextSet.Terminator(1);
+
+    assertThat(terminator)
+      .isEqualTo(terminator)
+      .isNotEqualTo(null)
+      .isNotEqualTo(new TextSet.Terminator(0))
+      .isEqualTo(new TextSet.Terminator(1));
+  }
+}
diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/index/CloneGroupTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/index/CloneGroupTest.java
new file mode 100644 (file)
index 0000000..54deaf3
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.duplications.index;
+
+import java.util.List;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CloneGroupTest {
+
+  @Test
+  public void test_equals() {
+    ClonePart clone = new ClonePart("id", 1, 1, 2);
+    CloneGroup group = composeGroup(1, 1, clone, List.of(clone));
+
+    assertThat(group)
+      .isEqualTo(group)
+      .isNotEqualTo(null)
+      .isNotEqualTo(new Object())
+      .isNotEqualTo(composeGroup(1, 1, clone, List.of()))
+      .isNotEqualTo(composeGroup(1, 1, new ClonePart("", 1, 1, 2), List.of(clone)))
+      .isNotEqualTo(composeGroup(0, 1, clone, List.of(clone)))
+      .isEqualTo(composeGroup(1, 1, clone, List.of(new ClonePart("id", 1, 1, 2))));
+  }
+
+  private static CloneGroup composeGroup(int length, int lengthInUnits, ClonePart origin, List<ClonePart> parts) {
+    return  CloneGroup.builder()
+      .setLength(length)
+      .setLengthInUnits(lengthInUnits)
+      .setOrigin(origin)
+      .setParts(parts)
+      .build();
+  }
+}
diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/index/ClonePartTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/index/ClonePartTest.java
new file mode 100644 (file)
index 0000000..a004d01
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.duplications.index;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ClonePartTest {
+
+  @Test
+  public void test_equals() {
+    ClonePart part = new ClonePart("id_1", 1, 2, 3);
+
+    assertThat(part)
+      .isEqualTo(part)
+      .isNotEqualTo(null)
+      .isNotEqualTo(new Object())
+      .isNotEqualTo(new ClonePart("id_1", 1, 2, 0))
+      .isNotEqualTo(new ClonePart("id_1", 1, 0, 3))
+      .isNotEqualTo(new ClonePart("id_1", 0, 2, 3))
+      .isNotEqualTo(new ClonePart("id_2", 1, 2, 3))
+      .isEqualTo(new ClonePart("id_1", 1, 2, 3));
+  }
+}
index c25f565894e62ff8a1cdb49a4ce34aaedd959f42..20517537537e28bfbe96281ac64214531b61abad 100644 (file)
  */
 package org.sonar.duplications.statement;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
 import java.util.ArrayList;
 import java.util.Arrays;
-
 import org.junit.Test;
 import org.sonar.duplications.token.Token;
 
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
 public class StatementTest {
 
   @Test(expected = IllegalArgumentException.class)
@@ -48,4 +48,17 @@ public class StatementTest {
     assertThat(statement.getEndLine(), is(2));
   }
 
+  @Test
+  public void test_equals() {
+    Statement statement = new Statement(1, 2, "value_1");
+
+    assertThat(statement)
+      .isEqualTo(statement)
+      .isNotEqualTo(null)
+      .isNotEqualTo(new Object())
+      .isNotEqualTo(new Statement(1, 2, "value_2"))
+      .isNotEqualTo(new Statement(1, 0, "value_1"))
+      .isNotEqualTo(new Statement(0, 2, "value_1"))
+      .isEqualTo(new Statement(1, 2, "value_1"));
+  }
 }
index 21e22d7c3175f8f3e0d06fc2db3c7c82b70e1d9d..3419864be98762de537619f7065db7b3b9509790 100644 (file)
  */
 package org.sonar.duplications.token;
 
+import org.assertj.core.api.Assertions;
 import org.junit.Test;
 
-import static org.junit.Assert.assertThat;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.not;
-
 public class TokenTest {
 
   @Test
-  public void shouldBeEqual() {
-    Token firstToken = new Token("MyValue", 1, 3);
-    Token secondToken = new Token("MyValue", 1, 3);
-
-    assertThat(firstToken, is(secondToken));
+  public void test_equals() {
+    Token token = new Token("value_1", 1, 2);
+
+    Assertions.assertThat(token)
+      .isEqualTo(token)
+      .isNotEqualTo(null)
+      .isNotEqualTo(new Object())
+      .isNotEqualTo(new Token("value_1", 1, 0))
+      .isNotEqualTo(new Token("value_1", 0, 2))
+      .isNotEqualTo(new Token("value_2", 1, 2))
+      .isEqualTo(new Token("value_1", 1, 2));
   }
-
-  @Test
-  public void shouldNotBeEqual() {
-    Token firstToken = new Token("MyValue", 1, 3);
-    Token secondToken = new Token("MySecondValue", 1, 3);
-    Token thirdToken = new Token("MyValue", 3, 3);
-    
-    assertThat(firstToken, not(is(secondToken)));
-    assertThat(firstToken, not(is(thirdToken)));
-  }
-
 }
index 50a89f72740786b45a9f3cc6ce0b5866778b9c24..7721daadd6d9324550f3fcd78ab5ebfa2896d946 100644 (file)
@@ -20,7 +20,9 @@
 package org.sonar.scanner.ci.vendors;
 
 import java.nio.file.Path;
+import java.util.Optional;
 import org.apache.commons.lang.StringUtils;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.lib.RepositoryBuilder;
@@ -99,10 +101,10 @@ public class Jenkins implements CiVendor {
 
     String refName = "refs/remotes/origin/" + gitBranch;
     try (Repository repo = builder.build()) {
-      Ref ref = repo.exactRef(refName);
-      if (ref != null) {
-        return ref.getObjectId().getName();
-      }
+      return Optional.ofNullable(repo.exactRef(refName))
+        .map(Ref::getObjectId)
+        .map(ObjectId::getName)
+        .orElse(null);
     } catch (Exception e) {
       LOG.debug("Couldn't find git sha1 in '{}': {}", refName, e.getMessage());
     }
index bfcbe5f37267b699ca5c9872b28cda60d50c6ce4..68d1049aeae5bc0d5567c51bd77b8aa9e40c0cba 100644 (file)
@@ -48,6 +48,7 @@ import org.eclipse.jgit.diff.RenameDetector;
 import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.lib.ConfigConstants;
 import org.eclipse.jgit.lib.NullProgressMonitor;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ObjectReader;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
@@ -369,12 +370,10 @@ public class GitScmProvider extends ScmProvider {
   public String revisionId(Path path) {
     RepositoryBuilder builder = getVerifiedRepositoryBuilder(path);
     try {
-      Ref head = getHead(builder.build());
-      if (head == null || head.getObjectId() == null) {
-        // can happen on fresh, empty repos
-        return null;
-      }
-      return head.getObjectId().getName();
+      return Optional.ofNullable(getHead(builder.build()))
+        .map(Ref::getObjectId)
+        .map(ObjectId::getName)
+        .orElse(null);
     } catch (IOException e) {
       throw new IllegalStateException("I/O error while getting revision ID for path: " + path, e);
     }