#502 Adjusted tests for logger introduction
This commit is contained in:
@ -22,24 +22,23 @@
|
||||
*/
|
||||
package com.iluwatar.doublechecked.locking;
|
||||
|
||||
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.mockito.ArgumentCaptor;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Date: 12/10/15 - 9:34 PM
|
||||
@ -48,31 +47,16 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
*/
|
||||
public class InventoryTest {
|
||||
|
||||
/**
|
||||
* The mocked standard out {@link PrintStream}, used to verify a steady increasing size of the
|
||||
* {@link Inventory} while adding items from multiple threads concurrently
|
||||
*/
|
||||
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(Inventory.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||
*/
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.setOut(this.stdOutOrig);
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,21 +96,32 @@ public class InventoryTest {
|
||||
assertNotNull(items);
|
||||
assertEquals(INVENTORY_SIZE, items.size());
|
||||
|
||||
// Capture all stdOut messages ...
|
||||
final ArgumentCaptor<String> stdOutCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(this.stdOutMock, times(INVENTORY_SIZE)).println(stdOutCaptor.capture());
|
||||
|
||||
// ... verify if we got all 1000
|
||||
final List<String> values = stdOutCaptor.getAllValues();
|
||||
assertEquals(INVENTORY_SIZE, values.size());
|
||||
assertEquals(INVENTORY_SIZE, appender.getLogSize());
|
||||
|
||||
// ... and check if the inventory size is increasing continuously
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
assertNotNull(values.get(i));
|
||||
assertTrue(values.get(i).contains("items.size()=" + (i + 1)));
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
assertTrue(appender.log.get(i).getFormattedMessage().contains("items.size()=" + (i + 1)));
|
||||
}
|
||||
|
||||
verifyNoMoreInteractions(this.stdOutMock);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private class InMemoryAppender extends AppenderBase<ILoggingEvent> {
|
||||
private List<ILoggingEvent> log = new LinkedList<>();
|
||||
|
||||
public InMemoryAppender(Class clazz) {
|
||||
((Logger) LoggerFactory.getLogger(clazz)).addAppender(this);
|
||||
start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
log.add(eventObject);
|
||||
}
|
||||
|
||||
public int getLogSize() {
|
||||
return log.size();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user