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);
}
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));
}
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/");
}
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()))));
}
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));
}
Aggregations