--- /dev/null
+/*
+ * 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;
if (this == o) {
return true;
}
- if (!(o instanceof SingleDeprecatedRuleKey)) {
+ if (o == null || getClass() != o.getClass()) {
return false;
}
SingleDeprecatedRuleKey that = (SingleDeprecatedRuleKey) o;
assertThat(singleDeprecatedRuleKey1)
.isNotNull()
.isNotEqualTo("")
+ .isNotEqualTo(null)
.isNotEqualTo(singleDeprecatedRuleKey2);
assertThat(singleDeprecatedRuleKey2).isNotEqualTo(singleDeprecatedRuleKey1);
@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
*/
@Override
public boolean equals(Object object) {
- if (!(object instanceof CloneGroup)) {
+ if (object == null || getClass() != object.getClass()) {
return false;
}
CloneGroup another = (CloneGroup) object;
@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
@Override
public boolean equals(Object obj) {
- if (!(obj instanceof Statement)) {
+ if (obj == null || getClass() != obj.getClass()) {
return false;
}
Statement other = (Statement) obj;
@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);
}
--- /dev/null
+/*
+ * 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));
+ }
+}
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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));
+ }
+}
*/
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)
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"));
+ }
}
*/
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)));
- }
-
}
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;
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());
}
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;
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);
}