From 873ce42305a4f53449065bb085732fbcb9b071ec Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Thu, 4 Nov 2021 10:20:46 +0100 Subject: [PATCH] SONAR-15574 improving the code quality --- .../ce/task/projectexport/steps/DumpElement.java | 14 +++++++------- .../task/projectexport/steps/DumpWriterImpl.java | 2 +- .../projectexport/steps/ExportSettingsStep.java | 3 +-- .../projectexport/steps/MutableDumpReaderImpl.java | 2 +- .../java/org/sonar/ce/task/util/Protobuf2.java | 4 ++-- .../component/ExportComponentsStepTest.java | 5 ----- .../issue/ExportIssuesChangelogStepTest.java | 2 +- .../sonar/ce/task/projectexport/rule/RuleTest.java | 6 +++--- .../steps/ExportLiveMeasuresStepTest.java | 6 +++--- .../steps/ExportMeasuresStepTest.java | 2 +- 10 files changed, 20 insertions(+), 26 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpElement.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpElement.java index 708fc374688..8f6e6783215 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpElement.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpElement.java @@ -28,7 +28,7 @@ import javax.annotation.concurrent.Immutable; * File element in the dump report. */ @Immutable -public class DumpElement { +public class DumpElement { public static final long NO_DATETIME = 0; public static final DumpElement METADATA = new DumpElement<>("metadata.pb", ProjectDump.Metadata.parser()); @@ -50,9 +50,9 @@ public class DumpElement { public static final DumpElement NEW_CODE_PERIODS = new DumpElement<>("new_code_periods.pb", ProjectDump.NewCodePeriod.parser()); private final String filename; - private final Parser parser; + private final Parser parser; - private DumpElement(String filename, Parser parser) { + private DumpElement(String filename, Parser parser) { this.filename = filename; this.parser = parser; } @@ -61,14 +61,14 @@ public class DumpElement { return filename; } - public Parser parser() { + public Parser parser() { return parser; } public static class IssueDumpElement extends DumpElement { - public final int NO_LINE = 0; - public final double NO_GAP = -1; - public final long NO_EFFORT = -1; + public static final int NO_LINE = 0; + public static final double NO_GAP = -1; + public static final long NO_EFFORT = -1; public IssueDumpElement() { super("issues.pb", ProjectDump.Issue.parser()); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpWriterImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpWriterImpl.java index 3905550a96d..bd24d132063 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpWriterImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/DumpWriterImpl.java @@ -69,7 +69,7 @@ public class DumpWriterImpl implements DumpWriter { } @Override - public StreamWriter newStreamWriter(DumpElement elt) { + public StreamWriter newStreamWriter(DumpElement elt) { checkNotPublished(); File file = new File(rootDir, elt.filename()); return StreamWriterImpl.create(file); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportSettingsStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportSettingsStep.java index 70b74b15f5e..7be3b98bb16 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportSettingsStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/ExportSettingsStep.java @@ -19,7 +19,6 @@ */ package org.sonar.ce.task.projectexport.steps; -import com.google.common.collect.ImmutableSet; import com.sonarsource.governance.projectdump.protobuf.ProjectDump; import java.util.List; import java.util.Set; @@ -41,7 +40,7 @@ public class ExportSettingsStep implements ComputationStep { * These properties are not exported as values depend on environment data that are not * exported within the dump (Quality Gate, users). */ - private static final Set IGNORED_KEYS = ImmutableSet.of("sonar.issues.defaultAssigneeLogin"); + private static final Set IGNORED_KEYS = Set.of("sonar.issues.defaultAssigneeLogin"); private final DbClient dbClient; private final ProjectHolder projectHolder; diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/MutableDumpReaderImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/MutableDumpReaderImpl.java index e6ef3605f42..b9dc5b9929c 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/MutableDumpReaderImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/steps/MutableDumpReaderImpl.java @@ -78,7 +78,7 @@ public class MutableDumpReaderImpl implements MutableDumpReader { } @Override - public MessageStream stream(DumpElement elt) { + public MessageStream stream(DumpElement elt) { File file = new File(tempRootDir(), elt.filename()); checkState(file.exists(), "Missing file: %s", file); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Protobuf2.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Protobuf2.java index ec1ef74aa24..86c8d76d39b 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Protobuf2.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Protobuf2.java @@ -52,7 +52,7 @@ public class Protobuf2 { return this; } - public MSG parseFrom(Parser parser, InputStream input) { + public M parseFrom(Parser parser, InputStream input) { try { return parser.parseFrom(input); } catch (InvalidProtocolBufferException e) { @@ -61,7 +61,7 @@ public class Protobuf2 { } @CheckForNull - public MSG parseDelimitedFrom(Parser parser, InputStream input) { + public M parseDelimitedFrom(Parser parser, InputStream input) { try { return parser.parseDelimitedFrom(input); } catch (InvalidProtocolBufferException e) { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/component/ExportComponentsStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/component/ExportComponentsStepTest.java index 7a0bade945f..c24cd2c7a10 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/component/ExportComponentsStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/component/ExportComponentsStepTest.java @@ -168,9 +168,4 @@ public class ExportComponentsStepTest { assertThat(underTest.getDescription()).isEqualTo("Export components"); } - @Test - public void name() { - System.err.println(new Date(1313272860000L)); - - } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/issue/ExportIssuesChangelogStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/issue/ExportIssuesChangelogStepTest.java index 4a1705280b6..2db984504da 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/issue/ExportIssuesChangelogStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/issue/ExportIssuesChangelogStepTest.java @@ -197,7 +197,7 @@ public class ExportIssuesChangelogStepTest { underTest.execute(new TestComputationStepContext()); ProjectDump.IssueChange issueChange = getSingleMessage(); - assertThat(issueChange.getCreatedAt()).isEqualTo(0L); + assertThat(issueChange.getCreatedAt()).isZero(); } @Test diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/rule/RuleTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/rule/RuleTest.java index f33cff5dcc6..28cab509a77 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/rule/RuleTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/rule/RuleTest.java @@ -64,7 +64,7 @@ public class RuleTest { @Test public void hashcode_is_based_on_repository_and_key() { - assertThat(underTest.hashCode()).isEqualTo(new Rule(SOME_DUMP_UUID, SOME_REPOSITORY, SOME_KEY).hashCode()); + assertThat(underTest).hasSameHashCodeAs(new Rule(SOME_DUMP_UUID, SOME_REPOSITORY, SOME_KEY)); assertThat(underTest.hashCode()).isNotEqualTo(new Rule(SOME_DUMP_UUID, SOME_KEY, SOME_REPOSITORY).hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(new Rule(SOME_DUMP_UUID, "other repository", SOME_KEY).hashCode()); assertThat(underTest.hashCode()).isNotEqualTo(new Rule(SOME_DUMP_UUID, SOME_REPOSITORY, "other key").hashCode()); @@ -72,12 +72,12 @@ public class RuleTest { @Test public void hashcode_ignores_dump_id() { - assertThat(underTest.hashCode()).isEqualTo(new Rule("uuid-8888", SOME_REPOSITORY, SOME_KEY).hashCode()); + assertThat(underTest).hasSameHashCodeAs(new Rule("uuid-8888", SOME_REPOSITORY, SOME_KEY)); } @Test public void toString_displays_all_fields() { - assertThat(underTest.toString()).isEqualTo("Rule{ref='uuid-12334', repository='some repository', key='some key'}"); + assertThat(underTest).hasToString("Rule{ref='uuid-12334', repository='some repository', key='some key'}"); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStepTest.java index b46e772ee77..0ed2e499887 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/ExportLiveMeasuresStepTest.java @@ -89,7 +89,7 @@ public class ExportLiveMeasuresStepTest { .extracting(ProjectDump.LiveMeasure::getMetricRef, m -> m.getDoubleValue().getValue(), ProjectDump.LiveMeasure::hasVariation) .containsOnly(tuple(0, 4711.0d, false)); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported"); - assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid()); + assertThat(metricRepository.getRefByUuid()).containsOnlyKeys(metric.getUuid()); } @Test @@ -161,7 +161,7 @@ public class ExportLiveMeasuresStepTest { "test", 7.0d)); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported"); - assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid()); + assertThat(metricRepository.getRefByUuid()).containsOnlyKeys(metric.getUuid()); } @Test @@ -191,7 +191,7 @@ public class ExportLiveMeasuresStepTest { "", false)); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported"); - assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid()); + assertThat(metricRepository.getRefByUuid()).containsOnlyKeys(metric.getUuid()); } @Test diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/ExportMeasuresStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/ExportMeasuresStepTest.java index 698d1a5be02..251bbc97183 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/ExportMeasuresStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/steps/ExportMeasuresStepTest.java @@ -142,7 +142,7 @@ public class ExportMeasuresStepTest { assertThat(exportedMeasures).extracting(ProjectDump.Measure::getAnalysisUuid).containsOnly(firstAnalysis.getUuid(), secondAnalysis.getUuid()); assertThat(exportedMeasures).extracting(ProjectDump.Measure::getMetricRef).containsOnly(0); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("2 measures exported"); - assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(NCLOC.getUuid()); + assertThat(metricRepository.getRefByUuid()).containsOnlyKeys(NCLOC.getUuid()); } @Test -- 2.39.5