⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmstemplate102tests.java

📁 Java/J2EE application framework based on [Expert One-on-One J2EE Design and Development] by Rod John
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2002-2004 the original author or authors.
 *
 * Licensed 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.springframework.jms.core;

import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.NamingException;

import junit.framework.TestCase;
import org.easymock.MockControl;

import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.jms.support.destination.JndiDestinationResolver;
import org.springframework.jndi.JndiTemplate;

/**
 * Unit tests for the JmsTemplate implemented using JMS 1.0.2.
 * @author Andre Biryukov
 * @author Mark Pollack
 */
public class JmsTemplate102Tests extends TestCase {

	private Context mockJndiContext;
	private MockControl mockJndiControl;

	private MockControl queueConnectionFactoryControl;
	private QueueConnectionFactory mockQueueConnectionFactory;

	private MockControl queueConnectionControl;
	private QueueConnection mockQueueConnection;

	private MockControl queueSessionControl;
	private QueueSession mockQueueSession;

	private MockControl queueControl;
	private Queue mockQueue;

	private MockControl topicConnectionFactoryControl;
	private TopicConnectionFactory mockTopicConnectionFactory;

	private MockControl topicConnectionControl;
	private TopicConnection mockTopicConnection;

	private MockControl topicSessionControl;
	private TopicSession mockTopicSession;

	private MockControl topicControl;
	private Topic mockTopic;

	private int deliveryMode = DeliveryMode.PERSISTENT;
	private int priority = 9;
	private int timeToLive = 10000;

	/**
	 * Create the mock objects for testing.
	 */
	protected void setUp() throws Exception {
		mockJndiControl = MockControl.createControl(Context.class);
		mockJndiContext = (Context) this.mockJndiControl.getMock();

		createMockforQueues();
		createMockforTopics();

		mockJndiContext.close();
		mockJndiControl.replay();

	}

	private void createMockforQueues() throws JMSException, NamingException {
		queueConnectionFactoryControl = MockControl.createControl(QueueConnectionFactory.class);
		mockQueueConnectionFactory = (QueueConnectionFactory) queueConnectionFactoryControl.getMock();

		queueConnectionControl = MockControl.createControl(QueueConnection.class);
		mockQueueConnection = (QueueConnection) queueConnectionControl.getMock();

		queueControl = MockControl.createControl(Queue.class);
		mockQueue = (Queue) queueControl.getMock();

		queueSessionControl = MockControl.createControl(QueueSession.class);
		mockQueueSession = (QueueSession) queueSessionControl.getMock();

		mockQueueConnectionFactory.createQueueConnection();
		queueConnectionFactoryControl.setReturnValue(mockQueueConnection);
		queueConnectionFactoryControl.replay();

		// TODO tests with TX=true
		mockQueueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		queueConnectionControl.setReturnValue(mockQueueSession);
		mockQueueSession.getTransacted();
		queueSessionControl.setReturnValue(false);

		mockJndiContext.lookup("testQueue");
		mockJndiControl.setReturnValue(mockQueue);
	}

	private void createMockforTopics() throws JMSException, NamingException {
		topicConnectionFactoryControl = MockControl.createControl(TopicConnectionFactory.class);
		mockTopicConnectionFactory = (TopicConnectionFactory) topicConnectionFactoryControl.getMock();

		topicConnectionControl = MockControl.createControl(TopicConnection.class);
		mockTopicConnection = (TopicConnection) topicConnectionControl.getMock();

		topicControl = MockControl.createControl(Topic.class);
		mockTopic = (Topic) topicControl.getMock();

		topicSessionControl = MockControl.createControl(TopicSession.class);
		mockTopicSession = (TopicSession) topicSessionControl.getMock();

		mockTopicConnectionFactory.createTopicConnection();
		topicConnectionFactoryControl.setReturnValue(mockTopicConnection);
		topicConnectionFactoryControl.replay();

		// TODO tests with TX =true
		mockTopicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
		topicConnectionControl.setReturnValue(mockTopicSession);

		mockJndiContext.lookup("testTopic");
		mockJndiControl.setReturnValue(mockTopic);
	}

	public void testTopicSessionCallback() throws Exception {
		JmsTemplate102 sender = new JmsTemplate102();
		sender.setPubSubDomain(true);
		sender.setConnectionFactory(mockTopicConnectionFactory);
		setJndiTemplate(sender);
		sender.afterPropertiesSet();

		mockTopicSession.getTransacted();
		topicSessionControl.setReturnValue(true);

		mockTopicSession.close();
		topicSessionControl.setVoidCallable(1);

		mockTopicConnection.close();
		topicConnectionControl.setVoidCallable(1);

		topicSessionControl.replay();
		topicConnectionControl.replay();

		sender.execute(new SessionCallback() {
			public Object doInJms(Session session) throws JMSException {
				boolean b = session.getTransacted();
				return null;
			}
		});

		topicConnectionFactoryControl.verify();
		topicConnectionControl.verify();
		topicSessionControl.verify();
	}

