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

📄 simplemessagelistenercontainertests.java

📁 struts+spring 源码 希望能给大家带来帮助
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		assertTrue(listener.listenerInvoked);
		mockMessage.verify();
		mockSession.verify();
		mockConnection.verify();
		mockConnectionFactory.verify();
	}

	public void testRegisteredExceptionListenerIsInvokedOnException() throws Exception {
		final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();

		MockControl mockSession = MockControl.createControl(Session.class);
		Session session = (Session) mockSession.getMock();
		// Queue gets created in order to create MessageConsumer for that Destination...
		session.createQueue(DESTINATION_NAME);
		mockSession.setReturnValue(QUEUE_DESTINATION);
		// and then the MessageConsumer gets created...
		session.createConsumer(QUEUE_DESTINATION, null); // no MessageSelector...
		mockSession.setReturnValue(messageConsumer);
		// an exception is thrown, so the rollback logic is being applied here...
		session.getTransacted();
		mockSession.setReturnValue(false);
		mockSession.replay();

		MockControl mockConnection = MockControl.createControl(Connection.class);
		Connection connection = (Connection) mockConnection.getMock();
		// session gets created in order to register MessageListener...
		connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode());
		mockConnection.setReturnValue(session);
		// and the connection is start()ed after the listener is registered...
		connection.start();
		mockConnection.setVoidCallable();
		mockConnection.replay();

		MockControl mockConnectionFactory = MockControl.createControl(ConnectionFactory.class);
		ConnectionFactory connectionFactory = (ConnectionFactory) mockConnectionFactory.getMock();
		connectionFactory.createConnection();
		mockConnectionFactory.setReturnValue(connection);
		mockConnectionFactory.replay();

		final JMSException theException = new JMSException(EXCEPTION_MESSAGE);

		this.container.setConnectionFactory(connectionFactory);
		this.container.setDestinationName(DESTINATION_NAME);
		this.container.setMessageListener(new SessionAwareMessageListener() {
			public void onMessage(Message message, Session session) throws JMSException {
				throw theException;
			}
		});

		MockControl mockExceptionListener = MockControl.createControl(ExceptionListener.class);
		ExceptionListener exceptionListener = (ExceptionListener) mockExceptionListener.getMock();
		exceptionListener.onException(theException);
		mockExceptionListener.setVoidCallable();
		mockExceptionListener.replay();

		this.container.setExceptionListener(exceptionListener);
		this.container.afterPropertiesSet();

		// manually trigger an Exception with the above bad MessageListener...
		MockControl mockMessage = MockControl.createControl(Message.class);
		final Message message = (Message) mockMessage.getMock();
		mockMessage.replay();

		// a Throwable from a MessageListener MUST simply be swallowed...
		messageConsumer.sendMessage(message);

		mockExceptionListener.verify();
		mockMessage.verify();
		mockSession.verify();
		mockConnection.verify();
		mockConnectionFactory.verify();
	}

	public void testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate() throws Exception {
		final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();

		MockControl mockSession = MockControl.createControl(Session.class);
		Session session = (Session) mockSession.getMock();
		// Queue gets created in order to create MessageConsumer for that Destination...
		session.createQueue(DESTINATION_NAME);
		mockSession.setReturnValue(QUEUE_DESTINATION);
		// and then the MessageConsumer gets created...
		session.createConsumer(QUEUE_DESTINATION, null); // no MessageSelector...
		mockSession.setReturnValue(messageConsumer);
		// an exception is thrown, so the rollback logic is being applied here...
		session.getTransacted();
		mockSession.setReturnValue(false);
		mockSession.replay();

		MockControl mockConnection = MockControl.createControl(Connection.class);
		Connection connection = (Connection) mockConnection.getMock();
		// session gets created in order to register MessageListener...
		connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode());
		mockConnection.setReturnValue(session);
		// and the connection is start()ed after the listener is registered...
		connection.start();
		mockConnection.setVoidCallable();
		mockConnection.replay();

		MockControl mockConnectionFactory = MockControl.createControl(ConnectionFactory.class);
		ConnectionFactory connectionFactory = (ConnectionFactory) mockConnectionFactory.getMock();
		connectionFactory.createConnection();
		mockConnectionFactory.setReturnValue(connection);
		mockConnectionFactory.replay();

		this.container.setConnectionFactory(connectionFactory);
		this.container.setDestinationName(DESTINATION_NAME);
		this.container.setMessageListener(new MessageListener() {
			public void onMessage(Message message) {
				throw new UnsupportedOperationException();
			}
		});
		this.container.afterPropertiesSet();

		// manually trigger an Exception with the above bad MessageListener...
		MockControl mockMessage = MockControl.createControl(Message.class);
		final Message message = (Message) mockMessage.getMock();
		mockMessage.replay();

		// a Throwable from a MessageListener MUST simply be swallowed...
		messageConsumer.sendMessage(message);

		mockMessage.verify();
		mockSession.verify();
		mockConnection.verify();
		mockConnectionFactory.verify();
	}

	public void testTransactedSessionsGetRollbackLogicAppliedAndThatExceptionsStillDo_NOT_Propagate() throws Exception {
		this.container.setSessionTransacted(true);

		final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();

		MockControl mockSession = MockControl.createControl(Session.class);
		Session session = (Session) mockSession.getMock();
		// Queue gets created in order to create MessageConsumer for that Destination...
		session.createQueue(DESTINATION_NAME);
		mockSession.setReturnValue(QUEUE_DESTINATION);
		// and then the MessageConsumer gets created...
		session.createConsumer(QUEUE_DESTINATION, null); // no MessageSelector...
		mockSession.setReturnValue(messageConsumer);
		// an exception is thrown, so the rollback logic is being applied here...
		session.getTransacted();
		mockSession.setReturnValue(true);
		// Session is rolled back 'cos it is transacted...
		session.rollback();
		mockSession.setVoidCallable();
		mockSession.replay();

		MockControl mockConnection = MockControl.createControl(Connection.class);
		Connection connection = (Connection) mockConnection.getMock();
		// session gets created in order to register MessageListener...
		connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode());
		mockConnection.setReturnValue(session);
		// and the connection is start()ed after the listener is registered...
		connection.start();
		mockConnection.setVoidCallable();
		mockConnection.replay();

		MockControl mockConnectionFactory = MockControl.createControl(ConnectionFactory.class);
		ConnectionFactory connectionFactory = (ConnectionFactory) mockConnectionFactory.getMock();
		connectionFactory.createConnection();
		mockConnectionFactory.setReturnValue(connection);
		mockConnectionFactory.replay();

		this.container.setConnectionFactory(connectionFactory);
		this.container.setDestinationName(DESTINATION_NAME);
		this.container.setMessageListener(new MessageListener() {
			public void onMessage(Message message) {
				throw new UnsupportedOperationException();
			}
		});
		this.container.afterPropertiesSet();

		// manually trigger an Exception with the above bad MessageListener...
		MockControl mockMessage = MockControl.createControl(Message.class);
		final Message message = (Message) mockMessage.getMock();
		mockMessage.replay();

		// a Throwable from a MessageListener MUST simply be swallowed...
		messageConsumer.sendMessage(message);

		mockMessage.verify();
		mockSession.verify();
		mockConnection.verify();
		mockConnectionFactory.verify();
	}

	public void testDestroyClosesConsumersSessionsAndConnectionInThatOrder() throws Exception {
		MockControl mockMessageConsumer = MockControl.createControl(MessageConsumer.class);
		MessageConsumer messageConsumer = (MessageConsumer) mockMessageConsumer.getMock();
		messageConsumer.setMessageListener(null);
		// anon. inner class passed in, so just expect a call...
		mockMessageConsumer.setMatcher(new AlwaysMatcher());
		mockMessageConsumer.setVoidCallable();
		// closing down...
		messageConsumer.close();
		mockMessageConsumer.setVoidCallable();
		mockMessageConsumer.replay();

		MockControl mockSession = MockControl.createControl(Session.class);
		Session session = (Session) mockSession.getMock();
		// Queue gets created in order to create MessageConsumer for that Destination...
		session.createQueue(DESTINATION_NAME);
		mockSession.setReturnValue(QUEUE_DESTINATION);
		// and then the MessageConsumer gets created...
		session.createConsumer(QUEUE_DESTINATION, null); // no MessageSelector...
		mockSession.setReturnValue(messageConsumer);
		// closing down...
		session.close();
		mockSession.setVoidCallable();
		mockSession.replay();

		MockControl mockConnection = MockControl.createControl(Connection.class);
		Connection connection = (Connection) mockConnection.getMock();
		// session gets created in order to register MessageListener...
		connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode());
		mockConnection.setReturnValue(session);
		// and the connection is start()ed after the listener is registered...
		connection.start();
		mockConnection.setVoidCallable();
		// closing down...
		connection.close();
		mockConnection.setVoidCallable();
		mockConnection.replay();

		MockControl mockConnectionFactory = MockControl.createControl(ConnectionFactory.class);
		ConnectionFactory connectionFactory = (ConnectionFactory) mockConnectionFactory.getMock();
		connectionFactory.createConnection();
		mockConnectionFactory.setReturnValue(connection);
		mockConnectionFactory.replay();

		this.container.setConnectionFactory(connectionFactory);
		this.container.setDestinationName(DESTINATION_NAME);

		this.container.setMessageListener(new TestMessageListener());
		this.container.afterPropertiesSet();
		this.container.destroy();

		mockMessageConsumer.verify();
		mockSession.verify();
		mockConnection.verify();
		mockConnectionFactory.verify();
	}


	private static class TestMessageListener implements MessageListener {

		public boolean executorInvoked = false;

		public boolean listenerInvoked = false;

		public void onMessage(Message message) {
			this.listenerInvoked = true;
		}
	}


	private static class SimpleMessageConsumer implements MessageConsumer {

		private MessageListener messageListener;

		public void sendMessage(Message message) throws JMSException {
			this.messageListener.onMessage(message);
		}

		public String getMessageSelector() throws JMSException {
			throw new UnsupportedOperationException();
		}

		public MessageListener getMessageListener() throws JMSException {
			return this.messageListener;
		}

		public void setMessageListener(MessageListener messageListener) throws JMSException {
			this.messageListener = messageListener;
		}

		public Message receive() throws JMSException {
			throw new UnsupportedOperationException();
		}

		public Message receive(long l) throws JMSException {
			throw new UnsupportedOperationException();
		}

		public Message receiveNoWait() throws JMSException {
			throw new UnsupportedOperationException();
		}

		public void close() throws JMSException {
			throw new UnsupportedOperationException();
		}
	}

}

⌨️ 快捷键说明

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