Search in sources :

Example 26 with InputFile

use of org.sonar.api.batch.fs.InputFile in project sonar-java by SonarSource.

the class SonarComponentsTest method add_issue_or_parse_error.

@Test
public void add_issue_or_parse_error() throws Exception {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    SensorContextTester context = SensorContextTester.create(new File(""));
    DefaultFileSystem fileSystem = context.fileSystem();
    File file = new File("file.java");
    TestInputFileBuilder inputFile = new TestInputFileBuilder("", "file.java");
    inputFile.setLines(45);
    int[] linesOffset = new int[45];
    linesOffset[35] = 12;
    linesOffset[42] = 1;
    inputFile.setOriginalLineOffsets(linesOffset);
    inputFile.setLastValidOffset(420);
    fileSystem.add(inputFile.build());
    when(this.checks.ruleKey(any(JavaCheck.class))).thenReturn(mock(RuleKey.class));
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
    sonarComponents.setSensorContext(context);
    sonarComponents.addIssue(file, expectedCheck, -5, "message on wrong line", null);
    sonarComponents.addIssue(file, expectedCheck, 42, "message on line", 1);
    sonarComponents.addIssue(new File("."), expectedCheck, 42, "message on line", 1);
    sonarComponents.addIssue(new File("unknown_file"), expectedCheck, 42, "message on line", 1);
    sonarComponents.reportIssue(new AnalyzerMessage(expectedCheck, file, 35, "other message", 0));
    assertThat(context.allIssues()).hasSize(3);
    RecognitionException parseError = new RecognitionException(new LexerException("parse error"));
    context.setRuntime(SonarRuntimeImpl.forSonarLint(V6_7));
    assertThat(sonarComponents.reportAnalysisError(parseError, file)).isTrue();
    context.setRuntime(SonarRuntimeImpl.forSonarQube(V6_7, SonarQubeSide.SCANNER));
    assertThat(sonarComponents.reportAnalysisError(parseError, file)).isFalse();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) LexerException(com.sonar.sslr.impl.LexerException) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 27 with InputFile

use of org.sonar.api.batch.fs.InputFile in project sonar-java by SonarSource.

the class SonarComponentsTest method fail_on_empty_location.

@Test
public void fail_on_empty_location() {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    RuleKey ruleKey = RuleKey.of("MyRepo", "CustomCheck");
    File file = new File("file.java");
    DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath()).initMetadata("class A {\n" + "  void foo() {\n" + "    System.out.println();\n" + "  }\n" + "}\n").build();
    SensorContextTester context = SensorContextTester.create(new File(""));
    DefaultFileSystem fileSystem = context.fileSystem();
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
    sonarComponents.setSensorContext(context);
    AnalyzerMessage.TextSpan emptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 10);
    AnalyzerMessage analyzerMessageEmptyLocation = new AnalyzerMessage(expectedCheck, file, emptyTextSpan, "message", 0);
    assertThatThrownBy(() -> sonarComponents.reportIssue(analyzerMessageEmptyLocation, ruleKey, inputFile, 0.0)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Issue location should not be empty");
    assertThat(context.allIssues()).isEmpty();
    AnalyzerMessage.TextSpan nonEmptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 15);
    AnalyzerMessage analyzerMessageValidLocation = new AnalyzerMessage(expectedCheck, file, nonEmptyTextSpan, "message", 0);
    sonarComponents.reportIssue(analyzerMessageValidLocation, ruleKey, inputFile, 0.0);
    assertThat(context.allIssues()).isNotEmpty();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 28 with InputFile

use of org.sonar.api.batch.fs.InputFile in project sonar-java by SonarSource.

the class CommonsCollectionsTest method getMetrics.

private Map<String, Double> getMetrics() {
    Map<String, Double> metrics = new HashMap<>();
    for (InputFile inputFile : context.fileSystem().inputFiles()) {
        for (org.sonar.api.batch.sensor.measure.Measure measure : context.measures(inputFile.key())) {
            if (measure.value() != null) {
                String key = measure.metric().key();
                double value = 0;
                try {
                    value = Double.parseDouble("" + measure.value());
                } catch (NumberFormatException nfe) {
                // do nothing
                }
                if (metrics.get(key) == null) {
                    metrics.put(key, value);
                } else {
                    metrics.put(key, metrics.get(key) + value);
                }
            }
        }
    }
    return metrics;
}
Also used : HashMap(java.util.HashMap) InputFile(org.sonar.api.batch.fs.InputFile)

Example 29 with InputFile

use of org.sonar.api.batch.fs.InputFile in project sonar-java by SonarSource.

the class JavaClasspathTest method libraries_should_read_dir_of_class_files.

@Test
public void libraries_should_read_dir_of_class_files() {
    fs = new DefaultFileSystem(new File("src/test/files/"));
    TestInputFileBuilder inputFile = new TestInputFileBuilder("", "foo.java");
    inputFile.setLanguage("java");
    inputFile.setType(InputFile.Type.MAIN);
    fs.add(inputFile.build());
    settings.setProperty(JavaClasspathProperties.SONAR_JAVA_LIBRARIES, "classpath");
    javaClasspath = createJavaClasspath();
    assertThat(javaClasspath.getElements()).hasSize(4);
    settings.setProperty(JavaClasspathProperties.SONAR_JAVA_LIBRARIES, "classpath/");
    javaClasspath = createJavaClasspath();
    assertThat(javaClasspath.getElements()).hasSize(4);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 30 with InputFile

use of org.sonar.api.batch.fs.InputFile in project sonar-java by SonarSource.

the class JavaClasspathTest method classpath_empty_if_only_test_files.

@Test
public void classpath_empty_if_only_test_files() throws Exception {
    fs = new DefaultFileSystem(new File("src/test/files/classpath/"));
    TestInputFileBuilder inputFile = new TestInputFileBuilder("", "plop.java");
    inputFile.setType(InputFile.Type.TEST);
    inputFile.setLanguage("java");
    fs.add(inputFile.build());
    javaClasspath = createJavaClasspath();
    assertThat(javaClasspath.getElements()).isEmpty();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)221 Test (org.junit.Test)120 File (java.io.File)100 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)58 AnalysisResult (org.sonar.scanner.mediumtest.AnalysisResult)32 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)24 FileSystem (org.sonar.api.batch.fs.FileSystem)21 FilePredicates (org.sonar.api.batch.fs.FilePredicates)19 IOException (java.io.IOException)18 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)15 List (java.util.List)14 Test (org.junit.jupiter.api.Test)12 BlameLine (org.sonar.api.batch.scm.BlameLine)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 Before (org.junit.Before)9 Path (java.nio.file.Path)8 HashMap (java.util.HashMap)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8