Search in sources :

Example 1 with JavaFileScanner

use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.

the class VisitorsBridgeTest method log_only_50_elements.

@Test
public void log_only_50_elements() throws Exception {
    DecimalFormat formatter = new DecimalFormat("00");
    IntFunction<String> classNotFoundName = i -> "NotFound" + formatter.format(i);
    VisitorsBridge visitorsBridge = new VisitorsBridge(Collections.singletonList((JavaFileScanner) context -> {
        assertThat(context.getSemanticModel()).isNotNull();
        ((SemanticModel) context.getSemanticModel()).classesNotFound().addAll(IntStream.range(0, 60).mapToObj(classNotFoundName).collect(Collectors.toList()));
    }), Lists.newArrayList(), null);
    checkFile("Foo.java", "class Foo {}", visitorsBridge);
    visitorsBridge.endOfAnalysis();
    assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Classes not found during the analysis : [" + IntStream.range(0, 50).mapToObj(classNotFoundName).sorted().collect(Collectors.joining(", ")) + ", ...]");
}
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) DecimalFormat(java.text.DecimalFormat) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) Test(org.junit.Test)

Example 2 with JavaFileScanner

use of org.sonar.plugins.java.api.JavaFileScanner 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 3 with JavaFileScanner

use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.

the class JavaAstScannerTest method should_handle_analysis_cancellation.

@Test
public void should_handle_analysis_cancellation() throws Exception {
    JavaFileScanner visitor = spy(new JavaFileScanner() {

        @Override
        public void scanFile(JavaFileScannerContext context) {
        // do nothing
        }
    });
    SonarComponents sonarComponents = mock(SonarComponents.class);
    when(sonarComponents.analysisCancelled()).thenReturn(true);
    JavaAstScanner scanner = new JavaAstScanner(JavaParser.createParser(), sonarComponents);
    scanner.setVisitorBridge(new VisitorsBridge(Lists.newArrayList(visitor), Lists.newArrayList(), sonarComponents));
    scanner.scan(ImmutableList.of(new File("src/test/files/metrics/NoSonar.java")));
    verifyZeroInteractions(visitor);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 4 with JavaFileScanner

use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.

the class JavaCheckVerifier method scanFile.

private static void scanFile(String filename, JavaFileScanner check, JavaCheckVerifier javaCheckVerifier, Collection<File> classpath, boolean withSemantic) {
    JavaFileScanner expectedIssueCollector = new ExpectedIssueCollector(javaCheckVerifier);
    VisitorsBridgeForTests visitorsBridge;
    File file = new File(filename);
    SonarComponents sonarComponents = CheckVerifier.sonarComponents(file);
    if (withSemantic) {
        visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(check, expectedIssueCollector), Lists.newArrayList(classpath), sonarComponents);
    } else {
        visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(check, expectedIssueCollector), sonarComponents);
    }
    JavaAstScanner.scanSingleFileForTests(file, visitorsBridge, javaCheckVerifier.javaVersion);
    VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
    if (testJavaFileScannerContext == null) {
        Fail.fail("Semantic was required but it was not possible to create it. Please checks the logs to find out the reason.");
    }
    javaCheckVerifier.checkIssues(testJavaFileScannerContext.getIssues(), javaCheckVerifier.providedJavaVersion);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) File(java.io.File) VisitorsBridgeForTests(org.sonar.java.model.VisitorsBridgeForTests)

Example 5 with JavaFileScanner

use of org.sonar.plugins.java.api.JavaFileScanner in project sonar-java by SonarSource.

the class JavaCheckVerifier method scanFile.

private void scanFile(String filename, JavaFileScanner[] checks, Collection<File> classpath) {
    List<JavaFileScanner> visitors = new ArrayList<>(Arrays.asList(checks));
    visitors.add(expectations.parser());
    VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(visitors, Lists.newArrayList(classpath), null);
    JavaAstScanner.scanSingleFileForTests(new File(filename), visitorsBridge);
    VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
    checkIssues(testJavaFileScannerContext.getIssues());
}
Also used : JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) ArrayList(java.util.ArrayList) File(java.io.File) VisitorsBridgeForTests(org.sonar.java.model.VisitorsBridgeForTests)

Aggregations

File (java.io.File)5 JavaFileScanner (org.sonar.plugins.java.api.JavaFileScanner)5 SonarComponents (org.sonar.java.SonarComponents)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)3 ImmutableList (com.google.common.collect.ImmutableList)2 Lists (com.google.common.collect.Lists)2 RecognitionException (com.sonar.sslr.api.RecognitionException)2 DecimalFormat (java.text.DecimalFormat)2 Collections (java.util.Collections)2 List (java.util.List)2 IntFunction (java.util.function.IntFunction)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Nullable (javax.annotation.Nullable)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Fail (org.assertj.core.api.Fail)2 Rule (org.junit.Rule)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2