Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate log4j-iostreams to use JUnit 5 APIs and compatible helper classes #3248

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions log4j-iostreams/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
*/
package org.apache.logging.log4j.io;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.junit.Before;
import org.junit.Test;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public abstract class AbstractLoggerOutputStreamTest extends AbstractStreamTest {

Expand All @@ -35,7 +36,11 @@ public abstract class AbstractLoggerOutputStreamTest extends AbstractStreamTest

protected abstract OutputStream createOutputStreamWrapper();

@Before
AbstractLoggerOutputStreamTest(LoggerContext context) {
super(context);
}

@BeforeEach
public void createStream() {
this.wrapped = createOutputStream();
this.out = createOutputStreamWrapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@
*/
package org.apache.logging.log4j.io;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;

import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import org.junit.Before;
import org.junit.Test;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public abstract class AbstractLoggerWriterTest extends AbstractStreamTest {
protected StringWriter wrapped;
protected Writer writer;

@Before
AbstractLoggerWriterTest(LoggerContext context) {
super(context);
}

@BeforeEach
public void createStream() {
this.wrapped = createWriter();
this.writer = createWriterWrapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,29 @@
*/
package org.apache.logging.log4j.io;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.jupiter.api.BeforeEach;

@LoggerContextSource("log4j2-streams-unit-test.xml")
public abstract class AbstractStreamTest {

protected static ExtendedLogger getExtendedLogger() {
return ctx.getLogger("UnitTestLogger");
private LoggerContext context = null;

AbstractStreamTest(LoggerContext context) {
this.context = context;
}

protected ExtendedLogger getExtendedLogger() {
return context.getLogger("UnitTestLogger");
}

protected static final String NEWLINE = System.lineSeparator();
Expand All @@ -39,20 +47,19 @@ protected static ExtendedLogger getExtendedLogger() {

protected static final String LAST = "last";

@ClassRule
public static LoggerContextRule ctx = new LoggerContextRule("log4j2-streams-unit-test.xml");

protected void assertMessages(final String... messages) {
final List<String> actualMsgs = ctx.getListAppender("UnitTest").getMessages();
assertEquals("Unexpected number of results.", messages.length, actualMsgs.size());
ListAppender listApp = context.getConfiguration().getAppender("UnitTest");
final List<String> actualMsgs = listApp.getMessages();
assertEquals(messages.length, actualMsgs.size(), "Unexpected number of results.");
for (int i = 0; i < messages.length; i++) {
final String start = LEVEL.name() + ' ' + messages[i];
assertThat(actualMsgs.get(i), startsWith(start));
}
}

@Before
@BeforeEach
public void clearAppender() {
ctx.getListAppender("UnitTest").clear();
ListAppender listApp = context.getConfiguration().getAppender("UnitTest");
listApp.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,45 @@
*/
package org.apache.logging.log4j.io;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.junit.jupiter.api.BeforeEach;

@LoggerContextSource("log4j2-streams-calling-info.xml")
public class IoBuilderCallerInfoTesting {

protected static Logger getExtendedLogger() {
return ctx.getLogger("ClassAndMethodLogger");
private LoggerContext context = null;

IoBuilderCallerInfoTesting(LoggerContext context) {
this.context = context;
}

protected Logger getExtendedLogger() {
return context.getLogger("ClassAndMethodLogger");
}

protected static Logger getLogger() {
protected Logger getLogger() {
return getExtendedLogger();
}

protected static final Level LEVEL = Level.WARN;

@ClassRule
public static LoggerContextRule ctx = new LoggerContextRule("log4j2-streams-calling-info.xml");

public void assertMessages(final String msg, final int size, final String methodName) {
final ListAppender appender = ctx.getListAppender("ClassAndMethod");
assertEquals(msg + ".size", size, appender.getMessages().size());
final ListAppender appender = context.getConfiguration().getAppender("ClassAndMethod");
assertEquals(size, appender.getMessages().size(), msg + ".size");
for (final String message : appender.getMessages()) {
assertEquals(msg + " has incorrect caller info", this.getClass().getName() + '.' + methodName, message);
assertEquals(this.getClass().getName() + '.' + methodName, message, msg + " has incorrect caller info");
}
}

@Before
@BeforeEach
public void clearAppender() {
ctx.getListAppender("ClassAndMethod").clear();
ListAppender listApp = context.getConfiguration().getAppender("ClassAndMethod");
listApp.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,33 @@
*/
package org.apache.logging.log4j.io;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

import java.io.PrintStream;
import java.util.List;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.appender.ListAppender;
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
import org.junit.ClassRule;
import org.junit.Test;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.junit.jupiter.api.Test;

@LoggerContextSource("log4j2-streams-calling-info.xml")
public class IoBuilderTest {

@ClassRule
public static LoggerContextRule context = new LoggerContextRule("log4j2-streams-calling-info.xml");
private LoggerContext context = null;

IoBuilderTest(LoggerContext context) {
this.context = context;
}

@Test
public void testNoArgBuilderCallerClassInfo() throws Exception {
try (final PrintStream ps = IoBuilder.forLogger().buildPrintStream()) {
ps.println("discarded");
final ListAppender app = context.getListAppender("IoBuilderTest");
final ListAppender app = context.getConfiguration().getAppender("IoBuilderTest");
final List<String> messages = app.getMessages();
assertThat(messages, not(empty()));
assertThat(messages, hasSize(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.junit.Before;
import org.junit.Test;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class LoggerBufferedInputStreamCallerInfoTest extends IoBuilderCallerInfoTesting {

private BufferedInputStream logIn;

LoggerBufferedInputStreamCallerInfoTest(LoggerContext context) {
super(context);
}

@Test
public void close() throws Exception {
this.logIn.read();
Expand Down Expand Up @@ -58,7 +63,7 @@ public void readBytesOffsetLen() throws Exception {
this.logIn.close();
}

@Before
@BeforeEach
public void setupStreams() {
final InputStream srcInputStream = new ByteArrayInputStream("a\nb\nc\nd".getBytes());
this.logIn = (BufferedInputStream) IoBuilder.forLogger(getLogger())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@

import java.io.InputStream;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LoggerContext;

public class LoggerBufferedInputStreamTest extends LoggerInputStreamTest {

LoggerBufferedInputStreamTest(LoggerContext context) {
super(context);
}

@Override
protected InputStream createInputStream() {
return IoBuilder.forLogger(getExtendedLogger())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
import java.io.StringReader;
import java.nio.CharBuffer;
import org.apache.logging.log4j.Level;
import org.junit.Before;
import org.junit.Test;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class LoggerBufferedReaderCallerInfoTest extends IoBuilderCallerInfoTesting {

BufferedReader logReader;

LoggerBufferedReaderCallerInfoTest(LoggerContext context) {
super(context);
}

@Test
public void close() throws Exception {
this.logReader.readLine();
Expand Down Expand Up @@ -76,7 +81,7 @@ public void readLine() throws Exception {
this.logReader.close();
}

@Before
@BeforeEach
public void setupReader() {
final Reader srcReader = new StringReader("a\nb\nc\nd");
this.logReader = (BufferedReader) IoBuilder.forLogger(getLogger())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
*/
package org.apache.logging.log4j.io;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.BufferedReader;
import java.io.Reader;
import org.junit.Test;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.jupiter.api.Test;

public class LoggerBufferedReaderTest extends LoggerReaderTest {
private BufferedReader bufferedReader;

LoggerBufferedReaderTest(LoggerContext context) {
super(context);
}

@Override
protected Reader createReader() {
return this.bufferedReader = (BufferedReader) IoBuilder.forLogger(getExtendedLogger())
Expand All @@ -36,9 +41,9 @@ protected Reader createReader() {

@Test
public void testReadLine() throws Exception {
assertEquals("first line", FIRST, this.bufferedReader.readLine());
assertEquals(FIRST, this.bufferedReader.readLine(), "first line");
assertMessages(FIRST);
assertEquals("second line", LAST, this.bufferedReader.readLine());
assertEquals(LAST, this.bufferedReader.readLine(), "second line");
assertMessages(FIRST, LAST);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LoggerContext;

public class LoggerFilterOutputStreamTest extends AbstractLoggerOutputStreamTest {

LoggerFilterOutputStreamTest(LoggerContext context) {
super(context);
}

@Override
protected ByteArrayOutputStream createOutputStream() {
return new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@

import java.io.StringWriter;
import java.io.Writer;
import org.apache.logging.log4j.core.LoggerContext;

public class LoggerFilterWriterTest extends AbstractLoggerWriterTest {

LoggerFilterWriterTest(LoggerContext context) {
super(context);
}

@Override
protected StringWriter createWriter() {
return new StringWriter();
Expand Down
Loading