Search in sources :

Example 1 with SensorContext

use of org.sonar.api.batch.sensor.SensorContext in project sonar-java by SonarSource.

the class SurefireJavaParserTest method should_store_zero_tests_when_directory_is_null_or_non_existing_or_a_file.

@Test
public void should_store_zero_tests_when_directory_is_null_or_non_existing_or_a_file() throws Exception {
    SensorContext context = mock(SensorContext.class);
    context = mock(SensorContext.class);
    parser.collect(context, getDirs("nonExistingReportsDirectory"), false);
    verify(context, never()).newMeasure();
    context = mock(SensorContext.class);
    parser.collect(context, getDirs("file.txt"), true);
    verify(context, never()).newMeasure();
}
Also used : SensorContext(org.sonar.api.batch.sensor.SensorContext) Test(org.junit.Test)

Example 2 with SensorContext

use of org.sonar.api.batch.sensor.SensorContext in project sonar-java by SonarSource.

the class SurefireJavaParserTest method shouldNotInsertZeroOnFiles.

@Test
public void shouldNotInsertZeroOnFiles() throws URISyntaxException {
    SensorContext context = mock(SensorContext.class);
    parser.collect(context, getDirs("noTests"), true);
    verify(context, never()).newMeasure();
}
Also used : SensorContext(org.sonar.api.batch.sensor.SensorContext) Test(org.junit.Test)

Example 3 with SensorContext

use of org.sonar.api.batch.sensor.SensorContext in project sonarqube by SonarSource.

the class OneIssuePerDirectorySensor method analyse.

private static void analyse(SensorContext context) {
    FileSystem fs = context.fileSystem();
    FilePredicates p = fs.predicates();
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
    StreamSupport.stream(fs.inputFiles(p.hasType(Type.MAIN)).spliterator(), false).map(file -> fs.inputDir(file.file().getParentFile())).filter(Objects::nonNull).distinct().forEach(inputDir -> {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(ruleKey).at(newIssue.newLocation().on(inputDir).message("This issue is generated for any non-empty directory")).save();
    });
}
Also used : Objects(java.util.Objects) RuleKey(org.sonar.api.rule.RuleKey) FileSystem(org.sonar.api.batch.fs.FileSystem) Type(org.sonar.api.batch.fs.InputFile.Type) StreamSupport(java.util.stream.StreamSupport) Sensor(org.sonar.api.batch.sensor.Sensor) SensorContext(org.sonar.api.batch.sensor.SensorContext) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) FilePredicates(org.sonar.api.batch.fs.FilePredicates) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) Objects(java.util.Objects)

Example 4 with SensorContext

use of org.sonar.api.batch.sensor.SensorContext in project sonar-java by SonarSource.

the class SurefireJavaParserTest method shouldInsertZeroWhenNoReports.

/**
 * See http://jira.codehaus.org/browse/SONAR-2371
 */
@Test
public void shouldInsertZeroWhenNoReports() throws URISyntaxException {
    SensorContext context = mock(SensorContext.class);
    parser.collect(context, getDirs("noReports"), true);
    verify(context, never()).newMeasure();
}
Also used : SensorContext(org.sonar.api.batch.sensor.SensorContext) Test(org.junit.Test)

Example 5 with SensorContext

use of org.sonar.api.batch.sensor.SensorContext in project sonarqube by SonarSource.

the class JavaCpdBlockIndexerSensor method execute.

@Override
public void execute(SensorContext context) {
    FilePredicates p = context.fileSystem().predicates();
    List<InputFile> sourceFiles = StreamSupport.stream(context.fileSystem().inputFiles(p.and(p.hasType(InputFile.Type.MAIN), p.hasLanguage("java"))).spliterator(), false).filter(f -> !((DefaultInputFile) f).isExcludedForDuplication()).collect(Collectors.toList());
    if (sourceFiles.isEmpty()) {
        return;
    }
    createIndex(sourceFiles);
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) SonarCpdBlockIndex(org.sonar.scanner.cpd.index.SonarCpdBlockIndex) Block(org.sonar.duplications.block.Block) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) LoggerFactory(org.slf4j.LoggerFactory) FilePredicates(org.sonar.api.batch.fs.FilePredicates) BlockChunker(org.sonar.duplications.block.BlockChunker) JavaTokenProducer(org.sonar.duplications.java.JavaTokenProducer) JavaStatementBuilder(org.sonar.duplications.java.JavaStatementBuilder) StreamSupport(java.util.stream.StreamSupport) Phase(org.sonar.api.batch.Phase) Logger(org.slf4j.Logger) StatementChunker(org.sonar.duplications.statement.StatementChunker) IOException(java.io.IOException) Reader(java.io.Reader) ProjectSensor(org.sonar.api.scanner.sensor.ProjectSensor) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) SensorContext(org.sonar.api.batch.sensor.SensorContext) TokenChunker(org.sonar.duplications.token.TokenChunker) FileNotFoundException(java.io.FileNotFoundException) Statement(org.sonar.duplications.statement.Statement) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) List(java.util.List) InputStream(java.io.InputStream) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Aggregations

SensorContext (org.sonar.api.batch.sensor.SensorContext)6 Test (org.junit.Test)3 SensorDescriptor (org.sonar.api.batch.sensor.SensorDescriptor)3 StreamSupport (java.util.stream.StreamSupport)2 FilePredicates (org.sonar.api.batch.fs.FilePredicates)2 InputFile (org.sonar.api.batch.fs.InputFile)2 Sensor (org.sonar.api.batch.sensor.Sensor)2 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)2 RuleKey (org.sonar.api.rule.RuleKey)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Clock (java.time.Clock)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1