Search in sources :

Example 1 with IssuableSubscriptionVisitor

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

the class JavaCheckVerifierTest method verify_missing_expected_issue.

@Test
public void verify_missing_expected_issue() {
    IssuableSubscriptionVisitor visitor = new FakeVisitor().withDefaultIssues().withoutIssue(1);
    try {
        JavaCheckVerifier.verify(FILENAME_ISSUES, visitor);
        Fail.fail("");
    } catch (AssertionError e) {
        assertThat(e).hasMessage("Expected {1=[{MESSAGE=message}]}");
    }
}
Also used : IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Test(org.junit.Test)

Example 2 with IssuableSubscriptionVisitor

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

the class JavaCheckVerifierTest method verify_line_issues_with_java_version.

@Test
public void verify_line_issues_with_java_version() {
    IssuableSubscriptionVisitor visitor = new FakeVisitor().withDefaultIssues();
    JavaCheckVerifier.verify(FILENAME_ISSUES, visitor, 7);
}
Also used : IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Test(org.junit.Test)

Example 3 with IssuableSubscriptionVisitor

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

the class JavaCheckVerifierTest method verify_combined_missing_expected_and_unexpected_issues.

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

Example 4 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)

Example 5 with IssuableSubscriptionVisitor

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

the class MethodMatcherTest method test_copy.

@Test
public void test_copy() throws Exception {
    MethodMatcher vanilla = MethodMatcher.create().typeDefinition("Test").name("f").withoutParameter();
    MethodMatcher copyInt = vanilla.copy().addParameter("int");
    MethodMatcher copyString = vanilla.copy().addParameter("java.lang.String");
    Map<MethodMatcher, List<Integer>> matches = new HashMap<>();
    matches.put(vanilla, new ArrayList<>());
    matches.put(copyInt, new ArrayList<>());
    matches.put(copyString, new ArrayList<>());
    JavaCheckVerifier.verifyNoIssue("src/test/files/matcher/Copy.java", new IssuableSubscriptionVisitor() {

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

        @Override
        public void visitNode(Tree tree) {
            MethodTree methodTree = (MethodTree) tree;
            matches.forEach((matcher, list) -> {
                if (matcher.matches(methodTree)) {
                    list.add(methodTree.firstToken().line());
                }
            });
        }
    });
    assertThat(matches.get(vanilla)).containsExactly(3);
    assertThat(matches.get(copyInt)).containsExactly(5);
    assertThat(matches.get(copyString)).containsExactly(7);
}
Also used : SubscriptionVisitor(org.sonar.java.ast.visitors.SubscriptionVisitor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) JavaAstScanner(org.sonar.java.ast.JavaAstScanner) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) ExpectedException(org.junit.rules.ExpectedException) Nullable(javax.annotation.Nullable) VisitorsBridge(org.sonar.java.model.VisitorsBridge) Test(org.junit.Test) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) Mockito.when(org.mockito.Mockito.when) File(java.io.File) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) List(java.util.List) Rule(org.junit.Rule) JavaCheckVerifier(org.sonar.java.se.JavaCheckVerifier) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Mockito.mock(org.mockito.Mockito.mock) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) HashMap(java.util.HashMap) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) 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