Search in sources :

Example 96 with AuditEvent

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

the class SarifLoggerTest method testAddExceptions.

@Test
public void testAddExceptions() throws IOException {
    final SarifLogger logger = new SarifLogger(outStream, AutomaticBean.OutputStreamOptions.CLOSE);
    logger.auditStarted(null);
    final Violation violation = new Violation(1, 1, "messages.properties", "null", null, null, getClass(), "found an error");
    final AuditEvent ev = new AuditEvent(this, null, violation);
    final Violation violation2 = new Violation(1, 1, "messages.properties", "null", null, null, getClass(), "found an error");
    final AuditEvent ev2 = new AuditEvent(this, "Test.java", violation2);
    logger.fileStarted(ev);
    logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
    logger.fileFinished(ev);
    logger.fileStarted(ev2);
    logger.addException(ev2, new TestException("msg2", new RuntimeException("msg2")));
    logger.fileFinished(ev);
    logger.auditFinished(null);
    verifyContent(getPath("ExpectedSarifLoggerDoubleException.sarif"), outStream);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 97 with AuditEvent

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

the class DefaultLoggerTest method testCtor.

@Test
public void testCtor() throws Exception {
    final OutputStream infoStream = new ByteArrayOutputStream();
    final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    final DefaultLogger dl = new DefaultLogger(infoStream, OutputStreamOptions.CLOSE, errorStream, OutputStreamOptions.CLOSE);
    dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
    dl.auditFinished(new AuditEvent(6000, "myfile"));
    final String output = errorStream.toString(StandardCharsets.UTF_8);
    final Constructor<?> cons = getConstructor();
    cons.setAccessible(true);
    final Object addExceptionMessage = cons.newInstance(DefaultLogger.ADD_EXCEPTION_MESSAGE, new String[] { "myfile" });
    final Method getMessage = addExceptionMessage.getClass().getDeclaredMethod("getMessage");
    getMessage.setAccessible(true);
    final Object returnValue = getMessage.invoke(addExceptionMessage);
    assertWithMessage("Invalid exception").that(output.contains(returnValue.toString())).isTrue();
    assertWithMessage("Invalid exception class").that(output.contains("java.lang.IllegalStateException: upsss")).isTrue();
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 98 with AuditEvent

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

the class DefaultLoggerTest method testCtorWithTwoParameters.

@Test
public void testCtorWithTwoParameters() {
    final OutputStream infoStream = new ByteArrayOutputStream();
    final DefaultLogger dl = new DefaultLogger(infoStream, OutputStreamOptions.CLOSE);
    dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
    dl.auditFinished(new AuditEvent(6000, "myfile"));
    final String output = infoStream.toString();
    assertWithMessage("Message should contain exception info, but was " + output).that(output.contains("java.lang.IllegalStateException: upsss")).isTrue();
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 99 with AuditEvent

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

the class DefaultLoggerTest method testNewCtor.

@Test
public void testNewCtor() throws Exception {
    final ResourceBundle bundle = ResourceBundle.getBundle(Definitions.CHECKSTYLE_BUNDLE, Locale.ROOT);
    final String auditStartedMessage = bundle.getString(DefaultLogger.AUDIT_STARTED_MESSAGE);
    final String auditFinishedMessage = bundle.getString(DefaultLogger.AUDIT_FINISHED_MESSAGE);
    final String addExceptionMessage = new MessageFormat(bundle.getString(DefaultLogger.ADD_EXCEPTION_MESSAGE), Locale.ROOT).format(new String[] { "myfile" });
    final String infoOutput;
    final String errorOutput;
    try (MockByteArrayOutputStream infoStream = new MockByteArrayOutputStream()) {
        try (MockByteArrayOutputStream errorStream = new MockByteArrayOutputStream()) {
            final DefaultLogger dl = new DefaultLogger(infoStream, OutputStreamOptions.CLOSE, errorStream, OutputStreamOptions.CLOSE);
            dl.auditStarted(null);
            dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
            dl.auditFinished(new AuditEvent(6000, "myfile"));
            infoOutput = infoStream.toString(StandardCharsets.UTF_8);
            errorOutput = errorStream.toString(StandardCharsets.UTF_8);
            assertWithMessage("Info stream should be closed").that(infoStream.closedCount).isGreaterThan(0);
            assertWithMessage("Error stream should be closed").that(errorStream.closedCount).isGreaterThan(0);
        }
    }
    assertWithMessage("Violation should contain message `audit started`").that(infoOutput).contains(auditStartedMessage);
    assertWithMessage("Violation should contain message `audit finished`").that(infoOutput).contains(auditFinishedMessage);
    assertWithMessage("Violation should contain exception info").that(errorOutput).contains(addExceptionMessage);
    assertWithMessage("Violation should contain exception message").that(errorOutput).contains("java.lang.IllegalStateException: upsss");
}
Also used : MessageFormat(java.text.MessageFormat) ResourceBundle(java.util.ResourceBundle) AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) Test(org.junit.jupiter.api.Test)

Example 100 with AuditEvent

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

the class SuppressFilterElementTest method testDecideByFileNameAndModuleMatchingModuleEqual.

@Test
public void testDecideByFileNameAndModuleMatchingModuleEqual() {
    final Violation violation = new Violation(10, 10, "", "", null, "MyModule", getClass(), null);
    final AuditEvent ev = new AuditEvent(this, "ATest.java", violation);
    final SuppressFilterElement myFilter = new SuppressFilterElement("Test", "Test", null, "MyModule", null, null);
    assertWithMessage("Filter should not accept invalid event").that(myFilter.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) TreeWalkerTest(com.puppycrawl.tools.checkstyle.TreeWalkerTest)

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