#502 Adjusted tests for logger introduction

This commit is contained in:
daniel-bryla
2016-11-04 11:47:06 +01:00
parent 27d6d500bc
commit e138163c4f
56 changed files with 1447 additions and 1602 deletions

View File

@ -22,17 +22,19 @@
*/
package com.iluwatar.facade;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.List;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Date: 12/9/15 - 9:40 PM
@ -41,35 +43,19 @@ import static org.mockito.internal.verification.VerificationModeFactory.times;
*/
public class DwarvenGoldmineFacadeTest {
/**
* The mocked standard out {@link PrintStream}, required since the actions on the gold mine facade
* don't have any influence on any other accessible objects, except for writing to std-out using
* {@link System#out}
*/
private final PrintStream stdOutMock = mock(PrintStream.class);
private InMemoryAppender appender;
/**
* Keep the original std-out so it can be restored after the test
*/
private final PrintStream stdOutOrig = System.out;
/**
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
*/
@Before
public void setUp() {
System.setOut(this.stdOutMock);
appender = new InMemoryAppender();
}
/**
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
*/
@After
public void tearDown() {
System.setOut(this.stdOutOrig);
appender.stop();
}
/**
/**
* Test a complete day cycle in the gold mine by executing all three different steps: {@link
* DwarvenGoldmineFacade#startNewDay()}, {@link DwarvenGoldmineFacade#digOutGold()} and {@link
* DwarvenGoldmineFacade#endDay()}.
@ -82,44 +68,68 @@ public class DwarvenGoldmineFacadeTest {
goldMine.startNewDay();
// On the start of a day, all workers should wake up ...
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger wakes up."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator wakes up."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger wakes up."));
assertTrue(appender.logContains("Dwarf gold digger wakes up."));
assertTrue(appender.logContains("Dwarf cart operator wakes up."));
assertTrue(appender.logContains("Dwarven tunnel digger wakes up."));
// ... and go to the mine
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger goes to the mine."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator goes to the mine."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger goes to the mine."));
assertTrue(appender.logContains("Dwarf gold digger goes to the mine."));
assertTrue(appender.logContains("Dwarf cart operator goes to the mine."));
assertTrue(appender.logContains("Dwarven tunnel digger goes to the mine."));
// No other actions were invoked, so the workers shouldn't have done (printed) anything else
verifyNoMoreInteractions(this.stdOutMock);
assertEquals(6, appender.getLogSize());
// Now do some actual work, start digging gold!
goldMine.digOutGold();
// Since we gave the dig command, every worker should be doing it's job ...
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger digs for gold."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator moves gold chunks out of the mine."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger creates another promising tunnel."));
assertTrue(appender.logContains("Dwarf gold digger digs for gold."));
assertTrue(appender.logContains("Dwarf cart operator moves gold chunks out of the mine."));
assertTrue(appender.logContains("Dwarven tunnel digger creates another promising tunnel."));
// Again, they shouldn't be doing anything else.
verifyNoMoreInteractions(this.stdOutMock);
assertEquals(9, appender.getLogSize());
// Enough gold, lets end the day.
goldMine.endDay();
// Check if the workers go home ...
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger goes home."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator goes home."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger goes home."));
assertTrue(appender.logContains("Dwarf gold digger goes home."));
assertTrue(appender.logContains("Dwarf cart operator goes home."));
assertTrue(appender.logContains("Dwarven tunnel digger goes home."));
// ... and go to sleep. We need well rested workers the next day :)
verify(this.stdOutMock, times(1)).println(eq("Dwarf gold digger goes to sleep."));
verify(this.stdOutMock, times(1)).println(eq("Dwarf cart operator goes to sleep."));
verify(this.stdOutMock, times(1)).println(eq("Dwarven tunnel digger goes to sleep."));
assertTrue(appender.logContains("Dwarf gold digger goes to sleep."));
assertTrue(appender.logContains("Dwarf cart operator goes to sleep."));
assertTrue(appender.logContains("Dwarven tunnel digger goes to sleep."));
// Every worker should be sleeping now, no other actions allowed
verifyNoMoreInteractions(this.stdOutMock);
assertEquals(15, appender.getLogSize());
}
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 int getLogSize() {
return log.size();
}
public boolean logContains(String message) {
return log.stream().anyMatch(event -> event.getFormattedMessage().equals(message));
}
}
}