JavaAstScanner { @VisibleForTesting public static void scanSingleFileForTests(InputFile file, VisitorsBridge visitorsBridge) { scanSingleFileForTests(file, visitorsBridge, new JavaVersionImpl(), null); } JavaAstScanner(@Nullable SonarComponents sonarComponents); void scan(Iterable<? extends InputFile> inputFiles); void setVisitorBridge(VisitorsBridge visitor); @VisibleForTesting static void scanSingleFileForTests(InputFile file, VisitorsBridge visitorsBridge); @VisibleForTesting static void scanSingleFileForTests(InputFile inputFile, VisitorsBridge visitorsBridge, JavaVersion javaVersion, @Nullable SonarComponents sonarComponents);  }
@Test void comments() { InputFile inputFile = TestUtils.inputFile("src/test/files/metrics/Comments.java"); NoSonarFilter noSonarFilter = mock(NoSonarFilter.class); JavaAstScanner.scanSingleFileForTests(inputFile, new VisitorsBridge(new Measurer(context, noSonarFilter))); verify(noSonarFilter).noSonarInFile(inputFile, Collections.singleton(15)); }
@Test void noSonarLines() throws Exception { InputFile inputFile = TestUtils.inputFile("src/test/files/metrics/NoSonar.java"); NoSonarFilter noSonarFilter = mock(NoSonarFilter.class); JavaAstScanner.scanSingleFileForTests(inputFile, new VisitorsBridge(new Measurer(context, noSonarFilter))); verify(noSonarFilter).noSonarInFile(inputFile, Collections.singleton(8)); NoSonarFilter noSonarFilterForTest = mock(NoSonarFilter.class); JavaAstScanner.scanSingleFileForTests(inputFile, new VisitorsBridge(new Measurer(context, noSonarFilterForTest).new TestFileMeasurer())); verify(noSonarFilterForTest).noSonarInFile(inputFile, Collections.singleton(8)); }
@Test void scan_single_file_with_dumb_file_should_not_fail() throws Exception { String filename = "!!dummy"; JavaAstScanner.scanSingleFileForTests(TestUtils.emptyInputFile(filename), new VisitorsBridge(null)); }
@Test void should_interrupt_analysis_when_InterruptedException_is_thrown() { InputFile inputFile = TestUtils.inputFile("src/test/files/metrics/NoSonar.java"); VisitorsBridge visitorsBridge = new VisitorsBridge(new CheckThrowingException(new RecognitionException(42, "interrupted", new InterruptedException()))); AnalysisException e = assertThrows(AnalysisException.class, () -> JavaAstScanner.scanSingleFileForTests(inputFile, visitorsBridge)); assertThat(e.getMessage()).isEqualTo("Analysis cancelled"); assertThat(e.getCause().getClass()).isEqualTo(RecognitionException.class); }
@Test void should_interrupt_analysis_when_InterruptedIOException_is_thrown() { InputFile inputFile = TestUtils.inputFile("src/test/files/metrics/NoSonar.java"); VisitorsBridge visitorsBridge = new VisitorsBridge(new CheckThrowingException(new RecognitionException(42, "interrupted", new InterruptedIOException()))); AnalysisException e = assertThrows(AnalysisException.class, () -> JavaAstScanner.scanSingleFileForTests(inputFile, visitorsBridge)); assertThat(e.getMessage()).isEqualTo("Analysis cancelled"); assertThat(e.getCause().getClass()).isEqualTo(RecognitionException.class); }