"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderTest.java" between
apache-log4j-2.11.1-src.tar.gz and apache-log4j-2.11.2-src.tar.gz

About: Apache Log4j 2 is a logging library for Java. Source distribution (Java).

JmsAppenderTest.java  (apache-log4j-2.11.1-src):JmsAppenderTest.java  (apache-log4j-2.11.2-src)
skipping to change at line 20 skipping to change at line 20
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the license for the specific language governing permissions and * See the license for the specific language governing permissions and
* limitations under the license. * limitations under the license.
*/ */
package org.apache.logging.log4j.core.appender.mom; 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.io.Serializable;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import javax.jms.Destination; import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.ObjectMessage; import javax.jms.ObjectMessage;
import javax.jms.Session; import javax.jms.Session;
import javax.jms.TextMessage; import javax.jms.TextMessage;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.categories.Appenders; import org.apache.logging.log4j.categories.Appenders;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.junit.JndiRule; import org.apache.logging.log4j.junit.JndiRule;
import org.apache.logging.log4j.junit.LoggerContextRule; import org.apache.logging.log4j.junit.LoggerContextRule;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.SimpleMessage; import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.message.StringMapMessage;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
import org.junit.rules.RuleChain; 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) @Category(Appenders.Jms.class)
public class JmsAppenderTest { public class JmsAppenderTest {
private static final String CONNECTION_FACTORY_NAME = "jms/connectionFactory "; private static final String CONNECTION_FACTORY_NAME = "jms/connectionFactory ";
private static final String QUEUE_FACTORY_NAME = "jms/queues"; private static final String QUEUE_FACTORY_NAME = "jms/queues";
private static final String TOPIC_FACTORY_NAME = "jms/topics"; private static final String TOPIC_FACTORY_NAME = "jms/topics";
private static final String DESTINATION_NAME = "jms/destination"; 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 QUEUE_NAME = "jms/queue";
private static final String TOPIC_NAME = "jms/topic"; private static final String TOPIC_NAME = "jms/topic";
private static final String LOG_MESSAGE = "Hello, world!"; private static final String LOG_MESSAGE = "Hello, world!";
private final ConnectionFactory connectionFactory = mock(ConnectionFactory.c lass); private final ConnectionFactory connectionFactory = mock(ConnectionFactory.c lass);
private final Connection connection = mock(Connection.class); private final Connection connection = mock(Connection.class);
private final Session session = mock(Session.class); private final Session session = mock(Session.class);
private final Destination destination = mock(Destination.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 messageProducer = mock(MessageProducer.class);
private final MessageProducer messageProducerMl = mock(MessageProducer.class );
private final TextMessage textMessage = mock(TextMessage.class); private final TextMessage textMessage = mock(TextMessage.class);
private final ObjectMessage objectMessage = mock(ObjectMessage.class); private final ObjectMessage objectMessage = mock(ObjectMessage.class);
private final MapMessage mapMessage = mock(MapMessage.class);
private final JndiRule jndiRule = new JndiRule(createBindings()); private final JndiRule jndiRule = new JndiRule(createBindings());
private final LoggerContextRule ctx = new LoggerContextRule("JmsAppenderTest .xml"); private final LoggerContextRule ctx = new LoggerContextRule("JmsAppenderTest .xml");
@Rule @Rule
public RuleChain rules = RuleChain.outerRule(jndiRule).around(ctx); 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())).will
Throw(IllegalArgumentException.class);
given(connection.createSession(eq(false), eq(Session.AUTO_ACKNOWLEDGE)))
.willReturn(session);
given(session.createProducer(eq(destination))).willReturn(messageProduce
r);
given(session.createProducer(eq(destinationMl))).willReturn(messageProdu
cerMl);
given(session.createTextMessage(anyString())).willReturn(textMessage);
given(session.createObjectMessage(isA(Serializable.class))).willReturn(o
bjectMessage);
given(session.createMapMessage()).willReturn(mapMessage);
}
private Map<String, Object> createBindings() { private Map<String, Object> createBindings() {
final ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>(); final ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
map.put(CONNECTION_FACTORY_NAME, connectionFactory); map.put(CONNECTION_FACTORY_NAME, connectionFactory);
map.put(DESTINATION_NAME, destination); map.put(DESTINATION_NAME, destination);
map.put(DESTINATION_NAME_ML, destinationMl);
map.put(QUEUE_FACTORY_NAME, connectionFactory); map.put(QUEUE_FACTORY_NAME, connectionFactory);
map.put(QUEUE_NAME, destination); map.put(QUEUE_NAME, destination);
map.put(TOPIC_FACTORY_NAME, connectionFactory); map.put(TOPIC_FACTORY_NAME, connectionFactory);
map.put(TOPIC_NAME, destination); map.put(TOPIC_NAME, destination);
return map; return map;
} }
public JmsAppenderTest() throws Exception { private Log4jLogEvent createLogEvent() {
// this needs to set up before LoggerContextRule return createLogEvent(new SimpleMessage(LOG_MESSAGE));
given(connectionFactory.createConnection()).willReturn(connection); }
given(connectionFactory.createConnection(anyString(), anyString())).will
Throw(IllegalArgumentException.class); private Log4jLogEvent createLogEvent(final Message message) {
given(connection.createSession(eq(false), eq(Session.AUTO_ACKNOWLEDGE))) // @formatter:off
.willReturn(session); return Log4jLogEvent.newBuilder()
given(session.createProducer(eq(destination))).willReturn(messageProduce .setLoggerName(JmsAppenderTest.class.getName())
r); .setLoggerFqcn(JmsAppenderTest.class.getName())
given(session.createTextMessage(anyString())).willReturn(textMessage); .setLevel(Level.INFO)
given(session.createObjectMessage(isA(Serializable.class))).willReturn(o .setMessage(message)
bjectMessage); .build();
// @formatter:on
}
private Log4jLogEvent createMapMessageLogEvent() {
final StringMapMessage mapMessage = new StringMapMessage();
return createLogEvent(mapMessage.with("testMesage", LOG_MESSAGE));
} }
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// we have 3 appenders all connecting to the same ConnectionFactory // we have 4 appenders all connecting to the same ConnectionFactory
then(connection).should(times(3)).start(); then(connection).should(times(4)).start();
} }
@Test @Test
public void testAppendToQueue() throws Exception { public void testAppendToQueue() throws Exception {
final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsA ppender"); final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsA ppender");
final LogEvent event = createLogEvent(); final LogEvent event = createLogEvent();
appender.append(event); appender.append(event);
then(session).should().createTextMessage(eq(LOG_MESSAGE)); then(session).should().createTextMessage(eq(LOG_MESSAGE));
then(textMessage).should().setJMSTimestamp(anyLong()); then(textMessage).should().setJMSTimestamp(anyLong());
then(messageProducer).should().send(textMessage); then(messageProducer).should().send(textMessage);
appender.stop(); appender.stop();
then(session).should().close(); then(session).should().close();
then(connection).should().close(); then(connection).should().close();
} }
@Test @Test
public void testAppendToQueueWithMessageLayout() throws Exception {
final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsA
ppender-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 { public void testJmsQueueAppenderCompatibility() throws Exception {
final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsQ ueueAppender"); final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsQ ueueAppender");
final LogEvent expected = createLogEvent(); final LogEvent expected = createLogEvent();
appender.append(expected); appender.append(expected);
then(session).should().createObjectMessage(eq(expected)); then(session).should().createObjectMessage(eq(expected));
then(objectMessage).should().setJMSTimestamp(anyLong()); then(objectMessage).should().setJMSTimestamp(anyLong());
then(messageProducer).should().send(objectMessage); then(messageProducer).should().send(objectMessage);
appender.stop(); appender.stop();
then(session).should().close(); then(session).should().close();
then(connection).should().close(); then(connection).should().close();
skipping to change at line 144 skipping to change at line 188
final LogEvent expected = createLogEvent(); final LogEvent expected = createLogEvent();
appender.append(expected); appender.append(expected);
then(session).should().createObjectMessage(eq(expected)); then(session).should().createObjectMessage(eq(expected));
then(objectMessage).should().setJMSTimestamp(anyLong()); then(objectMessage).should().setJMSTimestamp(anyLong());
then(messageProducer).should().send(objectMessage); then(messageProducer).should().send(objectMessage);
appender.stop(); appender.stop();
then(session).should().close(); then(session).should().close();
then(connection).should().close(); 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();
}
} }
 End of changes. 16 change blocks. 
32 lines changed or deleted 69 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)