Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.xyxy.platform.examples.showcase.demos.jms; import static org.assertj.core.api.Assertions.*; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.Session; import com.xyxy.platform.examples.showcase.demos.jms.advanced.AdvancedNotifyMessageListener; import com.xyxy.platform.examples.showcase.demos.jms.advanced.AdvancedNotifyMessageProducer; import com.xyxy.platform.examples.showcase.entity.User; import org.junit.Test; import org.junit.experimental.categories.Category; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import com.xyxy.platform.modules.core.test.category.UnStable; import com.xyxy.platform.modules.core.test.log.LogbackListAppender; import com.xyxy.platform.modules.core.test.spring.SpringContextTestCase; import com.xyxy.platform.modules.core.utils.Threads; @Category(UnStable.class) @DirtiesContext @ContextConfiguration(locations = { "/applicationContext.xml", "/jms/applicationContext-jms-advanced.xml" }) public class JmsAdvancedTest extends SpringContextTestCase { @Autowired private AdvancedNotifyMessageProducer notifyMessageProducer; @Resource private JmsTemplate advancedJmsTemplate; @Resource private Destination advancedNotifyTopic; @Test public void queueMessage() { Threads.sleep(1000); LogbackListAppender appender = new LogbackListAppender(); appender.addToLogger(AdvancedNotifyMessageListener.class); User user = new User(); user.setName("calvin"); user.setEmail("calvin@sringside.org.cn"); notifyMessageProducer.sendQueue(user); Threads.sleep(1000); assertThat(appender.getFirstMessage()) .isEqualTo("UserName:calvin, Email:calvin@sringside.org.cn, ObjectType:user"); } @Test public void topicMessage() { Threads.sleep(1000); LogbackListAppender appender = new LogbackListAppender(); appender.addToLogger(AdvancedNotifyMessageListener.class); User user = new User(); user.setName("calvin"); user.setEmail("calvin@sringside.org.cn"); notifyMessageProducer.sendTopic(user); Threads.sleep(1000); assertThat(appender.getFirstMessage()) .isEqualTo("UserName:calvin, Email:calvin@sringside.org.cn, ObjectType:user"); } @Test public void topicMessageWithWrongType() { Threads.sleep(1000); LogbackListAppender appender = new LogbackListAppender(); appender.addToLogger(AdvancedNotifyMessageListener.class); advancedJmsTemplate.send(advancedNotifyTopic, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { MapMessage message = session.createMapMessage(); message.setStringProperty("objectType", "role"); return message; } }); Threads.sleep(1000); assertThat(appender.isEmpty()).isTrue(); } }