#502 Adjusted tests for logger introduction
This commit is contained in:
@ -23,21 +23,35 @@
|
||||
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import com.iluwatar.reader.writer.lock.utils.InMemoryAppender;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class ReaderAndWriterTest extends StdOutTest {
|
||||
public class ReaderAndWriterTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReaderAndWriterTest.class);
|
||||
|
||||
@ -65,11 +79,10 @@ public class ReaderAndWriterTest extends StdOutTest {
|
||||
LOGGER.error("Error waiting for ExecutorService shutdown", e);
|
||||
}
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 finish");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 finish");
|
||||
assertTrue(appender.logContains("Reader 1 begin"));
|
||||
assertTrue(appender.logContains("Reader 1 finish"));
|
||||
assertTrue(appender.logContains("Writer 1 begin"));
|
||||
assertTrue(appender.logContains("Writer 1 finish"));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,11 +109,10 @@ public class ReaderAndWriterTest extends StdOutTest {
|
||||
LOGGER.error("Error waiting for ExecutorService shutdown", e);
|
||||
}
|
||||
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 finish");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 finish");
|
||||
assertTrue(appender.logContains("Writer 1 begin"));
|
||||
assertTrue(appender.logContains("Writer 1 finish"));
|
||||
assertTrue(appender.logContains("Reader 1 begin"));
|
||||
assertTrue(appender.logContains("Reader 1 finish"));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,22 +22,36 @@
|
||||
*/
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import com.iluwatar.reader.writer.lock.utils.InMemoryAppender;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class ReaderTest extends StdOutTest {
|
||||
public class ReaderTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender(Reader.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReaderTest.class);
|
||||
|
||||
@ -66,11 +80,9 @@ public class ReaderTest extends StdOutTest {
|
||||
|
||||
// Read operation will hold the read lock 250 milliseconds, so here we prove that multiple reads
|
||||
// can be performed in the same time.
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 2 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 1 finish");
|
||||
inOrder.verify(getStdOutMock()).println("Reader 2 finish");
|
||||
|
||||
assertTrue(appender.logContains("Reader 1 begin"));
|
||||
assertTrue(appender.logContains("Reader 2 begin"));
|
||||
assertTrue(appender.logContains("Reader 1 finish"));
|
||||
assertTrue(appender.logContains("Reader 2 finish"));
|
||||
}
|
||||
}
|
||||
|
@ -1,76 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Date: 12/10/15 - 8:37 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public abstract class StdOutTest {
|
||||
|
||||
/**
|
||||
* The mocked standard out {@link PrintStream}, required since some actions don't have any
|
||||
* influence on accessible objects, except for writing to std-out using {@link System#out}
|
||||
*/
|
||||
private final PrintStream stdOutMock = mock(PrintStream.class);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||
*/
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.setOut(this.stdOutOrig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mocked stdOut {@link PrintStream}
|
||||
*
|
||||
* @return The stdOut print stream mock, renewed before each test
|
||||
*/
|
||||
final PrintStream getStdOutMock() {
|
||||
return this.stdOutMock;
|
||||
}
|
||||
|
||||
}
|
@ -22,22 +22,36 @@
|
||||
*/
|
||||
package com.iluwatar.reader.writer.lock;
|
||||
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import com.iluwatar.reader.writer.lock.utils.InMemoryAppender;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* @author hongshuwei@gmail.com
|
||||
*/
|
||||
public class WriterTest extends StdOutTest {
|
||||
public class WriterTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender(Writer.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WriterTest.class);
|
||||
|
||||
@ -67,10 +81,9 @@ public class WriterTest extends StdOutTest {
|
||||
// Write operation will hold the write lock 250 milliseconds, so here we verify that when two
|
||||
// writer execute concurrently, the second writer can only writes only when the first one is
|
||||
// finished.
|
||||
final InOrder inOrder = inOrder(getStdOutMock());
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 1 finish");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 2 begin");
|
||||
inOrder.verify(getStdOutMock()).println("Writer 2 finish");
|
||||
assertTrue(appender.logContains("Writer 1 begin"));
|
||||
assertTrue(appender.logContains("Writer 1 finish"));
|
||||
assertTrue(appender.logContains("Writer 2 begin"));
|
||||
assertTrue(appender.logContains("Writer 2 finish"));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.reader.writer.lock.utils;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class InMemoryAppender extends AppenderBase<ILoggingEvent> {
|
||||
private List<ILoggingEvent> log = new LinkedList<>();
|
||||
|
||||
public InMemoryAppender(Class clazz) {
|
||||
((Logger) LoggerFactory.getLogger(clazz)).addAppender(this);
|
||||
start();
|
||||
}
|
||||
|
||||
public InMemoryAppender() {
|
||||
((Logger) LoggerFactory.getLogger("root")).addAppender(this);
|
||||
start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
log.add(eventObject);
|
||||
}
|
||||
|
||||
public boolean logContains(String message) {
|
||||
return log.stream().anyMatch(event -> event.getFormattedMessage().equals(message));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user