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 ofSensorContextwith 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 ActiveRulesactiveRules()Get list of active rules.voidaddContextProperty(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.Integerconditions(java.lang.String fileKey, int line)Configurationconfig()Get settings of the project.java.lang.IntegercoveredConditions(java.lang.String fileKey, int line)java.util.List<TokensLine>cpdTokens(java.lang.String componentKey)static SensorContextTestercreate(java.io.File moduleBaseDir)static SensorContextTestercreate(java.nio.file.Path moduleBaseDir)DefaultFileSystemfileSystem()Get filesystem of the project.java.util.Map<java.lang.String,java.lang.String>getContextProperties()VersiongetSonarQubeVersion()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.booleanisCancelled()Test if a cancellation of the analysis was requested.java.lang.IntegerlineHits(java.lang.String fileKey, int line)voidmarkForPublishing(InputFile inputFile)Indicate that a file should be published in the report sent to SonarQube.static java.lang.IntegermaxOrNull(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)InputModulemodule()NewAdHocRulenewAdHocRule()Fluent builder to create a newAdHocRule.NewAnalysisErrornewAnalysisError()Builder to declare errors that happened while processing a source file.NewCoveragenewCoverage()Builder to define coverage in a file.NewCpdTokensnewCpdTokens()Builder to define CPD tokens in a file.NewExternalIssuenewExternalIssue()Fluent builder to create a newExternalIssue.NewHighlightingnewHighlighting()Builder to define highlighting of a file.NewIssuenewIssue()Fluent builder to create a newIssue.<G extends java.io.Serializable>
NewMeasure<G>newMeasure()Fluent builder to create a newMeasure.NewSignificantCodenewSignificantCode()Builder to declare which parts of the code is significant code.NewSymbolTablenewSymbolTable()Builder to define symbol table of a file.InputProjectproject()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.SonarRuntimeruntime()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 runtimeSensorContextTestersetActiveRules(ActiveRules activeRules)voidsetCancelled(boolean cancelled)SensorContextTestersetFileSystem(DefaultFileSystem fs)SensorContextTestersetRuntime(SonarRuntime runtime)SensorContextTestersetSettings(Settings settings)Settingssettings()TextRangesignificantCodeTextRange(java.lang.String fileKey, int line)static java.lang.IntegersumOrNull(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:
settingsin interfaceSensorContext
-
config
public Configuration config()
Description copied from interface:SensorContextGet settings of the project.- Specified by:
configin interfaceSensorContext
-
setSettings
public SensorContextTester setSettings(Settings settings)
-
fileSystem
public DefaultFileSystem fileSystem()
Description copied from interface:SensorContextGet filesystem of the project.- Specified by:
fileSystemin interfaceSensorContext
-
setFileSystem
public SensorContextTester setFileSystem(DefaultFileSystem fs)
-
activeRules
public ActiveRules activeRules()
Description copied from interface:SensorContextGet list of active rules.- Specified by:
activeRulesin 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:
getSonarQubeVersionin interfaceSensorContext- See Also:
since version 6.0.
-
runtime
public SonarRuntime runtime()
Description copied from interface:SensorContextRuntime 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:
runtimein 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:SensorContextTest 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:
isCancelledin interfaceSensorContext
-
setCancelled
public void setCancelled(boolean cancelled)
-
module
public InputModule module()
- Specified by:
modulein interfaceSensorContext
-
project
public InputProject project()
Description copied from interface:SensorContextThe current project.- Specified by:
projectin interfaceSensorContext
-
newMeasure
public <G extends java.io.Serializable> NewMeasure<G> newMeasure()
Description copied from interface:SensorContextFluent builder to create a newMeasure. Don't forget to callNewMeasure.save()once all parameters are provided.- Specified by:
newMeasurein 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:SensorContextFluent builder to create a newIssue. Don't forget to callNewIssue.save()once all parameters are provided.- Specified by:
newIssuein interfaceSensorContext
-
allIssues
public java.util.Collection<Issue> allIssues()
-
newExternalIssue
public NewExternalIssue newExternalIssue()
Description copied from interface:SensorContextFluent builder to create a newExternalIssue. Don't forget to callNewExternalIssue.save()once all parameters are provided.- Specified by:
newExternalIssuein interfaceSensorContext
-
newAdHocRule
public NewAdHocRule newAdHocRule()
Description copied from interface:SensorContextFluent builder to create a newAdHocRule. Don't forget to callNewAdHocRule.save()once all parameters are provided.- Specified by:
newAdHocRulein 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:SensorContextBuilder to define highlighting of a file. Don't forget to callNewHighlighting.save()once all elements are provided.- Specified by:
newHighlightingin interfaceSensorContext
-
newCoverage
public NewCoverage newCoverage()
Description copied from interface:SensorContextBuilder to define coverage in a file. Don't forget to callNewCoverage.save().- Specified by:
newCoveragein interfaceSensorContext
-
newCpdTokens
public NewCpdTokens newCpdTokens()
Description copied from interface:SensorContextBuilder to define CPD tokens in a file. Don't forget to callNewCpdTokens.save().- Specified by:
newCpdTokensin interfaceSensorContext
-
newSymbolTable
public NewSymbolTable newSymbolTable()
Description copied from interface:SensorContextBuilder to define symbol table of a file. Don't forget to callNewSymbolTable.save()once all symbols are provided.- Specified by:
newSymbolTablein interfaceSensorContext
-
newAnalysisError
public NewAnalysisError newAnalysisError()
Description copied from interface:SensorContextBuilder to declare errors that happened while processing a source file. Don't forget to callNewAnalysisError.save().- Specified by:
newAnalysisErrorin 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:SensorContextAdd 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:
addContextPropertyin 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:SensorContextIndicate 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:
markForPublishingin interfaceSensorContext
-
newSignificantCode
public NewSignificantCode newSignificantCode()
Description copied from interface:SensorContextBuilder 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:
newSignificantCodein interfaceSensorContext
-
-