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