From 1d44e3b465b8df3ad3a0c63d17b2b24ef529fd79 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 4 Sep 2024 11:46:59 +0200 Subject: [PATCH] SONAR-22914 Move the ScannerMediumTester to testFixtures --- sonar-scanner-engine/build.gradle | 6 ++ .../scanner/mediumtest/AnalysisResult.java | 91 ++++++------------- .../mediumtest/FakeLanguagesLoader.java | 0 .../mediumtest/FakeLanguagesProvider.java | 0 .../mediumtest/FakePluginInstaller.java | 0 .../mediumtest/ScannerMediumTester.java | 0 .../scanner/mediumtest/package-info.java | 23 +++++ 7 files changed, 59 insertions(+), 61 deletions(-) rename sonar-scanner-engine/src/{main => testFixtures}/java/org/sonar/scanner/mediumtest/AnalysisResult.java (74%) rename sonar-scanner-engine/src/{main => testFixtures}/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java (100%) rename sonar-scanner-engine/src/{main => testFixtures}/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java (100%) rename sonar-scanner-engine/src/{main => testFixtures}/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java (100%) rename sonar-scanner-engine/src/{it => testFixtures}/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java (100%) create mode 100644 sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/package-info.java diff --git a/sonar-scanner-engine/build.gradle b/sonar-scanner-engine/build.gradle index 121d7eec295..4a7d8bc12ea 100644 --- a/sonar-scanner-engine/build.gradle +++ b/sonar-scanner-engine/build.gradle @@ -1,3 +1,7 @@ +plugins { + id 'java-test-fixtures' +} + sonar { properties { property 'sonar.projectName', "${projectTitle} :: Scanner Engine" @@ -50,6 +54,8 @@ dependencies { compileOnlyApi 'com.github.spotbugs:spotbugs-annotations' + testFixturesImplementation 'org.junit.jupiter:junit-jupiter-api' + testImplementation 'com.squareup.okhttp3:mockwebserver' testImplementation 'com.squareup.okhttp3:okhttp' testImplementation 'com.squareup.okio:okio' diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/AnalysisResult.java b/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/AnalysisResult.java similarity index 74% rename from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/AnalysisResult.java rename to sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/AnalysisResult.java index c4c8d099225..e3f282977fa 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/AnalysisResult.java +++ b/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/AnalysisResult.java @@ -25,7 +25,10 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Function; import javax.annotation.CheckForNull; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.fs.InputComponent; @@ -50,7 +53,7 @@ public class AnalysisResult implements AnalysisObserver { private static final Logger LOG = LoggerFactory.getLogger(AnalysisResult.class); - private Map inputFilesByKeys = new HashMap<>(); + private final Map inputFilesByKeys = new HashMap<>(); private InputProject project; private ScannerReportReader reader; @@ -85,36 +88,15 @@ public class AnalysisResult implements AnalysisObserver { } public List issuesFor(InputComponent inputComponent) { - return issuesFor(((DefaultInputComponent) inputComponent).scannerId()); + return readFromReport(inputComponent, ScannerReportReader::readComponentIssues); } public List externalIssuesFor(InputComponent inputComponent) { - return externalIssuesFor(((DefaultInputComponent) inputComponent).scannerId()); + return readFromReport(inputComponent, ScannerReportReader::readComponentExternalIssues); } public List issuesFor(Component reportComponent) { - int ref = reportComponent.getRef(); - return issuesFor(ref); - } - - private List issuesFor(int ref) { - List result = new ArrayList<>(); - try (CloseableIterator it = reader.readComponentIssues(ref)) { - while (it.hasNext()) { - result.add(it.next()); - } - } - return result; - } - - private List externalIssuesFor(int ref) { - List result = new ArrayList<>(); - try (CloseableIterator it = reader.readComponentExternalIssues(ref)) { - while (it.hasNext()) { - result.add(it.next()); - } - } - return result; + return readFromReport(reportComponent, ScannerReportReader::readComponentIssues); } public InputProject project() { @@ -132,21 +114,9 @@ public class AnalysisResult implements AnalysisObserver { public Map> allMeasures() { Map> result = new HashMap<>(); - List projectMeasures = new ArrayList<>(); - try (CloseableIterator it = reader.readComponentMeasures(((DefaultInputComponent) project).scannerId())) { - while (it.hasNext()) { - projectMeasures.add(it.next()); - } - } - result.put(project.key(), projectMeasures); + result.put(project.key(), readFromReport(project, ScannerReportReader::readComponentMeasures)); for (InputFile inputFile : inputFilesByKeys.values()) { - List measures = new ArrayList<>(); - try (CloseableIterator it = reader.readComponentMeasures(((DefaultInputComponent) inputFile).scannerId())) { - while (it.hasNext()) { - measures.add(it.next()); - } - } - result.put(inputFile.key(), measures); + result.put(inputFile.key(), readFromReport(inputFile, ScannerReportReader::readComponentMeasures)); } return result; } @@ -202,29 +172,11 @@ public class AnalysisResult implements AnalysisObserver { } public List duplicationsFor(InputFile file) { - List result = new ArrayList<>(); - int ref = ((DefaultInputComponent) file).scannerId(); - try (CloseableIterator it = getReportReader().readComponentDuplications(ref)) { - while (it.hasNext()) { - result.add(it.next()); - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return result; + return readFromReport(file, ScannerReportReader::readComponentDuplications); } public List duplicationBlocksFor(InputFile file) { - List result = new ArrayList<>(); - int ref = ((DefaultInputComponent) file).scannerId(); - try (CloseableIterator it = getReportReader().readCpdTextBlocks(ref)) { - while (it.hasNext()) { - result.add(it.next()); - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return result; + return readFromReport(file, ScannerReportReader::readCpdTextBlocks); } @CheckForNull @@ -244,8 +196,25 @@ public class AnalysisResult implements AnalysisObserver { } public List adHocRules() { - List result = new ArrayList<>(); - try (CloseableIterator it = getReportReader().readAdHocRules()) { + return readFromReport(ScannerReportReader::readAdHocRules); + } + + @NotNull + private List readFromReport(InputComponent component, BiFunction> readerMethod) { + int ref = ((DefaultInputComponent) component).scannerId(); + return readFromReport(r -> readerMethod.apply(r, ref)); + } + + @NotNull + private List readFromReport(Component component, BiFunction> readerMethod) { + int ref = component.getRef(); + return readFromReport(r -> readerMethod.apply(r, ref)); + } + + @NotNull + private List readFromReport(Function> readerMethod) { + List result = new ArrayList<>(); + try (CloseableIterator it = readerMethod.apply(getReportReader())) { while (it.hasNext()) { result.add(it.next()); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java b/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java similarity index 100% rename from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java rename to sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java b/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java similarity index 100% rename from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java rename to sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java b/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java similarity index 100% rename from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java rename to sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java diff --git a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java similarity index 100% rename from sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java rename to sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java diff --git a/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/package-info.java b/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/package-info.java new file mode 100644 index 00000000000..ec4d264d1e0 --- /dev/null +++ b/sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.scanner.mediumtest; + +import javax.annotation.ParametersAreNonnullByDefault; -- 2.39.5