Search in sources :

Example 6 with TreeWalkerAuditEvent

use of com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent in project checkstyle by checkstyle.

the class SuppressionXpathSingleFilterTest method testThrowException.

@Test
public void testThrowException() {
    final String xpath = "//CLASS_DEF[@text='InputSuppressionXpathSingleFilter']";
    final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter("InputSuppressionXpathSingleFilter", "Test", null, null, xpath);
    final Violation message = new Violation(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null);
    final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, null);
    try {
        filter.accept(ev);
        assertWithMessage("Exception is expected").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("Exception message does not match expected one").that(ex.getMessage()).contains("Cannot initialize context and evaluate query");
    }
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Example 7 with TreeWalkerAuditEvent

use of com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent in project checkstyle by checkstyle.

the class SuppressionXpathSingleFilterTest method testNullModuleIdAndNonMatchingChecks.

@Test
public void testNullModuleIdAndNonMatchingChecks() throws Exception {
    final String xpath = "NON_MATCHING_QUERY";
    final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter("InputSuppressionXpathSingleFilter", "NonMatchingRegexp", null, null, xpath);
    final TreeWalkerAuditEvent ev = createEvent(3, 0, TokenTypes.CLASS_DEF);
    assertWithMessage("Event should be accepted").that(filter.accept(ev)).isTrue();
}
Also used : TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Example 8 with TreeWalkerAuditEvent

use of com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent in project checkstyle by checkstyle.

the class SuppressionXpathSingleFilterTest method testNonMatchingChecks.

@Test
public void testNonMatchingChecks() throws Exception {
    final String xpath = "NON_MATCHING_QUERY";
    final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter("InputSuppressionXpathSingleFilter", "NonMatchingRegexp", null, "id19", xpath);
    final Violation message = new Violation(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "id19", getClass(), null);
    final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
    assertWithMessage("Event should be accepted").that(filter.accept(ev)).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Example 9 with TreeWalkerAuditEvent

use of com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent in project checkstyle by checkstyle.

the class SuppressionXpathSingleFilterTest method testDecideByMessage.

@Test
public void testDecideByMessage() throws Exception {
    final Violation message = new Violation(0, 0, TokenTypes.CLASS_DEF, "", "", null, null, null, getClass(), "Test");
    final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(), message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
    final SuppressionXpathSingleFilter filter1 = createSuppressionXpathSingleFilter(null, null, "Test", null, null);
    final SuppressionXpathSingleFilter filter2 = createSuppressionXpathSingleFilter(null, null, "Bad", null, null);
    assertWithMessage("Message match").that(filter1.accept(ev)).isFalse();
    assertWithMessage("Message not match").that(filter2.accept(ev)).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Example 10 with TreeWalkerAuditEvent

use of com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent in project checkstyle by checkstyle.

the class SuppressionXpathFilterTest method testAcceptOnNullFile.

@Test
public void testAcceptOnNullFile() throws Exception {
    final String fileName = null;
    final boolean optional = false;
    final SuppressionXpathFilter filter = createSuppressionXpathFilter(fileName, optional);
    final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "AnyJava.java", null, null);
    assertWithMessage("TreeWalker audit event on null file should be accepted, but was not").that(filter.accept(ev)).isTrue();
}
Also used : TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) Test(org.junit.jupiter.api.Test)

Aggregations

TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)49 Test (org.junit.jupiter.api.Test)47 Violation (com.puppycrawl.tools.checkstyle.api.Violation)17 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)8 File (java.io.File)6 FileText (com.puppycrawl.tools.checkstyle.api.FileText)5 Pattern (java.util.regex.Pattern)2