コード例 #1
0
 @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")));
 }
コード例 #2
0
  @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));
  }
コード例 #3
0
 @Before
 public void setUp() {
   squid = new Squid(new JavaSquidConfiguration());
   EmptyFileCheck check = new EmptyFileCheck();
   squid.registerVisitor(check);
   JavaAstScanner scanner = squid.register(JavaAstScanner.class);
   scanner.scanDirectory(SquidTestUtils.getFile("/metrics/commentedCode"));
   squid.decorateSourceCodeTreeWith(Metric.values());
   squid.register(SquidScanner.class).scan();
 }
コード例 #4
0
 private static JavaAstScanner create(
     JavaConfiguration conf, @Nullable VisitorsBridge visitorsBridge) {
   JavaAstScanner astScanner = new JavaAstScanner(JavaParser.createParser(conf.getCharset()));
   if (visitorsBridge != null) {
     visitorsBridge.setCharset(conf.getCharset());
     visitorsBridge.setJavaVersion(conf.javaVersion());
     astScanner.setVisitorBridge(visitorsBridge);
   }
   return astScanner;
 }
コード例 #5
0
 @Before
 public void setUp() {
   squid = new Squid(new JavaSquidConfiguration());
   MethodComplexityCheck check = new MethodComplexityCheck();
   check.setMax(5);
   squid.registerVisitor(check);
   JavaAstScanner scanner = squid.register(JavaAstScanner.class);
   scanner.scanFile(SquidTestUtils.getInputFile("/metrics/branches/ComplexBranches.java"));
   squid.decorateSourceCodeTreeWith(Metric.values());
   squid.register(SquidScanner.class).scan();
 }
コード例 #6
0
  @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")));
  }
コード例 #7
0
 @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);
 }
コード例 #8
0
  @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.");
    }
  }
コード例 #9
0
 @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));
 }
コード例 #10
0
 @Test
 public void detected() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/ClassCouplingCheck.java"),
           new VisitorsBridge(new ClassCouplingCheck()));
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(1)
       .withMessage(
           "Split this class into smaller and more specialized ones to reduce its dependencies on other classes from 21 to the maximum authorized 20 or less.")
       .next()
       .atLine(33)
       .next()
       .atLine(60)
       .next()
       .atLine(85)
       .next()
       .atLine(144)
       .withMessage(
           "Split this class into smaller and more specialized ones to reduce its dependencies on other classes from 21 to the maximum authorized 20 or less.")
       .next()
       .atLine(167)
       .noMore();
 }
コード例 #11
0
 @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));
 }
コード例 #12
0
  /**
   * 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();
  }
コード例 #13
0
 @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));
 }
コード例 #14
0
 @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));
 }
コード例 #15
0
 @Test
 public void test() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/NoSonar.java"), new VisitorsBridge(check));
   CheckMessagesVerifier.verify(file.getCheckMessages())
       .next()
       .atLine(2)
       .withMessage("Is //NOSONAR used to exclude false-positive or to hide real quality flaw ?")
       .noMore();
 }
コード例 #16
0
 @Test
 public void test_with_no_import() {
   check.maximumLineLength = 20;
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/TooLongLine_S00103_Check/LineLengthNoImport.java"),
           new VisitorsBridge(check));
   CheckMessagesVerifier.verify(file.getCheckMessages())
       .next()
       .atLine(3)
       .withMessage("Split this 28 characters long line (which is greater than 20 authorized).")
       .next()
       .atLine(4)
       .noMore();
 }
コード例 #17
0
  @Test
  public void custom() {
    ClassCouplingCheck check = new ClassCouplingCheck();
    check.max = 22;

    SourceFile file =
        JavaAstScanner.scanSingleFile(
            new File("src/test/files/checks/ClassCouplingCheck.java"), new VisitorsBridge(check));

    checkMessagesVerifier
        .verify(file.getCheckMessages())
        .next()
        .atLine(33)
        .withMessage(
            "Split this class into smaller and more specialized ones to reduce its dependencies on other classes from 23 to the maximum authorized 22 or less.");
  }
コード例 #18
0
  @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()))));
  }
 @Test
 public void detected() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/NoCheckstyleTagPresenceCheck.java"),
           new VisitorsBridge(new NoCheckstyleTagPresenceCheck()));
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(3)
       .withMessage("Remove usage of this \"CHECKSTYLE:OFF\" suppression comment filter.")
       .next()
       .atLine(9)
       .next()
       .atLine(12)
       .next()
       .atLine(14);
 }
コード例 #20
0
 @Test
 public void detected() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/UnusedTypeParameterCheck.java"),
           new VisitorsBridge(new UnusedTypeParameterCheck()));
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(1)
       .withMessage("S is not used in the class.")
       .next()
       .atLine(3)
       .withMessage("W is not used in the method.")
       .next()
       .atLine(5)
       .withMessage("V is not used in the interface.");
 }
 @Test
 public void detected() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/FieldNameMatchingTypeNameCheck.java"),
           new VisitorsBridge(new FieldNameMatchingTypeNameCheck()));
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(2)
       .withMessage("Rename field \"A\"")
       .next()
       .atLine(10)
       .withMessage("Rename field \"AcLass\"")
       .next()
       .atLine(11)
       .withMessage("Rename field \"AClass\"");
 }
 @Test
 public void detected() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/IgnoredStreamReturnValueCheck.java"),
           new VisitorsBridge(new IgnoredStreamReturnValueCheck()));
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(12)
       .withMessage("Check the return value of the \"read\" call to see how many bytes were read.")
       .next()
       .atLine(13)
       .withMessage("Check the return value of the \"skip\" call to see how many bytes were read.")
       .next()
       .atLine(14)
       .withMessage("Check the return value of the \"read\" call to see how many bytes were read.")
       .next()
       .atLine(15)
       .withMessage("Check the return value of the \"skip\" call to see how many bytes were read.")
       .noMore();
 }
コード例 #23
0
 private SourceFile getSourceFile(String listOfWarnings) {
   SuppressWarningsCheck check = new SuppressWarningsCheck();
   check.warningsCommaSeparated = listOfWarnings;
   return JavaAstScanner.scanSingleFile(
       new File("src/test/files/checks/SuppressWarningsCheck.java"), new VisitorsBridge(check));
 }