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

com.mockrunner.mock.jms.MockDestination 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.mock.jms;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.activemq.filter.mockrunner.Filter;

import com.mockrunner.base.NestedApplicationException;

/**
 * Mock implementation of JMS Destination.
 */
public abstract class MockDestination implements Destination
{
    private Set sessions;
    private List currentMessages;
    private List receivedMessages;

    public MockDestination()
    {
        sessions = new HashSet();
        currentMessages = new ArrayList();
        receivedMessages = new ArrayList();
    }
    
    /**
     * Adds a message and delivers it to the corresponding consumers. 
     * Implemented by {@link MockQueue} and {@link MockTopic}.
     * @param message the message
     */
    public abstract void addMessage(Message message) throws JMSException;
 
    /**
     * Adds a message to the list of current messages in this
     * destination. The message is not delivered to registered
     * consumers. Can be used to preload destinations with
     * test messages.
     * @param message the message
     */
    public void loadMessage(Message message)
    {
        addCurrentMessage(message);
    }
    
    /**
     * Returns if this destination contains messages.
     * @return false if there's at least one message,
     *         true otherwise
     */
    public boolean isEmpty()
    {
        return currentMessages.size() <= 0;
    }

    /**
     * Clears all current messages.
     */
    public void clear()
    {
        currentMessages.clear();
    }

    /**
     * Clears all current messages and resets the list of received messages.
     */
    public void reset()
    {
        currentMessages.clear();
        receivedMessages.clear();
    }

    /**
     * Returns the next message. The message will be removed from the list
     * of current messages. 
     * If there's no message, null will be returned.
     * @return the Message
     */
    public Message getMessage()
    {
        if(currentMessages.size() <= 0) return null;
        return (Message)currentMessages.remove(0);
    }
    
    /**
     * Returns the next message that matches the filter. 
     * The message will be removed from the list of current messages. 
     * If there's no matching message, null will be returned.
     * @param filter the message filter
     * @return the Message
     */
    public Message getMatchingMessage(Filter filter)
    {
        for(int ii = 0; ii < currentMessages.size(); ii++)
        {
            Message currentMessage = (Message)currentMessages.get(ii);
            try
            {
                if(filter.matches(currentMessage))
                {
                    currentMessages.remove(ii);
                    return currentMessage;
                }
            }
            catch(JMSException exc)
            {
                throw new NestedApplicationException(exc);
            }
        }
        return null;
    }

    /**
     * Returns a List of all current messages.
     * @return the List of messages
     */
    public List getCurrentMessageList()
    {
        return Collections.unmodifiableList(currentMessages);
    }

    /**
     * Returns a List of all received messages.
     * @return the List of messages
     */
    public List getReceivedMessageList()
    {
        return Collections.unmodifiableList(receivedMessages);
    }

    /**
     * Adds a Session.
     * @param session the session
     */
    public void addSession(Session session)
    {
        sessions.add(session);
    }
    
    /**
     * Removes a Session.
     * @param session the session
     */
    public void removeSession(Session session)
    {
        sessions.remove(session);
    }
    
    /**
     * Return a Set of all sessions.
     * @return a Set of all sessions
     */
    public Set sessionSet()
    {
        return Collections.unmodifiableSet(sessions);
    }
    
    protected void addReceivedMessage(Message message)
    {
        receivedMessages.add(message);
    }
    
    protected void addCurrentMessage(Message message)
    {
        currentMessages.add(message);
    }
    
    protected void acknowledgeMessage(Message message, MockSession session) throws JMSException
    {
        if(session.isAutoAcknowledge())
        {
            message.acknowledge();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy