@Test public void noSonarLines() throws Exception { File file = new File("src/test/files/metrics/NoSonar.java"); DefaultInputFile resource = new DefaultInputFile("", "src/test/files/metrics/NoSonar.java"); fs.add(resource); NoSonarFilter noSonarFilter = mock(NoSonarFilter.class); JavaAstScanner.scanSingleFileForTests( file, new VisitorsBridge(new Measurer(fs, context, noSonarFilter))); verify(noSonarFilter).noSonarInFile(resource, ImmutableSet.of(8)); // No Sonar on tests files NoSonarFilter noSonarFilterForTest = mock(NoSonarFilter.class); JavaAstScanner.scanSingleFileForTests( file, new VisitorsBridge(new Measurer(fs, context, noSonarFilterForTest).new TestFileMeasurer())); verify(noSonarFilterForTest).noSonarInFile(resource, ImmutableSet.of(8)); }
@Test public void scan_single_file_with_dumb_file_should_fail() throws Exception { thrown.expect(IllegalArgumentException.class); String filename = "!!dummy"; thrown.expectMessage(filename); JavaAstScanner.scanSingleFileForTests(new File(filename), new VisitorsBridge(null)); }
@Test public void comments() { File file = new File("src/test/files/metrics/Comments.java"); DefaultInputFile resource = new DefaultInputFile("", "src/test/files/metrics/Comments.java"); 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)); }
@Test public void test_fully_qualified_name() { File bytecodeDir = new File("target/test-classes"); ClassFullQualifiedNameVerifierVisitor visitor = new ClassFullQualifiedNameVerifierVisitor(bytecodeDir); JavaAstScanner.scanSingleFileForTests( new File("src/test/java/org/sonar/java/resolve/targets/FullyQualifiedName.java"), new VisitorsBridge( Collections.singletonList(visitor), Lists.newArrayList(bytecodeDir), null)); }
@Test public void should_interrupt_analysis_when_InterrptedIOException_is_thrown() throws Exception { 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())))); }
@VisibleForTesting public static void scanSingleFileForTests(File file, VisitorsBridge visitorsBridge) { scanSingleFileForTests(file, visitorsBridge, new JavaConfiguration(Charset.forName("UTF-8"))); }