001 package com.mockrunner.mock.jms;
002
003 import java.util.ArrayList;
004 import java.util.Collections;
005 import java.util.List;
006
007 import javax.jms.Connection;
008 import javax.jms.ConnectionConsumer;
009 import javax.jms.ConnectionMetaData;
010 import javax.jms.Destination;
011 import javax.jms.ExceptionListener;
012 import javax.jms.JMSException;
013 import javax.jms.ServerSessionPool;
014 import javax.jms.Session;
015 import javax.jms.Topic;
016
017 import com.mockrunner.jms.ConfigurationManager;
018 import com.mockrunner.jms.DestinationManager;
019
020 /**
021 * Mock implementation of JMS <code>Connection</code>.
022 * Please note: The interfaces <code>ConnectionConsumer</code>,
023 * <code>ServerSessionPool</code> and <code>ServerSession</code>
024 * are not meant for application use. Mockrunner provides very
025 * simple mock implementations but usually you won't need them.
026 */
027 public class MockConnection implements Connection
028 {
029 private ConnectionMetaData metaData;
030 private List sessions;
031 private String clientId;
032 private boolean started;
033 private boolean closed;
034 private ExceptionListener listener;
035 private JMSException exception;
036 private DestinationManager destinationManager;
037 private ConfigurationManager configurationManager;
038
039 public MockConnection(DestinationManager destinationManager, ConfigurationManager configurationManager)
040 {
041 metaData = new MockConnectionMetaData();
042 started = false;
043 closed = false;
044 exception = null;
045 this.destinationManager = destinationManager;
046 this.configurationManager = configurationManager;
047 sessions = new ArrayList();
048 }
049
050 /**
051 * Returns the {@link com.mockrunner.jms.DestinationManager}.
052 * @return the {@link com.mockrunner.jms.DestinationManager}
053 */
054 public DestinationManager getDestinationManager()
055 {
056 return destinationManager;
057 }
058
059 /**
060 * Returns the {@link com.mockrunner.jms.ConfigurationManager}.
061 * @return the {@link com.mockrunner.jms.ConfigurationManager}
062 */
063 public ConfigurationManager getConfigurationManager()
064 {
065 return configurationManager;
066 }
067
068 /**
069 * Returns the list of {@link MockSession} objects.
070 * @return the list
071 */
072 public List getSessionList()
073 {
074 return Collections.unmodifiableList(sessions);
075 }
076
077 /**
078 * Returns a {@link MockSession}. If there's no such
079 * {@link MockSession}, <code>null</code> is returned.
080 * @param index the index of the session object
081 * @return the session object
082 */
083 public MockSession getSession(int index)
084 {
085 if(sessions.size() <= index || index < 0) return null;
086 return (MockSession)sessions.get(index);
087 }
088
089 /**
090 * Set an exception that will be thrown when calling one
091 * of the interface methods. Since the mock implementation
092 * cannot fail like a full blown message server you can use
093 * this method to simulate server errors. After the exception
094 * was thrown it will be deleted.
095 * @param exception the exception to throw
096 */
097 public void setJMSException(JMSException exception)
098 {
099 this.exception = exception;
100 }
101
102 /**
103 * Throws a <code>JMSException</code> if one is set with
104 * {@link #setJMSException}. Informs the <code>ExceptionListener</code>
105 * and deletes the exception after throwing it.
106 */
107 public void throwJMSException() throws JMSException
108 {
109 if(null == exception) return;
110 JMSException tempException = exception;
111 exception = null;
112 if(listener != null)
113 {
114 listener.onException(tempException);
115 }
116 throw tempException;
117 }
118
119 /**
120 * You can use this to set the <code>ConnectionMetaData</code>.
121 * Usually this should not be necessary. Per default an instance
122 * of {@link MockConnectionMetaData} is returned when calling
123 * {@link #getMetaData}.
124 * @param metaData the meta data
125 */
126 public void setMetaData(ConnectionMetaData metaData)
127 {
128 this.metaData = metaData;
129 }
130
131 public Session createSession(boolean transacted, int acknowledgeMode) throws JMSException
132 {
133 throwJMSException();
134 MockSession session = new MockSession(this, transacted, acknowledgeMode);
135 sessions().add(session);
136 return session;
137 }
138
139 public ConnectionConsumer createConnectionConsumer(Destination destination, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException
140 {
141 throwJMSException();
142 return new MockConnectionConsumer(this, sessionPool);
143 }
144
145 public ConnectionConsumer createDurableConnectionConsumer(Topic topic, String subscriptionName, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException
146 {
147 return createConnectionConsumer(topic, messageSelector, sessionPool, maxMessages);
148 }
149
150 public ConnectionMetaData getMetaData() throws JMSException
151 {
152 throwJMSException();
153 return metaData;
154 }
155
156 public String getClientID() throws JMSException
157 {
158 throwJMSException();
159 return clientId;
160 }
161
162 public void setClientID(String clientId) throws JMSException
163 {
164 throwJMSException();
165 this.clientId = clientId;
166 }
167
168 public ExceptionListener getExceptionListener() throws JMSException
169 {
170 throwJMSException();
171 return listener;
172 }
173
174 public void setExceptionListener(ExceptionListener listener) throws JMSException
175 {
176 throwJMSException();
177 this.listener = listener;
178 }
179
180 public void start() throws JMSException
181 {
182 throwJMSException();
183 started = true;
184 }
185
186 public void stop() throws JMSException
187 {
188 throwJMSException();
189 started = false;
190 }
191
192 public void close() throws JMSException
193 {
194 throwJMSException();
195 for(int ii = 0; ii < sessions.size(); ii++)
196 {
197 Session session = (Session)sessions.get(ii);
198 session.close();
199 }
200 closed = true;
201 }
202
203 public boolean isStarted()
204 {
205 return started;
206 }
207
208 public boolean isStopped()
209 {
210 return !isStarted();
211 }
212
213 public boolean isClosed()
214 {
215 return closed;
216 }
217
218 protected List sessions()
219 {
220 return sessions;
221 }
222 }