Repository: logging-log4j2
Updated Branches:
  refs/heads/LOG4J2-1986 c4814a873 -> a5bb49876


LOG4J2-1986 More extensive unit test


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/72d0d12a
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/72d0d12a
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/72d0d12a

Branch: refs/heads/LOG4J2-1986
Commit: 72d0d12a77f66228ed12f21f9965b3e408271723
Parents: c4814a8
Author: Mikael StÃ¥ldal <[email protected]>
Authored: Sun Jul 23 21:13:59 2017 +0200
Committer: Mikael StÃ¥ldal <[email protected]>
Committed: Sun Jul 23 21:13:59 2017 +0200

----------------------------------------------------------------------
 .../core/parser/JsonLogEventParserTest.java     | 75 +++++++++++++++++---
 1 file changed, 67 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/72d0d12a/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
index 5684602..f4efd91 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/JsonLogEventParserTest.java
@@ -16,12 +16,22 @@
  */
 package org.apache.logging.log4j.core.parser;
 
+import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.LogEvent;
 import org.junit.Before;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
 
 public class JsonLogEventParserTest {
 
@@ -30,6 +40,8 @@ public class JsonLogEventParserTest {
     private static final String JSON = "{\n" +
             "  \"timeMillis\" : 1493121664118,\n" +
             "  \"thread\" : \"main\",\n" +
+            "  \"threadId\" : 1,\n" +
+            "  \"threadPriority\" : 5,\n" +
             "  \"level\" : \"INFO\",\n" +
             "  \"loggerName\" : \"HelloWorld\",\n" +
             "  \"marker\" : {\n" +
@@ -44,6 +56,7 @@ public class JsonLogEventParserTest {
             "  \"message\" : \"Hello, world!\",\n" +
             "  \"thrown\" : {\n" +
             "    \"commonElementCount\" : 0,\n" +
+            "    \"message\" : \"error message\",\n" +
             "    \"name\" : \"java.lang.RuntimeException\",\n" +
             "    \"extendedStackTrace\" : [ {\n" +
             "      \"class\" : \"logtest.Main\",\n" +
@@ -56,14 +69,12 @@ public class JsonLogEventParserTest {
             "    } ]\n" +
             "  },\n" +
             "  \"contextStack\" : [ \"one\", \"two\" ],\n" +
-            "  \"endOfBatch\" : false,\n" +
             "  \"loggerFqcn\" : 
\"org.apache.logging.log4j.spi.AbstractLogger\",\n" +
+            "  \"endOfBatch\" : false,\n" +
             "  \"contextMap\" : {\n" +
             "    \"bar\" : \"BAR\",\n" +
             "    \"foo\" : \"FOO\"\n" +
             "  },\n" +
-            "  \"threadId\" : 1,\n" +
-            "  \"threadPriority\" : 5,\n" +
             "  \"source\" : {\n" +
             "    \"class\" : \"logtest.Main\",\n" +
             "    \"method\" : \"main\",\n" +
@@ -80,9 +91,7 @@ public class JsonLogEventParserTest {
     @Test
     public void testString() throws ParseException {
         LogEvent logEvent = parser.parseFrom(JSON);
-        assertNotNull(logEvent);
-        assertEquals("HelloWorld", logEvent.getLoggerName());
-        // TODO assert more here
+        assertLogEvent(logEvent);
     }
 
     @Test(expected = ParseException.class)
@@ -100,4 +109,54 @@ public class JsonLogEventParserTest {
         parser.parseFrom("{\"foo\":\"bar\"}");
     }
 
+    @Test
+    public void testByteArray() throws ParseException {
+        LogEvent logEvent = 
parser.parseFrom(JSON.getBytes(StandardCharsets.UTF_8));
+        assertLogEvent(logEvent);
+    }
+
+    @Test
+    public void testByteArrayOffsetLength() throws ParseException {
+        byte[] bytes = ("abc" + JSON + "def").getBytes(StandardCharsets.UTF_8);
+        LogEvent logEvent = parser.parseFrom(bytes, 3, bytes.length - 6);
+        assertLogEvent(logEvent);
+    }
+
+    @Test
+    public void testReader() throws ParseException, IOException {
+        LogEvent logEvent = parser.parseFrom(new StringReader(JSON));
+        assertLogEvent(logEvent);
+    }
+
+    @Test
+    public void testInputStream() throws ParseException, IOException {
+        LogEvent logEvent = parser.parseFrom(new 
ByteArrayInputStream(JSON.getBytes(StandardCharsets.UTF_8)));
+        assertLogEvent(logEvent);
+    }
+
+    private void assertLogEvent(LogEvent logEvent) {
+        assertThat(logEvent, is(notNullValue()));
+        assertThat(logEvent.getTimeMillis(), equalTo(1493121664118L));
+        assertThat(logEvent.getThreadName(), equalTo("main"));
+        assertThat(logEvent.getThreadId(), equalTo(1L));
+        assertThat(logEvent.getThreadPriority(), equalTo(5));
+        assertThat(logEvent.getLevel(), equalTo(Level.INFO));
+        assertThat(logEvent.getLoggerName(), equalTo("HelloWorld"));
+        assertThat(logEvent.getMarker().getName(), equalTo("child"));
+        assertThat(logEvent.getMarker().getParents()[0].getName(), 
equalTo("parent"));
+        
assertThat(logEvent.getMarker().getParents()[0].getParents()[0].getName(),
+                equalTo("grandparent"));
+        assertThat(logEvent.getMessage().getFormattedMessage(), 
equalTo("Hello, world!"));
+        assertThat(logEvent.getThrown(), is(nullValue()));
+        assertThat(logEvent.getThrownProxy().getMessage(), equalTo("error 
message"));
+        assertThat(logEvent.getThrownProxy().getName(), 
equalTo("java.lang.RuntimeException"));
+        
assertThat(logEvent.getThrownProxy().getExtendedStackTrace()[0].getClassName(),
+                equalTo("logtest.Main"));
+        assertThat(logEvent.getLoggerFqcn(), 
equalTo("org.apache.logging.log4j.spi.AbstractLogger"));
+        assertThat(logEvent.getContextStack().asList(), 
equalTo(Arrays.asList("one", "two")));
+        assertThat((String) logEvent.getContextData().getValue("foo"), 
equalTo("FOO"));
+        assertThat((String) logEvent.getContextData().getValue("bar"), 
equalTo("BAR"));
+        assertThat(logEvent.getSource().getClassName(), 
equalTo("logtest.Main"));
+    }
+
 }

Reply via email to