1 package com.mockobjects.jms; 2 3 import com.mockobjects.*; 4 import javax.jms.*; 5 6 public class MockMessageConsumer extends MockObject implements MessageConsumer{ 7 private final ReturnValue myMessage = new ReturnValue("message"); 8 private boolean myExpiresOnTimeout = false; 9 private JMSException myException; 10 protected ExpectationCounter myCloseCalls = 11 new ExpectationCounter("MockMessageConsumer.close"); 12 protected ExpectationCounter myReceiveCalls = 13 new ExpectationCounter("MockMessageConsumer.receive"); 14 private ExpectationValue messageListener = 15 new ExpectationValue("messageListener"); 16 private final ExpectationValue timeout = new ExpectationValue("timeout"); 17 18 public void setExpectedMessageListener(MessageListener messageListener){ 19 this.messageListener.setExpected(messageListener); 20 } 21 22 public void close() throws JMSException { 23 throwExceptionIfAny(); 24 myCloseCalls.inc(); 25 } 26 27 public MessageListener getMessageListener() throws JMSException { 28 notImplemented(); 29 return null; 30 } 31 32 public String getMessageSelector() throws JMSException { 33 notImplemented(); 34 return null; 35 } 36 37 public Message receive() throws JMSException { 38 throwExceptionIfAny(); 39 myReceiveCalls.inc(); 40 if (myExpiresOnTimeout) { 41 synchronized(this) { 42 try { 43 wait(); 44 } catch (InterruptedException e) { 45 throw new junit.framework.AssertionFailedError( 46 "Thread interrupted"); 47 } 48 } 49 } 50 return (Message)myMessage.getValue(); 51 } 52 53 public Message receive(long timeout) throws JMSException { 54 this.timeout.setActual(timeout); 55 throwExceptionIfAny(); 56 myReceiveCalls.inc(); 57 if (myExpiresOnTimeout) { 58 return null; 59 } else { 60 return (Message)myMessage.getValue(); 61 } 62 } 63 64 public void setExpectedTimeout(long timeout){ 65 this.timeout.setExpected(timeout); 66 } 67 68 public Message receiveNoWait() throws JMSException { 69 throwExceptionIfAny(); 70 myReceiveCalls.inc(); 71 return (Message)myMessage.getValue(); 72 } 73 74 public void setExpectedCloseCalls(int callCount) { 75 myCloseCalls.setExpected(callCount); 76 } 77 78 public void setExpectedReceiveCalls(int callCount) { 79 myReceiveCalls.setExpected(callCount); 80 } 81 82 public void setupReceivedMessage(Message message) { 83 myMessage.setValue(message); 84 } 85 86 public void setupExpiresOnTimeout(boolean expiresOnTimeout) { 87 myExpiresOnTimeout = expiresOnTimeout; 88 } 89 90 public void setupThrowException(JMSException e) { 91 myException = e; 92 } 93 94 public void setMessageListener(MessageListener messageListener) 95 throws JMSException{ 96 this.messageListener.setActual(messageListener); 97 } 98 99 protected void throwExceptionIfAny() throws JMSException { 100 if (null != myException) { 101 throw myException; 102 } 103 } 104 } 105 | Popular Tags |