diff options
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r-- | sonar-scanner-engine/build.gradle | 6 | ||||
-rw-r--r-- | sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/AnalysisResult.java (renamed from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/AnalysisResult.java) | 91 | ||||
-rw-r--r-- | sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java (renamed from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java) | 0 | ||||
-rw-r--r-- | sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java (renamed from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java) | 0 | ||||
-rw-r--r-- | sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java (renamed from sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java) | 0 | ||||
-rw-r--r-- | sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java (renamed from sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java) | 0 | ||||
-rw-r--r-- | sonar-scanner-engine/src/testFixtures/java/org/sonar/scanner/mediumtest/package-info.java | 23 |
7 files changed, 59 insertions, 61 deletions
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 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<String, InputFile> inputFilesByKeys = new HashMap<>(); + private final Map<String, InputFile> inputFilesByKeys = new HashMap<>(); private InputProject project; private ScannerReportReader reader; @@ -85,36 +88,15 @@ public class AnalysisResult implements AnalysisObserver { } public List<ScannerReport.Issue> issuesFor(InputComponent inputComponent) { - return issuesFor(((DefaultInputComponent) inputComponent).scannerId()); + return readFromReport(inputComponent, ScannerReportReader::readComponentIssues); } public List<ScannerReport.ExternalIssue> externalIssuesFor(InputComponent inputComponent) { - return externalIssuesFor(((DefaultInputComponent) inputComponent).scannerId()); + return readFromReport(inputComponent, ScannerReportReader::readComponentExternalIssues); } public List<ScannerReport.Issue> issuesFor(Component reportComponent) { - int ref = reportComponent.getRef(); - return issuesFor(ref); - } - - private List<ScannerReport.Issue> issuesFor(int ref) { - List<ScannerReport.Issue> result = new ArrayList<>(); - try (CloseableIterator<ScannerReport.Issue> it = reader.readComponentIssues(ref)) { - while (it.hasNext()) { - result.add(it.next()); - } - } - return result; - } - - private List<ScannerReport.ExternalIssue> externalIssuesFor(int ref) { - List<ScannerReport.ExternalIssue> result = new ArrayList<>(); - try (CloseableIterator<ScannerReport.ExternalIssue> 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<String, List<ScannerReport.Measure>> allMeasures() { Map<String, List<ScannerReport.Measure>> result = new HashMap<>(); - List<ScannerReport.Measure> projectMeasures = new ArrayList<>(); - try (CloseableIterator<ScannerReport.Measure> 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<ScannerReport.Measure> measures = new ArrayList<>(); - try (CloseableIterator<ScannerReport.Measure> 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<ScannerReport.Duplication> duplicationsFor(InputFile file) { - List<ScannerReport.Duplication> result = new ArrayList<>(); - int ref = ((DefaultInputComponent) file).scannerId(); - try (CloseableIterator<ScannerReport.Duplication> 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<ScannerReport.CpdTextBlock> duplicationBlocksFor(InputFile file) { - List<ScannerReport.CpdTextBlock> result = new ArrayList<>(); - int ref = ((DefaultInputComponent) file).scannerId(); - try (CloseableIterator<ScannerReport.CpdTextBlock> 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<ScannerReport.AdHocRule> adHocRules() { - List<ScannerReport.AdHocRule> result = new ArrayList<>(); - try (CloseableIterator<ScannerReport.AdHocRule> it = getReportReader().readAdHocRules()) { + return readFromReport(ScannerReportReader::readAdHocRules); + } + + @NotNull + private <G> List<G> readFromReport(InputComponent component, BiFunction<ScannerReportReader, Integer, CloseableIterator<G>> readerMethod) { + int ref = ((DefaultInputComponent) component).scannerId(); + return readFromReport(r -> readerMethod.apply(r, ref)); + } + + @NotNull + private <G> List<G> readFromReport(Component component, BiFunction<ScannerReportReader, Integer, CloseableIterator<G>> readerMethod) { + int ref = component.getRef(); + return readFromReport(r -> readerMethod.apply(r, ref)); + } + + @NotNull + private <G> List<G> readFromReport(Function<ScannerReportReader, CloseableIterator<G>> readerMethod) { + List<G> result = new ArrayList<>(); + try (CloseableIterator<G> 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 index f2e2f48d556..f2e2f48d556 100644 --- 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 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 index 60097466670..60097466670 100644 --- 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 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 index a735827e65d..a735827e65d 100644 --- 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 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 index 59eb6290e79..59eb6290e79 100644 --- 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 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; |