	/**
	 * Test the execute(ProducerCallback) using a topic.
	 */
	public void testTopicProducerCallback() throws Exception {
		JmsTemplate102 sender = new JmsTemplate102();
		sender.setPubSubDomain(true);
		sender.setConnectionFactory(mockTopicConnectionFactory);
		setJndiTemplate(sender);
		sender.afterPropertiesSet();

		MockControl topicPublisherControl = MockControl.createControl(TopicPublisher.class);
		TopicPublisher mockTopicPublisher = (TopicPublisher) topicPublisherControl.getMock();

		mockTopicSession.createPublisher(null);
		topicSessionControl.setReturnValue(mockTopicPublisher);

		mockTopicSession.getTransacted();
		topicSessionControl.setReturnValue(true);

		mockTopicPublisher.getPriority();
		topicPublisherControl.setReturnValue(4);

		mockTopicSession.close();
		topicSessionControl.setVoidCallable(1);

		mockTopicConnection.close();
		topicConnectionControl.setVoidCallable(1);

		topicSessionControl.replay();
		topicConnectionControl.replay();

		sender.execute(new ProducerCallback() {
			public Object doInJms(Session session, MessageProducer msgProducer) throws JMSException {
				boolean b = session.getTransacted();
				int i = msgProducer.getPriority();
				return null;
			}
		});

		topicConnectionFactoryControl.verify();
		topicConnectionControl.verify();
		topicSessionControl.verify();
	}

	/**
	 * Test the method execute(SessionCallback action) with using the
	 * point to point domain as specified by the value of isPubSubDomain = false.
	 */
	public void testQueueSessionCallback() throws Exception {
		JmsTemplate102 sender = new JmsTemplate102();
		// Point-to-Point (queues) are the default domain
		sender.setConnectionFactory(mockQueueConnectionFactory);
		setJndiTemplate(sender);
		sender.afterPropertiesSet();

		mockQueueSession.close();
		queueSessionControl.setVoidCallable(1);

		mockQueueConnection.close();
		queueConnectionControl.setVoidCallable(1);

		queueSessionControl.replay();
		queueConnectionControl.replay();

		sender.execute(new SessionCallback() {
			public Object doInJms(Session session) throws JMSException {
				boolean b = session.getTransacted();
				return null;
			}
		});

		queueConnectionFactoryControl.verify();
		queueConnectionControl.verify();
		queueSessionControl.verify();
	}

	/**
	 * Test the method execute(ProducerCallback) with a Queue.
	 */
	public void testQueueProducerCallback() throws Exception {
		JmsTemplate102 sender = new JmsTemplate102();
		// Point-to-Point (queues) are the default domain.
		sender.setConnectionFactory(mockQueueConnectionFactory);
		setJndiTemplate(sender);
		sender.afterPropertiesSet();

		MockControl queueSenderControl = MockControl.createControl(QueueSender.class);
		QueueSender mockQueueSender = (QueueSender) queueSenderControl.getMock();

		mockQueueSession.createSender(null);
		queueSessionControl.setReturnValue(mockQueueSender);

		mockQueueSession.close();
		queueSessionControl.setVoidCallable(1);

		mockQueueSender.getPriority();
		queueSenderControl.setReturnValue(4);

		queueSenderControl.replay();

		mockQueueConnection.close();
		queueConnectionControl.setVoidCallable(1);

		queueSessionControl.replay();
		queueConnectionControl.replay();

		sender.execute(new ProducerCallback() {
			public Object doInJms(Session session, MessageProducer msgProducer)
			    throws JMSException {
				boolean b = session.getTransacted();
				int i = msgProducer.getPriority();
				return null;
			}
		});

		queueConnectionFactoryControl.verify();
		queueConnectionControl.verify();
		queueSessionControl.verify();
	}

	/**
	 * Test the setting of the JmsTemplate properties.
	 */
	public void testBeanProperties() throws Exception {
		JmsTemplate102 sender = new JmsTemplate102();
		sender.setConnectionFactory(mockQueueConnectionFactory);

		assertTrue("connection factory ok", sender.getConnectionFactory() == mockQueueConnectionFactory);

		JmsTemplate102 s102 = new JmsTemplate102();
		try {
			s102.afterPropertiesSet();
			fail("IllegalArgumentException not thrown. ConnectionFactory should be set");
		}
		catch (IllegalArgumentException e) {
			assertEquals("Exception message not matching", "connectionFactory is required", e.getMessage());
		}

		// The default is for the JmsTemplate102 to send to queues.
		// Test to make sure exeception is thrown and has reasonable message.
		s102 = new JmsTemplate102();
		s102.setConnectionFactory(mockTopicConnectionFactory);
		try {
			s102.afterPropertiesSet();
			fail("IllegalArgumentException not thrown. Mismatch of Destination and ConnectionFactory types.");
		}
		catch (IllegalArgumentException ex) {
			// expected
		}

		s102 = new JmsTemplate102();
		s102.setConnectionFactory(mockQueueConnectionFactory);
		s102.setPubSubDomain(true);
		try {
			s102.afterPropertiesSet();
			fail("IllegalArgumentException not thrown. Mismatch of Destination and ConnectionFactory types.");
		}
		catch (IllegalArgumentException ex) {
			// expected
		}
	}

	/**
	 * Test the method send(String destination, MessgaeCreator c) using
	 * a queue and default QOS values.
	 */
	public void testSendStringQueue() throws Exception {
		sendQueue(true, false, false);
	}

	/**
	 * Test the method send(String destination, MessageCreator c) when
	 * explicit QOS parameters are enabled, using a queue.
	 */
	public void testSendStringQueueWithQOS() throws Exception {
		sendQueue(false, false, false);
	}

	/**
	 * Test the method send(MessageCreator c) using default QOS values.
	 */
	public void testSendDefaultDestinationQueue() throws Exception {
		sendQueue(true, false, true);
	}

	/**
	 * Test the method send(MessageCreator c) using explicit QOS values.
	 */
	public void testSendDefaultDestinationQueueWithQOS() throws Exception {
		sendQueue(false, false, true);
	}

	/**
	 * Test the method send(String destination, MessageCreator c) using

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -