Search in sources :

Example 1 with SensorContextTester

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

the class VisitorsBridgeTest method rethrow_exception_when_hidden_property_set_to_true.

@Test
public void rethrow_exception_when_hidden_property_set_to_true() {
    NullPointerException npe = new NullPointerException("BimBadaboum");
    JavaFileScanner visitor = c -> {
        throw npe;
    };
    File currentFile = new File("");
    SensorContextTester sensorContextTester = SensorContextTester.create(currentFile);
    SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null, null);
    sonarComponents.setSensorContext(sensorContextTester);
    VisitorsBridge visitorsBridge = new VisitorsBridge(Collections.singleton(visitor), new ArrayList<>(), sonarComponents);
    visitorsBridge.setCurrentFile(currentFile);
    try {
        visitorsBridge.visitFile(null);
        assertThat(sonarComponents.analysisErrors).hasSize(1);
    } catch (Exception e) {
        e.printStackTrace();
        Fail.fail("Exception should be swallowed when property is not set");
    }
    sensorContextTester.settings().appendProperty(SonarComponents.FAIL_ON_EXCEPTION_KEY, "true");
    try {
        visitorsBridge.visitFile(null);
        Fail.fail("scanning of file should have raise an exception");
    } catch (Exception e) {
        assertThat(e).isSameAs(npe);
    }
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ArrayList(java.util.ArrayList) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) RecognitionException(com.sonar.sslr.api.RecognitionException) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) SonarComponents(org.sonar.java.SonarComponents) Nullable(javax.annotation.Nullable) IntFunction(java.util.function.IntFunction) JavaParser(org.sonar.java.ast.parser.JavaParser) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) DecimalFormat(java.text.DecimalFormat) Test(org.junit.Test) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) File(java.io.File) Fail(org.assertj.core.api.Fail) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) SemanticModel(org.sonar.java.resolve.SemanticModel) Collections(java.util.Collections) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) File(java.io.File) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 2 with SensorContextTester

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

the class JavaSquidSensorTest method verify_analysis_errors_are_collected_on_parse_error.

@Test
public void verify_analysis_errors_are_collected_on_parse_error() throws Exception {
    SensorContextTester context = createParseErrorContext();
    context.settings().setProperty(SonarComponents.COLLECT_ANALYSIS_ERRORS_KEY, true);
    executeJavaSquidSensor(context);
    String feedback = context.<String>measure("projectKey", "sonarjava_feedback").value();
    Collection<AnalysisError> analysisErrors = new Gson().fromJson(feedback, new TypeToken<Collection<AnalysisError>>() {
    }.getType());
    assertThat(analysisErrors).hasSize(1);
    AnalysisError analysisError = analysisErrors.iterator().next();
    assertThat(analysisError.getMessage()).startsWith("Parse error at line 6 column 1:");
    assertThat(analysisError.getCause()).startsWith("com.sonar.sslr.api.RecognitionException: Parse error at line 6 column 1:");
    assertThat(analysisError.getFilename()).endsWith("ParseError.java");
    assertThat(analysisError.getKind()).isEqualTo(AnalysisError.Kind.PARSE_ERROR);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) AnalysisError(org.sonar.java.AnalysisError) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 3 with SensorContextTester

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

the class JavaSquidSensorTest method createContext.

private static SensorContextTester createContext(InputFile.Type onType) throws IOException {
    SensorContextTester context = SensorContextTester.create(new File("src/test/java/").getAbsoluteFile());
    DefaultFileSystem fs = context.fileSystem();
    String effectiveKey = "org/sonar/plugins/java/JavaSquidSensorTest.java";
    File file = new File(fs.baseDir(), effectiveKey);
    DefaultInputFile inputFile = new TestInputFileBuilder("", effectiveKey).setLanguage("java").setModuleBaseDir(fs.baseDirPath()).setType(onType).initMetadata(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build();
    fs.add(inputFile);
    return context;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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)

Example 4 with SensorContextTester

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

the class JavaSquidSensorTest method testIssueCreation.

private void testIssueCreation(InputFile.Type onType, int expectedIssues) throws IOException {
    MapSettings settings = new MapSettings();
    NoSonarFilter noSonarFilter = mock(NoSonarFilter.class);
    SensorContextTester context = createContext(onType).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    DefaultFileSystem fs = context.fileSystem();
    SonarComponents sonarComponents = createSonarComponentsMock(context);
    DefaultJavaResourceLocator javaResourceLocator = new DefaultJavaResourceLocator(fs, new JavaClasspath(settings.asConfig(), fs));
    PostAnalysisIssueFilter postAnalysisIssueFilter = new PostAnalysisIssueFilter(fs);
    JavaSquidSensor jss = new JavaSquidSensor(sonarComponents, fs, javaResourceLocator, settings.asConfig(), noSonarFilter, postAnalysisIssueFilter);
    jss.execute(context);
    verify(noSonarFilter, times(1)).noSonarInFile(fs.inputFiles().iterator().next(), Sets.newHashSet(96));
    verify(sonarComponents, times(expectedIssues)).reportIssue(any(AnalyzerMessage.class));
    settings.setProperty(Java.SOURCE_VERSION, "wrongFormat");
    jss.execute(context);
    settings.setProperty(Java.SOURCE_VERSION, "1.7");
    jss.execute(context);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaClasspath(org.sonar.java.JavaClasspath) MapSettings(org.sonar.api.config.internal.MapSettings) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) PostAnalysisIssueFilter(org.sonar.java.filters.PostAnalysisIssueFilter) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) AnalyzerMessage(org.sonar.java.AnalyzerMessage) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem)

Example 5 with SensorContextTester

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

the class JavaSquidSensorTest method createParseErrorContext.

private SensorContextTester createParseErrorContext() throws IOException {
    File file = new File("src/test/files/ParseError.java");
    SensorContextTester context = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
    DefaultInputFile defaultFile = new TestInputFileBuilder(file.getParentFile().getPath(), file.getName()).setLanguage("java").initMetadata(new String(Files.readAllBytes(file.getAbsoluteFile().toPath()), StandardCharsets.UTF_8)).setCharset(StandardCharsets.UTF_8).build();
    context.fileSystem().add(defaultFile);
    return context;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File)

Aggregations

SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)56 Test (org.junit.Test)48 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)35 File (java.io.File)34 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)23 InputFile (org.sonar.api.batch.fs.InputFile)22 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)10 MapSettings (org.sonar.api.config.internal.MapSettings)8 SonarComponents (org.sonar.java.SonarComponents)7 Issue (org.sonar.api.batch.sensor.issue.Issue)5 RecognitionException (com.sonar.sslr.api.RecognitionException)4 Path (java.nio.file.Path)4 JavaCheck (org.sonar.plugins.java.api.JavaCheck)4 Lists (com.google.common.collect.Lists)3 Nullable (javax.annotation.Nullable)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Test (org.junit.jupiter.api.Test)3 RuleKey (org.sonar.api.rule.RuleKey)3 JavaResourceLocator (org.sonar.plugins.java.api.JavaResourceLocator)3