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