diff options
Diffstat (limited to 'sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java index c3aa2646c6c..e30d48256a1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java +++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java @@ -20,12 +20,16 @@ package org.sonar.batch.mediumtest; import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.SonarPlugin; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.batch.debt.internal.DefaultDebtModel; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputPath; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.batch.sensor.measure.Measure; import org.sonar.api.config.Settings; @@ -36,6 +40,9 @@ import org.sonar.api.resources.Languages; import org.sonar.batch.bootstrap.PluginsReferential; import org.sonar.batch.bootstrapper.Batch; import org.sonar.batch.bootstrapper.EnvironmentInformation; +import org.sonar.batch.highlighting.SyntaxHighlightingData; +import org.sonar.batch.highlighting.SyntaxHighlightingRule; +import org.sonar.batch.index.ComponentDataCache; import org.sonar.batch.protocol.input.ActiveRule; import org.sonar.batch.protocol.input.GlobalReferentials; import org.sonar.batch.protocol.input.ProjectReferentials; @@ -49,6 +56,9 @@ import org.sonar.batch.scan2.ScanTaskObserver; import org.sonar.batch.settings.SettingsReferential; import org.sonar.core.plugins.DefaultPluginMetadata; import org.sonar.core.plugins.RemotePlugin; +import org.sonar.core.source.SnapshotDataTypes; + +import javax.annotation.CheckForNull; import java.io.File; import java.io.FileReader; @@ -200,13 +210,18 @@ public class BatchMediumTester { } public static class TaskResult implements ScanTaskObserver { + + private static final Logger LOG = LoggerFactory.getLogger(BatchMediumTester.TaskResult.class); + private List<Issue> issues = new ArrayList<Issue>(); private List<Measure> measures = new ArrayList<Measure>(); private List<InputFile> inputFiles = new ArrayList<InputFile>(); private List<InputDir> inputDirs = new ArrayList<InputDir>(); + private Map<InputFile, SyntaxHighlightingData> highlightingPerFile = new HashMap<InputFile, SyntaxHighlightingData>(); @Override public void scanTaskCompleted(ProjectScanContainer container) { + LOG.info("Store analysis results in memory for later assertions in medium test"); for (Issue issue : container.getComponentByType(AnalyzerIssueCache.class).all()) { issues.add(issue); } @@ -223,6 +238,15 @@ public class BatchMediumTester { inputDirs.add((InputDir) inputPath); } } + + ComponentDataCache componentDataCache = container.getComponentByType(ComponentDataCache.class); + for (InputFile file : inputFiles) { + SyntaxHighlightingData highlighting = componentDataCache.getData(((DefaultInputFile) file).key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING); + if (highlighting != null) { + highlightingPerFile.put(file, highlighting); + } + } + } public List<Issue> issues() { @@ -240,6 +264,20 @@ public class BatchMediumTester { public List<InputDir> inputDirs() { return inputDirs; } + + /** + * Get highlighting type at a given position in an inputfile + * @param charIndex 0-based offset in file + */ + @CheckForNull + public HighlightingBuilder.TypeOfText highlightingTypeAt(InputFile file, int charIndex) { + for (SyntaxHighlightingRule sortedRule : highlightingPerFile.get(file).syntaxHighlightingRuleSet()) { + if (sortedRule.getStartPosition() <= charIndex && sortedRule.getEndPosition() > charIndex) { + return HighlightingBuilder.TypeOfText.forCssClass(sortedRule.getTextType()); + } + } + return null; + } } private static class FakeGlobalReferentialsLoader implements GlobalReferentialsLoader { |