Merge pull request #12130 from hkhan/JAVA-11417-fix-log4j2-test-failure
[JAVA-11417] Fix log4j2 JSON integration test
This commit is contained in:
commit
c3e0c5b1fb
|
@ -1,47 +1,58 @@
|
||||||
package com.baeldung.logging.log4j2.tests;
|
package com.baeldung.logging.log4j2.tests;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import java.io.ByteArrayOutputStream;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.apache.logging.log4j.core.Appender;
|
||||||
|
import org.apache.logging.log4j.core.appender.WriterAppender;
|
||||||
|
import org.apache.logging.log4j.core.layout.JsonLayout;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.logging.log4j2.Log4j2BaseIntegrationTest;
|
import java.io.CharArrayWriter;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import java.io.Writer;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest {
|
public class JSONLayoutIntegrationTest extends Log4j2BaseIntegrationTest {
|
||||||
|
|
||||||
private static Logger logger;
|
private Appender appender;
|
||||||
private ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
|
private Logger logger;
|
||||||
private PrintStream ps = new PrintStream(consoleOutput);
|
private final Writer writer = new CharArrayWriter();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
// Redirect console output to our stream
|
|
||||||
System.setOut(ps);
|
|
||||||
logger = LogManager.getLogger("CONSOLE_JSON_APPENDER");
|
logger = LogManager.getLogger("CONSOLE_JSON_APPENDER");
|
||||||
|
|
||||||
|
appender = WriterAppender.newBuilder()
|
||||||
|
.setTarget(writer)
|
||||||
|
.setLayout(JsonLayout.newBuilder().build())
|
||||||
|
.setName("json_layout_for_testing")
|
||||||
|
.build();
|
||||||
|
appender.start();
|
||||||
|
|
||||||
|
((org.apache.logging.log4j.core.Logger) logger).addAppender(appender);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() {
|
public void whenLogLayoutInJSON_thenOutputIsCorrectJSON() throws Exception {
|
||||||
logger.debug("Debug message");
|
logger.debug("Debug message");
|
||||||
String currentLog = consoleOutput.toString();
|
|
||||||
assertTrue(currentLog.isEmpty());
|
writer.flush();
|
||||||
assertTrue(isValidJSON(currentLog));
|
assertTrue(isValidJSON(writer.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isValidJSON(String jsonInString) {
|
@After
|
||||||
try {
|
public void cleanup() {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
((org.apache.logging.log4j.core.Logger) logger).removeAppender(appender);
|
||||||
mapper.readTree(jsonInString);
|
|
||||||
return true;
|
|
||||||
} catch (IOException e) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isValidJSON(String jsonInString) throws Exception {
|
||||||
|
JsonNode jsonNode = new ObjectMapper().readTree(jsonInString);
|
||||||
|
return jsonNode.get("message").asText().equals("Debug message");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue