Class SensorContextTester
- java.lang.Object
-
- org.sonar.api.batch.sensor.internal.SensorContextTester
-
- All Implemented Interfaces:
SensorContext
public class SensorContextTester extends java.lang.Object implements SensorContext
Utility class to help testingSensor
. This is not an API and method signature may evolve.Usage: call
create(File)
to create an "in memory" implementation ofSensorContext
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 methodsallIssues()
, ...
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ActiveRules
activeRules()
Get list of active rules.void
addContextProperty(java.lang.String key, java.lang.String value)
Add a property to the scanner context.java.util.Collection<AdHocRule>
allAdHocRules()
java.util.Collection<AnalysisError>
allAnalysisErrors()
java.util.Collection<ExternalIssue>
allExternalIssues()
java.util.Collection<Issue>
allIssues()
java.lang.Integer
conditions(java.lang.String fileKey, int line)
Configuration
config()
Get settings of the project.java.lang.Integer
coveredConditions(java.lang.String fileKey, int line)
java.util.List<TokensLine>
cpdTokens(java.lang.String componentKey)
static SensorContextTester
create(java.io.File moduleBaseDir)
static SensorContextTester
create(java.nio.file.Path moduleBaseDir)
DefaultFileSystem
fileSystem()
Get filesystem of the project.java.util.Map<java.lang.String,java.lang.String>
getContextProperties()
Version
getSonarQubeVersion()
Default value is the version of this API at compilation time.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.boolean
isCancelled()
Test if a cancellation of the analysis was requested.java.lang.Integer
lineHits(java.lang.String fileKey, int line)
void
markForPublishing(InputFile inputFile)
Indicate that a file should be published in the report sent to SonarQube.static java.lang.Integer
maxOrNull(java.lang.Integer o1, java.lang.Integer o2)
<G extends java.io.Serializable>
Measure<G>measure(java.lang.String componentKey, java.lang.String metricKey)
<G extends java.io.Serializable>
Measure<G>measure(java.lang.String componentKey, Metric<G> metric)
java.util.Collection<Measure>
measures(java.lang.String componentKey)
InputModule
module()
NewAdHocRule
newAdHocRule()
Fluent builder to create a newAdHocRule
.NewAnalysisError
newAnalysisError()
Builder to declare errors that happened while processing a source file.NewCoverage
newCoverage()
Builder to define coverage in a file.NewCpdTokens
newCpdTokens()
Builder to define CPD tokens in a file.NewExternalIssue
newExternalIssue()
Fluent builder to create a newExternalIssue
.NewHighlighting
newHighlighting()
Builder to define highlighting of a file.NewIssue
newIssue()
Fluent builder to create a newIssue
.<G extends java.io.Serializable>
NewMeasure<G>newMeasure()
Fluent builder to create a newMeasure
.NewSignificantCode
newSignificantCode()
Builder to declare which parts of the code is significant code.NewSymbolTable
newSymbolTable()
Builder to define symbol table of a file.InputProject
project()
The current project.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.SonarRuntime
runtime()
Runtime information, mainly: to be able to have different behaviours between SonarQube and SonarLint to enable new features depending on version of API available at runtimeSensorContextTester
setActiveRules(ActiveRules activeRules)
void
setCancelled(boolean cancelled)
SensorContextTester
setFileSystem(DefaultFileSystem fs)
SensorContextTester
setRuntime(SonarRuntime runtime)
SensorContextTester
setSettings(Settings settings)
Settings
settings()
TextRange
significantCodeTextRange(java.lang.String fileKey, int line)
static java.lang.Integer
sumOrNull(java.lang.Integer o1, java.lang.Integer o2)
-
-
-
Method Detail
-
create
public static SensorContextTester create(java.io.File moduleBaseDir)
-
create
public static SensorContextTester create(java.nio.file.Path moduleBaseDir)
-
settings
public Settings settings()
- Specified by:
settings
in interfaceSensorContext
-
config
public Configuration config()
Description copied from interface:SensorContext
Get settings of the project.- Specified by:
config
in interfaceSensorContext
-
setSettings
public SensorContextTester setSettings(Settings settings)
-
fileSystem
public DefaultFileSystem fileSystem()
Description copied from interface:SensorContext
Get filesystem of the project.- Specified by:
fileSystem
in interfaceSensorContext
-
setFileSystem
public SensorContextTester setFileSystem(DefaultFileSystem fs)
-
activeRules
public ActiveRules activeRules()
Description copied from interface:SensorContext
Get list of active rules.- Specified by:
activeRules
in interfaceSensorContext
-
setActiveRules
public SensorContextTester setActiveRules(ActiveRules activeRules)
-
getSonarQubeVersion
public Version getSonarQubeVersion()
Default value is the version of this API at compilation time. You can override it usingsetRuntime(SonarRuntime)
to test your Sensor behaviour.- Specified by:
getSonarQubeVersion
in interfaceSensorContext
- See Also:
since version 6.0.
-
runtime
public SonarRuntime runtime()
Description copied from interface:SensorContext
Runtime information, mainly:- to be able to have different behaviours between SonarQube and SonarLint
- to enable new features depending on version of API available at runtime
- Specified by:
runtime
in interfaceSensorContext
- See Also:
to override defaults (SonarQube scanner with version of this API as used at compilation time).
-
setRuntime
public SensorContextTester setRuntime(SonarRuntime runtime)
-
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 istrue
. For example it could be tested between each processed file.- Specified by:
isCancelled
in interfaceSensorContext
-
setCancelled
public void setCancelled(boolean cancelled)
-
module
public InputModule module()
- Specified by:
module
in interfaceSensorContext
-
project
public InputProject project()
Description copied from interface:SensorContext
The current project.- Specified by:
project
in interfaceSensorContext
-
newMeasure
public <G extends java.io.Serializable> NewMeasure<G> newMeasure()
Description copied from interface:SensorContext
Fluent builder to create a newMeasure
. Don't forget to callNewMeasure.save()
once all parameters are provided.- Specified by:
newMeasure
in interfaceSensorContext
-
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)
-
newIssue
public NewIssue newIssue()
Description copied from interface:SensorContext
Fluent builder to create a newIssue
. Don't forget to callNewIssue.save()
once all parameters are provided.- Specified by:
newIssue
in interfaceSensorContext
-
allIssues
public java.util.Collection<Issue> allIssues()
-
newExternalIssue
public NewExternalIssue newExternalIssue()
Description copied from interface:SensorContext
Fluent builder to create a newExternalIssue
. Don't forget to callNewExternalIssue.save()
once all parameters are provided.- Specified by:
newExternalIssue
in interfaceSensorContext
-
newAdHocRule
public NewAdHocRule newAdHocRule()
Description copied from interface:SensorContext
Fluent builder to create a newAdHocRule
. Don't forget to callNewAdHocRule.save()
once all parameters are provided.- Specified by:
newAdHocRule
in interfaceSensorContext
-
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)
-
newHighlighting
public NewHighlighting newHighlighting()
Description copied from interface:SensorContext
Builder to define highlighting of a file. Don't forget to callNewHighlighting.save()
once all elements are provided.- Specified by:
newHighlighting
in interfaceSensorContext
-
newCoverage
public NewCoverage newCoverage()
Description copied from interface:SensorContext
Builder to define coverage in a file. Don't forget to callNewCoverage.save()
.- Specified by:
newCoverage
in interfaceSensorContext
-
newCpdTokens
public NewCpdTokens newCpdTokens()
Description copied from interface:SensorContext
Builder to define CPD tokens in a file. Don't forget to callNewCpdTokens.save()
.- Specified by:
newCpdTokens
in interfaceSensorContext
-
newSymbolTable
public NewSymbolTable newSymbolTable()
Description copied from interface:SensorContext
Builder to define symbol table of a file. Don't forget to callNewSymbolTable.save()
once all symbols are provided.- Specified by:
newSymbolTable
in interfaceSensorContext
-
newAnalysisError
public NewAnalysisError newAnalysisError()
Description copied from interface:SensorContext
Builder to declare errors that happened while processing a source file. Don't forget to callNewAnalysisError.save()
.- Specified by:
newAnalysisError
in interfaceSensorContext
-
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 querylineOffset
- 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 querylineOffset
- 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)
Description copied from interface:SensorContext
Add a property to the scanner context. This context is available in Compute Engine when processing the report.
The properties starting with"sonar.analysis."
are included to the payload of webhooks.- Specified by:
addContextProperty
in interfaceSensorContext
- See Also:
PostProjectAnalysisTask.ProjectAnalysis.getScannerContext()
-
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(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 interfaceSensorContext
-
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 interfaceSensorContext
-
-