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 org.apache.activemq.ActiveMQConnectionFactory;
21  import org.logicblaze.lingo.jms.impl.DefaultJmsProducer;
22  import org.logicblaze.lingo.jms.impl.SingleThreadedRequestor;
23  
24  import javax.jms.Connection;
25  import javax.jms.ConnectionFactory;
26  import javax.jms.JMSException;
27  import javax.jms.Session;
28  
29  import junit.framework.TestCase;
30  
31  /***
32   * A useful base class for any JMS related test cases
33   *
34   * @version $Revision: 1.6 $
35   */
36  public abstract class JmsTestSupport extends TestCase {
37      protected ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
38      protected Connection connection;
39      protected JmsProducerConfig config = new JmsProducerConfig();
40  
41      protected JmsProducer createJmsProducer() throws JMSException {
42          return DefaultJmsProducer.newInstance(connectionFactory, config );
43      }
44  
45      protected Session createSession() throws JMSException {
46          return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
47      }
48  
49      protected Connection getConnection() throws JMSException {
50          if (connection == null) {
51              connection = connectionFactory.createConnection();
52              connection.start();
53          }
54          return connection;
55      }
56  
57      protected Requestor createRequestor(String serverDestinationName) throws Exception {
58          Session session = createSession();
59          JmsProducer producer = createJmsProducer();
60          return new SingleThreadedRequestor(connection, session, producer.getMessageProducer(), session.createQueue(serverDestinationName), false);
61      }
62  
63      protected Requestor createRequestor(String serverDestinationName, String clientDestinationName) throws Exception {
64          Session session = createSession();
65          JmsProducer producer = createJmsProducer();
66          return new SingleThreadedRequestor(connection, session, producer.getMessageProducer(), session.createQueue(serverDestinationName), false);
67      }
68  
69      protected void closeSession(JmsProxyFactoryBean factoryBean) throws JMSException {
70          Requestor requestor = factoryBean.getRequestor();
71          requestor.getSession().close();
72      }
73  
74      protected String getDestinationName() {
75          return "test." + getClass().getName() + "." + getName();
76      }
77  }