1 24 25 package org.objectweb.jtests.jms.conform.session; 26 27 import org.objectweb.jtests.jms.framework.*; 28 import junit.framework.*; 29 30 import javax.jms.*; 31 import java.util.Hashtable ; 32 33 41 public class TopicSessionTest extends PubSubTestCase { 42 43 47 public void testRollbackReceivedMessage() { 48 try { 49 publisherConnection.stop(); 50 publisherSession = publisherConnection.createTopicSession(true,0); 53 assertEquals(true, publisherSession.getTransacted()); 54 publisher = publisherSession.createPublisher(publisherTopic); 56 publisherConnection.start(); 57 58 subscriberConnection.stop(); 59 subscriberSession = subscriberConnection.createTopicSession(true,0); 62 assertEquals(true, subscriberSession.getTransacted()); 63 subscriber = subscriberSession.createSubscriber(subscriberTopic); 65 subscriberConnection.start(); 66 67 TextMessage message = publisherSession.createTextMessage(); 69 message.setText("testRollbackReceivedMessage"); 70 publisher.publish(message); 72 publisherSession.commit(); 74 75 Message msg1 = subscriber.receive(1000); 77 assertTrue("no message received", msg1 != null); 78 assertTrue(msg1 instanceof TextMessage); 79 assertEquals("testRollbackReceivedMessage", ((TextMessage)msg1).getText()); 80 81 subscriberSession.rollback(); 83 84 Message msg2 = subscriber.receive(1000); 86 assertTrue("no message received after rollbacking subscriber session.", msg2 != null); 87 assertTrue(msg2 instanceof TextMessage); 88 assertEquals("testRollbackReceivedMessage", ((TextMessage)msg2).getText()); 89 90 91 subscriberSession.commit(); 93 } catch (Exception e) { 94 fail(e); 95 } 96 } 97 98 102 public void testDurableSubscriber() { 103 try { 104 subscriber = subscriberSession.createDurableSubscriber(subscriberTopic,"testTopic"); 105 subscriberConnection.close(); 106 subscriberConnection = null; 107 108 TextMessage message = publisherSession.createTextMessage(); 109 message.setText("test"); 110 publisher.publish(message); 111 112 subscriberConnection = subscriberTCF.createTopicConnection(); 113 if (subscriberConnection.getClientID() == null) { 114 subscriberConnection.setClientID("subscriberConnection"); 115 } 116 subscriberSession = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 117 subscriber = subscriberSession.createDurableSubscriber(subscriberTopic, "testTopic"); 118 subscriberConnection.start(); 119 120 TextMessage m = (TextMessage)subscriber.receiveNoWait(); 121 assertTrue(m != null); 122 assertEquals("test", m.getText()); 123 } catch (JMSException e) { 124 fail(e); 125 } 126 finally 127 { 128 try 129 { 130 subscriberSession.unsubscribe("testTopic"); 131 } 132 catch (JMSException e) 133 { 134 fail(e); 135 } 136 } 137 } 138 139 142 public void testUnsubscribe() { 143 try { 144 subscriber = subscriberSession.createDurableSubscriber(subscriberTopic,"topic"); 145 subscriber.close(); 146 subscriberSession.unsubscribe("topic"); 148 } catch (JMSException e) { 149 fail(e); 150 } 151 } 152 153 157 public void testCreateDurableSubscriber_2() { 158 try { 159 subscriberSession.createDurableSubscriber(subscriberTopic, 160 "topic", 161 "definitely not a message selector!", 162 true); 163 fail("Should throw a javax.jms.InvalidSelectorException.\n"); 164 } catch (InvalidSelectorException e) { 165 } catch (JMSException e) { 166 fail("Should throw a javax.jms.InvalidSelectorException, not a "+ e); 167 } 168 } 169 170 174 public void testCreateDurableSubscriber_1() { 175 try { 176 subscriberSession.createDurableSubscriber((Topic)null, "topic"); 177 fail("Should throw a javax.jms.InvalidDestinationException.\n"); 178 } catch (InvalidDestinationException e) { 179 } catch (JMSException e) { 180 fail("Should throw a javax.jms.InvalidDestinationException, not a "+ e); 181 } 182 } 183 184 188 public void testCreateSubscriber_2() { 189 try { 190 subscriberSession.createSubscriber(subscriberTopic, 191 "definitely not a message selector!", 192 true); 193 fail("Should throw a javax.jms.InvalidSelectorException.\n"); 194 } catch (InvalidSelectorException e) { 195 } catch (JMSException e) { 196 fail("Should throw a javax.jms.InvalidSelectorException, not a "+ e); 197 } 198 } 199 200 204 public void testCreateSubscriber_1() { 205 try { 206 subscriberSession.createSubscriber((Topic)null); 207 fail("Should throw a javax.jms.InvalidDestinationException.\n"); 208 } catch (InvalidDestinationException e) { 209 } catch (JMSException e) { 210 fail("Should throw a javax.jms.InvalidDestinationException, not a "+ e); 211 } 212 } 213 214 217 public static Test suite() { 218 return new TestSuite(TopicSessionTest.class); 219 } 220 221 public TopicSessionTest(String name) { 222 super(name); 223 } 224 } 225 | Popular Tags |