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