1 18 package org.apache.activemq.test; 19 20 import java.lang.reflect.Array ; 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 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 name) { 53 super(name); 54 } 55 56 61 protected ActiveMQMessage createMessage() { 62 return new ActiveMQMessage(); 63 } 64 65 71 protected Destination createDestination(String subject) { 72 if (topic) { 73 return new ActiveMQTopic(subject); 74 } 75 else { 76 return new ActiveMQQueue(subject); 77 } 78 } 79 80 88 protected void assertTextMessagesEqual(Message [] firstSet, Message [] secondSet) throws JMSException { 89 assertTextMessagesEqual("", firstSet, secondSet); 90 } 91 92 99 protected void assertTextMessagesEqual(String messsage, Message [] firstSet, Message [] secondSet) throws JMSException { 100 assertEquals("Message count does not match: " + messsage, firstSet.length, secondSet.length); 101 102 for (int i = 0; i < secondSet.length; i++) { 103 TextMessage m1 = (TextMessage ) firstSet[i]; 104 TextMessage m2 = (TextMessage ) secondSet[i]; 105 assertTextMessageEqual("Message " + (i + 1) + " did not match : ", m1,m2); 106 } 107 } 108 109 116 protected void assertEquals(TextMessage m1, TextMessage m2) throws JMSException { 117 assertEquals("", m1, m2); 118 } 119 120 127 protected void assertTextMessageEqual(String message, TextMessage m1, TextMessage m2) throws JMSException { 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 144 protected void assertEquals(Message m1, Message m2) throws JMSException { 145 assertEquals("", m1, m2); 146 } 147 148 155 protected void assertEquals(String message, Message m1, Message m2) throws JMSException { 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 ) { 165 assertTextMessageEqual(message, (TextMessage )m1, (TextMessage )m2); 166 } else { 167 assertEquals(message, m1, m2); 168 } 169 } 170 171 177 protected ActiveMQConnectionFactory createConnectionFactory() throws Exception { 178 return new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); 179 } 180 181 187 protected Connection createConnection() throws Exception { 188 return getConnectionFactory().createConnection(); 189 } 190 191 197 public ActiveMQConnectionFactory getConnectionFactory() throws Exception { 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 211 protected String getConsumerSubject() { 212 return getSubject(); 213 } 214 215 220 protected String getProducerSubject() { 221 return getSubject(); 222 } 223 224 229 protected String getSubject() { 230 return getClass().getName() + "." + getName(); 231 } 232 233 protected void assertArrayEqual(String message, Object [] expected, Object [] 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 message, Object expected, Object 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 |