View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jms;
5   
6   import static org.hamcrest.MatcherAssert.assertThat;
7   
8   import javax.jms.Destination;
9   import javax.jms.JMSException;
10  import javax.jms.Message;
11  import javax.jms.MessageConsumer;
12  import javax.jms.MessageListener;
13  
14  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
15  import org.apache.commons.lang.builder.ToStringStyle;
16  import org.hamcrest.core.IsNot;
17  
18  import com.github.smokestack.exception.NeedsMockDefinitionException;
19  
20  /**
21   * @author gliptak
22   *
23   */
24  public class MockMessageConsumer implements MessageConsumer {
25  
26  	protected Destination destination;
27  
28  	public enum MessageConsumerState {NEW, CLOSE};
29  
30  	protected MessageConsumerState mockState=MessageConsumerState.NEW;
31  	
32  	protected String messageSelector;
33  	
34  	protected MessageListener messageListener;
35  
36  	protected boolean NoLocal;
37  
38  	public MockMessageConsumer(Destination destination) {
39  		this.destination=destination;
40  	}
41  
42  	public MockMessageConsumer(Destination destination, String messageSelector) {
43  		this.destination=destination;
44  		this.messageSelector=messageSelector;
45  	}
46  
47  	public MockMessageConsumer(Destination destination,	String messageSelector, boolean NoLocal) {
48  		this.destination=destination;
49  		this.messageSelector=messageSelector;
50  		this.NoLocal=NoLocal;
51  	}
52  
53  	/* (non-Javadoc)
54  	 * @see javax.jms.MessageConsumer#close()
55  	 */
56  	public void close() throws JMSException {
57  		_close();
58  		mockState=MessageConsumerState.CLOSE;
59  	}
60  
61  	public void _close() throws JMSException {
62  	}
63  
64  	/* (non-Javadoc)
65  	 * @see javax.jms.MessageConsumer#getMessageListener()
66  	 */
67  	public MessageListener getMessageListener() throws JMSException {
68  		assertThat("mockState", mockState, IsNot.not(MessageConsumerState.CLOSE));
69  		_getMessageListener();
70  		return messageListener;
71  	}
72  
73  	public MessageListener _getMessageListener() throws JMSException {
74  		return null;
75  	}
76  
77  	/* (non-Javadoc)
78  	 * @see javax.jms.MessageConsumer#getMessageSelector()
79  	 */
80  	public String getMessageSelector() throws JMSException {
81  		assertThat("mockState", mockState, IsNot.not(MessageConsumerState.CLOSE));
82  		_getMessageSelector();
83  		return messageSelector;
84  	}
85  
86  	public String _getMessageSelector() throws JMSException {
87  		return null;
88  	}
89  
90  	/* (non-Javadoc)
91  	 * @see javax.jms.MessageConsumer#receive()
92  	 */
93  	public Message receive() throws JMSException {
94  		assertThat("mockState", mockState, IsNot.not(MessageConsumerState.CLOSE));
95  		return _receive();
96  	}
97  
98  	public Message _receive() throws JMSException {
99  		throw new NeedsMockDefinitionException();
100 	}
101 
102 	/* (non-Javadoc)
103 	 * @see javax.jms.MessageConsumer#receive(long)
104 	 */
105 	public Message receive(long timeout) throws JMSException {
106 		assertThat("mockState", mockState, IsNot.not(MessageConsumerState.CLOSE));
107 		return _receive(timeout);
108 	}
109 
110 	public Message _receive(long timeout) throws JMSException {
111 		throw new NeedsMockDefinitionException();
112 	}
113 
114 	/* (non-Javadoc)
115 	 * @see javax.jms.MessageConsumer#receiveNoWait()
116 	 */
117 	public Message receiveNoWait() throws JMSException {
118 		assertThat("mockState", mockState, IsNot.not(MessageConsumerState.CLOSE));
119 		return _receiveNoWait();
120 	}
121 
122 	public Message _receiveNoWait() throws JMSException {
123 		throw new NeedsMockDefinitionException();
124 	}
125 
126 	/* (non-Javadoc)
127 	 * @see javax.jms.MessageConsumer#setMessageListener(javax.jms.MessageListener)
128 	 */
129 	public void setMessageListener(MessageListener messageListener) throws JMSException {
130 		assertThat("mockState", mockState, IsNot.not(MessageConsumerState.CLOSE));
131 		_setMessageListener(messageListener);	
132 		this.messageListener=messageListener;	
133 	}
134 
135 	public void _setMessageListener(MessageListener messageListener) throws JMSException {
136 	}
137 
138 	/**
139 	 * @return the mockState
140 	 */
141 	public MessageConsumerState getMockState() {
142 		return mockState;
143 	}
144 	
145 	@Override
146 	public String toString(){
147 		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
148 	}
149 }