From 7aca6f6f5c7fddc8daed3614c38845650b30239d Mon Sep 17 00:00:00 2001 From: Andrea Gazzarini Date: Tue, 17 Mar 2015 15:41:18 +0100 Subject: [PATCH] [ issue #15 ] Log (unit) test case --- .../java/org/gazzax/labs/solrdf/log/Log.java | 28 ++--- .../gazzax/labs/solrdf/log/LogTestCase.java | 114 ++++++++++++++++++ 2 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 solrdf/src/test/java/org/gazzax/labs/solrdf/log/LogTestCase.java diff --git a/solrdf/src/main/java/org/gazzax/labs/solrdf/log/Log.java b/solrdf/src/main/java/org/gazzax/labs/solrdf/log/Log.java index a328701..e1d0dc8 100644 --- a/solrdf/src/main/java/org/gazzax/labs/solrdf/log/Log.java +++ b/solrdf/src/main/java/org/gazzax/labs/solrdf/log/Log.java @@ -1,7 +1,7 @@ package org.gazzax.labs.solrdf.log; import org.slf4j.Logger; - +import static org.gazzax.labs.solrdf.log.MessageFactory.*; /** * Logger wrapper. * A simple SLF4j wrapper that avoids "if" statements within the code. @@ -11,7 +11,7 @@ */ public class Log { - private final Logger _logger; + private final Logger logger; /** * Builds a new Log wrapper with a given logger. @@ -19,7 +19,7 @@ public class Log { * @param logger the {@link Logger} implementation. */ public Log(final Logger logger) { - _logger = logger; + this.logger = logger; } /** @@ -28,7 +28,7 @@ public Log(final Logger logger) { * @param message the log message. */ public void info(final String message) { - _logger.info(message); + logger.info(message); } /** @@ -38,7 +38,7 @@ public void info(final String message) { * @param values values that will replace the message placeholders. */ public void info(final String message, final Object ... values) { - _logger.info(String.format(message, values)); + logger.info(createMessage(message, values)); } /** @@ -47,7 +47,7 @@ public void info(final String message, final Object ... values) { * @param message the log message. */ public void debug(final String message) { - _logger.debug(message); + logger.debug(message); } /** @@ -57,8 +57,8 @@ public void debug(final String message) { * @param values values that will replace the message placeholders. */ public void debug(final String message, final Object ... values) { - if (_logger.isDebugEnabled()) { - _logger.debug(String.format(message, values)); + if (isDebugEnabled()) { + logger.debug(createMessage(message, values)); } } @@ -69,7 +69,7 @@ public void debug(final String message, final Object ... values) { * @param values values that will replace the message placeholders. */ public void error(final String message, final Object ... values) { - _logger.error(String.format(message, values)); + logger.error(createMessage(message, values)); } /** @@ -80,7 +80,7 @@ public void error(final String message, final Object ... values) { * @param values values that will replace the message placeholders. */ public void error(final String message, final Throwable cause, final Object ... values) { - _logger.error(String.format(message, values)); + logger.error(createMessage(message, values), cause); } /** @@ -90,7 +90,7 @@ public void error(final String message, final Throwable cause, final Object ... * @param cause the underlying cause. */ public void error(final String message, final Throwable cause) { - _logger.error(message, cause); + logger.error(message, cause); } /** @@ -99,7 +99,7 @@ public void error(final String message, final Throwable cause) { * @param message the log message. */ public void warning(final String message) { - _logger.warn(message); + logger.warn(message); } /** @@ -109,7 +109,7 @@ public void warning(final String message) { * @param values values that will replace the message placeholders. */ public void warning(final String message, final Object ... values) { - _logger.warn(String.format(message, values)); + logger.warn(createMessage(message, values)); } /** @@ -118,6 +118,6 @@ public void warning(final String message, final Object ... values) { * @return true if the DEBUG level has been enabled for this logger. */ public boolean isDebugEnabled() { - return _logger.isDebugEnabled(); + return logger.isDebugEnabled(); } } \ No newline at end of file diff --git a/solrdf/src/test/java/org/gazzax/labs/solrdf/log/LogTestCase.java b/solrdf/src/test/java/org/gazzax/labs/solrdf/log/LogTestCase.java new file mode 100644 index 0000000..fa75e37 --- /dev/null +++ b/solrdf/src/test/java/org/gazzax/labs/solrdf/log/LogTestCase.java @@ -0,0 +1,114 @@ +package org.gazzax.labs.solrdf.log; + +import static org.mockito.Mockito.*; +import static org.gazzax.labs.solrdf.TestUtility.*; + +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; + +/** + * Test case for {@link Log} class. + * + * @author Andrea Gazzarini + * @since 1.0 + */ +public class LogTestCase { + final Object [] randomValues = { randomString(), randomString() }; + + private Log log; + private Logger logger; + + /** + * Setup fixture for this test case. + */ + @Before + public void setUp() { + logger = mock(Logger.class); + log = new Log(logger); + } + + @Test + public void infoWithMessage() { + final String message = randomString(); + log.info(message); + verify(logger).info(message); + } + + @Test + public void infoWithMessageAndPlaceHolders() { + final String message = randomString(); + final Object [] values = {randomString(), randomString()}; + + log.info(message, values); + verify(logger).info(anyString()); + } + + @Test + public void debugWithMessage() { + final String message = randomString(); + log.debug(message); + verify(logger).debug(message); + } + + @Test + public void debugWithMessageAndPlaceHolders() { + final String message = randomString(); + final Object [] values = {randomString(), randomString()}; + + when(logger.isDebugEnabled()).thenReturn(true); + + log.debug(message, values); + verify(logger).isDebugEnabled(); + verify(logger).debug(anyString()); + } + + @Test + public void debugWithDebugLevelDisabled() { + final String message = randomString(); + final Object [] values = {randomString(), randomString()}; + + when(logger.isDebugEnabled()).thenReturn(false); + + log.debug(message, values); + verify(logger).isDebugEnabled(); + verifyNoMoreInteractions(logger); + } + + @Test + public void errorWithMessageAndPlaceHolders() { + log.error(randomString(), randomValues); + verify(logger).error(anyString()); + } + + @Test + public void errorWithMessageThrowableAndPlaceHolders() { + final Exception exception = new Exception(); + log.error(randomString(), exception, randomValues); + verify(logger).error(anyString(), any(Exception.class)); + } + + @Test + public void errorWithMessageAndThrowable() { + final String message = randomString(); + final Exception exception = new Exception(); + + log.error(message, exception); + verify(logger).error(message, exception); + } + + @Test + public void warningWithMessage() { + final String message = randomString(); + log.warning(message); + verify(logger).warn(message); + } + + @Test + public void warningWithMessageAndPlaceHolders() { + final String message = randomString(); + + log.warning(message, randomValues); + verify(logger).warn(anyString()); + } +} \ No newline at end of file