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);
}
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();
}
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();
}
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");
}
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();
}
Aggregations