-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#489 Make slf4j-api provided scope and some tests of using slf4j module.
Making slf4j-api provided scope should reduce dependency hell for end users as almost all will already have slf4j-api on their classpath. Also added a couple of manual tests to make sure module is working as expected.
- Loading branch information
Showing
2 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
openhtmltopdf-slf4j/src/test/java/com/openhtmltopdf/slf4j/Slf4JLoggerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.openhtmltopdf.slf4j; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
|
||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder; | ||
import com.openhtmltopdf.util.XRLog; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
|
||
public class Slf4JLoggerTest { | ||
|
||
@BeforeClass | ||
public static void levelFormat() { | ||
// We use this property to differentiate log messages | ||
// going through slf4j to those going through the default | ||
// java.util.logging logger implementation. | ||
System.setProperty("org.slf4j.simpleLogger.levelInBrackets", "true"); | ||
} | ||
|
||
/** | ||
* This is a simple automatic test to ensure log messages are | ||
* delivered by slf4j in the console. | ||
*/ | ||
@Test | ||
public void testLogger() throws IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
PrintStream ps = new PrintStream(baos, true, "UTF-8"); | ||
PrintStream old = System.err; | ||
System.setErr(ps); | ||
|
||
XRLog.setLoggerImpl(new Slf4jLogger()); | ||
runWithLogOutput(); | ||
|
||
ps.flush(); | ||
String log = baos.toString("UTF-8"); | ||
|
||
old.println(log); | ||
System.setErr(old); | ||
|
||
Assert.assertThat(log, containsString("] [INFO]")); | ||
Assert.assertThat(log, containsString("] [WARN]")); | ||
} | ||
|
||
/** | ||
* This is a manual test to show that log level can be changed | ||
* by the concrete log implementation. | ||
* | ||
* It should not output info level messages. | ||
* | ||
* NOTE: This test should be run in isolation as the system property | ||
* only takes affect if set before the logger is first created. | ||
*/ | ||
@Test | ||
public void testLoggerWarnLevel() throws IOException { | ||
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "warn"); | ||
XRLog.setLoggerImpl(new Slf4jLogger()); | ||
|
||
runWithLogOutput(); | ||
} | ||
|
||
private void runWithLogOutput() throws IOException { | ||
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { | ||
PdfRendererBuilder builder = new PdfRendererBuilder(); | ||
|
||
builder.useFastMode(); | ||
builder.toStream(os); | ||
builder.withHtmlContent("<>INVALID-XML", null); | ||
builder.run(); | ||
} catch (Exception e) { | ||
// Ignore. | ||
} | ||
} | ||
} |