KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > usecases > TestSupport


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.usecases;
19
20 import org.apache.activemq.ActiveMQConnectionFactory;
21 import org.apache.activemq.command.ActiveMQMessage;
22 import org.apache.activemq.command.ActiveMQQueue;
23 import org.apache.activemq.command.ActiveMQTopic;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import javax.jms.Connection JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Message JavaDoc;
31 import javax.jms.TextMessage JavaDoc;
32
33 import junit.framework.TestCase;
34
35
36 /**
37  * Useful base class for unit test cases
38  *
39  * @version $Revision: 1.1.1.1 $
40  */

41 public class TestSupport extends TestCase {
42     protected Log log = LogFactory.getLog(getClass());
43     protected ActiveMQConnectionFactory connectionFactory;
44     protected boolean topic = true;
45
46     public TestSupport() {
47         super();
48     }
49
50     public TestSupport(String JavaDoc name) {
51         super(name);
52     }
53
54     protected ActiveMQMessage createMessage() {
55         return new ActiveMQMessage();
56     }
57
58     protected Destination JavaDoc createDestination(String JavaDoc subject) {
59         if (topic) {
60             return new ActiveMQTopic(subject);
61         }
62         else {
63             return new ActiveMQQueue(subject);
64         }
65     }
66
67     protected void assertTextMessagesEqual(Message[] firstSet, Message[] secondSet) throws JMSException JavaDoc {
68         assertTextMessagesEqual("", firstSet, secondSet);
69     }
70     /**
71      * @param messsage
72      * @param firstSet
73      * @param secondSet
74      */

75     protected void assertTextMessagesEqual(String JavaDoc messsage, Message[] firstSet, Message[] secondSet) throws JMSException JavaDoc {
76         assertEquals("Message count does not match: " + messsage, firstSet.length, secondSet.length);
77         for (int i = 0; i < secondSet.length; i++) {
78             TextMessage JavaDoc m1 = (TextMessage JavaDoc) firstSet[i];
79             TextMessage JavaDoc m2 = (TextMessage JavaDoc) secondSet[i];
80             assertTextMessageEqual("Message " + (i + 1) + " did not match : ", m1,m2);
81         }
82     }
83     
84     protected void assertEquals(TextMessage JavaDoc m1, TextMessage JavaDoc m2) throws JMSException JavaDoc {
85         assertEquals("", m1, m2);
86     }
87
88     /**
89      * @param message
90      * @param firstSet
91      * @param secondSet
92      */

93     protected void assertTextMessageEqual(String JavaDoc message, TextMessage JavaDoc m1, TextMessage JavaDoc m2) throws JMSException JavaDoc {
94         assertFalse(message + ": expected {" + m1 + "}, but was {" + m2 + "}", m1 == null ^ m2 == null);
95         if( m1 == null )
96             return;
97         assertEquals(message, m1.getText(), m2.getText());
98     }
99
100     protected void assertEquals(Message m1, Message m2) throws JMSException JavaDoc {
101         assertEquals("", m1, m2);
102     }
103     /**
104      * @param message
105      * @param firstSet
106      * @param secondSet
107      */

108     protected void assertEquals(String JavaDoc message, Message m1, Message m2) throws JMSException JavaDoc {
109         assertFalse(message + ": expected {" + m1 + "}, but was {" + m2 + "}", m1 == null ^ m2 == null);
110         if( m1 == null )
111             return;
112         assertTrue(message + ": expected {" + m1 + "}, but was {" + m2 + "}", m1.getClass()==m2.getClass());
113         if( m1 instanceof TextMessage JavaDoc ) {
114             assertTextMessageEqual(message, (TextMessage JavaDoc)m1, (TextMessage JavaDoc)m2);
115         } else {
116             assertEquals(message, m1, m2);
117         }
118     }
119
120     protected ActiveMQConnectionFactory createConnectionFactory() throws Exception JavaDoc {
121         return new ActiveMQConnectionFactory("vm://localhost");
122     }
123
124     /**
125      * Factory method to create a new connection
126      */

127     protected Connection JavaDoc createConnection() throws Exception JavaDoc {
128         return getConnectionFactory().createConnection();
129     }
130
131     public ActiveMQConnectionFactory getConnectionFactory() throws Exception JavaDoc {
132         if (connectionFactory == null) {
133             connectionFactory = createConnectionFactory();
134             assertTrue("Should have created a connection factory!", connectionFactory != null);
135         }
136         return connectionFactory;
137     }
138
139     protected String JavaDoc getConsumerSubject() {
140         return getSubject();
141     }
142
143     protected String JavaDoc getProducerSubject() {
144         return getSubject();
145     }
146
147     protected String JavaDoc getSubject() {
148         return getClass().getName() + "." + getName();
149     }
150 }
151
Popular Tags