LOG4J2-1334 MutableLogEvent unit test initial version
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/37066643 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/37066643 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/37066643 Branch: refs/heads/LOG4J2-1365 Commit: 37066643e99ae35d9689f4ad1ed6d9296dfb9f3a Parents: aa4ce1e Author: rpopma <[email protected]> Authored: Sun Apr 17 20:43:44 2016 +0900 Committer: rpopma <[email protected]> Committed: Sun Apr 17 20:43:44 2016 +0900 ---------------------------------------------------------------------- .../log4j/core/impl/MutableLogEvent.java | 1 + .../log4j/core/impl/MutableLogEventTest.java | 86 ++++++++++++++++++++ 2 files changed, 87 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/37066643/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java index 9ca179b..9edb6f2 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/MutableLogEvent.java @@ -66,6 +66,7 @@ public class MutableLogEvent implements LogEvent, ReusableMessage { this.threadName = event.getThreadName(); this.threadPriority = event.getThreadPriority(); this.endOfBatch = event.isEndOfBatch(); + this.includeLocation = event.isIncludeLocation(); this.nanoTime = event.getNanoTime(); setMessage(event.getMessage()); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/37066643/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/MutableLogEventTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/MutableLogEventTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/MutableLogEventTest.java new file mode 100644 index 0000000..23a49f4 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/MutableLogEventTest.java @@ -0,0 +1,86 @@ +package org.apache.logging.log4j.core.impl;/* + * 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. + */ + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; +import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.message.Message; +import org.apache.logging.log4j.message.SimpleMessage; +import org.apache.logging.log4j.spi.MutableThreadContextStack; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the MutableLogEvent class. + */ +public class MutableLogEventTest { + private static final Map<String, String> CONTEXTMAP = createContextMap(); + private static final ThreadContext.ContextStack STACK = new MutableThreadContextStack(Arrays.asList("abc", "xyz")); + + private static Map<String,String> createContextMap() { + Map<String,String> result = new HashMap<>(); + result.put("a", "1"); + result.put("b", "2"); + return result; + } + + @Test + public void testInitFromCopiesAllFields() { +// private ThrowableProxy thrownProxy; + Log4jLogEvent source = Log4jLogEvent.newBuilder() // + .setContextMap(CONTEXTMAP) // + .setContextStack(STACK) // + .setEndOfBatch(true) // + .setIncludeLocation(true) // + .setLevel(Level.FATAL) // + .setLoggerFqcn("a.b.c.d.e") // + .setLoggerName("my name is Logger") // + .setMarker(MarkerManager.getMarker("on your marks")) // + .setMessage(new SimpleMessage("msg in a bottle")) // + .setNanoTime(1234567) // + .setSource(new StackTraceElement("myclass", "mymethod", "myfile", 123)) // + .setThreadId(100).setThreadName("threadname").setThreadPriority(10) // + .setThrown(new RuntimeException("run")) // + .setTimeMillis(987654321) + .build(); + MutableLogEvent mutable = new MutableLogEvent(); + mutable.initFrom(source); + assertEquals("contextMap", CONTEXTMAP, mutable.getContextMap()); + assertEquals("stack", STACK, mutable.getContextStack()); + assertEquals("endOfBatch", true, mutable.isEndOfBatch()); + assertEquals("IncludeLocation()", true, mutable.isIncludeLocation()); + assertEquals("level", Level.FATAL, mutable.getLevel()); + assertEquals("LoggerFqcn()", source.getLoggerFqcn(), mutable.getLoggerFqcn()); + assertEquals("LoggerName", source.getLoggerName(), mutable.getLoggerName()); + assertEquals("marker", source.getMarker(), mutable.getMarker()); + assertEquals("msg", source.getMessage(), mutable.getMessage()); + assertEquals("nano", source.getNanoTime(), mutable.getNanoTime()); + assertEquals("src", source.getSource(), mutable.getSource()); + assertEquals("tid", source.getThreadId(), mutable.getThreadId()); + assertEquals("tname", source.getThreadName(), mutable.getThreadName()); + assertEquals("tpriority", source.getThreadPriority(), mutable.getThreadPriority()); + assertEquals("throwns", source.getThrown(), mutable.getThrown()); + assertEquals("proxy", source.getThrownProxy(), mutable.getThrownProxy()); + assertEquals("millis", source.getTimeMillis(), mutable.getTimeMillis()); + } +} \ No newline at end of file
