@Test public void should_propagate_SOError() { thrown.expect(StackOverflowError.class); JavaAstScanner scanner = defaultJavaAstScanner(); scanner.setVisitorBridge(new VisitorsBridge(new CheckThrowingSOError())); scanner.scan(ImmutableList.of(new File("src/test/resources/AstScannerNoParseError.txt"))); }
@Test public void should_not_fail_whole_analysis_upon_parse_error_and_notify_audit_listeners() { FakeAuditListener listener = spy(new FakeAuditListener()); JavaAstScanner scanner = defaultJavaAstScanner(); scanner.setVisitorBridge(new VisitorsBridge(listener)); scanner.scan(ImmutableList.of(new File("src/test/resources/AstScannerParseError.txt"))); verify(listener).processRecognitionException(any(RecognitionException.class)); }
@Test public void should_report_analysis_error_in_sonarLint_context_withSQ_6_0() throws Exception { JavaAstScanner scanner = defaultJavaAstScanner(); FakeAuditListener listener = spy(new FakeAuditListener()); SonarComponents sonarComponents = mock(SonarComponents.class); when(sonarComponents.reportAnalysisError(any(RecognitionException.class), any(File.class))) .thenReturn(true); scanner.setVisitorBridge( new VisitorsBridge( Lists.newArrayList(listener), Lists.newArrayList(), sonarComponents, false)); scanner.scan(ImmutableList.of(new File("src/test/resources/AstScannerParseError.txt"))); verify(sonarComponents).reportAnalysisError(any(RecognitionException.class), any(File.class)); verifyZeroInteractions(listener); }
@Test public void should_propagate_visitor_exception_when_no_parse_error() { JavaAstScanner scanner = defaultJavaAstScanner(); scanner.setVisitorBridge( new VisitorsBridge(new CheckThrowingException(new NullPointerException("foo")))); thrown.expectMessage("SonarQube is unable to analyze file"); thrown.expect( new AnalysisExceptionBaseMatcher( NullPointerException.class, "instanceof AnalysisException with NullPointerException cause")); scanner.scan(ImmutableList.of(new File("src/test/resources/AstScannerNoParseError.txt"))); }
@VisibleForTesting public static void scanSingleFileForTests( File file, VisitorsBridge visitorsBridge, JavaConfiguration conf) { if (!file.isFile()) { throw new IllegalArgumentException("File '" + file + "' not found."); } JavaAstScanner scanner = create(conf, visitorsBridge); scanner.scan(Collections.singleton(file)); Collection<SourceCode> sources = scanner.getIndex().search(new QueryByType(SourceFile.class)); if (sources.size() != 1) { throw new IllegalStateException( "Only one SourceFile was expected whereas " + sources.size() + " has been returned."); } }
/** * Helper method for testing checks without having to deploy them on a Sonar instance. Can be * dropped when support for CheckMessageVerifier will be dropped. * * @deprecated As of release 3.6, should use {@link * org.sonar.java.checks.verifier.JavaCheckVerifier#verify(String filename, JavaFileScanner * check)} for rules unit tests. */ @VisibleForTesting @Deprecated public static SourceFile scanSingleFile(File file, VisitorsBridge visitorsBridge) { if (!file.isFile()) { throw new IllegalArgumentException("File '" + file + "' not found."); } JavaAstScanner scanner = create(new JavaConfiguration(Charset.forName("UTF-8")), visitorsBridge); scanner.scan(Collections.singleton(file)); Collection<SourceCode> sources = scanner.getIndex().search(new QueryByType(SourceFile.class)); if (sources.size() != 1) { throw new IllegalStateException( "Only one SourceFile was expected whereas " + sources.size() + " has been returned."); } return (SourceFile) sources.iterator().next(); }