@Test
 public void shouldDetectEmptyFiles() {
   SourceFile file = (SourceFile) squid.search("org/foo/CommentedOutFile.java");
   assertThat(file.getCheckMessages().size(), is(1));
   CheckMessage message = file.getCheckMessages().iterator().next();
   assertThat(message.getLine(), nullValue());
   assertThat(message.getDefaultMessage(), is("This Java file is empty"));
 }
 @Test
 public void test_non_empty_file() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/NonEmptyFile.java"), new VisitorsBridge(check));
   CheckMessagesVerifier.verify(file.getCheckMessages()).noMore();
 }
 @Test
 public void detected() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/ReturnEmptyArrayyNotNullCheck.java"),
           new ReturnEmptyArrayyNotNullCheck());
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(16)
       .withMessage("Return an empty array instead of null.")
       .next()
       .atLine(25)
       .next()
       .atLine(35)
       .next()
       .atLine(40)
       .next()
       .atLine(44)
       .next()
       .atLine(48)
       .next()
       .atLine(53)
       .withMessage("Return an empty collection instead of null.")
       .next()
       .atLine(57)
       .withMessage("Return an empty array instead of null.")
       .next()
       .atLine(61);
 }
 @Test
 public void test() {
   SourceFile file =
       FlexAstScanner.scanSingleFile(new File("src/test/resources/checks/StarTypeUse.as"), check);
   CheckMessagesVerifier.verify(file.getCheckMessages())
       .next()
       .atLine(5)
       .withMessage("Remove usage of this \"star\" type")
       .next()
       .atLine(6)
       .noMore();
 }
 @Test
 public void check_with_exclusion() {
   check.exclude = "com.sun.imageio,com.sun.jersey";
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/SunPackagesUsedCheck.java"), check);
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(10)
       .withMessage("Replace this usage of Sun classes by ones from the Java API.");
 }
 private void saveViolations(File sonarFile, SourceFile squidFile) {
   Collection<CheckMessage> messages = squidFile.getCheckMessages();
   if (messages != null) {
     for (CheckMessage message : messages) {
       Violation violation =
           Violation.create(annotationCheckFactory.getActiveRule(message.getCheck()), sonarFile)
               .setLineId(message.getLine())
               .setMessage(message.getText(Locale.ENGLISH));
       context.saveViolation(violation);
     }
   }
 }
  @Test
  public void test() {
    ElseIfWithoutElseCheck check = new ElseIfWithoutElseCheck();

    SourceFile file =
        JavaScriptAstScanner.scanSingleFile(
            new File("src/test/resources/checks/elseIfWithoutElse.js"), check);
    CheckMessagesVerifier.verify(file.getCheckMessages())
        .next()
        .atLine(15)
        .withMessage("End this if...else if construct by an else clause.")
        .noMore();
  }
Example #8
0
  @Test
  public void check() {
    XPathCheck check = new XPathCheck();
    check.xpathQuery = "//IDENTIFIER[string-length(@tokenValue) >= 10]";
    check.message = "Avoid identifiers which are too long!";

    SourceFile file = scanFile("/checks/xpath.cs", check);
    CheckMessagesVerifier.verify(file.getCheckMessages())
        .next()
        .atLine(7)
        .withMessage("Avoid identifiers which are too long!")
        .noMore();
  }
  @Test
  public void check() {
    XPathCheck check = new XPathCheck();
    check.xpathQuery = "//IDENTIFIER[string-length(@tokenValue) >= 10]";
    check.message = "Avoid identifiers which are too long!";

    SourceFile file =
        CSharpAstScanner.scanSingleFile(new File("src/test/resources/checks/xpath.cs"), check);
    checkMessagesVerifier
        .verify(file.getCheckMessages())
        .next()
        .atLine(7)
        .withMessage("Avoid identifiers which are too long!");
  }
 @Test
 public void detected() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/RightCurlyBraceDifferentLineAsNextBlockCheck.java"),
           new RightCurlyBraceDifferentLineAsNextBlockCheck());
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(5)
       .withMessage("Move this \"else\" keyword to a new dedicated line.")
       .next()
       .atLine(14)
       .withMessage("Move this \"catch\" keyword to a new dedicated line.");
 }
  @Test
  public void test() {
    OneStatementPerLineCheck check = new OneStatementPerLineCheck();

    SourceFile file =
        PythonAstScanner.scanSingleFile(
            new File("src/test/resources/checks/oneStatementPerLine.py"), check);
    CheckMessagesVerifier.verify(file.getCheckMessages())
        .next()
        .atLine(1)
        .withMessage(
            "At most one statement is allowed per line, but 2 statements were found on this line.")
        .next()
        .atLine(2)
        .withMessage(
            "At most one statement is allowed per line, but 3 statements were found on this line.")
        .noMore();
  }
 @Test
 public void detected() {
   SourceFile file =
       JavaAstScanner.scanSingleFile(
           new File("src/test/files/checks/SunPackagesUsedCheck.java"), check);
   checkMessagesVerifier
       .verify(file.getCheckMessages())
       .next()
       .atLine(1)
       .withMessage("Replace this usage of Sun classes by ones from the Java API.")
       .next()
       .atLine(2)
       .next()
       .atLine(7)
       .next()
       .atLine(8)
       .next()
       .atLine(10);
 }
  @Test
  public void test() {
    ActionScript2Check check = new ActionScript2Check();

    SourceFile file =
        FlexAstScanner.scanSingleFile(
            new File("src/test/resources/checks/ActionScript2.as"), check);
    CheckMessagesVerifier.verify(file.getCheckMessages())
        .next()
        .atLine(1)
        .withMessage("'intrinsic' not available in ActionScript 3.0")
        .next()
        .atLine(2)
        .withMessage("Operator '<>' not available in ActionScript 3.0")
        .next()
        .atLine(4)
        .withMessage("Operator 'not' not available in ActionScript 3.0")
        .next()
        .atLine(6)
        .withMessage("'set variable statement' not available in ActionScript 3.0")
        .noMore();
  }
  @Test
  public void test() {
    ParenthesesCheck check = new ParenthesesCheck();

    SourceFile file =
        JavaScriptAstScanner.scanSingleFile(
            new File("src/test/resources/checks/parentheses.js"), check);
    CheckMessagesVerifier.verify(file.getCheckMessages())
        .next()
        .atLine(2)
        .next()
        .atLine(8)
        .next()
        .atLine(12)
        .next()
        .atLine(15)
        .next()
        .atLine(23)
        .next()
        .atLine(26)
        .next()
        .atLine(30)
        .noMore();
  }
 @Test
 public void shouldNotLogOnCorrectFiles() {
   SourceFile file = (SourceFile) squid.search("CommentedCode.java");
   assertThat(file.getCheckMessages().size(), is(0));
 }