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

📄 jmstemplate102tests.java

📁 spring的源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

	public void testQueueReceiveDestinationWithClientAcknowledgeWithSelector() throws Exception {
		doTestReceive(false, true, false, false, true, true, true, false);
	}

	public void testQueueReceiveStringDestinationWithSelector() throws Exception {
		doTestReceive(false, false, false, false, false, true, false, false);
	}

	public void testQueueReceiveAndConvertDefaultDestination() throws Exception {
		doTestReceive(false, false, true, true, false, false, false, true);
	}

	public void testQueueReceiveAndConvertStringDestination() throws Exception {
		doTestReceive(false, false, false, true, false, false, true, false);
	}

	public void testQueueReceiveAndConvertDestination() throws Exception {
		doTestReceive(false, true, false, true, false, false, true, true);
	}

	public void testQueueReceiveAndConvertDefaultDestinationWithSelector() throws Exception {
		doTestReceive(false, false, true, true, false, true, true, true);
	}

	public void testQueueReceiveAndConvertStringDestinationWithSelector() throws Exception {
		doTestReceive(false, false, false, true, false, true, true, false);
	}

	public void testQueueReceiveAndConvertDestinationWithSelector() throws Exception {
		doTestReceive(false, true, false, true, false, true, false, true);
	}

	public void testTopicReceiveDefaultDestination() throws Exception {
		doTestReceive(true, false, true, false, false, false, false, false);
	}

	public void testTopicReceiveDestination() throws Exception {
		doTestReceive(true, true, false, false, false, false, true, false);
	}

	public void testTopicReceiveDestinationWithClientAcknowledge() throws Exception {
		doTestReceive(true, true, false, false, true, false, false, true);
	}

	public void testTopicReceiveStringDestination() throws Exception {
		doTestReceive(true, false, false, false, false, false, true, true);
	}

	public void testTopicReceiveDefaultDestinationWithSelector() throws Exception {
		doTestReceive(true, false, true, false, false, true, true, true);
	}

	public void testTopicReceiveDestinationWithSelector() throws Exception {
		doTestReceive(true, true, false, false, false, true, false, true);
	}

	public void testTopicReceiveDestinationWithClientAcknowledgeWithSelector() throws Exception {
		doTestReceive(true, true, false, false, true, true, true, false);
	}

	public void testTopicReceiveStringDestinationWithSelector() throws Exception {
		doTestReceive(true, false, false, false, false, true, false, false);
	}

	public void testTopicReceiveAndConvertDefaultDestination() throws Exception {
		doTestReceive(true, false, true, true, false, false, false, true);
	}

	public void testTopicReceiveAndConvertStringDestination() throws Exception {
		doTestReceive(true, false, false, true, false, false, true, false);
	}

	public void testTopicReceiveAndConvertDestination() throws Exception {
		doTestReceive(true, true, false, true, false, false, true, true);
	}

	public void testTopicReceiveAndConvertDefaultDestinationWithSelector() throws Exception {
		doTestReceive(true, false, true, true, false, true, true, true);
	}

	public void testTopicReceiveAndConvertStringDestinationWithSelector() throws Exception {
		doTestReceive(true, false, false, true, false, true, true, false);
	}

	public void testTopicReceiveAndConvertDestinationWithSelector() throws Exception {
		doTestReceive(true, true, false, true, false, true, false, true);
	}

	private void doTestReceive(
			boolean pubSub,
			boolean explicitDestination, boolean useDefaultDestination, boolean testConverter,
			boolean clientAcknowledge, boolean messageSelector, boolean noLocal, boolean timeout)
			throws Exception {

		JmsTemplate102 template = createTemplate();
		template.setPubSubDomain(pubSub);
		if (pubSub) {
			template.setConnectionFactory(mockTopicConnectionFactory);
		}
		else {
			template.setConnectionFactory(mockQueueConnectionFactory);
		}

		// Override the default settings for client ack used in the test setup.
		// Can't use Session.getAcknowledgeMode()
		if (pubSub) {
			topicConnectionControl.reset();
			if (clientAcknowledge) {
				template.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
				mockTopicConnection.createTopicSession(useTransactedTemplate(), Session.CLIENT_ACKNOWLEDGE);
			}
			else {
				template.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
				mockTopicConnection.createTopicSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE);
			}
			topicConnectionControl.setReturnValue(mockTopicSession);
		}
		else {
			queueConnectionControl.reset();
			if (clientAcknowledge) {
				template.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
				mockQueueConnection.createQueueSession(useTransactedTemplate(), Session.CLIENT_ACKNOWLEDGE);
			}
			else {
				template.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
				mockQueueConnection.createQueueSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE);
			}
			queueConnectionControl.setReturnValue(mockQueueSession);
		}

		Destination dest = pubSub ? (Destination) mockTopic : (Destination) mockQueue;

		if (useDefaultDestination) {
			template.setDefaultDestination(dest);
		}
		if (noLocal) {
			template.setPubSubNoLocal(true);
		}
		if (timeout) {
			template.setReceiveTimeout(1000);
		}

		if (pubSub) {
			mockTopicConnection.start();
			topicConnectionControl.setVoidCallable(1);
			mockTopicConnection.close();
			topicConnectionControl.setVoidCallable(1);
		}
		else {
			mockQueueConnection.start();
			queueConnectionControl.setVoidCallable(1);
			mockQueueConnection.close();
			queueConnectionControl.setVoidCallable(1);
		}

		String selectorString = "selector";
		MockControl messageConsumerControl = null;
		MessageConsumer mockMessageConsumer = null;

		if (pubSub) {
			messageConsumerControl = MockControl.createControl(TopicSubscriber.class);
			TopicSubscriber mockTopicSubscriber = (TopicSubscriber) messageConsumerControl.getMock();
			mockMessageConsumer = mockTopicSubscriber;
			mockTopicSession.createSubscriber(mockTopic, messageSelector ? selectorString : null, noLocal);
			topicSessionControl.setReturnValue(mockTopicSubscriber);
		}
		else {
			messageConsumerControl = MockControl.createControl(QueueReceiver.class);
			QueueReceiver mockQueueReceiver = (QueueReceiver) messageConsumerControl.getMock();
			mockMessageConsumer = mockQueueReceiver;
			mockQueueSession.createReceiver(mockQueue, messageSelector ? selectorString : null);
			queueSessionControl.setReturnValue(mockQueueReceiver);
		}

		if (useTransactedTemplate()) {
			if (pubSub) {
				mockTopicSession.commit();
				topicSessionControl.setVoidCallable(1);
			}
			else {
				mockQueueSession.commit();
				queueSessionControl.setVoidCallable(1);
			}
		}

		if (pubSub) {
			mockTopicSession.close();
			topicSessionControl.setVoidCallable(1);
		}
		else {
			mockQueueSession.close();
			queueSessionControl.setVoidCallable(1);
		}

		MockControl messageControl = MockControl.createControl(TextMessage.class);
		TextMessage mockMessage = (TextMessage) messageControl.getMock();

		if (testConverter) {
			mockMessage.getText();
			messageControl.setReturnValue("Hello World!");
		}
		if (!useTransactedSession() && clientAcknowledge) {
			mockMessage.acknowledge();
			messageControl.setVoidCallable(1);
		}

		if (pubSub) {
			topicSessionControl.replay();
			topicConnectionControl.replay();
		}
		else {
			queueSessionControl.replay();
			queueConnectionControl.replay();
		}
		messageControl.replay();

		if (timeout) {
			mockMessageConsumer.receive(1000);
		}
		else {
			mockMessageConsumer.receive();
		}
		messageConsumerControl.setReturnValue(mockMessage);
		mockMessageConsumer.close();
		messageConsumerControl.setVoidCallable(1);

		messageConsumerControl.replay();

		Message message = null;
		String textFromMessage = null;

		if (useDefaultDestination) {
			if (testConverter) {
				textFromMessage = (String)
						(messageSelector ? template.receiveSelectedAndConvert(selectorString) :
						template.receiveAndConvert());
			}
			else {
				message = (messageSelector ? template.receiveSelected(selectorString) : template.receive());
			}
		}
		else if (explicitDestination) {
			if (testConverter) {
				textFromMessage = (String)
						(messageSelector ? template.receiveSelectedAndConvert(dest, selectorString) :
						template.receiveAndConvert(dest));
			}
			else {
				message = (messageSelector ? template.receiveSelected(dest, selectorString) :
						template.receive(dest));
			}
		}
		else {
			String destinationName = (pubSub ? "testTopic" : "testQueue");
			if (testConverter) {
				textFromMessage = (String)
						(messageSelector ? template.receiveSelectedAndConvert(destinationName, selectorString) :
						template.receiveAndConvert(destinationName));
			}
			else {
				message = (messageSelector ? template.receiveSelected(destinationName, selectorString) :
						template.receive(destinationName));
			}
		}

		if (pubSub) {
			topicConnectionFactoryControl.verify();
			topicConnectionControl.verify();
			topicSessionControl.verify();
		}
		else {
			queueConnectionFactoryControl.verify();
			queueConnectionControl.verify();
			queueSessionControl.verify();
		}
		messageConsumerControl.verify();
		messageControl.verify();

		if (testConverter) {
			assertEquals("Message text should be equal", "Hello World!", textFromMessage);
		}
		else {
			assertEquals("Messages should refer to the same object", message, mockMessage);
		}
	}

	public void testIllegalStateException() throws Exception {
		doTestJmsException(new javax.jms.IllegalStateException(""), org.springframework.jms.IllegalStateException.class);
	}

	public void testInvalidClientIDException() throws Exception {
		doTestJmsException(new javax.jms.InvalidClientIDException(""), InvalidClientIDException.class);
	}

	public void testInvalidDestinationException() throws Exception {
		doTestJmsException(new javax.jms.InvalidDestinationException(""), InvalidDestinationException.class);
	}

	public void testInvalidSelectorException() throws Exception {
		doTestJmsException(new javax.jms.InvalidSelectorException(""), InvalidSelectorException.class);
	}

	public void testJmsSecurityException() throws Exception {
		doTestJmsException(new javax.jms.JMSSecurityException(""), JmsSecurityException.class);
	}

	public void testMessageEOFException() throws Exception {
		doTestJmsException(new javax.jms.MessageEOFException(""), MessageEOFException.class);
	}

	public void testMessageFormatException() throws Exception {
		doTestJmsException(new javax.jms.MessageFormatException(""), MessageFormatException.class);
	}

	public void testMessageNotReadableException() throws Exception {
		doTestJmsException(new javax.jms.MessageNotReadableException(""), MessageNotReadableException.class);
	}

	public void testMessageNotWriteableException() throws Exception {
		doTestJmsException(new javax.jms.MessageNotWriteableException(""), MessageNotWriteableException.class);
	}

	public void testResourceAllocationException() throws Exception {
		doTestJmsException(new javax.jms.ResourceAllocationException(""), ResourceAllocationException.class);
	}

	public void testTransactionInProgressException() throws Exception {
		doTestJmsException(new javax.jms.TransactionInProgressException(""), TransactionInProgressException.class);
	}

	public void testTransactionRolledBackException() throws Exception {
		doTestJmsException(new javax.jms.TransactionRolledBackException(""), TransactionRolledBackException.class);
	}

	public void testUncategorizedJmsException() throws Exception {
		doTestJmsException(new javax.jms.JMSException(""), UncategorizedJmsException.class);
	}

	protected void doTestJmsException(JMSException original, Class thrownExceptionClass) throws Exception {
		JmsTemplate template = createTemplate();
		template.setConnectionFactory(mockQueueConnectionFactory);
		template.setMessageConverter(new SimpleMessageConverter());
		String s = "Hello world";

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

		queueSessionControl.reset();
		mockQueueSession.createSender(mockQueue);
		queueSessionControl.setReturnValue(mockQueueSender);
		mockQueueSession.createTextMessage("Hello world");
		queueSessionControl.setReturnValue(mockMessage);

		mockQueueSender.send(mockMessage);
		queueSenderControl.setThrowable(original, 1);
		mockQueueSender.close();
		queueSenderControl.setVoidCallable(1);

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

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

		try {
			template.convertAndSend(mockQueue, s);
			fail("Should have thrown JmsException");
		}
		catch (JmsException wrappedEx) {
			// expected
			assertEquals(thrownExceptionClass, wrappedEx.getClass());
			assertEquals(original, wrappedEx.getCause());
		}

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

}

⌨️ 快捷键说明

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