KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > 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;
19
20 import java.io.File 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.5 $
42  */

43 public class TestSupport extends TestCase {
44
45     protected Log log = LogFactory.getLog(getClass());
46     protected ActiveMQConnectionFactory connectionFactory;
47     protected boolean topic = true;
48
49     public TestSupport() {
50         super();
51     }
52
53     public TestSupport(String JavaDoc name) {
54         super(name);
55     }
56
57     protected ActiveMQMessage createMessage() {
58         return new ActiveMQMessage();
59     }
60
61     protected Destination JavaDoc createDestination(String JavaDoc subject) {
62         if (topic) {
63             return new ActiveMQTopic(subject);
64         }
65         else {
66             return new ActiveMQQueue(subject);
67         }
68     }
69
70     protected Destination JavaDoc createDestination() {
71         return createDestination(getDestinationString());
72     }
73
74     /**
75      * Returns the name of the destination used in this test case
76      */

77     protected String JavaDoc getDestinationString() {
78         return getClass().getName() + "." + getName();
79     }
80     
81     
82     /**
83      * @param messsage
84      * @param firstSet
85      * @param secondSet
86      */

87     protected void assertTextMessagesEqual(String JavaDoc messsage, Message[] firstSet, Message[] secondSet) throws JMSException JavaDoc {
88         assertEquals("Message count does not match: " + messsage, firstSet.length, secondSet.length);
89         for (int i = 0; i < secondSet.length; i++) {
90             TextMessage JavaDoc m1 = (TextMessage JavaDoc) firstSet[i];
91             TextMessage JavaDoc m2 = (TextMessage JavaDoc) secondSet[i];
92             assertFalse("Message " + (i + 1) + " did not match : " + messsage + ": expected {" + m1 + "}, but was {" + m2 + "}", m1 == null ^ m2 == null);
93             assertEquals("Message " + (i + 1) + " did not match: " + messsage + ": expected {" + m1 + "}, but was {" + m2 + "}", m1.getText(), m2.getText());
94         }
95     }
96
97     protected ActiveMQConnectionFactory createConnectionFactory() throws Exception JavaDoc {
98         return new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
99     }
100
101     /**
102      * Factory method to create a new connection
103      */

104     protected Connection JavaDoc createConnection() throws Exception JavaDoc {
105         return getConnectionFactory().createConnection();
106     }
107
108     public ActiveMQConnectionFactory getConnectionFactory() throws Exception JavaDoc {
109         if (connectionFactory == null) {
110             connectionFactory = createConnectionFactory();
111             assertTrue("Should have created a connection factory!", connectionFactory != null);
112         }
113         return connectionFactory;
114     }
115
116     protected String JavaDoc getConsumerSubject() {
117         return getSubject();
118     }
119
120     protected String JavaDoc getProducerSubject() {
121         return getSubject();
122     }
123
124     protected String JavaDoc getSubject() {
125         return getClass().getName() + "." + getName();
126     }
127     
128     
129     public static void recursiveDelete(File JavaDoc f) {
130         if( f.isDirectory() ) {
131             File JavaDoc[] files = f.listFiles();
132             for (int i = 0; i < files.length; i++) {
133                 recursiveDelete(files[i]);
134             }
135         }
136         f.delete();
137     }
138
139     public static void removeMessageStore() {
140         if( System.getProperty("activemq.store.dir")!=null ) {
141             recursiveDelete(new File JavaDoc(System.getProperty("activemq.store.dir")));
142         }
143         if( System.getProperty("derby.system.home")!=null ) {
144             recursiveDelete(new File JavaDoc(System.getProperty("derby.system.home")));
145         }
146     }
147 }
148
Popular Tags