diff options
15 files changed, 202 insertions, 41 deletions
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 index 00000000000..89412a49d45 --- /dev/null +++ b/server/sonar-ce/src/main/java/org/sonar/ce/analysis/cache/cleaning/package-info.java @@ -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; diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/SingleDeprecatedRuleKey.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/SingleDeprecatedRuleKey.java index 56dcc37dfe6..e5655148c00 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/SingleDeprecatedRuleKey.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/SingleDeprecatedRuleKey.java @@ -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; diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/SingleDeprecatedRuleKeyTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/SingleDeprecatedRuleKeyTest.java index 587c88b35cc..182b52d273e 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/SingleDeprecatedRuleKeyTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/SingleDeprecatedRuleKeyTest.java @@ -119,6 +119,7 @@ public class SingleDeprecatedRuleKeyTest { assertThat(singleDeprecatedRuleKey1) .isNotNull() .isNotEqualTo("") + .isNotEqualTo(null) .isNotEqualTo(singleDeprecatedRuleKey2); assertThat(singleDeprecatedRuleKey2).isNotEqualTo(singleDeprecatedRuleKey1); diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java index 01c1bd268ec..16764bae35b 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java @@ -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 diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java index bfc6897e0fd..402682247b2 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java @@ -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; diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java index daae5d9ace9..204b07effc0 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java @@ -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 diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java b/sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java index bb0b6c37b40..d92aebf2de9 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java @@ -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; diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java b/sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java index 6f2f3d00e05..58a080588d6 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java @@ -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 index 00000000000..1c19aac6de2 --- /dev/null +++ b/sonar-duplications/src/test/java/org/sonar/duplications/detector/suffixtree/TextSetTest.java @@ -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 index 00000000000..54deaf3e479 --- /dev/null +++ b/sonar-duplications/src/test/java/org/sonar/duplications/index/CloneGroupTest.java @@ -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 index 00000000000..a004d014352 --- /dev/null +++ b/sonar-duplications/src/test/java/org/sonar/duplications/index/ClonePartTest.java @@ -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)); + } +} diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java index c25f565894e..20517537537 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/statement/StatementTest.java @@ -19,15 +19,15 @@ */ 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")); + } } diff --git a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java index 21e22d7c317..3419864be98 100644 --- a/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java +++ b/sonar-duplications/src/test/java/org/sonar/duplications/token/TokenTest.java @@ -19,31 +19,22 @@ */ 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))); - } - } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java index 50a89f72740..7721daadd6d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java @@ -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()); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java index bfcbe5f3726..68d1049aeae 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java @@ -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); } |