com.mockrunner.jms.TopicTransmissionManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-jms Show documentation
Show all versions of mockrunner-jms Show documentation
Mock classes for Java Messaging System
package com.mockrunner.jms;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jms.JMSException;
import javax.jms.TopicPublisher;
import javax.jms.TopicSubscriber;
import com.mockrunner.mock.jms.MockConnection;
import com.mockrunner.mock.jms.MockSession;
import com.mockrunner.mock.jms.MockTopic;
import com.mockrunner.mock.jms.MockTopicPublisher;
import com.mockrunner.mock.jms.MockTopicSubscriber;
/**
* This class is used to create topic publishers and subscribers.
* It can be also used to access all created classes in tests.
*/
public class TopicTransmissionManager implements Serializable
{
private MockConnection connection;
private MockSession session;
private List topicPublisherList;
private List topicSubscriberList;
private Map topicDurableSubscriberMap;
public TopicTransmissionManager(MockConnection connection, MockSession session)
{
this.connection = connection;
this.session = session;
topicPublisherList = new ArrayList();
topicSubscriberList = new ArrayList();
topicDurableSubscriberMap = new HashMap();
}
/**
* Closes all senders, receivers, browsers, publishers and subscribers.
*/
public void closeAll()
{
closeAllTopicPublishers();
closeAllTopicSubscribers();
closeAllTopicDurableSubscribers();
}
/**
* Closes all topic publishers.
*/
public void closeAllTopicPublishers()
{
for (Object aTopicPublisherList : topicPublisherList) {
TopicPublisher publisher = (TopicPublisher) aTopicPublisherList;
try {
publisher.close();
} catch (JMSException ignored) {
}
}
}
/**
* Closes all topic subscribers.
*/
public void closeAllTopicSubscribers()
{
for (Object aTopicSubscriberList : topicSubscriberList) {
TopicSubscriber subscriber = (TopicSubscriber) aTopicSubscriberList;
try {
subscriber.close();
} catch (JMSException ignored) {
}
}
}
/**
* Closes all durable topic subscribers.
*/
public void closeAllTopicDurableSubscribers()
{
for (Object o : topicDurableSubscriberMap.keySet()) {
TopicSubscriber subscriber = (TopicSubscriber) topicDurableSubscriberMap.get(o);
try {
subscriber.close();
} catch (JMSException ignored) {
}
}
}
/**
* Creates a new TopicPublisher
for the specified
* Topic
. Usually this method is called
* by {@link com.mockrunner.mock.jms.MockTopicSession#createPublisher}.
* @param topic the Topic
* @return the created TopicPublisher
*/
public MockTopicPublisher createTopicPublisher(MockTopic topic)
{
MockTopicPublisher publisher = new MockTopicPublisher(connection, session, topic);
topicPublisherList.add(publisher);
return publisher;
}
/**
* Returns a TopicPublisher
by its index or
* null
, if no such TopicPublisher
is
* present.
* @param index the index of the TopicPublisher
* @return the TopicPublisher
*/
public MockTopicPublisher getTopicPublisher(int index)
{
if(topicPublisherList.size() <= index || index < 0) return null;
return (MockTopicPublisher)topicPublisherList.get(index);
}
/**
* Returns a TopicPublisher
by the name of its
* corresponding Topic
. If there's more than
* one TopicPublisher
object for the specified name,
* the first one will be returned.
* @param topicName the name of the Topic
* @return the TopicPublisher
*/
public MockTopicPublisher getTopicPublisher(String topicName)
{
List publishers = getTopicPublisherList(topicName);
if(publishers.size() <= 0) return null;
return (MockTopicPublisher)publishers.get(0);
}
/**
* Returns the list of the TopicPublisher
objects
* for a specific Topic
.
* @param topicName the name of the Topic
* @return the list of TopicPublisher
objects
*/
public List getTopicPublisherList(String topicName)
{
List resultList = new ArrayList();
for (Object aTopicPublisherList : topicPublisherList) {
TopicPublisher publisher = (TopicPublisher) aTopicPublisherList;
try {
if (publisher.getTopic().getTopicName().equals(topicName)) {
resultList.add(publisher);
}
} catch (JMSException ignored) {
}
}
return Collections.unmodifiableList(resultList);
}
/**
* Returns the list of all TopicPublisher
objects.
* @return the list of TopicPublisher
objects
*/
public List getTopicPublisherList()
{
return Collections.unmodifiableList(topicPublisherList);
}
/**
* Creates a new TopicSubscriber
for the specified
* Topic
. Usually this method is called
* by {@link com.mockrunner.mock.jms.MockTopicSession#createSubscriber}.
* @param topic the Topic
* @param messageSelector the message selector
* @param noLocal the no local flag
* @return the created TopicSubscriber
*/
public MockTopicSubscriber createTopicSubscriber(MockTopic topic, String messageSelector, boolean noLocal)
{
MockTopicSubscriber subscriber = new MockTopicSubscriber(connection, session, topic, messageSelector, noLocal);
subscriber.setDurable(false);
topicSubscriberList.add(subscriber);
return subscriber;
}
/**
* Returns a TopicSubscriber
by its index or
* null
, if no such TopicSubscriber
is
* present.
* @param index the index of the TopicSubscriber
* @return the TopicSubscriber
*/
public MockTopicSubscriber getTopicSubscriber(int index)
{
if(topicSubscriberList.size() <= index || index < 0) return null;
return (MockTopicSubscriber)topicSubscriberList.get(index);
}
/**
* Returns a TopicSubscriber
by the name of its
* corresponding Topic
. If there's more than
* one TopicSubscriber
object for the specified name,
* the first one will be returned.
* @param topicName the name of the Topic
* @return the TopicSubscriber
*/
public MockTopicSubscriber getTopicSubscriber(String topicName)
{
List subscribers = getTopicSubscriberList(topicName);
if(subscribers.size() <= 0) return null;
return (MockTopicSubscriber)subscribers.get(0);
}
/**
* Returns the list of the TopicSubscriber
objects
* for a specific Topic
.
* @param topicName the name of the Topic
* @return the list of TopicSubscriber
objects
*/
public List getTopicSubscriberList(String topicName)
{
List resultList = new ArrayList();
for (Object aTopicSubscriberList : topicSubscriberList) {
TopicSubscriber subscriber = (TopicSubscriber) aTopicSubscriberList;
try {
if (subscriber.getTopic().getTopicName().equals(topicName)) {
resultList.add(subscriber);
}
} catch (JMSException ignored) {
}
}
return Collections.unmodifiableList(resultList);
}
/**
* Returns the list of all TopicSubscriber
objects.
* @return the list of TopicSubscriber
objects
*/
public List getTopicSubscriberList()
{
return Collections.unmodifiableList(topicSubscriberList);
}
/**
* Creates a new durable TopicSubscriber
for the specified
* Topic
. Usually this method is called
* by {@link com.mockrunner.mock.jms.MockTopicSession#createDurableSubscriber}.
* @param topic the Topic
* @param name the name of the subscription
* @param messageSelector the message selector
* @param noLocal the no local flag
* @return the created TopicSubscriber
*/
public MockTopicSubscriber createDurableTopicSubscriber(MockTopic topic, String name, String messageSelector, boolean noLocal)
{
MockTopicSubscriber subscriber = new MockTopicSubscriber(connection, session, topic, messageSelector, noLocal);
subscriber.setDurable(true);
subscriber.setName(name);
topicDurableSubscriberMap.put(name, subscriber);
return subscriber;
}
/**
* Returns a durable TopicSubscriber
by its name or
* null
, if no such durable TopicSubscriber
is
* present.
* @param name the name of the subscription
* @return the TopicSubscriber
*/
public MockTopicSubscriber getDurableTopicSubscriber(String name)
{
return (MockTopicSubscriber)topicDurableSubscriberMap.get(name);
}
/**
* Deletes a durable TopicSubscriber
.
* @param name the name of the subscription
*/
public void removeTopicDurableSubscriber(String name)
{
topicDurableSubscriberMap.remove(name);
}
/**
* Returns the map of all durable TopicSubscriber
objects
* for a specific Topic
.
* @param topicName the name of the Topic
* @return the map of TopicSubscriber
objects
*/
public Map getDurableTopicSubscriberMap(String topicName)
{
Map resultMap = new HashMap();
for (Object nextName : topicDurableSubscriberMap.keySet()) {
MockTopicSubscriber subscriber = (MockTopicSubscriber) topicDurableSubscriberMap.get(nextName);
try {
if (null != subscriber && subscriber.getTopic().getTopicName().equals(topicName)) {
resultMap.put(nextName, subscriber);
}
} catch (JMSException ignored) {
}
}
return resultMap;
}
/**
* Returns the map of all durable TopicSubscriber
objects.
* @return the map of TopicSubscriber
objects
*/
public Map getDurableTopicSubscriberMap()
{
return Collections.unmodifiableMap(topicDurableSubscriberMap);
}
}