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,
032: TopicConnectionFactory {
033: private DestinationManager destinationManager;
034: private ConfigurationManager configurationManager;
035: private List connections;
036: private JMSException exception;
037:
038: public MockConnectionFactory(DestinationManager destinationManager,
039: ConfigurationManager configurationManager) {
040: connections = new ArrayList();
041: this .destinationManager = destinationManager;
042: this .configurationManager = configurationManager;
043: exception = null;
044: }
045:
046: public Connection createConnection() throws JMSException {
047: MockConnection connection = new MockConnection(
048: destinationManager, configurationManager);
049: connection.setJMSException(exception);
050: connections.add(connection);
051: return connection;
052: }
053:
054: public Connection createConnection(String name, String password)
055: throws JMSException {
056: return createConnection();
057: }
058:
059: public QueueConnection createQueueConnection() throws JMSException {
060: MockQueueConnection connection = new MockQueueConnection(
061: destinationManager(), configurationManager());
062: connection.setJMSException(exception());
063: connections().add(connection);
064: return connection;
065: }
066:
067: public QueueConnection createQueueConnection(String name,
068: String password) throws JMSException {
069: return createQueueConnection();
070: }
071:
072: public TopicConnection createTopicConnection() throws JMSException {
073: MockTopicConnection connection = new MockTopicConnection(
074: destinationManager(), configurationManager());
075: connection.setJMSException(exception());
076: connections().add(connection);
077: return connection;
078: }
079:
080: public TopicConnection createTopicConnection(String name,
081: String password) throws JMSException {
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: this .exception = exception;
095: }
096:
097: /**
098: * Clears the list of connections
099: */
100: public void clearConnections() {
101: connections.clear();
102: }
103:
104: /**
105: * Returns the connection with the specified index
106: * or <code>null</code> if no such connection
107: * exists.
108: * @param index the index
109: * @return the connection
110: */
111: public MockConnection getConnection(int index) {
112: if (connections.size() <= index)
113: return null;
114: return (MockConnection) connections.get(index);
115: }
116:
117: /**
118: * Returns the latest created connection
119: * or <code>null</code> if no such connection
120: * exists.
121: * @return the connection
122: */
123: public MockConnection getLatestConnection() {
124: if (connections.size() == 0)
125: return null;
126: return (MockConnection) connections.get(connections.size() - 1);
127: }
128:
129: protected DestinationManager destinationManager() {
130: return destinationManager;
131: }
132:
133: protected ConfigurationManager configurationManager() {
134: return configurationManager;
135: }
136:
137: protected List connections() {
138: return connections;
139: }
140:
141: protected JMSException exception() {
142: return exception;
143: }
144: }
|