KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > test > 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.test;
19
20 import java.lang.reflect.Array JavaDoc;
21
22 import javax.jms.Connection JavaDoc;
23 import javax.jms.Destination JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.TextMessage JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.apache.activemq.ActiveMQConnectionFactory;
31 import org.apache.activemq.command.ActiveMQMessage;
32 import org.apache.activemq.command.ActiveMQQueue;
33 import org.apache.activemq.command.ActiveMQTopic;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37
38 /**
39  * Useful base class for unit test cases
40  *
41  * @version $Revision: 1.4 $
42  */

43 public abstract class TestSupport extends TestCase {
44     final static protected Log log = LogFactory.getLog(TestSupport.class);
45     protected ActiveMQConnectionFactory connectionFactory;
46     protected boolean topic = true;
47
48     public TestSupport() {
49         super();
50     }
51
52     public TestSupport(String JavaDoc name) {
53         super(name);
54     }
55
56     /**
57      * Creates an ActiveMQMessage.
58      *
59      * @return ActiveMQMessage
60      */

61     protected ActiveMQMessage createMessage() {
62         return new ActiveMQMessage();
63     }
64
65     /**
66      * Creates a destination.
67      *
68      * @param subject - topic or queue name.
69      * @return Destination - either an ActiveMQTopic or ActiveMQQUeue.
70      */

71     protected Destination JavaDoc createDestination(String JavaDoc subject) {
72         if (topic) {
73             return new ActiveMQTopic(subject);
74         }
75         else {
76             return new ActiveMQQueue(subject);
77         }
78     }
79
80     /**
81      * Tests if firstSet and secondSet are equal.
82      *
83      * @param messsage - string to be displayed when the assertion fails.
84      * @param firstSet[] - set of messages to be compared with its counterpart in the secondset.
85      * @param secondSet[] - set of messages to be compared with its counterpart in the firstset.
86      * @throws JMSException
87      */

88     protected void assertTextMessagesEqual(Message JavaDoc[] firstSet, Message JavaDoc[] secondSet) throws JMSException JavaDoc {
89         assertTextMessagesEqual("", firstSet, secondSet);
90     }
91     
92     /**
93      * Tests if firstSet and secondSet are equal.
94      *
95      * @param messsage - string to be displayed when the assertion fails.
96      * @param firstSet[] - set of messages to be compared with its counterpart in the secondset.
97      * @param secondSet[] - set of messages to be compared with its counterpart in the firstset.
98      */

99     protected void assertTextMessagesEqual(String JavaDoc messsage, Message JavaDoc[] firstSet, Message JavaDoc[] secondSet) throws JMSException JavaDoc {
100         assertEquals("Message count does not match: " + messsage, firstSet.length, secondSet.length);
101
102         for (int i = 0; i < secondSet.length; i++) {
103             TextMessage JavaDoc m1 = (TextMessage JavaDoc) firstSet[i];
104             TextMessage JavaDoc m2 = (TextMessage JavaDoc) secondSet[i];
105             assertTextMessageEqual("Message " + (i + 1) + " did not match : ", m1,m2);
106         }
107     }
108     
109     /**
110      * Tests if m1 and m2 are equal.
111      *
112      * @param m1 - message to be compared with m2.
113      * @param m2 - message to be compared with m1.
114      * @throws JMSException
115      */

116     protected void assertEquals(TextMessage JavaDoc m1, TextMessage JavaDoc m2) throws JMSException JavaDoc {
117         assertEquals("", m1, m2);
118     }
119
120     /**
121      * Tests if m1 and m2 are equal.
122      *
123      * @param message - string to be displayed when the assertion fails.
124      * @param m1 - message to be compared with m2.
125      * @param m2 - message to be compared with m1.
126      */

127     protected void assertTextMessageEqual(String JavaDoc message, TextMessage JavaDoc m1, TextMessage JavaDoc m2) throws JMSException JavaDoc {
128         assertFalse(message + ": expected {" + m1 + "}, but was {" + m2 + "}", m1 == null ^ m2 == null);
129         
130         if( m1 == null ) {
131             return;
132         }
133         
134         assertEquals(message, m1.getText(), m2.getText());
135     }
136
137     /**
138      * Tests if m1 and m2 are equal.
139      *
140      * @param m1 - message to be compared with m2.
141      * @param m2 - message to be compared with m1.
142      * @throws JMSException
143      */

144     protected void assertEquals(Message JavaDoc m1, Message JavaDoc m2) throws JMSException JavaDoc {
145         assertEquals("", m1, m2);
146     }
147     
148     /**
149      * Tests if m1 and m2 are equal.
150      *
151      * @param message - error message.
152      * @param m1 - message to be compared with m2.
153      * @param m2 -- message to be compared with m1.
154      */

155     protected void assertEquals(String JavaDoc message, Message JavaDoc m1, Message JavaDoc m2) throws JMSException JavaDoc {
156         assertFalse(message + ": expected {" + m1 + "}, but was {" + m2 + "}", m1 == null ^ m2 == null);
157         
158         if( m1 == null ){
159             return;
160         }
161         
162         assertTrue(message + ": expected {" + m1 + "}, but was {" + m2 + "}", m1.getClass()==m2.getClass());
163         
164         if( m1 instanceof TextMessage JavaDoc ) {
165             assertTextMessageEqual(message, (TextMessage JavaDoc)m1, (TextMessage JavaDoc)m2);
166         } else {
167             assertEquals(message, m1, m2);
168         }
169     }
170
171     /**
172      * Creates an ActiveMQConnectionFactory.
173      *
174      * @return ActiveMQConnectionFactory
175      * @throws Exception
176      */

177     protected ActiveMQConnectionFactory createConnectionFactory() throws Exception JavaDoc {
178         return new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
179     }
180
181     /**
182      * Factory method to create a new connection.
183      *
184      * @return connection
185      * @throws Exception
186      */

187     protected Connection JavaDoc createConnection() throws Exception JavaDoc {
188         return getConnectionFactory().createConnection();
189     }
190
191     /**
192      * Creates an ActiveMQ connection factory.
193      *
194      * @return connectionFactory
195      * @throws Exception
196      */

197     public ActiveMQConnectionFactory getConnectionFactory() throws Exception JavaDoc {
198         if (connectionFactory == null) {
199             connectionFactory = createConnectionFactory();
200             assertTrue("Should have created a connection factory!", connectionFactory != null);
201         }
202         
203         return connectionFactory;
204     }
205
206     /**
207      * Returns the consumer subject.
208      *
209      * @return String
210      */

211     protected String JavaDoc getConsumerSubject() {
212         return getSubject();
213     }
214
215     /**
216      * Returns the producer subject.
217      *
218      * @return String
219      */

220     protected String JavaDoc getProducerSubject() {
221         return getSubject();
222     }
223
224     /**
225      * Returns the subject.
226      *
227      * @return String
228      */

229     protected String JavaDoc getSubject() {
230         return getClass().getName() + "." + getName();
231     }
232
233     protected void assertArrayEqual(String JavaDoc message, Object JavaDoc[] expected, Object JavaDoc[] actual) {
234         assertEquals(message + ". Array length", expected.length, actual.length);
235         for (int i = 0; i < expected.length; i++) {
236             assertEquals(message + ". element: " + i, expected[i], actual[i]);
237         }
238     }
239     
240     protected void assertPrimitiveArrayEqual(String JavaDoc message, Object JavaDoc expected, Object JavaDoc actual) {
241         int length = Array.getLength(expected);
242         assertEquals(message + ". Array length", length, Array.getLength(actual));
243         for (int i = 0; i < length; i++) {
244             assertEquals(message + ". element: " + i, Array.get(expected, i), Array.get(actual, i));
245         }
246     }
247 }
248
Popular Tags