You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2018/10/30 17:19:48 UTC

logging-log4j2 git commit: New test to assert that Log4j MapMessages are mapped to JMS MapMessages.

Repository: logging-log4j2
Updated Branches:
  refs/heads/release-2.x 4a4b60abb -> 5718f6047


New test to assert that Log4j MapMessages are mapped to JMS MapMessages.

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

Branch: refs/heads/release-2.x
Commit: 5718f6047bcf632ae8f450cd6f21fd81b6db74cd
Parents: 4a4b60a
Author: Gary Gregory <ga...@gmail.com>
Authored: Tue Oct 30 11:19:45 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Tue Oct 30 11:19:45 2018 -0600

----------------------------------------------------------------------
 .../core/appender/mom/JmsAppenderTest.java      | 88 ++++++++++++++------
 .../src/test/resources/JmsAppenderTest.xml      |  5 ++
 2 files changed, 66 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5718f604/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderTest.java
index d48167e..8696089 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderTest.java
@@ -17,12 +17,23 @@
 
 package org.apache.logging.log4j.core.appender.mom;
 
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isA;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.BDDMockito.then;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+
 import java.io.Serializable;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
 import javax.jms.Destination;
+import javax.jms.MapMessage;
 import javax.jms.MessageProducer;
 import javax.jms.ObjectMessage;
 import javax.jms.Session;
@@ -34,6 +45,7 @@ import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.junit.JndiRule;
 import org.apache.logging.log4j.junit.LoggerContextRule;
+import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.SimpleMessage;
 import org.junit.Before;
 import org.junit.Rule;
@@ -41,15 +53,6 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.RuleChain;
 
