Repository: logging-log4j-audit Updated Branches: refs/heads/master d9663a73b -> bbeb45647
LOG4J2-2428: pass the event handler defined on the factory to the events Project: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/commit/06dbe889 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/tree/06dbe889 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/diff/06dbe889 Branch: refs/heads/master Commit: 06dbe88948259bff82ffe91a6feb62983adde10a Parents: d9663a7 Author: Andrei Ivanov <[email protected]> Authored: Sun Sep 30 22:58:03 2018 +0300 Committer: Andrei Ivanov <[email protected]> Committed: Sun Sep 30 22:58:03 2018 +0300 ---------------------------------------------------------------------- .../logging/log4j/audit/LogEventFactory.java | 2 +- .../logging/log4j/audit/TransferTest.java | 87 +++++++++++++++++--- 2 files changed, 77 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/06dbe889/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java ---------------------------------------------------------------------- diff --git a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java index 5657bf4..8746209 100644 --- a/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java +++ b/log4j-audit/log4j-audit-api/src/main/java/org/apache/logging/log4j/audit/LogEventFactory.java @@ -248,7 +248,7 @@ public class LogEventFactory { private final AuditMessage msg; private final Class<?> intrface; - private AuditExceptionHandler auditExceptionHandler = DEFAULT_HANDLER; + private AuditExceptionHandler auditExceptionHandler = defaultExceptionHandler; AuditProxy(AuditMessage msg, Class<?> intrface) { this.msg = msg; http://git-wip-us.apache.org/repos/asf/logging-log4j-audit/blob/06dbe889/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java ---------------------------------------------------------------------- diff --git a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java index 3a8c628..7e920cc 100644 --- a/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java +++ b/log4j-audit/log4j-audit-api/src/test/java/org/apache/logging/log4j/audit/TransferTest.java @@ -21,25 +21,32 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.commons.lang3.mutable.MutableBoolean; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.LoggingException; import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.audit.event.Transfer; +import org.apache.logging.log4j.audit.exception.AuditException; import org.apache.logging.log4j.audit.exception.ConstraintValidationException; import org.apache.logging.log4j.core.Appender; -import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.config.Configuration; -import org.apache.logging.log4j.test.appender.ListAppender; -import org.junit.Before; -import org.junit.BeforeClass; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.config.AbstractConfiguration; +import org.apache.logging.log4j.test.appender.AlwaysFailAppender; +import org.junit.Rule; import org.junit.Test; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; /** * */ public class TransferTest extends BaseEventTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + + private final String failingAppenderName = "failingAppenderName"; @Test(expected = ConstraintValidationException.class) public void testValidationFailureForMissingRequestContextAttribute() { @@ -103,13 +110,71 @@ public class TransferTest extends BaseEventTest { } List<String> msgs = app.getMessages(); assertNotNull("No messages", msgs); - assertTrue("No messages", msgs.size() == 2); + assertEquals("No messages", 2, msgs.size()); String msg = msgs.get(0); assertTrue("No companyId", msg.contains("companyId=\"12345\"")); assertTrue("No ipAddress", msg.contains("ipAddress=\"127.0.0.1\"")); assertTrue("No toAccount", msg.contains("toAccount=\"123456\"")); } + private AbstractConfiguration setUpFailingAppender() { + Logger auditLogger = (Logger) LogManager.getContext(false).getLogger("AuditLogger"); + AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); + + Appender appender = AlwaysFailAppender.createAppender(failingAppenderName); + appender.start(); + config.addLoggerAppender(auditLogger, appender); + + return config; + } + + private Transfer setUpMinimumEvent() { + ThreadContext.put("accountNumber", "12345"); + ThreadContext.put("userId", "JohnDoe"); + ThreadContext.put("loginId", "TestUser"); + + Transfer transfer = LogEventFactory.getEvent(Transfer.class); + transfer.setToAccount(123456); + transfer.setFromAccount(111111); + transfer.setAmount(new BigDecimal(111.55)); + return transfer; + } + + @Test + public void testDefaultExceptionHandlerIsInvokedOnEventLogFailure() { + AbstractConfiguration config = setUpFailingAppender(); + + exception.expect(AuditException.class); + exception.expectCause(isA(LoggingException.class)); + exception.expectMessage("Error logging event transfer"); + + Transfer transfer = setUpMinimumEvent(); + try { + transfer.logEvent(); + } finally { + config.removeAppender(failingAppenderName); + } + } + + @Test + public void testCustomExceptionHandlerIsPassedToEvent() { + AbstractConfiguration config = setUpFailingAppender(); + + MutableBoolean exceptionHandled = new MutableBoolean(false); + AuditExceptionHandler exceptionHandler = (message, ex) -> { + assertThat(ex, instanceOf(LoggingException.class)); + exceptionHandled.setTrue(); + }; + LogEventFactory.setDefaultHandler(exceptionHandler); + + Transfer transfer = setUpMinimumEvent(); + transfer.logEvent(); + + assertTrue("Exception was not handled through the custom handler", exceptionHandled.isTrue()); + + config.removeAppender(failingAppenderName); + } + @Test(expected = ConstraintValidationException.class) public void testAuditLogWithMissingRequestContextAttribute() { ThreadContext.put("userId", "JohnDoe"); @@ -162,10 +227,10 @@ public class TransferTest extends BaseEventTest { } List<String> msgs = app.getMessages(); assertNotNull("No messages", msgs); - assertTrue("No messages", msgs.size() == 1); + assertEquals("No messages", 1, msgs.size()); String msg = msgs.get(0); assertTrue("No companyId", msg.contains("companyId=\"12345\"")); assertTrue("No ipAddress", msg.contains("ipAddress=\"127.0.0.1\"")); assertTrue("No toAccount", msg.contains("toAccount=\"123456\"")); } -} +} \ No newline at end of file
