Search in sources :

Example 46 with Violation

use of com.puppycrawl.tools.checkstyle.api.Violation in project checkstyle by checkstyle.

the class SuppressionCommentFilterTest method getTagsAfterExecution.

/**
 * Calls the filter with a minimal set of inputs and returns a list of
 * {@link SuppressionCommentFilter} internal type {@code Tag}.
 * Our goal is 100% test coverage, for this we use white-box testing.
 * So we need access to the implementation details. For this reason,
 * it is necessary to use reflection to gain access to the inner field here.
 *
 * @return {@code Tag} list
 */
private static List<Comparable<Object>> getTagsAfterExecution(SuppressionCommentFilter filter, String filename, String... lines) {
    final FileContents contents = new FileContents(new FileText(new File(filename), Arrays.asList(lines)));
    for (int lineNo = 0; lineNo < lines.length; lineNo++) {
        final int colNo = lines[lineNo].indexOf("//");
        if (colNo >= 0) {
            contents.reportSingleLineComment(lineNo + 1, colNo);
        }
    }
    final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, filename, new Violation(1, null, null, null, null, Object.class, ""), null);
    filter.accept(dummyEvent);
    return TestUtil.getInternalState(filter, "tags");
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File)

Example 47 with Violation

use of com.puppycrawl.tools.checkstyle.api.Violation 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 48 with Violation

use of com.puppycrawl.tools.checkstyle.api.Violation 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 49 with Violation

use of com.puppycrawl.tools.checkstyle.api.Violation 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 50 with Violation

use of com.puppycrawl.tools.checkstyle.api.Violation in project checkstyle by checkstyle.

the class SuppressWithPlainTextCommentFilterTest method testFilterWithDirectory.

@Test
public void testFilterWithDirectory() throws IOException {
    final SuppressWithPlainTextCommentFilter filter = new SuppressWithPlainTextCommentFilter();
    final AuditEvent event = new AuditEvent(this, getPath(""), new Violation(1, 1, "bundle", "key", null, SeverityLevel.ERROR, "moduleId", getClass(), "customMessage"));
    assertWithMessage("filter should accept directory").that(filter.accept(event)).isTrue();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Aggregations

Violation (com.puppycrawl.tools.checkstyle.api.Violation)109 Test (org.junit.jupiter.api.Test)98 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)51 TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)17 File (java.io.File)14 TreeWalkerTest (com.puppycrawl.tools.checkstyle.TreeWalkerTest)13 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)10 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)8 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)7 FileText (com.puppycrawl.tools.checkstyle.api.FileText)7 TreeSet (java.util.TreeSet)6 DetailAstImpl (com.puppycrawl.tools.checkstyle.DetailAstImpl)5 ParseErrorMessage (com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage)3 SeverityLevel (com.puppycrawl.tools.checkstyle.api.SeverityLevel)3 DebugAuditAdapter (com.puppycrawl.tools.checkstyle.internal.testmodules.DebugAuditAdapter)3 DebugFilter (com.puppycrawl.tools.checkstyle.internal.testmodules.DebugFilter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 Method (java.lang.reflect.Method)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2