1 /*** 2 * 3 * Copyright 2005 LogicBlaze, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 **/ 18 package org.logicblaze.lingo.jms; 19 20 import junit.framework.TestCase; 21 import org.activemq.ActiveMQConnectionFactory; 22 import org.logicblaze.lingo.jms.impl.DefaultJmsProducer; 23 import org.logicblaze.lingo.jms.impl.SingleThreadedRequestor; 24 25 import javax.jms.Connection; 26 import javax.jms.ConnectionFactory; 27 import javax.jms.JMSException; 28 import javax.jms.Session; 29 30 /*** 31 * A useful base class for any JMS related test cases 32 * 33 * @version $Revision: 1.2 $ 34 */ 35 public abstract class JmsTestSupport extends TestCase { 36 protected ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); 37 protected Connection connection; 38 39 protected JmsProducer createJmsProducer() throws JMSException { 40 return DefaultJmsProducer.newInstance(connectionFactory); 41 } 42 43 protected Session createSession() throws JMSException { 44 return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE); 45 } 46 47 protected Connection getConnection() throws JMSException { 48 if (connection == null) { 49 connection = connectionFactory.createConnection(); 50 connection.start(); 51 } 52 return connection; 53 } 54 55 protected Requestor createRequestor(String serverDestinationName) throws Exception { 56 Session session = createSession(); 57 JmsProducer producer = createJmsProducer(); 58 return new SingleThreadedRequestor(session, producer, session.createQueue(serverDestinationName)); 59 } 60 61 protected Requestor createRequestor(String serverDestinationName, String clientDestinationName) throws Exception { 62 Session session = createSession(); 63 JmsProducer producer = createJmsProducer(); 64 return new SingleThreadedRequestor(session, producer, session.createQueue(serverDestinationName)); 65 } 66 67 protected void closeSession(JmsProxyFactoryBean factoryBean) throws JMSException { 68 Requestor requestor = factoryBean.getRequestor(); 69 requestor.getSession().close(); 70 } 71 72 protected String getDestinationName() { 73 return "test." + getClass().getName() + "." + getName(); 74 } 75 }