Source code: org/activemq/TestSupport.java
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.activemq;
19
20 import java.io.File;
21
22 import junit.framework.TestCase;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.activemq.message.ActiveMQMessage;
26 import org.activemq.message.ActiveMQQueue;
27 import org.activemq.message.ActiveMQTopic;
28
29 import javax.jms.Connection;
30 import javax.jms.Destination;
31 import javax.jms.JMSException;
32 import javax.jms.Message;
33 import javax.jms.TextMessage;
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 name) {
51 super(name);
52 }
53
54 protected ActiveMQMessage createMessage() {
55 return new ActiveMQMessage();
56 }
57
58 protected Destination createDestination(String subject) {
59 if (topic) {
60 return new ActiveMQTopic(subject);
61 }
62 else {
63 return new ActiveMQQueue(subject);
64 }
65 }
66
67 /**
68 * @param messsage
69 * @param firstSet
70 * @param secondSet
71 */
72 protected void assertTextMessagesEqual(String messsage, Message[] firstSet, Message[] secondSet) throws JMSException {
73 assertEquals("Message count does not match: " + messsage, firstSet.length, secondSet.length);
74 for (int i = 0; i < secondSet.length; i++) {
75 TextMessage m1 = (TextMessage) firstSet[i];
76 TextMessage m2 = (TextMessage) secondSet[i];
77 assertFalse("Message " + (i + 1) + " did not match : " + messsage + ": expected {" + m1 + "}, but was {" + m2 + "}", m1 == null ^ m2 == null);
78 assertEquals("Message " + (i + 1) + " did not match: " + messsage + ": expected {" + m1 + "}, but was {" + m2 + "}", m1.getText(), m2.getText());
79 }
80 }
81
82 protected ActiveMQConnectionFactory createConnectionFactory() throws Exception {
83 return new ActiveMQConnectionFactory("vm://localhost");
84 }
85
86 /**
87 * Factory method to create a new connection
88 */
89 protected Connection createConnection() throws Exception {
90 return getConnectionFactory().createConnection();
91 }
92
93 public ActiveMQConnectionFactory getConnectionFactory() throws Exception {
94 if (connectionFactory == null) {
95 connectionFactory = createConnectionFactory();
96 assertTrue("Should have created a connection factory!", connectionFactory != null);
97 }
98 return connectionFactory;
99 }
100
101 protected String getConsumerSubject() {
102 return getSubject();
103 }
104
105 protected String getProducerSubject() {
106 return getSubject();
107 }
108
109 protected String getSubject() {
110 return getClass().getName() + "." + getName();
111 }
112
113
114 public static void recursiveDelete(File f) {
115 if( f.isDirectory() ) {
116 File[] files = f.listFiles();
117 for (int i = 0; i < files.length; i++) {
118 recursiveDelete(files[i]);
119 }
120 }
121 f.delete();
122 }
123
124 public static void removeMessageStore() {
125 if( System.getProperty("activemq.store.dir")!=null ) {
126 recursiveDelete(new File(System.getProperty("activemq.store.dir")));
127 }
128 if( System.getProperty("derby.system.home")!=null ) {
129 recursiveDelete(new File(System.getProperty("derby.system.home")));
130 }
131 }
132
133
134
135 }