Set up testing

This commit is contained in:
Nicolai Parlog 2020-03-10 21:22:49 +01:00 committed by Ceki Gulcu
parent e2f0fca4ad
commit b67dabb03d
2 changed files with 90 additions and 0 deletions

View File

@ -23,6 +23,19 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.0-alpha0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,77 @@
package org.slf4j.jdk;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.ServiceLoader;
import static org.junit.Assert.*;
public class SLF4JSystemLoggerTest {
private static final PrintStream ERROR_OUT = System.err;
private static final ByteArrayOutputStream OUTPUT = new ByteArrayOutputStream();
private static final PrintStream ERROR_OUT_REPLACEMENT = new PrintStream(OUTPUT);
@Before
public void setUp() {
System.setErr(ERROR_OUT_REPLACEMENT);
}
@After
public void tearDown() {
System.setErr(ERROR_OUT);
}
@Test
public void loggerFinderLoadedAsOnlyService() {
// this method asserts that there is exactly one `LoggerFinder` and its of the correct type
getSLF4JSystemLoggerFinder();
}
@Test
public void loggerFinderReturnsLogger() {
var logger = getSLF4JSystemLogger();
assertNotNull("LoggerFinder must return logger", logger);
}
@Test
public void loggerLogsMessage() {
var logger = getSLF4JSystemLogger();
var message = "Test system logging!";
logger.log(System.Logger.Level.INFO, message);
String output = getOutput();
assertTrue("Captured output should contain logged message.", output.contains(message));
}
private static System.LoggerFinder getSLF4JSystemLoggerFinder() {
var loggerFinders = new ArrayList<System.LoggerFinder>();
// this fails when test is executed from the module path
// because the module declaration does not declare
// `uses System.LoggerFinder`
ServiceLoader.load(System.LoggerFinder.class).forEach(loggerFinders::add);
assertEquals("There should be exactly one `LoggerFinder`.", 1, loggerFinders.size());
var loggerFinder = loggerFinders.get(0);
assertEquals("The `LoggerFinder` should be of type `SLF4JSystemLoggerFinder`.",
SLF4JSystemLoggerFinder.class,
loggerFinder.getClass());
return loggerFinder;
}
private static System.Logger getSLF4JSystemLogger() {
var loggerFinder = getSLF4JSystemLoggerFinder();
return loggerFinder.getLogger("TestLogger", SLF4JSystemLoggerTest.class.getModule());
}
private static String getOutput() {
ERROR_OUT_REPLACEMENT.flush();
return OUTPUT.toString();
}
}