Class SensorContextTester

  • All Implemented Interfaces:
    org.sonar.api.batch.sensor.SensorContext

    public class SensorContextTester
    extends java.lang.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 Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.sonar.api.batch.rule.ActiveRules activeRules()  
      void addContextProperty​(java.lang.String key, java.lang.String value)  
      java.util.Collection<org.sonar.api.batch.sensor.rule.AdHocRule> allAdHocRules()  
      java.util.Collection<org.sonar.api.batch.sensor.error.AnalysisError> allAnalysisErrors()  
      java.util.Collection<org.sonar.api.batch.sensor.issue.ExternalIssue> allExternalIssues()  
      java.util.Collection<org.sonar.api.batch.sensor.issue.Issue> allIssues()  
      java.lang.Integer conditions​(java.lang.String fileKey, int line)  
      org.sonar.api.config.Configuration config()  
      java.lang.Integer coveredConditions​(java.lang.String fileKey, int line)  
      java.util.List<org.sonar.api.batch.sensor.cpd.internal.TokensLine> cpdTokens​(java.lang.String componentKey)  
      static SensorContextTester create​(java.io.File moduleBaseDir)  
      static SensorContextTester create​(java.nio.file.Path moduleBaseDir)  
      DefaultFileSystem fileSystem()  
      java.util.Map<java.lang.String,​java.lang.String> getContextProperties()  
      org.sonar.api.utils.Version getSonarQubeVersion()
      Default value is the version of this API at compilation time.
      java.util.List<org.sonar.api.batch.sensor.highlighting.TypeOfText> highlightingTypeAt​(java.lang.String componentKey, int line, int lineOffset)
      Return list of syntax highlighting applied for a given position in a file.
      boolean isCancelled()  
      java.lang.Integer lineHits​(java.lang.String fileKey, int line)  
      void markForPublishing​(org.sonar.api.batch.fs.InputFile inputFile)  
      static java.lang.Integer maxOrNull​(java.lang.Integer o1, java.lang.Integer o2)  
      <G extends java.io.Serializable>
      org.sonar.api.batch.sensor.measure.Measure<G>
      measure​(java.lang.String componentKey, java.lang.String metricKey)  
      <G extends java.io.Serializable>
      org.sonar.api.batch.sensor.measure.Measure<G>
      measure​(java.lang.String componentKey, org.sonar.api.measures.Metric<G> metric)  
      java.util.Collection<org.sonar.api.batch.sensor.measure.Measure> measures​(java.lang.String componentKey)  
      org.sonar.api.batch.fs.InputModule module()  
      org.sonar.api.batch.sensor.rule.NewAdHocRule newAdHocRule()  
      org.sonar.api.batch.sensor.error.NewAnalysisError newAnalysisError()  
      org.sonar.api.batch.sensor.coverage.NewCoverage newCoverage()  
      org.sonar.api.batch.sensor.cpd.NewCpdTokens newCpdTokens()  
      org.sonar.api.batch.sensor.issue.NewExternalIssue newExternalIssue()  
      org.sonar.api.batch.sensor.highlighting.NewHighlighting newHighlighting()  
      org.sonar.api.batch.sensor.issue.NewIssue newIssue()  
      <G extends java.io.Serializable>
      org.sonar.api.batch.sensor.measure.NewMeasure<G>
      newMeasure()  
      org.sonar.api.batch.sensor.code.NewSignificantCode newSignificantCode()  
      org.sonar.api.batch.sensor.symbol.NewSymbolTable newSymbolTable()  
      org.sonar.api.scanner.fs.InputProject project()  
      java.util.Collection<org.sonar.api.batch.fs.TextRange> referencesForSymbolAt​(java.lang.String componentKey, int line, int lineOffset)
      Return list of symbol references ranges for the symbol at a given position in a file.
      org.sonar.api.SonarRuntime runtime()  
      SensorContextTester setActiveRules​(org.sonar.api.batch.rule.ActiveRules activeRules)  
      void setCancelled​(boolean cancelled)  
      SensorContextTester setFileSystem​(DefaultFileSystem fs)  
      SensorContextTester setRuntime​(org.sonar.api.SonarRuntime runtime)  
      SensorContextTester setSettings​(org.sonar.api.config.Settings settings)  
      org.sonar.api.config.Settings settings()  
      org.sonar.api.batch.fs.TextRange significantCodeTextRange​(java.lang.String fileKey, int line)  
      static java.lang.Integer sumOrNull​(java.lang.Integer o1, java.lang.Integer o2)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • settings

        public org.sonar.api.config.Settings 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​(org.sonar.api.config.Settings settings)
      • fileSystem

        public DefaultFileSystem fileSystem()
        Specified by:
        fileSystem in interface org.sonar.api.batch.sensor.SensorContext
      • 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
      • isCancelled

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

        public void setCancelled​(boolean cancelled)
      • 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 java.io.Serializable> org.sonar.api.batch.sensor.measure.NewMeasure<G> newMeasure()
        Specified by:
        newMeasure in interface org.sonar.api.batch.sensor.SensorContext
      • measures

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

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

        public <G extends java.io.Serializable> org.sonar.api.batch.sensor.measure.Measure<G> measure​(java.lang.String componentKey,
                                                                                                      java.lang.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 java.util.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 java.util.Collection<org.sonar.api.batch.sensor.issue.ExternalIssue> allExternalIssues()
      • allAdHocRules

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

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

        @CheckForNull
        public java.lang.Integer lineHits​(java.lang.String fileKey,
                                          int line)
      • sumOrNull

        @CheckForNull
        public static java.lang.Integer sumOrNull​(@Nullable
                                                  java.lang.Integer o1,
                                                  @Nullable
                                                  java.lang.Integer o2)
      • conditions

        @CheckForNull
        public java.lang.Integer conditions​(java.lang.String fileKey,
                                            int line)
      • coveredConditions

        @CheckForNull
        public java.lang.Integer coveredConditions​(java.lang.String fileKey,
                                                   int line)
      • significantCodeTextRange

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

        @CheckForNull
        public static java.lang.Integer maxOrNull​(@Nullable
                                                  java.lang.Integer o1,
                                                  @Nullable
                                                  java.lang.Integer o2)
      • cpdTokens

        @CheckForNull
        public java.util.List<org.sonar.api.batch.sensor.cpd.internal.TokensLine> cpdTokens​(java.lang.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 java.util.List<org.sonar.api.batch.sensor.highlighting.TypeOfText> highlightingTypeAt​(java.lang.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 java.util.Collection<org.sonar.api.batch.fs.TextRange> referencesForSymbolAt​(java.lang.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​(java.lang.String key,
                                       java.lang.String value)
        Specified by:
        addContextProperty in interface org.sonar.api.batch.sensor.SensorContext
      • getContextProperties

        public java.util.Map<java.lang.String,​java.lang.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
      • newSignificantCode

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