1 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 ; 28 import javax.jms.Destination ; 29 import javax.jms.JMSException ; 30 import javax.jms.Message ; 31 import javax.jms.TextMessage ; 32 33 import junit.framework.TestCase; 34 35 36 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 protected void assertTextMessagesEqual(Message[] firstSet, Message[] secondSet) throws JMSException { 68 assertTextMessagesEqual("", firstSet, secondSet); 69 } 70 75 protected void assertTextMessagesEqual(String messsage, Message[] firstSet, Message[] secondSet) throws JMSException { 76 assertEquals("Message count does not match: " + messsage, firstSet.length, secondSet.length); 77 for (int i = 0; i < secondSet.length; i++) { 78 TextMessage m1 = (TextMessage ) firstSet[i]; 79 TextMessage m2 = (TextMessage ) secondSet[i]; 80 assertTextMessageEqual("Message " + (i + 1) + " did not match : ", m1,m2); 81 } 82 } 83 84 protected void assertEquals(TextMessage m1, TextMessage m2) throws JMSException { 85 assertEquals("", m1, m2); 86 } 87 88 93 protected void assertTextMessageEqual(String message, TextMessage m1, TextMessage m2) throws JMSException { 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 { 101 assertEquals("", m1, m2); 102 } 103 108 protected void assertEquals(String message, Message m1, Message m2) throws JMSException { 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 ) { 114 assertTextMessageEqual(message, (TextMessage )m1, (TextMessage )m2); 115 } else { 116 assertEquals(message, m1, m2); 117 } 118 } 119 120 protected ActiveMQConnectionFactory createConnectionFactory() throws Exception { 121 return new ActiveMQConnectionFactory("vm://localhost"); 122 } 123 124 127 protected Connection createConnection() throws Exception { 128 return getConnectionFactory().createConnection(); 129 } 130 131 public ActiveMQConnectionFactory getConnectionFactory() throws Exception { 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 getConsumerSubject() { 140 return getSubject(); 141 } 142 143 protected String getProducerSubject() { 144 return getSubject(); 145 } 146 147 protected String getSubject() { 148 return getClass().getName() + "." + getName(); 149 } 150 } 151 | Popular Tags |