diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2024-09-04 11:46:59 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-09-12 20:02:54 +0000 |
commit | 1d44e3b465b8df3ad3a0c63d17b2b24ef529fd79 (patch) | |
tree | 3b86b40a8d1d656b934dd284f7eadc007c61661f /sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest | |
parent | e56fc5a6aa170161d32e171cf3b499a691924bd2 (diff) | |
download | sonarqube-1d44e3b465b8df3ad3a0c63d17b2b24ef529fd79.tar.gz sonarqube-1d44e3b465b8df3ad3a0c63d17b2b24ef529fd79.zip |
SONAR-22914 Move the ScannerMediumTester to testFixtures
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest')
4 files changed, 0 insertions, 468 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/AnalysisResult.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/AnalysisResult.java deleted file mode 100644 index c4c8d099225..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/AnalysisResult.java +++ /dev/null @@ -1,257 +0,0 @@ -/* - * 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. - */ -package org.sonar.scanner.mediumtest; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.annotation.CheckForNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextPointer; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.DefaultInputComponent; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.highlighting.TypeOfText; -import org.sonar.api.scanner.fs.InputProject; -import org.sonar.core.util.CloseableIterator; -import org.sonar.scanner.protocol.output.FileStructure; -import org.sonar.scanner.protocol.output.ScannerReport; -import org.sonar.scanner.protocol.output.ScannerReport.Component; -import org.sonar.scanner.protocol.output.ScannerReport.Symbol; -import org.sonar.scanner.protocol.output.ScannerReportReader; -import org.sonar.scanner.report.ScannerReportUtils; -import org.sonar.scanner.scan.SpringProjectScanContainer; -import org.sonar.scanner.scan.filesystem.InputComponentStore; - -public class AnalysisResult implements AnalysisObserver { - - private static final Logger LOG = LoggerFactory.getLogger(AnalysisResult.class); - - private Map<String, InputFile> inputFilesByKeys = new HashMap<>(); - private InputProject project; - private ScannerReportReader reader; - - @Override - public void analysisCompleted(SpringProjectScanContainer container) { - LOG.info("Store analysis results in memory for later assertions in medium test"); - FileStructure fileStructure = container.getComponentByType(FileStructure.class); - reader = new ScannerReportReader(fileStructure); - project = container.getComponentByType(InputProject.class); - - storeFs(container); - - } - - public ScannerReportReader getReportReader() { - return reader; - } - - private void storeFs(SpringProjectScanContainer container) { - InputComponentStore inputFileCache = container.getComponentByType(InputComponentStore.class); - for (InputFile inputPath : inputFileCache.inputFiles()) { - inputFilesByKeys.put(((DefaultInputFile) inputPath).getProjectRelativePath(), inputPath); - } - } - - public Component getReportComponent(InputComponent inputComponent) { - return getReportReader().readComponent(((DefaultInputComponent) inputComponent).scannerId()); - } - - public Component getReportComponent(int scannerId) { - return getReportReader().readComponent(scannerId); - } - - public List<ScannerReport.Issue> issuesFor(InputComponent inputComponent) { - return issuesFor(((DefaultInputComponent) inputComponent).scannerId()); - } - - public List<ScannerReport.ExternalIssue> externalIssuesFor(InputComponent inputComponent) { - return externalIssuesFor(((DefaultInputComponent) inputComponent).scannerId()); - } - - 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; - } - - public InputProject project() { - return project; - } - - public Collection<InputFile> inputFiles() { - return inputFilesByKeys.values(); - } - - @CheckForNull - public InputFile inputFile(String relativePath) { - return inputFilesByKeys.get(relativePath); - } - - 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); - 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); - } - return result; - } - - /** - * Get highlighting types at a given position in an inputfile - * - * @param lineOffset 0-based offset in file - */ - public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) { - int ref = ((DefaultInputComponent) file).scannerId(); - if (!reader.hasSyntaxHighlighting(ref)) { - return Collections.emptyList(); - } - TextPointer pointer = file.newPointer(line, lineOffset); - List<TypeOfText> result = new ArrayList<>(); - try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) { - while (it.hasNext()) { - ScannerReport.SyntaxHighlightingRule rule = it.next(); - TextRange ruleRange = toRange(file, rule.getRange()); - if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { - result.add(ScannerReportUtils.toBatchType(rule.getType())); - } - } - } catch (Exception e) { - throw new IllegalStateException("Can't read syntax highlighting for " + file, e); - } - return result; - } - - private static TextRange toRange(InputFile file, ScannerReport.TextRange reportRange) { - return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset())); - } - - /** - * Get list of all start positions of a symbol in an inputfile - * - * @param symbolStartLine 0-based start offset for the symbol in file - * @param symbolStartLineOffset 0-based end offset for the symbol in file - */ - @CheckForNull - public List<ScannerReport.TextRange> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) { - int ref = ((DefaultInputComponent) file).scannerId(); - try (CloseableIterator<Symbol> symbols = getReportReader().readComponentSymbols(ref)) { - while (symbols.hasNext()) { - Symbol symbol = symbols.next(); - if (symbol.getDeclaration().getStartLine() == symbolStartLine && symbol.getDeclaration().getStartOffset() == symbolStartLineOffset) { - return symbol.getReferenceList(); - } - } - } - return Collections.emptyList(); - } - - 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; - } - - 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; - } - - @CheckForNull - public ScannerReport.LineCoverage coverageFor(InputFile file, int line) { - int ref = ((DefaultInputComponent) file).scannerId(); - try (CloseableIterator<ScannerReport.LineCoverage> it = getReportReader().readComponentCoverage(ref)) { - while (it.hasNext()) { - ScannerReport.LineCoverage coverage = it.next(); - if (coverage.getLine() == line) { - return coverage; - } - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return null; - } - - public List<ScannerReport.AdHocRule> adHocRules() { - List<ScannerReport.AdHocRule> result = new ArrayList<>(); - try (CloseableIterator<ScannerReport.AdHocRule> it = getReportReader().readAdHocRules()) { - while (it.hasNext()) { - result.add(it.next()); - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return result; - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java deleted file mode 100644 index f2e2f48d556..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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. - */ -package org.sonar.scanner.mediumtest; - -import java.util.HashMap; -import java.util.Map; -import javax.annotation.Priority; -import org.sonar.api.resources.Languages; -import org.sonar.scanner.repository.language.Language; -import org.sonar.scanner.repository.language.LanguagesLoader; -import org.sonar.scanner.repository.language.SupportedLanguageDto; - -@Priority(1) -public class FakeLanguagesLoader implements LanguagesLoader { - - private final Map<String, Language> languageMap = new HashMap<>(); - - public FakeLanguagesLoader() { - languageMap.put("xoo", new Language(new SupportedLanguageDto("xoo", "xoo", new String[] { ".xoo" }, new String[0]))); - } - - public FakeLanguagesLoader(Languages languages) { - for (org.sonar.api.resources.Language language : languages.all()) { - languageMap.put(language.getKey(), new Language(new SupportedLanguageDto(language.getKey(), language.getName(), language.getFileSuffixes(), language.filenamePatterns()))); - } - } - @Override - public Map<String, Language> load() { - return languageMap; - } - - public void addLanguage(String key, String name, String[] suffixes, String[] filenamePatterns) { - languageMap.put(key, new Language(new SupportedLanguageDto(key, name, suffixes, filenamePatterns))); - } - -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java deleted file mode 100644 index 60097466670..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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. - */ -package org.sonar.scanner.mediumtest; - -import javax.annotation.Priority; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Languages; -import org.springframework.context.annotation.Bean; - -@Priority(1) -public class FakeLanguagesProvider { - - private Languages languages = new Languages(); - - @Bean("Languages") - public Languages provide() { - return this.languages; - } - - public void addLanguage(String key, String name, boolean publishAllFiles) { - this.languages.add(new FakeLanguage(key, name, publishAllFiles)); - } - - private static class FakeLanguage implements Language { - - private final String name; - private final String key; - private final boolean publishAllFiles; - - public FakeLanguage(String key, String name, boolean publishAllFiles) { - this.name = name; - this.key = key; - this.publishAllFiles = publishAllFiles; - } - - @Override - public String getKey() { - return this.key; - } - - @Override - public String getName() { - return this.name; - } - - @Override - public String[] getFileSuffixes() { - return new String[0]; - } - - @Override - public boolean publishAllFiles() { - return this.publishAllFiles; - } - } - - -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java deleted file mode 100644 index a735827e65d..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakePluginInstaller.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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. - */ -package org.sonar.scanner.mediumtest; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import javax.annotation.Priority; -import org.sonar.api.Plugin; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.plugin.PluginType; -import org.sonar.scanner.bootstrap.PluginInstaller; -import org.sonar.scanner.bootstrap.ScannerPlugin; - -@Priority(1) -public class FakePluginInstaller implements PluginInstaller { - - private final Map<String, ScannerPlugin> pluginsByKeys = new HashMap<>(); - private final List<LocalPlugin> mediumTestPlugins = new ArrayList<>(); - private final List<LocalPlugin> optionalMediumTestPlugins = new ArrayList<>(); - - public FakePluginInstaller add(String pluginKey, File jarFile, long lastUpdatedAt) { - pluginsByKeys.put(pluginKey, new ScannerPlugin(pluginKey, lastUpdatedAt, PluginType.BUNDLED, PluginInfo.create(jarFile))); - return this; - } - - public FakePluginInstaller add(String pluginKey, Plugin instance) { - mediumTestPlugins.add(new LocalPlugin(pluginKey, instance, Set.of())); - return this; - } - - public FakePluginInstaller addOptional(String pluginKey, Set<String> requiredForLanguages, Plugin instance) { - optionalMediumTestPlugins.add(new LocalPlugin(pluginKey, instance, requiredForLanguages)); - return this; - } - - @Override - public Map<String, ScannerPlugin> installAllPlugins() { - return pluginsByKeys; - } - - @Override - public Map<String, ScannerPlugin> installRequiredPlugins() { - return pluginsByKeys; - } - - @Override - public Map<String, ScannerPlugin> installPluginsForLanguages(Set<String> languageKeys) { - return pluginsByKeys; - } - - @Override - public List<LocalPlugin> installLocals() { - return mediumTestPlugins; - } - - @Override - public List<LocalPlugin> installOptionalLocals(Set<String> languageKeys) { - return optionalMediumTestPlugins.stream() - .filter(plugin -> languageKeys.stream().anyMatch(lang -> plugin.requiredForLanguages().contains(lang))) - .toList(); - } -} |