#502 Adjusted tests for logger introduction
This commit is contained in:
@ -22,28 +22,44 @@
|
||||
*/
|
||||
package com.iluwatar.delegation.simple;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import com.iluwatar.delegation.simple.printers.CanonPrinter;
|
||||
import com.iluwatar.delegation.simple.printers.EpsonPrinter;
|
||||
import com.iluwatar.delegation.simple.printers.HpPrinter;
|
||||
import org.junit.Rule;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.SystemOutRule;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DelegateTest {
|
||||
|
||||
private static final String MESSAGE = "Test Message Printed";
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Rule
|
||||
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
private static final String MESSAGE = "Test Message Printed";
|
||||
|
||||
@Test
|
||||
public void testCanonPrinter() throws Exception {
|
||||
PrinterController printerController = new PrinterController(new CanonPrinter());
|
||||
printerController.print(MESSAGE);
|
||||
|
||||
assertEquals("Canon Printer : Test Message Printed", systemOutRule.getLog());
|
||||
assertEquals("Canon Printer : Test Message Printed", appender.getLastMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -51,7 +67,7 @@ public class DelegateTest {
|
||||
PrinterController printerController = new PrinterController(new HpPrinter());
|
||||
printerController.print(MESSAGE);
|
||||
|
||||
assertEquals("HP Printer : Test Message Printed", systemOutRule.getLog());
|
||||
assertEquals("HP Printer : Test Message Printed", appender.getLastMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -59,7 +75,30 @@ public class DelegateTest {
|
||||
PrinterController printerController = new PrinterController(new EpsonPrinter());
|
||||
printerController.print(MESSAGE);
|
||||
|
||||
assertEquals("Epson Printer : Test Message Printed", systemOutRule.getLog());
|
||||
assertEquals("Epson Printer : Test Message Printed", appender.getLastMessage());
|
||||
}
|
||||
|
||||
private class InMemoryAppender extends AppenderBase<ILoggingEvent> {
|
||||
|
||||
private List<ILoggingEvent> log = new LinkedList<>();
|
||||
|
||||
public InMemoryAppender() {
|
||||
((Logger) LoggerFactory.getLogger("root")).addAppender(this);
|
||||
start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
log.add(eventObject);
|
||||
}
|
||||
|
||||
public String getLastMessage() {
|
||||
return log.get(log.size() - 1).getFormattedMessage();
|
||||
}
|
||||
|
||||
public int getLogSize() {
|
||||
return log.size();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user