Search in sources :

Example 6 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class SonarSymbolTableVisitorTest method sonar_symbol_table.

@Test
public void sonar_symbol_table() throws Exception {
    File file = temp.newFile().getAbsoluteFile();
    Files.write(Files.toString(new File("src/test/files/highlighter/SonarSymTable.java"), StandardCharsets.UTF_8).replaceAll("\\r\\n", "\n").replaceAll("\\n", EOL), file, StandardCharsets.UTF_8);
    lines = Files.readLines(file, StandardCharsets.UTF_8);
    String content = Joiner.on(EOL).join(lines);
    fs.add(new TestInputFileBuilder("", file.getName()).initMetadata(content).build());
    JavaAstScanner.scanSingleFileForTests(file, new VisitorsBridge(ImmutableList.of(), sonarComponents.getJavaClasspath(), sonarComponents));
    String componentKey = ":" + file.getName();
    verifyUsages(componentKey, 1, 17, reference(5, 2), reference(9, 10));
    // Example class declaration
    verifyUsages(componentKey, 4, 6);
    verifyUsages(componentKey, 4, 14);
    // list field
    verifyUsages(componentKey, 5, 15, reference(10, 9));
    // Example empty constructor
    verifyUsages(componentKey, 6, 2);
    // Do not reference constructor of class using this() and super() as long as SONAR-5894 is not fixed
    // verify(symboltableBuilder).newReference(any(Symbol.class), eq(offset(7, 5)));
    // Example list constructor
    verifyUsages(componentKey, 9, 2, reference(7, 4));
    // list local var
    verifyUsages(componentKey, 9, 23, reference(10, 16));
    // method
    verifyUsages(componentKey, 12, 6);
    // label
    verifyUsages(componentKey, 13, 4);
    // Enum
    verifyUsages(componentKey, 16, 7);
    verifyUsages(componentKey, 17, 5);
    // Do not reference constructor of enum as it can leads to failure in analysis as long as SONAR-5894 is not fixed
    // verify(symboltableBuilder).newReference(any(Symbol.class), eq(offset(14, 5)));
    verifyUsages(componentKey, 18, 4, reference(17, 4));
    verifyUsages(componentKey, 21, 3, reference(21, 19));
    verifyUsages(componentKey, 21, 11);
    verifyUsages(componentKey, 21, 21);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) VisitorsBridge(org.sonar.java.model.VisitorsBridge) File(java.io.File) Test(org.junit.Test)

Example 7 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class MethodJavaSymbolTest method test.

@Test
public void test() {
    File bytecodeDir = new File("target/test-classes");
    MethodVisitor methodVisitor = new MethodVisitor(Sets.newHashSet(28, 32, 40, 44, 46, 56, 72, 76, 84, 89, 91, 98, 100, 102), new HashSet<Integer>());
    JavaAstScanner.scanSingleFileForTests(new File("src/test/java/org/sonar/java/resolve/targets/MethodSymbols.java"), new VisitorsBridge(Collections.singleton(methodVisitor), Lists.newArrayList(bytecodeDir), null));
}
Also used : VisitorsBridge(org.sonar.java.model.VisitorsBridge) File(java.io.File) Test(org.junit.Test)

Example 8 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class JavaAstScannerTest method should_swallow_log_and_report_checks_exceptions.

@Test
public void should_swallow_log_and_report_checks_exceptions() {
    JavaAstScanner scanner = defaultJavaAstScanner();
    SonarComponents sonarComponent = new SonarComponents(null, context.fileSystem(), null, null, null, null);
    sonarComponent.setSensorContext(context);
    scanner.setVisitorBridge(new VisitorsBridge(Collections.singleton(new CheckThrowingException(new NullPointerException("foo"))), new ArrayList<>(), sonarComponent));
    File scannedFile = new File("src/test/resources/AstScannerNoParseError.txt");
    scanner.scan(ImmutableList.of(scannedFile));
    assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1).contains("Unable to run check class org.sonar.java.ast.JavaAstScannerTest$CheckThrowingException -  on file " + scannedFile.getPath() + ", To help improve SonarJava, please report this problem to SonarSource : see https://www.sonarqube.org/community/");
    assertThat(sonarComponent.analysisErrors).hasSize(1);
    assertThat(sonarComponent.analysisErrors.get(0).getKind()).isSameAs(AnalysisError.Kind.CHECK_ERROR);
    logTester.clear();
    scanner.setVisitorBridge(new VisitorsBridge(new AnnotatedCheck(new NullPointerException("foo"))));
    scannedFile = new File("src/test/resources/AstScannerParseError.txt");
    scanner.scan(ImmutableList.of(scannedFile));
    assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(3).contains("Unable to run check class org.sonar.java.ast.JavaAstScannerTest$AnnotatedCheck - AnnotatedCheck on file " + scannedFile.getPath() + ", To help improve SonarJava, please report this problem to SonarSource : see https://www.sonarqube.org/community/");
}
Also used : SonarComponents(org.sonar.java.SonarComponents) ArrayList(java.util.ArrayList) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 9 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class JavaAstScannerTest method should_interrupt_analysis_when_InterruptedIOException_is_thrown.

@Test
public void should_interrupt_analysis_when_InterruptedIOException_is_thrown() {
    File file = new File("src/test/files/metrics/NoSonar.java");
    thrown.expectMessage("Analysis cancelled");
    thrown.expect(new AnalysisExceptionBaseMatcher(RecognitionException.class, "instanceof AnalysisException with RecognitionException cause"));
    JavaAstScanner.scanSingleFileForTests(file, new VisitorsBridge(new CheckThrowingException(new RecognitionException(42, "interrupted", new InterruptedIOException()))));
}
Also used : InterruptedIOException(java.io.InterruptedIOException) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 10 with VisitorsBridge

use of org.sonar.java.model.VisitorsBridge in project sonar-java by SonarSource.

the class JavaAstScannerTest method comments.

@Test
public void comments() {
    File file = new File("src/test/files/metrics/Comments.java");
    DefaultInputFile resource = new TestInputFileBuilder("", "src/test/files/metrics/Comments.java").build();
    fs.add(resource);
    NoSonarFilter noSonarFilter = mock(NoSonarFilter.class);
    JavaAstScanner.scanSingleFileForTests(file, new VisitorsBridge(new Measurer(fs, context, noSonarFilter)));
    verify(noSonarFilter).noSonarInFile(resource, ImmutableSet.of(15));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Measurer(org.sonar.java.Measurer) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Aggregations

VisitorsBridge (org.sonar.java.model.VisitorsBridge)26 File (java.io.File)25 Test (org.junit.Test)24 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)11 RecognitionException (com.sonar.sslr.api.RecognitionException)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)4 SonarComponents (org.sonar.java.SonarComponents)4 ArrayList (java.util.ArrayList)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 Measurer (org.sonar.java.Measurer)2 SubscriptionVisitor (org.sonar.java.ast.visitors.SubscriptionVisitor)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 InterruptedIOException (java.io.InterruptedIOException)1 HashMap (java.util.HashMap)1 BeforeClass (org.junit.BeforeClass)1 SensorContext (org.sonar.api.batch.SensorContext)1 InputPath (org.sonar.api.batch.fs.InputPath)1 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)1