public class SensorContextTester extends java.lang.Object implements SensorContext
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()
, ...
Modifier and Type | Method and 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 current module, or of the project for a global Sensor.
|
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 current module.
|
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(java.lang.String componentKey,
Metric<G> metric) |
<G extends java.io.Serializable> |
measure(java.lang.String componentKey,
java.lang.String metricKey) |
java.util.Collection<Measure> |
measures(java.lang.String componentKey) |
InputModule |
module() |
NewAdHocRule |
newAdHocRule()
Fluent builder to create a new
AdHocRule . |
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 new
ExternalIssue . |
NewHighlighting |
newHighlighting()
Builder to define highlighting of a file.
|
NewIssue |
newIssue()
Fluent builder to create a new
Issue . |
<G extends java.io.Serializable> |
newMeasure()
Fluent builder to create a new
Measure . |
NewSignificantCode |
newSignificantCode()
Builder to declare which parts of the code is significant code.
|
NewSymbolTable |
newSymbolTable()
Builder to define symbol table of a file.
|
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 runtime
|
SensorContextTester |
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) |
public static SensorContextTester create(java.io.File moduleBaseDir)
public static SensorContextTester create(java.nio.file.Path moduleBaseDir)
public Settings settings()
settings
in interface SensorContext
public Configuration config()
SensorContext
config
in interface SensorContext
public SensorContextTester setSettings(Settings settings)
public DefaultFileSystem fileSystem()
SensorContext
fileSystem
in interface SensorContext
public SensorContextTester setFileSystem(DefaultFileSystem fs)
public ActiveRules activeRules()
SensorContext
activeRules
in interface SensorContext
public SensorContextTester setActiveRules(ActiveRules activeRules)
public Version getSonarQubeVersion()
setRuntime(SonarRuntime)
to test your Sensor behaviour.getSonarQubeVersion
in interface SensorContext
since version 6.0.
public SonarRuntime runtime()
SensorContext
runtime
in interface SensorContext
to override defaults (SonarQube scanner with version
of this API as used at compilation time).
public SensorContextTester setRuntime(SonarRuntime runtime)
public boolean isCancelled()
SensorContext
true
. For example it could be tested between each processed file.isCancelled
in interface SensorContext
public void setCancelled(boolean cancelled)
public InputModule module()
module
in interface SensorContext
public <G extends java.io.Serializable> NewMeasure<G> newMeasure()
SensorContext
Measure
. Don't forget to call NewMeasure.save()
once all parameters are provided.newMeasure
in interface SensorContext
public java.util.Collection<Measure> measures(java.lang.String componentKey)
public <G extends java.io.Serializable> Measure<G> measure(java.lang.String componentKey, Metric<G> metric)
public <G extends java.io.Serializable> Measure<G> measure(java.lang.String componentKey, java.lang.String metricKey)
public NewIssue newIssue()
SensorContext
Issue
. Don't forget to call NewIssue.save()
once all parameters are provided.newIssue
in interface SensorContext
public java.util.Collection<Issue> allIssues()
public NewExternalIssue newExternalIssue()
SensorContext
ExternalIssue
. Don't forget to call NewExternalIssue.save()
once all parameters are provided.newExternalIssue
in interface SensorContext
public NewAdHocRule newAdHocRule()
SensorContext
AdHocRule
. Don't forget to call NewAdHocRule.save()
once all parameters are provided.newAdHocRule
in interface SensorContext
public java.util.Collection<ExternalIssue> allExternalIssues()
public java.util.Collection<AdHocRule> allAdHocRules()
public java.util.Collection<AnalysisError> allAnalysisErrors()
@CheckForNull public java.lang.Integer lineHits(java.lang.String fileKey, int line)
@CheckForNull public static java.lang.Integer sumOrNull(@Nullable java.lang.Integer o1, @Nullable java.lang.Integer o2)
@CheckForNull public java.lang.Integer conditions(java.lang.String fileKey, int line)
@CheckForNull public java.lang.Integer coveredConditions(java.lang.String fileKey, int line)
@CheckForNull public TextRange significantCodeTextRange(java.lang.String fileKey, int line)
@CheckForNull public static java.lang.Integer maxOrNull(@Nullable java.lang.Integer o1, @Nullable java.lang.Integer o2)
@CheckForNull public java.util.List<TokensLine> cpdTokens(java.lang.String componentKey)
public NewHighlighting newHighlighting()
SensorContext
NewHighlighting.save()
once all elements are provided.newHighlighting
in interface SensorContext
public NewCoverage newCoverage()
SensorContext
NewCoverage.save()
.newCoverage
in interface SensorContext
public NewCpdTokens newCpdTokens()
SensorContext
NewCpdTokens.save()
.newCpdTokens
in interface SensorContext
public NewSymbolTable newSymbolTable()
SensorContext
NewSymbolTable.save()
once all symbols are provided.newSymbolTable
in interface SensorContext
public NewAnalysisError newAnalysisError()
SensorContext
NewAnalysisError.save()
.newAnalysisError
in interface SensorContext
public java.util.List<TypeOfText> highlightingTypeAt(java.lang.String componentKey, int line, int lineOffset)
componentKey
- Key of the file like 'myProjectKey:src/foo.php'line
- Line you want to querylineOffset
- Offset you want to query.@CheckForNull public java.util.Collection<TextRange> referencesForSymbolAt(java.lang.String componentKey, int line, int lineOffset)
componentKey
- Key of the file like 'myProjectKey:src/foo.php'line
- Line you want to querylineOffset
- Offset you want to query.public void addContextProperty(java.lang.String key, java.lang.String value)
SensorContext
"sonar.analysis."
are included to the
payload of webhooks.addContextProperty
in interface SensorContext
PostProjectAnalysisTask.ProjectAnalysis.getScannerContext()
public java.util.Map<java.lang.String,java.lang.String> getContextProperties()
SensorContext.addContextProperty(String, String)
.public void markForPublishing(InputFile inputFile)
SensorContext
markForPublishing
in interface SensorContext
public NewSignificantCode newSignificantCode()
SensorContext
newSignificantCode
in interface SensorContext