diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2021-11-18 16:45:02 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-11-20 20:03:51 +0000 |
commit | 3f9445c25962f914ccdd889c002ddd8e19b0cf89 (patch) | |
tree | eb4f34a62b7164a71fd0bbe34d6f59621a687d8d /sonar-core/src/test | |
parent | aa640be9e2b9cfa2c027ef0768940b31ebb5536c (diff) | |
download | sonarqube-3f9445c25962f914ccdd889c002ddd8e19b0cf89.tar.gz sonarqube-3f9445c25962f914ccdd889c002ddd8e19b0cf89.zip |
Improve code quality
Diffstat (limited to 'sonar-core/src/test')
12 files changed, 59 insertions, 52 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java b/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java index 08bfc56bbad..e7383ec187b 100644 --- a/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java +++ b/sonar-core/src/test/java/org/sonar/core/hash/LineRangeTest.java @@ -43,7 +43,7 @@ public class LineRangeTest { @Test public void check_getters() { LineRange range = new LineRange(1, 2); - assertThat(range.startOffset()).isEqualTo(1); + assertThat(range.startOffset()).isOne(); assertThat(range.endOffset()).isEqualTo(2); } } diff --git a/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java b/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java index 0fa8e152b52..54a2494bee0 100644 --- a/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/hash/SourceLinesHashesComputerTest.java @@ -38,14 +38,14 @@ public class SourceLinesHashesComputerTest { @Test public void hash_of_empty_string_is_empty_string() { - assertThat(hashSingleLine("")).isEqualTo(""); + assertThat(hashSingleLine("")).isEmpty(); } @Test public void tab_and_spaces_are_ignored_from_hash() { - assertThat(hashSingleLine(" ")).isEqualTo(""); - assertThat(hashSingleLine("\t")).isEqualTo(""); - assertThat(hashSingleLine("\t \t \t\t ")).isEqualTo(""); + assertThat(hashSingleLine(" ")).isEmpty(); + assertThat(hashSingleLine("\t")).isEmpty(); + assertThat(hashSingleLine("\t \t \t\t ")).isEmpty(); String abHash = hashSingleLine("ab"); assertThat(hashSingleLine("a b")).isEqualTo(abHash); diff --git a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java index f147fd6fbbb..528d6b91740 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java @@ -182,10 +182,11 @@ public class DefaultIssueTest { DefaultIssue a1 = new DefaultIssue().setKey("AAA"); DefaultIssue a2 = new DefaultIssue().setKey("AAA"); DefaultIssue b = new DefaultIssue().setKey("BBB"); - assertThat(a1).isEqualTo(a1); - assertThat(a1).isEqualTo(a2); - assertThat(a1).isNotEqualTo(b); - assertThat(a1.hashCode()).isEqualTo(a1.hashCode()); + assertThat(a1) + .isEqualTo(a1) + .isEqualTo(a2) + .isNotEqualTo(b) + .hasSameHashCodeAs(a1); } @Test diff --git a/sonar-core/src/test/java/org/sonar/core/issue/tracking/AbstractTrackerTest.java b/sonar-core/src/test/java/org/sonar/core/issue/tracking/AbstractTrackerTest.java index 726da91c4cc..cda34339ffe 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/tracking/AbstractTrackerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/tracking/AbstractTrackerTest.java @@ -125,7 +125,7 @@ public class AbstractTrackerTest { assertThat(k1).isNotEqualTo(k2); assertThat(k2).isNotEqualTo(k1); assertThat(k1).isNotEqualTo(new Object()); - assertThat(k1).isNotEqualTo(null); + assertThat(k1).isNotNull(); } } } diff --git a/sonar-core/src/test/java/org/sonar/core/platform/PluginClassLoaderDefTest.java b/sonar-core/src/test/java/org/sonar/core/platform/PluginClassLoaderDefTest.java index eda637e1527..d50fb85e73e 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/PluginClassLoaderDefTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/PluginClassLoaderDefTest.java @@ -33,8 +33,9 @@ public class PluginClassLoaderDefTest { assertThat(one.equals(one)).isTrue(); assertThat(one.equals(oneBis)).isTrue(); - assertThat(one.hashCode()).isEqualTo(one.hashCode()); - assertThat(one.hashCode()).isEqualTo(oneBis.hashCode()); + assertThat(one) + .hasSameHashCodeAs(one) + .hasSameHashCodeAs(oneBis); assertThat(one.equals(two)).isFalse(); assertThat(one.equals("one")).isFalse(); diff --git a/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java b/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java index 8363d2cdd16..3c326f8ad59 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java @@ -99,8 +99,8 @@ public class PluginInfoTest { assertThat(java1.equals(null)).isFalse(); assertThat(javaNoVersion.equals(javaNoVersion)).isTrue(); - assertThat(java1.hashCode()).isEqualTo(java1.hashCode()); - assertThat(javaNoVersion.hashCode()).isEqualTo(javaNoVersion.hashCode()); + assertThat(java1).hasSameHashCodeAs(java1); + assertThat(javaNoVersion).hasSameHashCodeAs(javaNoVersion); } /** diff --git a/sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java b/sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java index bf57622c075..a1729463b15 100644 --- a/sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java +++ b/sonar-core/src/test/java/org/sonar/core/platform/ServerIdTest.java @@ -248,19 +248,21 @@ public class ServerIdTest { String otherDatasetId = randomAlphabetic(datasetIdLength - 1) + 'b'; ServerId newServerId = ServerId.of(databaseId, datasetId); - assertThat(newServerId).isEqualTo(newServerId); - assertThat(newServerId).isEqualTo(ServerId.of(databaseId, datasetId)); - assertThat(newServerId).isNotEqualTo(new Object()); - assertThat(newServerId).isNotEqualTo(null); - assertThat(newServerId).isNotEqualTo(ServerId.of(otherDatabaseId, datasetId)); - assertThat(newServerId).isNotEqualTo(ServerId.of(databaseId, otherDatasetId)); - assertThat(newServerId).isNotEqualTo(ServerId.of(otherDatabaseId, otherDatasetId)); + assertThat(newServerId) + .isEqualTo(newServerId) + .isEqualTo(ServerId.of(databaseId, datasetId)) + .isNotEqualTo(new Object()) + .isNotNull() + .isNotEqualTo(ServerId.of(otherDatabaseId, datasetId)) + .isNotEqualTo(ServerId.of(databaseId, otherDatasetId)) + .isNotEqualTo(ServerId.of(otherDatabaseId, otherDatasetId)); ServerId oldServerId = ServerId.parse(datasetId); - assertThat(oldServerId).isEqualTo(oldServerId); - assertThat(oldServerId).isEqualTo(ServerId.parse(datasetId)); - assertThat(oldServerId).isNotEqualTo(ServerId.parse(otherDatasetId)); - assertThat(oldServerId).isNotEqualTo(ServerId.of(databaseId, datasetId)); + assertThat(oldServerId) + .isEqualTo(oldServerId) + .isEqualTo(ServerId.parse(datasetId)) + .isNotEqualTo(ServerId.parse(otherDatasetId)) + .isNotEqualTo(ServerId.of(databaseId, datasetId)); } @Test @@ -272,19 +274,22 @@ public class ServerIdTest { String otherDatasetId = randomAlphabetic(datasetIdLength - 1) + 'b'; ServerId newServerId = ServerId.of(databaseId, datasetId); - assertThat(newServerId.hashCode()).isEqualTo(newServerId.hashCode()); - assertThat(newServerId.hashCode()).isEqualTo(ServerId.of(databaseId, datasetId).hashCode()); - assertThat(newServerId.hashCode()).isNotEqualTo(new Object().hashCode()); - assertThat(newServerId.hashCode()).isNotEqualTo(null); - assertThat(newServerId.hashCode()).isNotEqualTo(ServerId.of(otherDatabaseId, datasetId).hashCode()); - assertThat(newServerId.hashCode()).isNotEqualTo(ServerId.of(databaseId, otherDatasetId).hashCode()); - assertThat(newServerId.hashCode()).isNotEqualTo(ServerId.of(otherDatabaseId, otherDatasetId).hashCode()); + assertThat(newServerId) + .hasSameHashCodeAs(newServerId) + .hasSameHashCodeAs(ServerId.of(databaseId, datasetId)); + assertThat(newServerId.hashCode()) + .isNotEqualTo(new Object().hashCode()) + .isNotEqualTo(ServerId.of(otherDatabaseId, datasetId).hashCode()) + .isNotEqualTo(ServerId.of(databaseId, otherDatasetId).hashCode()) + .isNotEqualTo(ServerId.of(otherDatabaseId, otherDatasetId).hashCode()); ServerId oldServerId = ServerId.parse(datasetId); - assertThat(oldServerId.hashCode()).isEqualTo(oldServerId.hashCode()); - assertThat(oldServerId.hashCode()).isEqualTo(ServerId.parse(datasetId).hashCode()); - assertThat(oldServerId.hashCode()).isNotEqualTo(ServerId.parse(otherDatasetId).hashCode()); - assertThat(oldServerId.hashCode()).isNotEqualTo(ServerId.of(databaseId, datasetId).hashCode()); + assertThat(oldServerId) + .hasSameHashCodeAs(oldServerId) + .hasSameHashCodeAs(ServerId.parse(datasetId)); + assertThat(oldServerId.hashCode()) + .isNotEqualTo(ServerId.parse(otherDatasetId).hashCode()) + .isNotEqualTo(ServerId.of(databaseId, datasetId).hashCode()); } @DataProvider diff --git a/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java index d07e489dff2..3ef87dcfbc9 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java @@ -191,7 +191,7 @@ public class DefaultHttpDownloaderTest { new DefaultHttpDownloader(new MapSettings().asConfig()).download(new URI(baseUrl), toFile); assertThat(toFile).exists(); - assertThat(toFile.length()).isGreaterThan(10l); + assertThat(toFile.length()).isGreaterThan(10L); } @Test diff --git a/sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java b/sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java index d83b5781fc2..a5d1f3fde23 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/ProtobufJsonFormatTest.java @@ -86,7 +86,7 @@ public class ProtobufJsonFormatTest { // field is present assertThat(msg.hasStringField()).isTrue(); - assertThat(msg.getStringField()).isEqualTo(""); + assertThat(msg.getStringField()).isEmpty(); assertThat(toJson(msg)).isEqualTo("{\"stringField\":\"\"}"); } diff --git a/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java b/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java index 73bc718b8c0..674e034a823 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java @@ -89,7 +89,7 @@ public class ProtobufTest { CloseableIterator<Fake> it = Protobuf.readStream(file, Fake.parser()); Fake read = it.next(); assertThat(read.getLabel()).isEqualTo("one"); - assertThat(read.getLine()).isEqualTo(1); + assertThat(read.getLine()).isOne(); read = it.next(); assertThat(read.getLabel()).isEqualTo("two"); assertThat(read.hasLine()).isFalse(); diff --git a/sonar-core/src/test/java/org/sonar/core/util/logs/DefaultProfilerTest.java b/sonar-core/src/test/java/org/sonar/core/util/logs/DefaultProfilerTest.java index 86aa1523b63..5cee989900d 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/logs/DefaultProfilerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/logs/DefaultProfilerTest.java @@ -75,7 +75,7 @@ public class DefaultProfilerTest { Thread.sleep(2); assertThat(tester.logs()).containsOnly("Register rules 1"); long timing = underTest.stopTrace(); - assertThat(timing).isGreaterThan(0); + assertThat(timing).isPositive(); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(1)).startsWith("Register rules 1 (done) | time=" + timing); tester.clear(); @@ -85,7 +85,7 @@ public class DefaultProfilerTest { Thread.sleep(2); assertThat(tester.logs()).containsOnly("Register rules"); timing = underTest.stopTrace(); - assertThat(timing).isGreaterThan(0); + assertThat(timing).isPositive(); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(1)).startsWith("Register rules (done) | time=" + timing); tester.clear(); @@ -95,7 +95,7 @@ public class DefaultProfilerTest { Thread.sleep(2); assertThat(tester.logs()).containsOnly("Register rules"); timing = underTest.stopTrace(); - assertThat(timing).isGreaterThan(0); + assertThat(timing).isPositive(); assertThat(tester.logs()).hasSize(2); assertThat(tester.logs().get(1)).startsWith("Register rules (done) | time=" + timing); } diff --git a/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java b/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java index 3bb3ee06e71..29b45eaec50 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java @@ -184,10 +184,10 @@ public class MoreCollectorsTest { @Test public void uniqueIndex_empty_stream_returns_empty_map() { - assertThat(Collections.<MyObj>emptyList().stream().collect(uniqueIndex(MyObj::getId))).isEmpty(); - assertThat(Collections.<MyObj>emptyList().stream().collect(uniqueIndex(MyObj::getId, 6))).isEmpty(); - assertThat(Collections.<MyObj>emptyList().stream().collect(uniqueIndex(MyObj::getId, MyObj::getText))).isEmpty(); - assertThat(Collections.<MyObj>emptyList().stream().collect(uniqueIndex(MyObj::getId, MyObj::getText, 10))).isEmpty(); + assertThat(Stream.<MyObj>empty().collect(uniqueIndex(MyObj::getId))).isEmpty(); + assertThat(Stream.<MyObj>empty().collect(uniqueIndex(MyObj::getId, 6))).isEmpty(); + assertThat(Stream.<MyObj>empty().collect(uniqueIndex(MyObj::getId, MyObj::getText))).isEmpty(); + assertThat(Stream.<MyObj>empty().collect(uniqueIndex(MyObj::getId, MyObj::getText, 10))).isEmpty(); } @Test @@ -369,8 +369,8 @@ public class MoreCollectorsTest { @Test public void index_empty_stream_returns_empty_map() { - assertThat(Collections.<MyObj>emptyList().stream().collect(index(MyObj::getId)).size()).isZero(); - assertThat(Collections.<MyObj>emptyList().stream().collect(index(MyObj::getId, MyObj::getText)).size()).isZero(); + assertThat(Stream.<MyObj>empty().collect(index(MyObj::getId)).size()).isZero(); + assertThat(Stream.<MyObj>empty().collect(index(MyObj::getId, MyObj::getText)).size()).isZero(); } @Test @@ -462,8 +462,8 @@ public class MoreCollectorsTest { @Test public void unorderedIndex_empty_stream_returns_empty_map() { - assertThat(Collections.<MyObj>emptyList().stream().collect(unorderedIndex(MyObj::getId)).size()).isZero(); - assertThat(Collections.<MyObj>emptyList().stream().collect(unorderedIndex(MyObj::getId, MyObj::getText)).size()).isZero(); + assertThat(Stream.<MyObj>empty().collect(unorderedIndex(MyObj::getId)).size()).isZero(); + assertThat(Stream.<MyObj>empty().collect(unorderedIndex(MyObj::getId, MyObj::getText)).size()).isZero(); } @Test @@ -555,7 +555,7 @@ public class MoreCollectorsTest { @Test public void unorderedFlattenIndex_empty_stream_returns_empty_map() { - assertThat(Collections.<MyObj2>emptyList().stream() + assertThat(Stream.<MyObj2>empty() .collect(unorderedFlattenIndex(MyObj2::getId, MyObj2::getTexts)) .size()).isZero(); } @@ -619,7 +619,7 @@ public class MoreCollectorsTest { @Test public void join_on_empty_stream_returns_empty_string() { - assertThat(Collections.emptyList().stream().collect(join(Joiner.on(",")))).isEmpty(); + assertThat(Stream.empty().collect(join(Joiner.on(",")))).isEmpty(); } @Test |