Search in sources :

Example 51 with Violation

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

the class SuppressWithPlainTextCommentFilterTest method testAcceptThrowsIllegalStateExceptionAsFileNotFound.

@Test
public void testAcceptThrowsIllegalStateExceptionAsFileNotFound() {
    final Violation message = new Violation(1, 1, 1, TokenTypes.CLASS_DEF, "messages.properties", "key", null, SeverityLevel.ERROR, null, getClass(), null);
    final String fileName = "nonexisting_file";
    final AuditEvent auditEvent = new AuditEvent(this, fileName, message);
    final SuppressWithPlainTextCommentFilter filter = new SuppressWithPlainTextCommentFilter();
    try {
        filter.accept(auditEvent);
        assertWithMessage(IllegalStateException.class.getSimpleName() + " is expected").fail();
    } catch (IllegalStateException ex) {
        assertWithMessage("Invalid exception message").that(ex.getMessage()).isEqualTo("Cannot read source file: " + fileName);
        final Throwable cause = ex.getCause();
        assertWithMessage("Exception cause has invalid type").that(cause).isInstanceOf(FileNotFoundException.class);
        assertWithMessage("Invalid exception message").that(cause).hasMessageThat().isEqualTo(fileName + " (No such file or directory)");
    }
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) FileNotFoundException(java.io.FileNotFoundException) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 52 with Violation

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

the class SuppressionFilterTest method testAcceptFalse.

@Test
public void testAcceptFalse() throws Exception {
    final String fileName = getPath("InputSuppressionFilterSuppress.xml");
    final boolean optional = false;
    final SuppressionFilter filter = createSuppressionFilter(fileName, optional);
    final Violation message = new Violation(1, 1, null, "msg", null, SeverityLevel.ERROR, null, getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "ATest.java", message);
    assertWithMessage("Audit event should be rejected when there is a matching suppression").that(filter.accept(ev)).isFalse();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 53 with Violation

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

the class SuppressionXpathFilterTest method testReject.

@Test
public void testReject() throws Exception {
    final boolean optional = false;
    final SuppressionXpathFilter filter = createSuppressionXpathFilter(getPath("InputSuppressionXpathFilterIdAndQuery.xml"), optional);
    final File file = new File(getPath("InputSuppressionXpathFilter.java"));
    final Violation message = new Violation(3, 0, TokenTypes.CLASS_DEF, "", "", null, null, "777", getClass(), null);
    final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null, "file1.java", message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
    assertWithMessage("TreeWalker audit event should be rejected").that(filter.accept(ev)).isFalse();
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) TreeWalkerAuditEvent(com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 54 with Violation

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

the class XpathFilterElementTest method testNonMatchingChecks.

@Test
public void testNonMatchingChecks() throws Exception {
    final String xpath = "NON_MATCHING_QUERY";
    final XpathFilterElement filter = new XpathFilterElement("InputXpathFilterElementSuppressByXpath", "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 55 with Violation

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

the class XpathFilterElementTest method testDecideByMessage.

@Test
public void testDecideByMessage() throws Exception {
    final Violation message = new Violation(1, 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 XpathFilterElement filter1 = new XpathFilterElement(null, null, "Test", null, null);
    final XpathFilterElement filter2 = new XpathFilterElement(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)

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