001 package com.mockrunner.mock.jms;
002
003 import java.util.ArrayList;
004 import java.util.List;
005
006 import javax.jms.Connection;
007 import javax.jms.JMSException;
008 import javax.jms.QueueConnection;
009 import javax.jms.QueueConnectionFactory;
010 import javax.jms.TopicConnection;
011 import javax.jms.TopicConnectionFactory;
012
013 import com.mockrunner.jms.ConfigurationManager;
014 import com.mockrunner.jms.DestinationManager;
015
016 /**
017 * Mock implementation of JMS <code>ConnectionFactory</code>.
018 * Can be used as generic factory for JMS 1.1.
019 * Also implements <code>QueueConnectionFactory</code> and
020 * <code>TopicConnectionFactory</code> and can be used to
021 * create queue and topic connections as well as generic
022 * JMS 1.1 connections. It is recommended to use
023 * {@link com.mockrunner.mock.jms.MockQueueConnectionFactory}
024 * if you only use queues and
025 * {@link com.mockrunner.mock.jms.MockTopicConnectionFactory}
026 * if you only use topics.
027 * This implementation is primary for generic JMS 1.1 connections
028 * but can also be used, if a server provides one implementation
029 * for both domains (which is not portable).
030 */
031 public class MockConnectionFactory implements QueueConnectionFactory, TopicConnectionFactory
032 {
033 private DestinationManager destinationManager;
034 private ConfigurationManager configurationManager;
035 private List connections;
036 private JMSException exception;
037
038 public MockConnectionFactory(DestinationManager destinationManager, ConfigurationManager configurationManager)
039 {
040 connections = new ArrayList();
041 this.destinationManager = destinationManager;
042 this.configurationManager = configurationManager;
043 exception = null;
044 }
045
046 public Connection createConnection() throws JMSException
047 {
048 MockConnection connection = new MockConnection(destinationManager, configurationManager);
049 connection.setJMSException(exception);
050 connections.add(connection);
051 return connection;
052 }
053
054 public Connection createConnection(String name, String password) throws JMSException
055 {
056 return createConnection();
057 }
058
059 public QueueConnection createQueueConnection() throws JMSException
060 {
061 MockQueueConnection connection = new MockQueueConnection(destinationManager(), configurationManager());
062 connection.setJMSException(exception());
063 connections().add(connection);
064 return connection;
065 }
066
067 public QueueConnection createQueueConnection(String name, String password) throws JMSException
068 {
069 return createQueueConnection();
070 }
071
072 public TopicConnection createTopicConnection() throws JMSException
073 {
074 MockTopicConnection connection = new MockTopicConnection(destinationManager(), configurationManager());
075 connection.setJMSException(exception());
076 connections().add(connection);
077 return connection;
078 }
079
080 public TopicConnection createTopicConnection(String name, String password) throws JMSException
081 {
082 return createTopicConnection();
083 }
084
085 /**
086 * Set an exception that will be passed to all
087 * created connections. This can be used to
088 * simulate server errors. Check out
089 * {@link MockConnection#setJMSException}
090 * for details.
091 * @param exception the exception
092 */
093 public void setJMSException(JMSException exception)
094 {
095 this.exception = exception;
096 }
097
098 /**
099 * Clears the list of connections
100 */
101 public void clearConnections()
102 {
103 connections.clear();
104 }
105
106 /**
107 * Returns the connection with the specified index
108 * or <code>null</code> if no such connection
109 * exists.
110 * @param index the index
111 * @return the connection
112 */
113 public MockConnection getConnection(int index)
114 {
115 if(connections.size() <= index) return null;
116 return (MockConnection)connections.get(index);
117 }
118
119 /**
120 * Returns the latest created connection
121 * or <code>null</code> if no such connection
122 * exists.
123 * @return the connection
124 */
125 public MockConnection getLatestConnection()
126 {
127 if(connections.size() == 0) return null;
128 return (MockConnection)connections.get(connections.size() - 1);
129 }
130
131 protected DestinationManager destinationManager()
132 {
133 return destinationManager;
134 }
135
136 protected ConfigurationManager configurationManager()
137 {
138 return configurationManager;
139 }
140
141 protected List connections()
142 {
143 return connections;
144 }
145
146 protected JMSException exception()
147 {
148 return exception;
149 }
150 }