1 18 package org.apache.activemq; 19 20 import java.io.File ; 21 22 import javax.jms.Connection ; 23 import javax.jms.Destination ; 24 import javax.jms.JMSException ; 25 import javax.jms.Message ; 26 import javax.jms.TextMessage ; 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 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 name) { 54 super(name); 55 } 56 57 protected ActiveMQMessage createMessage() { 58 return new ActiveMQMessage(); 59 } 60 61 protected Destination createDestination(String subject) { 62 if (topic) { 63 return new ActiveMQTopic(subject); 64 } 65 else { 66 return new ActiveMQQueue(subject); 67 } 68 } 69 70 protected Destination createDestination() { 71 return createDestination(getDestinationString()); 72 } 73 74 77 protected String getDestinationString() { 78 return getClass().getName() + "." + getName(); 79 } 80 81 82 87 protected void assertTextMessagesEqual(String messsage, Message[] firstSet, Message[] secondSet) throws JMSException { 88 assertEquals("Message count does not match: " + messsage, firstSet.length, secondSet.length); 89 for (int i = 0; i < secondSet.length; i++) { 90 TextMessage m1 = (TextMessage ) firstSet[i]; 91 TextMessage m2 = (TextMessage ) 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 { 98 return new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); 99 } 100 101 104 protected Connection createConnection() throws Exception { 105 return getConnectionFactory().createConnection(); 106 } 107 108 public ActiveMQConnectionFactory getConnectionFactory() throws Exception { 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 getConsumerSubject() { 117 return getSubject(); 118 } 119 120 protected String getProducerSubject() { 121 return getSubject(); 122 } 123 124 protected String getSubject() { 125 return getClass().getName() + "." + getName(); 126 } 127 128 129 public static void recursiveDelete(File f) { 130 if( f.isDirectory() ) { 131 File [] 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 (System.getProperty("activemq.store.dir"))); 142 } 143 if( System.getProperty("derby.system.home")!=null ) { 144 recursiveDelete(new File (System.getProperty("derby.system.home"))); 145 } 146 } 147 } 148 | Popular Tags |