BrokerTestSupport xref

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    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.codehaus.activemq.service;
    19
    20  import junit.framework.TestCase;
    21  import org.codehaus.activemq.broker.Broker;
    22  import org.codehaus.activemq.broker.impl.DefaultBroker;
    23  import org.codehaus.activemq.filter.Filter;
    24  import org.codehaus.activemq.filter.FilterFactory;
    25  import org.codehaus.activemq.filter.FilterFactoryImpl;
    26  import org.codehaus.activemq.message.ActiveMQDestination;
    27  import org.codehaus.activemq.message.ActiveMQMessage;
    28  import org.codehaus.activemq.message.ActiveMQQueue;
    29  import org.codehaus.activemq.message.ActiveMQTopic;
    30  import org.codehaus.activemq.message.ConsumerInfo;
    31  import org.codehaus.activemq.util.IdGenerator;
    32
    33  import javax.jms.JMSException;
    34
    35  /***
    36   * Useful base class for Broker related test cases
    37   *
    38   * @version $Revision: 1.4 $
    39   */
    40  public abstract class BrokerTestSupport extends TestCase {
    41
    42      private static final String BROKER_NAME = "TEST_BROKER";
    43
    44      protected Broker broker;
    45      protected FilterFactory filterFactory = new FilterFactoryImpl();
    46      protected IdGenerator idGenerator = new IdGenerator();
    47
    48      protected boolean isTopic() {
    49          return true;
    50      }
    51
    52      protected ActiveMQMessage createMessage(String subject) {
    53          ActiveMQMessage message = new ActiveMQMessage();
    54          message.setJMSMessageID(idGenerator.generateId());
    55          message.setJMSDestination(createDestination(subject));
    56          return message;
    57      }
    58
    59      protected Filter createFilter(String subject, String selector) throws JMSException {
    60          return filterFactory.createFilter(createDestination(subject), selector);
    61      }
    62
    63      protected ActiveMQDestination createDestination(String subject) {
    64          if (isTopic()) {
    65              return new ActiveMQTopic(subject);
    66          }
    67          else {
    68              return new ActiveMQQueue(subject);
    69          }
    70      }
    71
    72      protected Broker createStore() {
    73          return new DefaultBroker(BROKER_NAME);
    74      }
    75
    76      protected ConsumerInfo createConsumer(String subject) {
    77          return createConsumer(subject, null);
    78      }
    79
    80      protected ConsumerInfo createConsumer(String subject, String selector) {
    81          ConsumerInfo info = new ConsumerInfo();
    82          info.setConsumerId(idGenerator.generateId());
    83          info.setDestination(createDestination(subject));
    84          info.setSelector(selector);
    85          return info;
    86      }
    87
    88      protected void setUp() throws Exception {
    89          broker = new DefaultBroker(BROKER_NAME);
    90          broker.start();
    91      }
    92
    93      protected void tearDown() throws Exception {
    94          broker.stop();
    95      }
    96  }