Added LogHandler Unittest for formated log messages. Additionally refactored LogHandlerTest class.
@@ -2,7 +2,6 @@ package io.jenkins.plugins.analysis.core.util;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
-import java.io.UnsupportedEncodingException;
import org.junit.jupiter.api.Test;
@@ -14,44 +13,53 @@ import static io.jenkins.plugins.analysis.core.assertions.Assertions.*;
import static org.mockito.Mockito.*;
/**
- * Tests the class {@link LogHandlerTest}.
+ * Tests the class {@link LogHandler}.
*
* @author Andreas Neumeier
*/
class LogHandlerTest {
private static final String LOG_HANDLER_NAME = "TestHandler";
private static final String MESSAGE = "TestMessage";
- private static final TaskListener TASK_LISTENER = mock(TaskListener.class);
+
+ LogHandler createMockedLogHandler(final PrintStream printStream) {
+ TaskListener taskListener = mock(TaskListener.class);
+ when(taskListener.getLogger()).thenReturn(printStream);
+ return spy(new LogHandler(taskListener, LOG_HANDLER_NAME));
+ }
@Test
- void shouldLogErrorMessage() {
+ void shouldLogInfoAndErrorMessage() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
-
- when(TASK_LISTENER.getLogger()).thenReturn(printStream);
- LogHandler logHandler = spy(new LogHandler(TASK_LISTENER, LOG_HANDLER_NAME));
+ LogHandler logHandler = createMockedLogHandler(printStream);
Report report = new Report();
+ report.logInfo(MESSAGE);
report.logError(MESSAGE);
logHandler.log(report);
- String output = outputStream.toString().replace(System.getProperty("line.separator"), "");
- assertThat(output).isEqualTo("[" + LOG_HANDLER_NAME + "] [-ERROR-] " + MESSAGE);
+ // Assert if the output has the following structure:
+ // "[LOG_HANDLER_NAME] [-ERROR-] MESSAGE\n
+ // [LOG_HANDLER_NAME] MESSAGE\n"
+ assertThat(outputStream.toString()).isEqualTo(String.format(
+ "[%s] [-ERROR-] %s" + System.getProperty("line.separator")
+ + "[%s] %s" + System.getProperty("line.separator"),
+ LOG_HANDLER_NAME, MESSAGE, LOG_HANDLER_NAME, MESSAGE));
}
@Test
- void shouldLogInfoMessage() {
+ void shouldLogFormattedMessage() {
+ final String logFormat = "Skipping '%s'";
+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
+ LogHandler logHandler = createMockedLogHandler(printStream);
- when(TASK_LISTENER.getLogger()).thenReturn(printStream);
- LogHandler logHandler = spy(new LogHandler(TASK_LISTENER, LOG_HANDLER_NAME));
+ logHandler.log(logFormat, MESSAGE);
- Report report = new Report();
- report.logInfo(MESSAGE);
- logHandler.log(report);
-
- String output = outputStream.toString().replace(System.getProperty("line.separator"), "");
- assertThat(output).isEqualTo("[" + LOG_HANDLER_NAME + "] " + MESSAGE);
+ assertThat(outputStream.toString()).isEqualTo(
+ String.format("[%s] " + logFormat + System.getProperty("line.separator"), LOG_HANDLER_NAME, MESSAGE)
+ );
}
+
}
\ No newline at end of file
