1 package net.walend.jmsbridge.test; 2 3 import java.util.List ; 4 import java.util.ArrayList ; 5 import java.util.Set ; 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 9 import javax.jms.QueueConnection ; 10 import javax.jms.Queue ; 11 import javax.jms.QueueSession ; 12 import javax.jms.QueueSender ; 13 import javax.jms.Message ; 14 import javax.jms.ObjectMessage ; 15 import javax.jms.QueueReceiver ; 16 import javax.jms.Session ; 17 import javax.jms.JMSException ; 18 import javax.jms.MessageListener ; 19 20 import junit.framework.TestSuite; 21 import junit.framework.Test; 22 23 import net.walend.toolkit.junit.TestCase; 24 25 import net.walend.somnifugi.SomniJNDIBypass; 26 import net.walend.somnifugi.SomniDefaultExceptionListener; 27 28 import net.walend.jmsbridge.QueueBridge; 29 30 35 36 public class QueueTest extends TestCase 37 { 38 public QueueTest(String testName) 39 { 40 super(testName); 41 } 42 43 protected void sendTenObjects(QueueConnection connection,Queue queue) 44 { 45 try 46 { 47 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 48 QueueSender sender = session.createSender(queue); 49 for(int i=0;i<10;i++) 50 { 51 Message message = session.createObjectMessage(new Integer (i)); 52 sender.send(message); 53 } 54 } 55 catch(JMSException jmse) 56 { 57 fail(jmse); 58 } 59 } 60 61 protected void receiveTenObjects(QueueConnection connection,Queue queue) 62 { 63 try 64 { 65 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 66 QueueReceiver receiver = session.createReceiver(queue); 67 List <Object > results = new ArrayList <Object >(); 68 List <Object > expected = new ArrayList <Object >(); 69 70 for(int i=0;i<10;i++) 71 { 72 expected.add(new Integer (i)); 73 ObjectMessage message = (ObjectMessage )receiver.receive(100); 74 75 results.add(message.getObject()); 76 } 77 assertTrue("Expected results to be "+expected+", but got "+results,results.equals(expected)); 78 } 79 catch(JMSException jmse) 80 { 81 fail(jmse); 82 } 83 } 84 85 public void testHotStart() 86 { 87 try 88 { 89 QueueConnection connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection(); 90 connection.start(); 91 92 Queue bridgeIn = SomniJNDIBypass.IT.getQueue("brddgeInTest"); 93 94 Queue bridgeOut = SomniJNDIBypass.IT.getQueue("bridgeOutTest"); 95 96 97 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 98 99 QueueReceiver source = session.createReceiver(bridgeIn); 100 QueueSender sink = session.createSender(bridgeOut); 101 102 QueueBridge bridge = new QueueBridge(source,sink,session,new SomniDefaultExceptionListener()); 104 105 sendTenObjects(connection,bridgeIn); 106 107 receiveTenObjects(connection,bridgeOut); 108 } 109 catch(JMSException jmse) 110 { 111 fail(jmse); 112 } 113 } 114 115 protected class TenObjectSender 116 implements Runnable 117 { 118 private QueueConnection connection; 119 private Queue queue; 120 121 public TenObjectSender(QueueConnection connection,Queue queue) 122 { 123 this.connection = connection; 124 this.queue = queue; 125 } 126 127 public void run() 128 { 129 try 130 { 131 Thread.sleep(10); 132 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 133 QueueSender sender = session.createSender(queue); 134 for(int i=0;i<10;i++) 135 { 136 Message message = session.createObjectMessage(new Integer (i)); 137 sender.send(message); 138 } 139 } 140 catch(InterruptedException ie) 141 { 142 fail(ie); 143 } 144 catch(JMSException jmse) 145 { 146 fail(jmse); 147 } 148 } 149 } 150 151 protected class TenObjectReceiver 152 implements Runnable 153 { 154 private QueueConnection connection; 155 private Queue queue; 156 157 public TenObjectReceiver(QueueConnection connection,Queue queue) 158 { 159 this.connection = connection; 160 this.queue = queue; 161 } 162 163 public void run() 164 { 165 try 166 { 167 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 168 QueueReceiver receiver = session.createReceiver(queue); 169 List <Object > results = new ArrayList <Object >(); 170 List <Object > expected = new ArrayList <Object >(); 171 172 for(int i=0;i<10;i++) 173 { 174 expected.add(new Integer (i)); 175 ObjectMessage message = (ObjectMessage )receiver.receive(100); 176 177 results.add(message.getObject()); 178 } 179 assertTrue("Expected results to be "+expected+", but got "+results,results.equals(expected)); 180 } 181 catch(JMSException jmse) 182 { 183 fail(jmse); 184 } 185 } 186 } 187 188 public void testTwoThreads() 189 { 190 try 191 { 192 QueueConnection connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection(); 193 connection.start(); 194 195 Queue bridgeIn = SomniJNDIBypass.IT.getQueue("twoThreadsIn"); 196 197 Queue bridgeOut = SomniJNDIBypass.IT.getQueue("twoThreadsOut"); 198 199 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 200 201 QueueReceiver source = session.createReceiver(bridgeIn); 202 QueueSender sink = session.createSender(bridgeOut); 203 204 QueueBridge bridge = new QueueBridge(source,sink,session,new SomniDefaultExceptionListener()); 206 207 Thread sendThread = new Thread (new TenObjectSender(connection,bridgeIn)); 208 sendThread.start(); 209 210 Thread receiveThread = new Thread (new TenObjectReceiver(connection,bridgeOut)); 211 receiveThread.start(); 212 213 sendThread.join(10000); 214 receiveThread.join(10000); 215 216 } 217 catch(JMSException jmse) 218 { 219 fail(jmse); 220 } 221 catch(InterruptedException ie) 222 { 223 fail(ie); 224 } 225 } 226 227 public void testProperties() 228 { 229 try 230 { 231 QueueConnection connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection(); 232 connection.start(); 233 234 Queue bridgeIn = SomniJNDIBypass.IT.getQueue("brddgeInTest"); 235 236 Queue bridgeOut = SomniJNDIBypass.IT.getQueue("bridgeOutTest"); 237 238 239 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 240 241 QueueReceiver source = session.createReceiver(bridgeIn); 242 QueueSender sink = session.createSender(bridgeOut); 243 244 QueueBridge bridge = new QueueBridge(source,sink,session,new SomniDefaultExceptionListener()); 246 247 QueueSession session2 = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 248 QueueSender sender = session2.createSender(bridgeIn); 249 Message message = session.createObjectMessage(new Integer (0)); 250 251 message.setStringProperty("propName","test"); 252 253 sender.send(message); 254 255 QueueSession session3 = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 256 QueueReceiver receiver = session3.createReceiver(bridgeOut); 257 258 ObjectMessage message2 = (ObjectMessage )receiver.receive(100); 259 260 assertTrue("message2 should not be null.",null!=message2); 261 assertTrue("message2 should contain a property named 'propName'",message2.propertyExists("propName")); 262 263 assertTrue("message2's property 'propName' should be 'test' but is "+message2.getStringProperty("propName"),"test".equals(message2.getStringProperty("propName"))); 264 265 } 266 catch(JMSException jmse) 267 { 268 fail(jmse); 269 } 270 } 271 272 public static Test suite() 273 { 274 TestSuite suite = new TestSuite() ; 275 276 suite.addTest(new QueueTest("testHotStart")); 277 suite.addTest(new QueueTest("testTwoThreads")); 278 suite.addTest(new QueueTest("testProperties")); 279 280 return suite; 281 } 282 } 283 284 285 307 308 309 | Popular Tags |