Search in sources :

Example 21 with IssuableSubscriptionVisitor

use of org.sonar.plugins.java.api.IssuableSubscriptionVisitor in project sonar-java by SonarSource.

the class JavaCheckVerifierTest method rule_without_annotation_should_fail_with_a_clear_message.

@Test
public void rule_without_annotation_should_fail_with_a_clear_message() {
    class NotAnnotatedCheck extends IssuableSubscriptionVisitor {

        @Override
        public List<Tree.Kind> nodesToVisit() {
            return Collections.singletonList(Tree.Kind.METHOD);
        }

        @Override
        public void visitNode(Tree tree) {
            reportIssue(tree, "yolo", Collections.emptyList(), 42);
        }
    }
    NotAnnotatedCheck check = new NotAnnotatedCheck();
    Throwable throwable = catchThrowable(() -> JavaCheckVerifier.verify("src/test/files/JavaCheckVerifierNoIssue.java", check));
    assertThat(throwable).isInstanceOf(AssertionError.class).hasMessage("Rules should be annotated with '@Rule(key = \"...\")' annotation (org.sonar.check.Rule).");
}
Also used : IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Tree(org.sonar.plugins.java.api.tree.Tree) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Test(org.junit.Test)

Example 22 with IssuableSubscriptionVisitor

use of org.sonar.plugins.java.api.IssuableSubscriptionVisitor in project sonar-java by SonarSource.

the class JavaCheckVerifierTest method verify_unexpected_issue.

@Test
public void verify_unexpected_issue() {
    IssuableSubscriptionVisitor visitor = new FakeVisitor().withDefaultIssues().withIssue(4, "extra message");
    try {
        JavaCheckVerifier.verify(FILENAME_ISSUES, visitor);
        Fail.fail("");
    } catch (AssertionError e) {
        assertThat(e).hasMessage("Unexpected at [4]");
    }
}
Also used : IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Test(org.junit.Test)

Example 23 with IssuableSubscriptionVisitor

use of org.sonar.plugins.java.api.IssuableSubscriptionVisitor in project sonar-java by SonarSource.

the class JavaCheckVerifierTest method test_with_no_semantic.

@Test
public void test_with_no_semantic() throws Exception {
    IssuableSubscriptionVisitor noIssueVisitor = new FakeVisitor();
    JavaCheckVerifier.verifyNoIssueWithoutSemantic(FILENAME_ISSUES, noIssueVisitor);
    JavaCheckVerifier.verifyNoIssueWithoutSemantic(FILENAME_NO_ISSUE, noIssueVisitor);
    try {
        JavaCheckVerifier.verifyNoIssueWithoutSemantic(FILENAME_ISSUES, new FakeVisitor().withDefaultIssues());
        Fail.fail("");
    } catch (AssertionError e) {
        assertThat(e.getMessage()).contains("No issues expected but got:");
    }
}
Also used : IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Test(org.junit.Test)

Example 24 with IssuableSubscriptionVisitor

use of org.sonar.plugins.java.api.IssuableSubscriptionVisitor in project sonar-java by SonarSource.

the class JavaCheckVerifierTest method verify_with_provided_test_jar.

@Test
public void verify_with_provided_test_jar() throws IOException {
    String testJarsPathname = "target/my-test-jars";
    File file = new File(testJarsPathname);
    if (file.exists()) {
        file.delete();
    }
    if (file.mkdir()) {
        IssuableSubscriptionVisitor visitor = new FakeVisitor().withDefaultIssues();
        JavaCheckVerifier.verify(FILENAME_ISSUES, visitor, testJarsPathname);
        file.delete();
    } else {
        Fail.fail("");
    }
}
Also used : IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)24 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)24 File (java.io.File)5 Tree (org.sonar.plugins.java.api.tree.Tree)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)1 Rule (org.junit.Rule)1 ExpectedException (org.junit.rules.ExpectedException)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1 JavaAstScanner (org.sonar.java.ast.JavaAstScanner)1 SubscriptionVisitor (org.sonar.java.ast.visitors.SubscriptionVisitor)1 JavaTree (org.sonar.java.model.JavaTree)1