1 22 package org.jboss.test.jbossmq.test; 23 24 import javax.jms.Connection ; 25 import javax.jms.JMSException ; 26 import javax.jms.Message ; 27 import javax.jms.MessageConsumer ; 28 import javax.jms.MessageProducer ; 29 import javax.jms.Session ; 30 31 import junit.framework.Test; 32 33 import org.jboss.mq.SpyDestination; 34 import org.jboss.test.jbossmq.JBossMQMicrocontainerTest; 35 36 42 public class ReceiveAfterErrorUnitTestCase extends JBossMQMicrocontainerTest 43 { 44 public ReceiveAfterErrorUnitTestCase(String name) throws Exception 45 { 46 super(name); 47 } 48 49 public static Test suite() 50 { 51 return suite(ReceiveAfterErrorUnitTestCase.class); 52 } 53 54 public interface ReceiveOperation 55 { 56 Message receive(MessageConsumer consumer) throws JMSException ; 57 } 58 59 public void testReceiveAfterError() throws Exception 60 { 61 receiveAfterError(new ReceiveOperation() 62 { 63 public Message receive(MessageConsumer consumer) throws JMSException 64 { 65 return consumer.receive(); 66 } 67 }); 68 } 69 70 public void testReceiveWithWaitAfterError() throws Exception 71 { 72 receiveAfterError(new ReceiveOperation() 73 { 74 public Message receive(MessageConsumer consumer) throws JMSException 75 { 76 return consumer.receive(1000); 77 } 78 }); 79 } 80 81 public void testReceiveNoWaitAfterError() throws Exception 82 { 83 receiveAfterError(new ReceiveOperation() 84 { 85 public Message receive(MessageConsumer consumer) throws JMSException 86 { 87 return consumer.receiveNoWait(); 88 } 89 }); 90 } 91 92 protected void receiveAfterError(ReceiveOperation operation) throws Exception 93 { 94 SpyDestination destination = createQueue("testQueue"); 95 try 96 { 97 Connection connection = createConnection(); 98 try 99 { 100 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 101 connection.start(); 102 103 MessageProducer producer = session.createProducer(destination); 104 Message message = session.createMessage(); 105 producer.send(message); 106 107 MessageConsumer consumer = session.createConsumer(destination); 108 109 raiseReceiveError(true); 111 try 112 { 113 operation.receive(consumer); 114 fail("Should not be here!"); 115 } 116 catch (Throwable t) 117 { 118 checkThrowable(JMSException .class, t); 119 } 120 raiseReceiveError(false); 121 122 message = operation.receive(consumer); 123 assertNotNull(message); 124 } 125 finally 126 { 127 connection.close(); 128 } 129 } 130 finally 131 { 132 removeDestination(destination); 133 } 134 } 135 } 136 137 | Popular Tags |