All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mockrunner.jms.TopicTransmissionManager Maven / Gradle / Ivy

Go to download

Mockrunner is a lightweight framework for unit testing applications in the J2EE environment. It supports servlets, filters, tag classes and Struts actions. It includes a JDBC a JMS and a JCA test framework and can be used to test EJB based applications.

The newest version!
package com.mockrunner.jms;

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
{
    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(int ii = 0; ii < topicPublisherList.size(); ii++)
        {
            TopicPublisher publisher = (TopicPublisher)topicPublisherList.get(ii);
            try
            {
                publisher.close();
            }
            catch(JMSException exc)
            {

            }
        }
    }

    /**
     * Closes all topic subscribers.
     */
    public void closeAllTopicSubscribers()
    {
        for(int ii = 0; ii < topicSubscriberList.size(); ii++)
        {
            TopicSubscriber subscriber = (TopicSubscriber)topicSubscriberList.get(ii);
            try
            {
                subscriber.close();
            }
            catch(JMSException exc)
            {

            }
        }
    }

    /**
     * Closes all durable topic subscribers.
     */
    public void closeAllTopicDurableSubscribers()
    {
        Iterator keys = topicDurableSubscriberMap.keySet().iterator();
        while(keys.hasNext())
        {
            TopicSubscriber subscriber = (TopicSubscriber)topicDurableSubscriberMap.get(keys.next());
            try
            {
                subscriber.close();
            }
            catch(JMSException exc)
            {

            }
        }
    }
    
    /**
     * 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(int ii = 0; ii < topicPublisherList.size(); ii++)
        {
            TopicPublisher publisher = (TopicPublisher)topicPublisherList.get(ii);
            try
            {
                if(publisher.getTopic().getTopicName().equals(topicName))
                {
                    resultList.add(publisher);
                }
            }
            catch(JMSException exc)
            {
        
            }
        }
        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(int ii = 0; ii < topicSubscriberList.size(); ii++)
        {
            TopicSubscriber subscriber = (TopicSubscriber)topicSubscriberList.get(ii);
            try
            {
                if(subscriber.getTopic().getTopicName().equals(topicName))
                {
                    resultList.add(subscriber);
                }
            }
            catch(JMSException exc)
            {
    
            }
        }
        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();
        Iterator subscriberNames = topicDurableSubscriberMap.keySet().iterator();
        while(subscriberNames.hasNext())
        {
            Object nextName = subscriberNames.next();
            MockTopicSubscriber subscriber = (MockTopicSubscriber)topicDurableSubscriberMap.get(nextName);
            try
            {
                if(null != subscriber && subscriber.getTopic().getTopicName().equals(topicName))
                {
                    resultMap.put(nextName, subscriber);
                }
            }
            catch(JMSException exc)
            {
                
            }
        }
        return resultMap;
    }

    /**
     * Returns the map of all durable TopicSubscriber objects.
     * @return the map of TopicSubscriber objects
     */
    public Map getDurableTopicSubscriberMap()
    {
        return Collections.unmodifiableMap(topicDurableSubscriberMap);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy