Skip to content

Commit

Permalink
Add CompoundJobLoggerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
fabri1983 committed Sep 21, 2019
1 parent d8ea3ea commit ca3ab12
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.fabri1983.refactorexample.joblogger.enhanced.implementation;

import java.io.File;
import java.io.IOException;

import org.fabri1983.refactorexample.joblogger.category.AllLoggersCategoryTest;
import org.fabri1983.refactorexample.joblogger.category.EnhancedLoggerCategoryTest;
import org.fabri1983.refactorexample.joblogger.enhanced.contract.IEnhancedJobLogger;
import org.fabri1983.refactorexample.joblogger.enhanced.factory.JobLoggerFactory;
import org.fabri1983.refactorexample.joblogger.util.StandardConsoleRedirector;
import org.fabri1983.refactorexample.joblogger.util.XmlDomUtil;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TemporaryFolder;

@Category({ EnhancedLoggerCategoryTest.class, AllLoggersCategoryTest.class })
public class CompoundJobLoggerTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void whenCreatingCompoundJobLogger_thenLoggersAreCreated() throws IOException {

// given: a log file in temporary folder
File tempFile = temporaryFolder.newFile("logFile.txt");

// given: a File Job Logger
IEnhancedJobLogger fileLogger = JobLoggerFactory.newFileJobLogger(tempFile);

// given: a Console Job Logger
IEnhancedJobLogger consoleLogger = JobLoggerFactory.newConsoleJobLogger();

// given: a logger compounded by two loggers
IEnhancedJobLogger compoundLogger = JobLoggerFactory.newCompoundJobLogger(fileLogger, consoleLogger);

// then: job loggers were created
Assert.assertNotNull(fileLogger);
Assert.assertNotNull(consoleLogger);
Assert.assertNotNull(compoundLogger);
}

@Test
public void whenCreatingCompoundJobLogger_thenMessageIsLoggedInEachLogger() throws Exception {

// given: a log file in temporary folder
File tempFile = temporaryFolder.newFile("logFile.txt");

// given: a custom console
StandardConsoleRedirector.saveStdErr(); // JobLogger's ConsoleHandler prints out to StdErr
StandardConsoleRedirector.setCustomConsole();

// given: a File Job Logger
IEnhancedJobLogger fileLogger = JobLoggerFactory.newFileJobLogger(tempFile);

// given: a Console Job Logger
IEnhancedJobLogger consoleLogger = JobLoggerFactory.newConsoleJobLogger();

// given: a logger compounded by two loggers
IEnhancedJobLogger compoundLogger = JobLoggerFactory.newCompoundJobLogger(fileLogger, consoleLogger);

// when: logging a message on the compound logger
String infoMessage = "this is an info message";
compoundLogger.info(infoMessage);

// then: retrieve logged message from custom console
String consoleLoggedMessage = StandardConsoleRedirector.getCurrentConsoleContent();

// then: restore system console
StandardConsoleRedirector.restoreStd();
StandardConsoleRedirector.closeCustomConsole();

// then: get logged message from file
String fileLoggedMessage = XmlDomUtil.getMessagesFromXmlFile(tempFile).get(0);

// then: console message contains expected message
Assert.assertTrue(consoleLoggedMessage.contains(infoMessage));
// then: file message matches expected message
Assert.assertEquals(infoMessage, fileLoggedMessage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void whenCreatingLogWithConsoleOuput_thenConsoleContainsMessage() throws
String errorMessage = "error message";
logger.error(errorMessage);

// then: retrieve logged message from cusotm console
// then: retrieve logged message from custom console
String loggedMessages = StandardConsoleRedirector.getCurrentConsoleContent();

// then: restore system console
Expand Down

0 comments on commit ca3ab12

Please sign in to comment.