You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/04/18 16:26:35 UTC

logging-log4j2 git commit: LOG4J2-1334 ReusableLogEventFactory unit tests

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 45a856dc9 -> c989349ef


LOG4J2-1334 ReusableLogEventFactory unit tests


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

Branch: refs/heads/master
Commit: c989349ef86874c9eccaf5205196b18f2eec3df2
Parents: 45a856d
Author: rpopma <rp...@apache.org>
Authored: Mon Apr 18 23:26:39 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Mon Apr 18 23:26:39 2016 +0900

----------------------------------------------------------------------
 .../core/impl/ReusableLogEventFactoryTest.java  | 99 ++++++++++++++++++++
 1 file changed, 99 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c989349e/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactoryTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactoryTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactoryTest.java
new file mode 100644
index 0000000..2d297f4
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactoryTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.impl;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.message.Message;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests the ReusableLogEventFactory class.
+ */
+public class ReusableLogEventFactoryTest {
+
+    @Test
+    public void testCreateEventReturnsSameInstance() throws Exception {
+        final ReusableLogEventFactory factory = new ReusableLogEventFactory();
+        LogEvent event1 = callCreateEvent(factory, "a", Level.DEBUG, new SimpleMessage("abc"), null);
+        LogEvent event2 = callCreateEvent(factory, "b", Level.INFO, new SimpleMessage("xyz"), null);
+        assertSame(event1, event2);
+    }
+
+    @Test
+    public void testCreateEventOverwritesFields() throws Exception {
+        final ReusableLogEventFactory factory = new ReusableLogEventFactory();
+        LogEvent event1 = callCreateEvent(factory, "a", Level.DEBUG, new SimpleMessage("abc"), null);
+        assertEquals("logger", "a", event1.getLoggerName());
+        assertEquals("level", Level.DEBUG, event1.getLevel());
+        assertEquals("msg", new SimpleMessage("abc"), event1.getMessage());
+
+        LogEvent event2 = callCreateEvent(factory, "b", Level.INFO, new SimpleMessage("xyz"), null);
+        assertSame(event1, event2);
+
+        assertEquals("logger", "b", event1.getLoggerName());
+        assertEquals("level", Level.INFO, event1.getLevel());
+        assertEquals("msg", new SimpleMessage("xyz"), event1.getMessage());
+        assertEquals("logger", "b", event2.getLoggerName());
+        assertEquals("level", Level.INFO, event2.getLevel());
+        assertEquals("msg", new SimpleMessage("xyz"), event2.getMessage());
+    }
+
+    private LogEvent callCreateEvent(final ReusableLogEventFactory factory, final String logger, final Level level,
+            final Message message, final Throwable thrown) {
+        return factory.createEvent(logger, null, getClass().getName(), level, message, null, thrown);
+    }
+
+    @Test
+    public void testCreateEventReturnsThreadLocalInstance() throws Exception {
+        final ReusableLogEventFactory factory = new ReusableLogEventFactory();
+        final LogEvent[] event1 = new LogEvent[1];
+        final LogEvent[] event2 = new LogEvent[1];
+        Thread t1 = new Thread("THREAD 1") {
+            public void run() {
+                event1[0] = callCreateEvent(factory, "a", Level.DEBUG, new SimpleMessage("abc"), null);
+            }
+        };
+        Thread t2 = new Thread("Thread 2") {
+            public void run() {
+                event2[0] = callCreateEvent(factory, "b", Level.INFO, new SimpleMessage("xyz"), null);
+            }
+        };
+        t1.start();
+        t2.start();
+        t1.join();
+        t2.join();
+        assertNotNull(event1[0]);
+        assertNotNull(event2[0]);
+        assertNotSame(event1[0], event2[0]);
+        assertEquals("logger", "a", event1[0].getLoggerName());
+        assertEquals("level", Level.DEBUG, event1[0].getLevel());
+        assertEquals("msg", new SimpleMessage("abc"), event1[0].getMessage());
+        assertEquals("thread name", "THREAD 1", event1[0].getThreadName());
+        assertEquals("tid", t1.getId(), event1[0].getThreadId());
+
+        assertEquals("logger", "b", event2[0].getLoggerName());
+        assertEquals("level", Level.INFO, event2[0].getLevel());
+        assertEquals("msg", new SimpleMessage("xyz"), event2[0].getMessage());
+        assertEquals("thread name", "Thread 2", event2[0].getThreadName());
+        assertEquals("tid", t2.getId(), event2[0].getThreadId());
+    }
+
+}
\ No newline at end of file