-import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.ArgumentMatchers.isA;
-import static org.mockito.BDDMockito.given;
-import static org.mockito.BDDMockito.then;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
-
 @Category(Appenders.Jms.class)
 public class JmsAppenderTest {
 
@@ -57,6 +60,7 @@ public class JmsAppenderTest {
     private static final String QUEUE_FACTORY_NAME = "jms/queues";
     private static final String TOPIC_FACTORY_NAME = "jms/topics";
     private static final String DESTINATION_NAME = "jms/destination";
+    private static final String DESTINATION_NAME_ML = "jms/destination-ml";
     private static final String QUEUE_NAME = "jms/queue";
     private static final String TOPIC_NAME = "jms/topic";
     private static final String LOG_MESSAGE = "Hello, world!";
@@ -65,9 +69,12 @@ public class JmsAppenderTest {
     private final Connection connection = mock(Connection.class);
     private final Session session = mock(Session.class);
     private final Destination destination = mock(Destination.class);
+    private final Destination destinationMl = mock(Destination.class);
     private final MessageProducer messageProducer = mock(MessageProducer.class);
+    private final MessageProducer messageProducerMl = mock(MessageProducer.class);
     private final TextMessage textMessage = mock(TextMessage.class);
     private final ObjectMessage objectMessage = mock(ObjectMessage.class);
+    private final MapMessage mapMessage = mock(MapMessage.class);
 
     private final JndiRule jndiRule = new JndiRule(createBindings());
     private final LoggerContextRule ctx = new LoggerContextRule("JmsAppenderTest.xml");
@@ -75,10 +82,23 @@ public class JmsAppenderTest {
     @Rule
     public RuleChain rules = RuleChain.outerRule(jndiRule).around(ctx);
 
+    public JmsAppenderTest() throws Exception {
+        // this needs to set up before LoggerContextRule
+        given(connectionFactory.createConnection()).willReturn(connection);
+        given(connectionFactory.createConnection(anyString(), anyString())).willThrow(IllegalArgumentException.class);
+        given(connection.createSession(eq(false), eq(Session.AUTO_ACKNOWLEDGE))).willReturn(session);
+        given(session.createProducer(eq(destination))).willReturn(messageProducer);
+        given(session.createProducer(eq(destinationMl))).willReturn(messageProducerMl);
+        given(session.createTextMessage(anyString())).willReturn(textMessage);
+        given(session.createObjectMessage(isA(Serializable.class))).willReturn(objectMessage);
+        given(session.createMapMessage()).willReturn(mapMessage);
+    }
+
     private Map<String, Object> createBindings() {
         final ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
         map.put(CONNECTION_FACTORY_NAME, connectionFactory);
         map.put(DESTINATION_NAME, destination);
+        map.put(DESTINATION_NAME_ML, destinationMl);
         map.put(QUEUE_FACTORY_NAME, connectionFactory);
         map.put(QUEUE_NAME, destination);
         map.put(TOPIC_FACTORY_NAME, connectionFactory);
@@ -86,20 +106,30 @@ public class JmsAppenderTest {
         return map;
     }
 
-    public JmsAppenderTest() throws Exception {
-        // this needs to set up before LoggerContextRule
-        given(connectionFactory.createConnection()).willReturn(connection);
-        given(connectionFactory.createConnection(anyString(), anyString())).willThrow(IllegalArgumentException.class);
-        given(connection.createSession(eq(false), eq(Session.AUTO_ACKNOWLEDGE))).willReturn(session);
-        given(session.createProducer(eq(destination))).willReturn(messageProducer);
-        given(session.createTextMessage(anyString())).willReturn(textMessage);
-        given(session.createObjectMessage(isA(Serializable.class))).willReturn(objectMessage);
+    private  Log4jLogEvent createLogEvent() {
+        return createLogEvent(new SimpleMessage(LOG_MESSAGE));
+    }
+
+    private Log4jLogEvent createLogEvent(final Message message) {
+        // @formatter:off
+        return Log4jLogEvent.newBuilder()
+            .setLoggerName(JmsAppenderTest.class.getName())
+            .setLoggerFqcn(JmsAppenderTest.class.getName())
+            .setLevel(Level.INFO)
+            .setMessage(message)
+            .build();
+        // @formatter:on
+    }
+
+    private Log4jLogEvent createMapMessageLogEvent() {
+        org.apache.logging.log4j.message.MapMessage<?, String> mapMessage = new org.apache.logging.log4j.message.MapMessage<>();
+        return createLogEvent(mapMessage.with("testMesage", LOG_MESSAGE));
     }
 
     @Before
     public void setUp() throws Exception {
         // we have 3 appenders all connecting to the same ConnectionFactory
-        then(connection).should(times(3)).start();
+        then(connection).should(times(4)).start();
     }
 
     @Test
@@ -116,6 +146,19 @@ public class JmsAppenderTest {
     }
 
     @Test
+    public void testAppendToQueueWithMessageLayout() throws Exception {
+        final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsAppender-MessageLayout");
+        final LogEvent event = createMapMessageLogEvent();
+        appender.append(event);
+        then(session).should().createMapMessage();
+        then(mapMessage).should().setJMSTimestamp(anyLong());
+        then(messageProducerMl).should().send(mapMessage);
+        appender.stop();
+        then(session).should().close();
+        then(connection).should().close();
+    }
+
+    @Test
     public void testJmsQueueAppenderCompatibility() throws Exception {
         final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsQueueAppender");
         final LogEvent expected = createLogEvent();
@@ -141,13 +184,4 @@ public class JmsAppenderTest {
         then(connection).should().close();
     }
 
-    private static Log4jLogEvent createLogEvent() {
-        return Log4jLogEvent.newBuilder()
-            .setLoggerName(JmsAppenderTest.class.getName())
-            .setLoggerFqcn(JmsAppenderTest.class.getName())
-            .setLevel(Level.INFO)
-            .setMessage(new SimpleMessage(LOG_MESSAGE))
-            .build();
-    }
-
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/5718f604/log4j-core/src/test/resources/JmsAppenderTest.xml
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/resources/JmsAppenderTest.xml b/log4j-core/src/test/resources/JmsAppenderTest.xml
index 9a5f951..f275ac4 100644
--- a/log4j-core/src/test/resources/JmsAppenderTest.xml
+++ b/log4j-core/src/test/resources/JmsAppenderTest.xml
@@ -22,6 +22,11 @@
          destinationBindingName="jms/destination">
       <PatternLayout pattern="%m"/>
     </JMS>
+    <JMS name="JmsAppender-MessageLayout"
+         factoryBindingName="jms/connectionFactory"
+         destinationBindingName="jms/destination-ml">
+      <MessageLayout />
+    </JMS>
     <!-- backwards compatibility tests -->
     <JMSQueue name="JmsQueueAppender"
               factoryBindingName="jms/queues"