    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;
    19
    20  import junit.framework.TestCase;
    21  import org.codehaus.activemq.message.ActiveMQMessage;
    22  import org.codehaus.activemq.message.ActiveMQQueue;
    23  import org.codehaus.activemq.message.ActiveMQTopic;
    24
    25  import javax.jms.Connection;
    26  import javax.jms.Destination;
    27  import javax.jms.JMSException;
    28  import javax.jms.Message;
    29  import javax.jms.TextMessage;
    30
    31
    32  /***
    33   * Useful base class for unit test cases
    34   *
    35   * @version $Revision: 1.7 $
    36   */
    37  public class TestSupport extends TestCase {
    38      protected ActiveMQConnectionFactory connectionFactory;
    39      protected boolean topic = true;
    40
    41      public TestSupport() {
    42          super();
    43      }
    44
    45      public TestSupport(String name) {
    46          super(name);
    47      }
    48
    49      protected ActiveMQMessage createMessage() {
    50          return new ActiveMQMessage();
    51      }
    52
    53      protected Destination createDestination(String subject) {
    54          if (topic) {
    55              return new ActiveMQTopic(subject);
    56          }
    57          else {
    58              return new ActiveMQQueue(subject);
    59          }
    60      }
    61
    62      /***
    63       * @param messsage
    64       * @param firstSet
    65       * @param secondSet
    66       */
    67      protected void assertTextMessagesEqual(String messsage, Message[] firstSet, Message[] secondSet) throws JMSException {
    68          assertEquals("Message count does not match: " + messsage, firstSet.length, secondSet.length);
    69          for (int i = 0; i < secondSet.length; i++) {
    70              TextMessage m1 = (TextMessage) firstSet[i];
    71              TextMessage m2 = (TextMessage) secondSet[i];
    72              assertFalse("Message " + (i + 1) + " did not match : " + messsage + ": expected {" + m1 + "}, but was {" + m2 + "}", m1 == null ^ m2 == null);
    73              assertEquals("Message " + (i + 1) + " did not match: " + messsage + ": expected {" + m1 + "}, but was {" + m2 + "}", m1.getText(), m2.getText());
    74          }
    75      }
    76
    77      protected ActiveMQConnectionFactory createConnectionFactory() {
    78          return new ActiveMQConnectionFactory("vm://localhost");
    79      }
    80
    81      /***
    82       * Factory method to create a new connection
    83       */
    84      protected Connection createConnection() throws JMSException {
    85          return getConnectionFactory().createConnection();
    86      }
    87
    88      public ActiveMQConnectionFactory getConnectionFactory() {
    89          if (connectionFactory == null) {
    90              connectionFactory = createConnectionFactory();
    91              assertTrue("Should have created a connection factory!", connectionFactory != null);
    92          }
    93          return connectionFactory;
    94      }
    95
    96      protected String getSubject() {
    97          return getClass().getName() + "." + getName();
    98      }
    99  }