Search in sources :

Example 46 with AuditEvent

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

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

the class SuppressionFilterTest method testExistingSuppressionFileWithTrueOptional.

@Test
public void testExistingSuppressionFileWithTrueOptional() throws Exception {
    final String fileName = getPath("InputSuppressionFilterNone.xml");
    final boolean optional = true;
    final SuppressionFilter filter = createSuppressionFilter(fileName, optional);
    final AuditEvent ev = new AuditEvent(this, "AnyFile.java", null);
    assertWithMessage("Suppression file with true optional was not accepted").that(filter.accept(ev)).isTrue();
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 48 with AuditEvent

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

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

the class SuppressionFilterTest method testAccept.

@Test
public void testAccept() throws Exception {
    final String fileName = getPath("InputSuppressionFilterNone.xml");
    final boolean optional = false;
    final SuppressionFilter filter = createSuppressionFilter(fileName, optional);
    final AuditEvent ev = new AuditEvent(this, "ATest.java", null);
    assertWithMessage("Audit event should be excepted when there are no suppressions").that(filter.accept(ev)).isTrue();
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 50 with AuditEvent

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

the class SuppressionFilterTest method testAcceptOnNullFile.

@Test
public void testAcceptOnNullFile() throws CheckstyleException {
    final String fileName = null;
    final boolean optional = false;
    final SuppressionFilter filter = createSuppressionFilter(fileName, optional);
    final AuditEvent ev = new AuditEvent(this, "AnyJava.java", null);
    assertWithMessage("Audit event on null file should be excepted, but was not").that(filter.accept(ev)).isTrue();
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Aggregations

AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)110 Test (org.junit.jupiter.api.Test)76 Violation (com.puppycrawl.tools.checkstyle.api.Violation)51 Test (org.junit.Test)21 TreeWalkerTest (com.puppycrawl.tools.checkstyle.TreeWalkerTest)15 LocalizedMessage (com.puppycrawl.tools.checkstyle.api.LocalizedMessage)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 OutputStream (java.io.OutputStream)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)4 SeverityLevel (com.puppycrawl.tools.checkstyle.api.SeverityLevel)4 CloseAndFlushTestByteArrayOutputStream (com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream)4 Method (java.lang.reflect.Method)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2 ArrayList (java.util.ArrayList)2 CheckstyleCheckerListener (org.apache.maven.plugins.checkstyle.exec.CheckstyleCheckerListener)2 CheckstyleResults (org.apache.maven.plugins.checkstyle.exec.CheckstyleResults)2 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1