1 package net.walend.somnifugi.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 import java.util.Hashtable ; 9 10 import javax.naming.InitialContext ; 11 import javax.naming.Context ; 12 import javax.naming.NamingException ; 13 14 import javax.jms.QueueConnectionFactory ; 15 import javax.jms.QueueConnection ; 16 import javax.jms.Queue ; 17 import javax.jms.QueueSession ; 18 import javax.jms.QueueSender ; 19 import javax.jms.Message ; 20 import javax.jms.ObjectMessage ; 21 import javax.jms.TextMessage ; 22 import javax.jms.QueueReceiver ; 23 import javax.jms.Session ; 24 import javax.jms.JMSException ; 25 import javax.jms.MessageListener ; 26 import javax.jms.DeliveryMode ; 27 import javax.jms.QueueRequestor ; 28 import javax.jms.MessageFormatException ; 29 30 import junit.framework.TestSuite; 31 import junit.framework.Test; 32 33 import net.walend.toolkit.junit.TestCase; 34 35 import net.walend.somnifugi.SomniJNDIBypass; 36 import net.walend.somnifugi.SomniQueueConnectionFactory; 37 38 43 44 public class MessageTest extends TestCase 45 { 46 public MessageTest(String testName) 47 { 48 super(testName); 49 } 50 51 protected void sendObjects(QueueConnection connection,Queue queue,int howMany) 52 { 53 try 54 { 55 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 56 QueueSender sender = session.createSender(queue); 57 for(int i=0;i<howMany;i++) 58 { 59 Message message = session.createObjectMessage(new Integer (i)); 60 61 sender.send(message); 62 } 63 } 64 catch(JMSException jmse) 65 { 66 fail(jmse); 67 } 68 } 69 70 protected ObjectMessage receiveMessage(QueueConnection connection,Queue queue) 71 { 72 try 73 { 74 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 75 QueueReceiver receiver = session.createReceiver(queue); 76 77 return (ObjectMessage )receiver.receive(100); 78 } 79 catch(JMSException jmse) 80 { 81 fail(jmse); 82 return null; 83 } 84 } 85 86 protected void receiveNoObjects(QueueConnection connection,Queue queue) 87 { 88 try 89 { 90 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 91 QueueReceiver receiver = session.createReceiver(queue); 92 93 for(int i=0;i<10;i++) 94 { 95 ObjectMessage message = (ObjectMessage )receiver.receive(10); 96 97 assertTrue("Message should be null but is "+message,message==null); 98 } 99 } 100 catch(JMSException jmse) 101 { 102 fail(jmse); 103 } 104 } 105 106 public void testProperties() 107 { 108 try 109 { 110 QueueConnection connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection(); 111 112 TestExceptionListener exceptionListener = new TestExceptionListener(); 113 connection.setExceptionListener(exceptionListener); 114 115 connection.start(); 116 117 Queue queue = SomniJNDIBypass.IT.getQueue("testProperties"); 118 119 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 120 QueueSender sender = session.createSender(queue); 121 Message message = session.createObjectMessage(new Integer (0)); 122 123 message.setBooleanProperty("true prop",true); 124 message.setBooleanProperty("false prop",false); 125 126 message.setByteProperty("byte prop",(byte)13); 127 128 message.setObjectProperty("double prop",new Double (233.322)); 129 130 sender.send(message); 131 132 Message message2 = receiveMessage(connection,queue); 133 134 assertTrue(message2.getBooleanProperty("true prop")); 135 assertTrue(!message2.getBooleanProperty("false prop")); 136 assertTrue(!message2.getBooleanProperty("never set prop")); 137 138 assertTrue(message2.getByteProperty("byte prop")==(byte)13); 139 140 assertTrue(message2.getObjectProperty("double prop").equals(new Double (233.322))); 141 assertTrue(message2.getDoubleProperty("double prop")==233.322); 142 assertTrue(message2.getObjectProperty("byte prop").equals(new Byte ((byte)13))); 143 144 try 145 { 146 message2.getByteProperty("true prop"); 147 fail("true prop should not return a byte"); 148 } 149 catch(MessageFormatException mfe) 150 { 151 } 153 154 receiveNoObjects(connection,queue); 155 156 connection.close(); 157 158 if(exceptionListener.caughtException()) 159 { 160 fail(exceptionListener.getException()); 161 } 162 } 163 catch(JMSException jmse) 164 { 165 fail(jmse); 166 } 167 } 168 169 public void testTextMessage() 170 { 171 try 172 { 173 QueueConnection connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection(); 174 175 TestExceptionListener exceptionListener = new TestExceptionListener(); 176 connection.setExceptionListener(exceptionListener); 177 178 connection.start(); 179 180 Queue queue = SomniJNDIBypass.IT.getQueue("textMessageTest"); 181 182 QueueSession session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE); 183 QueueSender sender = session.createSender(queue); 184 Message message = session.createTextMessage("Hi!"); 185 186 sender.send(message); 187 188 QueueReceiver receiver = session.createReceiver(queue); 189 190 TextMessage message2 = (TextMessage )receiver.receive(100); 191 192 assertTrue(message2.getText().equals("Hi!")); 193 194 receiveNoObjects(connection,queue); 195 196 connection.close(); 197 198 if(exceptionListener.caughtException()) 199 { 200 fail(exceptionListener.getException()); 201 } 202 } 203 catch(JMSException jmse) 204 { 205 fail(jmse); 206 } 207 } 208 209 public static Test suite() 210 { 211 TestSuite suite = new TestSuite() ; 212 213 suite.addTest(new MessageTest("testProperties")); 214 suite.addTest(new MessageTest("testTextMessage")); 215 216 return suite; 217 } 218 } 219 220 221 243 244 245 | Popular Tags |