Class SensorContextTester

  • All Implemented Interfaces:
    SensorContext

    public class SensorContextTester
    extends java.lang.Object
    implements 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 Detail

      • isCancelled

        public boolean isCancelled()
        Description copied from interface: SensorContext
        Test if a cancellation of the analysis was requested. Sensors should periodically test this flag and gracefully stop if value is true. For example it could be tested between each processed file.
        Specified by:
        isCancelled in interface SensorContext
      • setCancelled

        public void setCancelled​(boolean cancelled)
      • measures

        public java.util.Collection<Measure> measures​(java.lang.String componentKey)
      • measure

        public <G extends java.io.Serializable> Measure<G> measure​(java.lang.String componentKey,
                                                                   Metric<G> metric)
      • measure

        public <G extends java.io.Serializable> Measure<G> measure​(java.lang.String componentKey,
                                                                   java.lang.String metricKey)
      • allIssues

        public java.util.Collection<Issue> allIssues()
      • allExternalIssues

        public java.util.Collection<ExternalIssue> allExternalIssues()
      • allAdHocRules

        public java.util.Collection<AdHocRule> allAdHocRules()
      • allAnalysisErrors

        public java.util.Collection<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 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<TokensLine> cpdTokens​(java.lang.String componentKey)
      • highlightingTypeAt

        public java.util.List<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<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.
      • markForPublishing

        public void markForPublishing​(InputFile inputFile)
        Description copied from interface: SensorContext
        Indicate that a file should be published in the report sent to SonarQube. Files are automatically marked if any data is created for it (issues, highlighting, coverage, etc.).
        Specified by:
        markForPublishing in interface SensorContext
      • newSignificantCode

        public NewSignificantCode newSignificantCode()
        Description copied from interface: SensorContext
        Builder to declare which parts of the code is significant code. Ranges that are not reported as significant code will be ignored and won't be considered when calculating which lines were modified. If the significant code is not reported for a file, it is assumed that the entire file is significant code.
        Specified by:
        newSignificantCode in interface SensorContext