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}. Deletes the exception.
105         */
106        public void throwJMSException() throws JMSException
107        {
108            if(null == exception) return;
109            JMSException tempException = exception;
110            exception = null;
111            throw tempException;
112        }
113        
114        /**
115         * Calls the <code>ExceptionListener</code>
116         * if an exception is set {@link #setJMSException}.
117         * Deletes the exception after calling the <code>ExceptionListener</code>.
118         */
119        public void callExceptionListener()
120        {
121            JMSException tempException = exception;
122            exception = null;
123            callExceptionListener(tempException);
124        }
125        
126        /**
127         * Calls the <code>ExceptionListener</code>
128         * using the specified exception.
129         * @param exception the exception
130         */
131        public void callExceptionListener(JMSException exception)
132        {
133            if(listener != null && exception != null)
134            {
135                listener.onException(exception);
136            }
137        }
138        
139        /**
140         * You can use this to set the <code>ConnectionMetaData</code>.
141         * Usually this should not be necessary. Per default an instance
142         * of {@link MockConnectionMetaData} is returned when calling
143         * {@link #getMetaData}.
144         * @param metaData the meta data
145         */
146        public void setMetaData(ConnectionMetaData metaData)
147        {
148            this.metaData = metaData;
149        }
150        
151        public Session createSession(boolean transacted, int acknowledgeMode) throws JMSException
152        {
153            throwJMSException();
154            MockSession session = new MockSession(this, transacted, acknowledgeMode);
155            sessions().add(session);
156            return session;
157        }
158        
159        public ConnectionConsumer createConnectionConsumer(Destination destination, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException
160        {
161            throwJMSException();
162            return new MockConnectionConsumer(this, sessionPool);
163        }
164    
165        public ConnectionConsumer createDurableConnectionConsumer(Topic topic, String subscriptionName, String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException
166        {
167            return createConnectionConsumer(topic, messageSelector, sessionPool, maxMessages);
168        }
169        
170        public ConnectionMetaData getMetaData() throws JMSException
171        {
172            throwJMSException();
173            return metaData;
174        }
175            
176        public String getClientID() throws JMSException
177        {
178            throwJMSException();
179            return clientId;
180        }
181    
182        public void setClientID(String clientId) throws JMSException
183        {
184            throwJMSException();
185            this.clientId = clientId;
186        }  
187    
188        public ExceptionListener getExceptionListener() throws JMSException
189        {
190            throwJMSException();
191            return listener;
192        }
193    
194        public void setExceptionListener(ExceptionListener listener) throws JMSException
195        {
196            throwJMSException();
197            this.listener = listener;
198        }
199    
200        public void start() throws JMSException
201        {
202            throwJMSException();
203            started = true;
204        }
205    
206        public void stop() throws JMSException
207        {
208            throwJMSException();
209            started = false;
210        }
211    
212        public void close() throws JMSException
213        {
214            throwJMSException();
215            for(int ii = 0; ii < sessions.size(); ii++)
216            {
217                Session session = (Session)sessions.get(ii);
218                session.close();
219            }
220            closed = true;
221        }
222        
223        public boolean isStarted()
224        {
225            return started;
226        }
227        
228        public boolean isStopped()
229        {
230            return !isStarted();
231        }
232        
233        public boolean isClosed()
234        {
235            return closed;
236        }
237        
238        protected List sessions()
239        {
240            return sessions;
241        }
242    }