Skip to content

Commit

Permalink
#489 Make slf4j-api provided scope and some tests of using slf4j module.
Browse files Browse the repository at this point in the history
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
danfickle committed Jul 11, 2020
1 parent 3a3ad28 commit 7b223c2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
30 changes: 27 additions & 3 deletions openhtmltopdf-slf4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,39 @@

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.19</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.19</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.19</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
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.
}
}
}

0 comments on commit 7b223c2

Please sign in to comment.