1 18 19 package org.apache.activemq.spring; 20 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import junit.framework.TestCase; 25 26 import org.apache.activemq.broker.BrokerService; 27 import org.springframework.context.support.AbstractApplicationContext; 28 import org.springframework.context.support.ClassPathXmlApplicationContext; 29 30 public class SpringTest extends TestCase { 31 32 private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 33 .getLog(SpringTest.class); 34 35 protected AbstractApplicationContext context; 36 protected SpringConsumer consumer; 37 protected SpringProducer producer; 38 39 44 public void testSenderWithSpringXmlEmbeddedPooledBrokerConfiguredViaXml() throws Exception { 45 String config = "spring-embedded-pooled.xml"; 46 47 Thread.currentThread().setContextClassLoader(SpringTest.class.getClassLoader()); 48 ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext(config); 49 50 BrokerService bs1 = (BrokerService) context1.getBean("broker1"); 51 assertNotNull(bs1); 52 BrokerService bs2 = (BrokerService) context1.getBean("broker2"); 53 assertNotNull(bs1); 54 55 assertEquals(bs1, bs2); 57 58 ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext(config); 60 61 BrokerService bs3 = (BrokerService) context2.getBean("broker1"); 62 assertNotNull(bs3); 63 BrokerService bs4 = (BrokerService) context2.getBean("broker2"); 64 assertNotNull(bs4); 65 66 assertEquals(bs1, bs3); 68 assertEquals(bs1, bs4); 69 70 assertTrue(bs1.isStarted()); 72 73 context2.close(); 75 assertTrue(bs1.isStarted()); 76 77 context1.close(); 79 assertFalse(bs1.isStarted()); 80 81 } 82 83 89 public void testSenderWithSpringXml() throws Exception { 90 String config = "spring.xml"; 91 assertSenderConfig(config); 92 } 93 94 101 public void testSenderWithSpringXmlAndQueue() throws Exception { 102 String config = "spring-queue.xml"; 103 assertSenderConfig(config); 104 } 105 106 112 public void testSenderWithSpringXmlUsingJNDI() throws Exception { 113 String config = "spring-jndi.xml"; 114 assertSenderConfig(config); 115 } 116 117 124 public void testSenderWithSpringXmlEmbeddedBrokerConfiguredViaXml() throws Exception { 125 String config = "spring-embedded.xml"; 126 assertSenderConfig(config); 127 } 128 129 135 public void XtestSenderWithSpringXmlUsingSpring2NamespacesWithEmbeddedBrokerConfiguredViaXml() throws Exception { 136 String config = "spring-embedded-xbean.xml"; 137 assertSenderConfig(config); 138 } 139 140 147 protected void assertSenderConfig(String config) throws Exception { 148 Thread.currentThread().setContextClassLoader(SpringTest.class.getClassLoader()); 149 context = new ClassPathXmlApplicationContext(config); 150 151 consumer = (SpringConsumer) context.getBean("consumer"); 152 assertTrue("Found a valid consumer", consumer != null); 153 154 consumer.start(); 155 156 Thread.sleep(1000); 158 consumer.flushMessages(); 159 160 producer = (SpringProducer) context.getBean("producer"); 161 assertTrue("Found a valid producer", producer != null); 162 163 producer.start(); 164 165 consumer.waitForMessagesToArrive(producer.getMessageCount()); 167 168 List messages = consumer.flushMessages(); 170 log.info("Consumer has received messages...."); 171 for (Iterator iter = messages.iterator(); iter.hasNext();) { 172 Object message = iter.next(); 173 log.info("Received: " + message); 174 } 175 176 assertEquals("Message count", producer.getMessageCount(), messages.size()); 177 } 178 179 184 protected void tearDown() throws Exception { 185 if (consumer != null) { 186 consumer.stop(); 187 } 188 if (producer != null) { 189 producer.stop(); 190 } 191 192 if (context != null) { 193 context.destroy(); 194 } 195 } 196 197 } 198 | Popular Tags |