Class SensorContextTester

java.lang.Object
org.sonar.api.batch.sensor.internal.SensorContextTester
All Implemented Interfaces:
org.sonar.api.batch.sensor.SensorContext

public class SensorContextTester extends Object implements org.sonar.api.batch.sensor.SensorContext
Utility class to help testing Sensor. This is not an API and method signature may evolve.

Usage: call create(File) to create an "in memory" implementation of SensorContext with a filesystem initialized with provided baseDir.

You have to manually register inputFiles using:

   sensorContextTester.fileSystem().add(new DefaultInputFile("myProjectKey", "src/Foo.java")
 .setLanguage("java")
 .initMetadata("public class Foo {\n}"));
 

Then pass it to your Sensor. You can then query elements provided by your sensor using methods allIssues(), ...

  • Method Details

    • create

      public static SensorContextTester create(File moduleBaseDir)
    • create

      public static SensorContextTester create(Path moduleBaseDir)
    • settings

      public MapSettings settings()
      Specified by:
      settings in interface org.sonar.api.batch.sensor.SensorContext
    • config

      public org.sonar.api.config.Configuration config()
      Specified by:
      config in interface org.sonar.api.batch.sensor.SensorContext
    • setSettings

      public SensorContextTester setSettings(MapSettings settings)
    • fileSystem

      public DefaultFileSystem fileSystem()
      Specified by:
      fileSystem in interface org.sonar.api.batch.sensor.SensorContext
    • setFileSystem

      public SensorContextTester setFileSystem(DefaultFileSystem fs)
    • activeRules

      public org.sonar.api.batch.rule.ActiveRules activeRules()
      Specified by:
      activeRules in interface org.sonar.api.batch.sensor.SensorContext
    • setActiveRules

      public SensorContextTester setActiveRules(org.sonar.api.batch.rule.ActiveRules activeRules)
    • getSonarQubeVersion

      public org.sonar.api.utils.Version getSonarQubeVersion()
      Default value is the version of this API at compilation time. You can override it using setRuntime(SonarRuntime) to test your Sensor behaviour.
      Specified by:
      getSonarQubeVersion in interface org.sonar.api.batch.sensor.SensorContext
    • runtime

      public org.sonar.api.SonarRuntime runtime()
      Specified by:
      runtime in interface org.sonar.api.batch.sensor.SensorContext
      See Also:
    • setRuntime

      public SensorContextTester setRuntime(org.sonar.api.SonarRuntime runtime)
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in interface org.sonar.api.batch.sensor.SensorContext
    • setCancelled

      public void setCancelled(boolean cancelled)
    • canSkipUnchangedFiles

      public boolean canSkipUnchangedFiles()
      Specified by:
      canSkipUnchangedFiles in interface org.sonar.api.batch.sensor.SensorContext
    • setCanSkipUnchangedFiles

      public SensorContextTester setCanSkipUnchangedFiles(boolean canSkipUnchangedFiles)
    • module

      public org.sonar.api.batch.fs.InputModule module()
      Specified by:
      module in interface org.sonar.api.batch.sensor.SensorContext
    • project

      public org.sonar.api.scanner.fs.InputProject project()
      Specified by:
      project in interface org.sonar.api.batch.sensor.SensorContext
    • newMeasure

      public <G extends Serializable> org.sonar.api.batch.sensor.measure.NewMeasure<G> newMeasure()
      Specified by:
      newMeasure in interface org.sonar.api.batch.sensor.SensorContext
    • measures

      public Collection<org.sonar.api.batch.sensor.measure.Measure> measures(String componentKey)
    • measure

      public <G extends Serializable> org.sonar.api.batch.sensor.measure.Measure<G> measure(String componentKey, org.sonar.api.measures.Metric<G> metric)
    • measure

      public <G extends Serializable> org.sonar.api.batch.sensor.measure.Measure<G> measure(String componentKey, String metricKey)
    • newIssue

      public org.sonar.api.batch.sensor.issue.NewIssue newIssue()
      Specified by:
      newIssue in interface org.sonar.api.batch.sensor.SensorContext
    • allIssues

      public Collection<org.sonar.api.batch.sensor.issue.Issue> allIssues()
    • newExternalIssue

      public org.sonar.api.batch.sensor.issue.NewExternalIssue newExternalIssue()
      Specified by:
      newExternalIssue in interface org.sonar.api.batch.sensor.SensorContext
    • newAdHocRule

      public org.sonar.api.batch.sensor.rule.NewAdHocRule newAdHocRule()
      Specified by:
      newAdHocRule in interface org.sonar.api.batch.sensor.SensorContext
    • allExternalIssues

      public Collection<org.sonar.api.batch.sensor.issue.ExternalIssue> allExternalIssues()
    • allAdHocRules

      public Collection<org.sonar.api.batch.sensor.rule.AdHocRule> allAdHocRules()
    • allAnalysisErrors

      public Collection<org.sonar.api.batch.sensor.error.AnalysisError> allAnalysisErrors()
    • lineHits

      @CheckForNull public Integer lineHits(String fileKey, int line)
    • sumOrNull

      @CheckForNull public static Integer sumOrNull(@Nullable Integer o1, @Nullable Integer o2)
    • conditions

      @CheckForNull public Integer conditions(String fileKey, int line)
    • coveredConditions

      @CheckForNull public Integer coveredConditions(String fileKey, int line)
    • significantCodeTextRange

      @CheckForNull public org.sonar.api.batch.fs.TextRange significantCodeTextRange(String fileKey, int line)
    • maxOrNull

      @CheckForNull public static Integer maxOrNull(@Nullable Integer o1, @Nullable Integer o2)
    • cpdTokens

      @CheckForNull public List<org.sonar.api.batch.sensor.cpd.internal.TokensLine> cpdTokens(String componentKey)
    • newHighlighting

      public org.sonar.api.batch.sensor.highlighting.NewHighlighting newHighlighting()
      Specified by:
      newHighlighting in interface org.sonar.api.batch.sensor.SensorContext
    • newCoverage

      public org.sonar.api.batch.sensor.coverage.NewCoverage newCoverage()
      Specified by:
      newCoverage in interface org.sonar.api.batch.sensor.SensorContext
    • newCpdTokens

      public org.sonar.api.batch.sensor.cpd.NewCpdTokens newCpdTokens()
      Specified by:
      newCpdTokens in interface org.sonar.api.batch.sensor.SensorContext
    • newSymbolTable

      public org.sonar.api.batch.sensor.symbol.NewSymbolTable newSymbolTable()
      Specified by:
      newSymbolTable in interface org.sonar.api.batch.sensor.SensorContext
    • newAnalysisError

      public org.sonar.api.batch.sensor.error.NewAnalysisError newAnalysisError()
      Specified by:
      newAnalysisError in interface org.sonar.api.batch.sensor.SensorContext
    • highlightingTypeAt

      public List<org.sonar.api.batch.sensor.highlighting.TypeOfText> highlightingTypeAt(String componentKey, int line, int lineOffset)
      Return list of syntax highlighting applied for a given position in a file. The result is a list because in theory you can apply several styles to the same range.
      Parameters:
      componentKey - Key of the file like 'myProjectKey:src/foo.php'
      line - Line you want to query
      lineOffset - Offset you want to query.
      Returns:
      List of styles applied to this position or empty list if there is no highlighting at this position.
    • referencesForSymbolAt

      @CheckForNull public Collection<org.sonar.api.batch.fs.TextRange> referencesForSymbolAt(String componentKey, int line, int lineOffset)
      Return list of symbol references ranges for the symbol at a given position in a file.
      Parameters:
      componentKey - Key of the file like 'myProjectKey:src/foo.php'
      line - Line you want to query
      lineOffset - Offset you want to query.
      Returns:
      List of references for the symbol (potentially empty) or null if there is no symbol at this position.
    • addContextProperty

      public void addContextProperty(String key, String value)
      Specified by:
      addContextProperty in interface org.sonar.api.batch.sensor.SensorContext
    • getContextProperties

      public Map<String,String> getContextProperties()
      Returns:
      an immutable map of the context properties defined with SensorContext.addContextProperty(String, String).
      Since:
      6.1
    • markForPublishing

      public void markForPublishing(org.sonar.api.batch.fs.InputFile inputFile)
      Specified by:
      markForPublishing in interface org.sonar.api.batch.sensor.SensorContext
    • markAsUnchanged

      public void markAsUnchanged(org.sonar.api.batch.fs.InputFile inputFile)
      Specified by:
      markAsUnchanged in interface org.sonar.api.batch.sensor.SensorContext
    • nextCache

      public org.sonar.api.batch.sensor.cache.WriteCache nextCache()
      Specified by:
      nextCache in interface org.sonar.api.batch.sensor.SensorContext
    • setNextCache

      public void setNextCache(org.sonar.api.batch.sensor.cache.WriteCache writeCache)
    • previousCache

      public org.sonar.api.batch.sensor.cache.ReadCache previousCache()
      Specified by:
      previousCache in interface org.sonar.api.batch.sensor.SensorContext
    • setPreviousCache

      public void setPreviousCache(org.sonar.api.batch.sensor.cache.ReadCache cache)
    • isCacheEnabled

      public boolean isCacheEnabled()
      Specified by:
      isCacheEnabled in interface org.sonar.api.batch.sensor.SensorContext
    • setCacheEnabled

      public void setCacheEnabled(boolean enabled)
    • newSignificantCode

      public org.sonar.api.batch.sensor.code.NewSignificantCode newSignificantCode()
      Specified by:
      newSignificantCode in interface org.sonar.api.batch.sensor.SensorContext