LOG4J2-1986 Fix tests and remove support for InputStream and Reader
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/ab2d3262 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/ab2d3262 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/ab2d3262 Branch: refs/heads/LOG4J2-1986 Commit: ab2d3262c81f1b6c9ce40a916febdef82c4ea63c Parents: b96f522 Author: Mikael Ståldal <[email protected]> Authored: Wed Jul 26 21:29:19 2017 +0200 Committer: Mikael Ståldal <[email protected]> Committed: Wed Jul 26 21:29:19 2017 +0200 ---------------------------------------------------------------------- .../parser/AbstractJacksonLogEventParser.java | 37 ++++---------------- .../log4j/core/parser/LogEventParser.java | 19 ++-------- .../log4j/core/parser/TextLogEventParser.java | 16 +-------- .../core/parser/JsonLogEventParserTest.java | 29 ++++++++------- .../core/parser/XmlLogEventParserTest.java | 24 ++++++------- .../core/parser/YamlLogEventParserTest.java | 24 ++++++------- 6 files changed, 47 insertions(+), 102 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab2d3262/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java index 8b1ca55..a1b5264 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/AbstractJacksonLogEventParser.java @@ -16,49 +16,28 @@ */ package org.apache.logging.log4j.core.parser; -import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; class AbstractJacksonLogEventParser implements TextLogEventParser { - final ObjectReader objectReader; + private final ObjectReader objectReader; AbstractJacksonLogEventParser(ObjectMapper objectMapper) { + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); this.objectReader = objectMapper.readerFor(Log4jLogEvent.class); } @Override - public LogEvent parseFrom(InputStream input) throws IOException, ParseException { - try { - return objectReader.readValue(input); - } catch (JsonProcessingException e) { - throw new ParseException(e); - } - } - - @Override - public LogEvent parseFrom(Reader input) throws IOException, ParseException { - try { - return objectReader.readValue(input); - } catch (JsonProcessingException e) { - throw new ParseException(e); - } - } - - @Override public LogEvent parseFrom(String input) throws ParseException { try { return objectReader.readValue(input); - } catch (JsonProcessingException e) { - throw new ParseException(e); } catch (IOException e) { - throw new RuntimeException(e); + throw new ParseException(e); } } @@ -66,10 +45,8 @@ class AbstractJacksonLogEventParser implements TextLogEventParser { public LogEvent parseFrom(byte[] input) throws ParseException { try { return objectReader.readValue(input); - } catch (JsonProcessingException e) { - throw new ParseException(e); } catch (IOException e) { - throw new RuntimeException(e); + throw new ParseException(e); } } @@ -77,10 +54,8 @@ class AbstractJacksonLogEventParser implements TextLogEventParser { public LogEvent parseFrom(byte[] input, int offset, int length) throws ParseException { try { return objectReader.readValue(input, offset, length); - } catch (JsonProcessingException e) { - throw new ParseException(e); } catch (IOException e) { - throw new RuntimeException(e); + throw new ParseException(e); } } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab2d3262/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java index aaf7c30..6d071bd 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/LogEventParser.java @@ -18,27 +18,12 @@ package org.apache.logging.log4j.core.parser; import org.apache.logging.log4j.core.LogEvent; -import java.io.IOException; -import java.io.InputStream; - /** * Parse the output from a layout into instances of {@link LogEvent}. */ public interface LogEventParser { - - /** - * Parse from an InputStream, which is expected to contain exactly one log event. - * - * @param input the input stream, which will be closed - * - * @return the parsed LogEvent, never {@literal null}. - * @throws IOException if unable to read from the input - * @throws ParseException if the input is malformed and cannot be parsed as a LogEvent - */ - LogEvent parseFrom(InputStream input) throws IOException, ParseException; - /** - * Parse from a byte array, which is expected to contain exactly one log event. + * Parses a byte array, which is expected to contain exactly one log event. * * @param input the byte array * @@ -48,7 +33,7 @@ public interface LogEventParser { LogEvent parseFrom(byte[] input) throws ParseException; /** - * Parse from a specified range of a byte array. The specified range is expected to contain + * Parses a specified range in a byte array. The specified range is expected to contain * exactly one log event. * * @param input the byte array http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab2d3262/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java index 34af930..e7e366f 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/parser/TextLogEventParser.java @@ -18,26 +18,12 @@ package org.apache.logging.log4j.core.parser; import org.apache.logging.log4j.core.LogEvent; -import java.io.IOException; -import java.io.Reader; - /** * Parse the output from a text based layout into instances of {@link LogEvent}. */ public interface TextLogEventParser extends LogEventParser { /** - * Parse from a Reader, which is expected to contain exactly one log event. - * - * @param input the reader, which will be closed - * - * @return the parsed LogEvent, never {@literal null}. - * @throws IOException if unable to read from the Reader - * @throws ParseException if the input is malformed and cannot be parsed as a LogEvent - */ - LogEvent parseFrom(Reader input) throws IOException, ParseException; - - /** - * Parse from a String, which is expected to contain exactly one log event. + * Parses a String, which is expected to contain exactly one log event. * * @param input the string * http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab2d3262/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 5263704..ab8ea4c 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 @@ -97,32 +97,35 @@ public class JsonLogEventParserTest extends LogEventParserTest { } @Test(expected = ParseException.class) - public void testStringInvalidProperty() throws ParseException { - parser.parseFrom("{\"foo\":\"bar\"}"); + public void testStringJsonArray() throws ParseException { + parser.parseFrom("[]"); } @Test - public void testByteArray() throws ParseException { - LogEvent logEvent = parser.parseFrom(JSON.getBytes(StandardCharsets.UTF_8)); - assertLogEvent(logEvent); + public void testEmptyObject() throws ParseException { + parser.parseFrom("{}"); + } + + @Test(expected = ParseException.class) + public void testStringWrongPropertyType() throws ParseException { + parser.parseFrom("{\"timeMillis\":\"foobar\"}"); } @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); + public void testStringIgnoreInvalidProperty() throws ParseException { + parser.parseFrom("{\"foo\":\"bar\"}"); } @Test - public void testReader() throws ParseException, IOException { - LogEvent logEvent = parser.parseFrom(new StringReader(JSON)); + public void testByteArray() throws ParseException { + LogEvent logEvent = parser.parseFrom(JSON.getBytes(StandardCharsets.UTF_8)); assertLogEvent(logEvent); } @Test - public void testInputStream() throws ParseException, IOException { - LogEvent logEvent = parser.parseFrom(new ByteArrayInputStream(JSON.getBytes(StandardCharsets.UTF_8))); + 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); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ab2d3262/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 index 1d8f3c1..d24fb32 100644 --- 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 @@ -96,8 +96,18 @@ public class XmlLogEventParserTest extends LogEventParserTest { parser.parseFrom("foobar"); } + @Test + public void testEmptyObject() throws ParseException { + parser.parseFrom("<Event></Event>"); + } + @Test(expected = ParseException.class) - public void testStringInvalidProperty() throws ParseException { + public void testStringWrongPropertyType() throws ParseException { + parser.parseFrom("<Event><timeMillis>foobar</timeMillis></Event>"); + } + + @Test + public void testStringIgnoreInvalidProperty() throws ParseException { parser.parseFrom("<Event><foo>bar</foo></Event>"); } @@ -114,16 +124,4 @@ public class XmlLogEventParserTest extends LogEventParserTest { 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/ab2d3262/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 index c4a7de2..2145793 100644 --- 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 @@ -90,8 +90,18 @@ public class YamlLogEventParserTest extends LogEventParserTest { parser.parseFrom("foobar"); } + @Test + public void testEmptyObject() throws ParseException { + parser.parseFrom("---\n"); + } + @Test(expected = ParseException.class) - public void testStringInvalidProperty() throws ParseException { + public void testStringWrongPropertyType() throws ParseException { + parser.parseFrom("---\ntimeMillis: \"foobar\"\n"); + } + + @Test + public void testStringIgnoreInvalidProperty() throws ParseException { parser.parseFrom("---\nfoo: \"bar\"\n"); } @@ -108,16 +118,4 @@ public class YamlLogEventParserTest extends LogEventParserTest { 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); - } - }
