LOG4J2-1986 Unit tests for YAML and XML
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/a5bb4987 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a5bb4987 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a5bb4987 Branch: refs/heads/LOG4J2-1986 Commit: a5bb49876cb0b9a2083d50fbb4904c9f15b959cb Parents: 72d0d12 Author: Mikael Ståldal <[email protected]> Authored: Sun Jul 23 21:51:15 2017 +0200 Committer: Mikael Ståldal <[email protected]> Committed: Sun Jul 23 21:51:15 2017 +0200 ---------------------------------------------------------------------- .../core/parser/JsonLogEventParserTest.java | 35 +---- .../log4j/core/parser/LogEventParserTest.java | 55 ++++++++ .../core/parser/XmlLogEventParserTest.java | 129 +++++++++++++++++++ .../core/parser/YamlLogEventParserTest.java | 123 ++++++++++++++++++ 4 files changed, 308 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a5bb4987/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 f4efd91..5263704 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,7 +16,6 @@ */ 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; @@ -25,15 +24,8 @@ 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 { +public class JsonLogEventParserTest extends LogEventParserTest { private JsonLogEventParser parser; @@ -134,29 +126,4 @@ public class JsonLogEventParserTest { 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")); - } - } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a5bb4987/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/LogEventParserTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/LogEventParserTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/LogEventParserTest.java new file mode 100644 index 0000000..d995da0 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/LogEventParserTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.parser; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.LogEvent; + +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 abstract class LogEventParserTest { + protected 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")); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a5bb4987/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/XmlLogEventParserTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/XmlLogEventParserTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/XmlLogEventParserTest.java new file mode 100644 index 0000000..802b7d4 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/XmlLogEventParserTest.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.parser; + +import org.apache.logging.log4j.core.LogEvent; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; + +public class XmlLogEventParserTest extends LogEventParserTest { + + private XmlLogEventParser parser; + + private static final String XML = "<Event xmlns=\"http://logging.apache.org/log4j/2.0/events\"\n" + + " timeMillis=\"1493121664118\"\n" + + " level=\"INFO\"\n" + + " loggerName=\"HelloWorld\"\n" + + " endOfBatch=\"false\"\n" + + " thread=\"main\"\n" + + " loggerFqcn=\"org.apache.logging.log4j.spi.AbstractLogger\"\n" + + " threadId=\"1\"\n" + + " threadPriority=\"5\">\n" + + " <Marker name=\"child\">\n" + + " <Parents>\n" + + " <Marker name=\"parent\">\n" + + " <Parents>\n" + + " <Marker name=\"grandparent\"/>\n" + + " </Parents>\n" + + " </Marker>\n" + + " </Parents>\n" + + " </Marker>\n" + + " <Message>Hello, world!</Message>\n" + + " <ContextMap>\n" + + " <item key=\"bar\" value=\"BAR\"/>\n" + + " <item key=\"foo\" value=\"FOO\"/>\n" + + " </ContextMap>\n" + + " <ContextStack>\n" + + " <ContextStackItem>one</ContextStackItem>\n" + + " <ContextStackItem>two</ContextStackItem>\n" + + " </ContextStack>\n" + + " <Source\n" + + " class=\"logtest.Main\"\n" + + " method=\"main\"\n" + + " file=\"Main.java\"\n" + + " line=\"29\"/>\n" + + " <Thrown commonElementCount=\"0\" message=\"error message\" name=\"java.lang.RuntimeException\">\n" + + " <ExtendedStackTrace>\n" + + " <ExtendedStackTraceItem\n" + + " class=\"logtest.Main\"\n" + + " method=\"main\"\n" + + " file=\"Main.java\"\n" + + " line=\"29\"\n" + + " exact=\"true\"\n" + + " location=\"classes/\"\n" + + " version=\"?\"/>\n" + + " </ExtendedStackTrace>\n" + + " </Thrown>\n" + + "</Event>"; + + @Before + public void setup() { + parser = new XmlLogEventParser(); + } + + @Test + public void testString() throws ParseException { + LogEvent logEvent = parser.parseFrom(XML); + assertLogEvent(logEvent); + } + + @Test(expected = ParseException.class) + public void testStringEmpty() throws ParseException { + parser.parseFrom(""); + } + + @Test(expected = ParseException.class) + public void testStringInvalidXml() throws ParseException { + parser.parseFrom("foobar"); + } + + @Test(expected = ParseException.class) + public void testStringInvalidProperty() throws ParseException { + parser.parseFrom("{\"foo\":\"bar\"}"); + } + + @Test + public void testByteArray() throws ParseException { + LogEvent logEvent = parser.parseFrom(XML.getBytes(StandardCharsets.UTF_8)); + assertLogEvent(logEvent); + } + + @Test + public void testByteArrayOffsetLength() throws ParseException { + byte[] bytes = ("abc" + XML + "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(XML)); + assertLogEvent(logEvent); + } + + @Test + public void testInputStream() throws ParseException, IOException { + LogEvent logEvent = parser.parseFrom(new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8))); + assertLogEvent(logEvent); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a5bb4987/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/YamlLogEventParserTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/YamlLogEventParserTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/YamlLogEventParserTest.java new file mode 100644 index 0000000..157e376 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/parser/YamlLogEventParserTest.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.parser; + +import org.apache.logging.log4j.core.LogEvent; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; + +public class YamlLogEventParserTest extends LogEventParserTest { + + private YamlLogEventParser parser; + + private static final String YAML = "---\n" + + "timeMillis: 1493121664118\n" + + "thread: \"main\"\n" + + "level: \"INFO\"\n" + + "loggerName: \"HelloWorld\"\n" + + "marker:\n" + + " name: \"child\"\n" + + " parents:\n" + + " - name: \"parent\"\n" + + " parents:\n" + + " - name: \"grandparent\"\n" + + "message: \"Hello, world!\"\n" + + "thrown:\n" + + " commonElementCount: 0\n" + + " message: \"error message\"\n" + + " name: \"java.lang.RuntimeException\"\n" + + " extendedStackTrace:\n" + + " - class: \"logtest.Main\"\n" + + " method: \"main\"\n" + + " file: \"Main.java\"\n" + + " line: 29\n" + + " exact: true\n" + + " location: \"classes/\"\n" + + " version: \"?\"\n" + + "contextStack:\n" + + "- \"one\"\n" + + "- \"two\"\n" + + "endOfBatch: false\n" + + "loggerFqcn: \"org.apache.logging.log4j.spi.AbstractLogger\"\n" + + "contextMap:\n" + + " bar: \"BAR\"\n" + + " foo: \"FOO\"\n" + + "threadId: 1\n" + + "threadPriority: 5\n" + + "source:\n" + + " class: \"logtest.Main\"\n" + + " method: \"main\"\n" + + " file: \"Main.java\"\n" + + " line: 29"; + + @Before + public void setup() { + parser = new YamlLogEventParser(); + } + + @Test + public void testString() throws ParseException { + LogEvent logEvent = parser.parseFrom(YAML); + assertLogEvent(logEvent); + } + + @Test(expected = ParseException.class) + public void testStringEmpty() throws ParseException { + parser.parseFrom(""); + } + + @Test(expected = ParseException.class) + public void testStringInvalidYaml() throws ParseException { + parser.parseFrom("foobar"); + } + + @Test(expected = ParseException.class) + public void testStringInvalidProperty() throws ParseException { + parser.parseFrom("{\"foo\":\"bar\"}"); + } + + @Test + public void testByteArray() throws ParseException { + LogEvent logEvent = parser.parseFrom(YAML.getBytes(StandardCharsets.UTF_8)); + assertLogEvent(logEvent); + } + + @Test + public void testByteArrayOffsetLength() throws ParseException { + byte[] bytes = ("abc" + YAML + "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(YAML)); + assertLogEvent(logEvent); + } + + @Test + public void testInputStream() throws ParseException, IOException { + LogEvent logEvent = parser.parseFrom(new ByteArrayInputStream(YAML.getBytes(StandardCharsets.UTF_8))); + assertLogEvent(logEvent); + } + +}
