001: package com.mockrunner.mock.jms;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.HashSet;
006: import java.util.List;
007: import java.util.Set;
008:
009: import javax.jms.Destination;
010: import javax.jms.JMSException;
011: import javax.jms.Message;
012: import javax.jms.Session;
013:
014: import org.activemq.filter.mockrunner.Filter;
015:
016: import com.mockrunner.base.NestedApplicationException;
017:
018: /**
019: * Mock implementation of JMS <code>Destination</code>.
020: */
021: public abstract class MockDestination implements Destination {
022: private Set sessions;
023: private List currentMessages;
024: private List receivedMessages;
025:
026: public MockDestination() {
027: sessions = new HashSet();
028: currentMessages = new ArrayList();
029: receivedMessages = new ArrayList();
030: }
031:
032: /**
033: * Adds a message and delivers it to the corresponding consumers.
034: * Implemented by {@link MockQueue} and {@link MockTopic}.
035: * @param message the message
036: */
037: public abstract void addMessage(Message message)
038: throws JMSException;
039:
040: /**
041: * Adds a message to the list of current messages in this
042: * destination. The message is not delivered to registered
043: * consumers. Can be used to preload destinations with
044: * test messages.
045: * @param message the message
046: */
047: public void loadMessage(Message message) {
048: addCurrentMessage(message);
049: }
050:
051: /**
052: * Returns if this destination contains messages.
053: * @return <code>false</code> if there's at least one message,
054: * <code>true</code> otherwise
055: */
056: public boolean isEmpty() {
057: return currentMessages.size() <= 0;
058: }
059:
060: /**
061: * Clears all current messages.
062: */
063: public void clear() {
064: currentMessages.clear();
065: }
066:
067: /**
068: * Clears all current messages and resets the list of received messages.
069: */
070: public void reset() {
071: currentMessages.clear();
072: receivedMessages.clear();
073: }
074:
075: /**
076: * Returns the next message. The message will be removed from the list
077: * of current messages.
078: * If there's no message, <code>null</code> will be returned.
079: * @return the <code>Message</code>
080: */
081: public Message getMessage() {
082: if (currentMessages.size() <= 0)
083: return null;
084: return (Message) currentMessages.remove(0);
085: }
086:
087: /**
088: * Returns the next message that matches the filter.
089: * The message will be removed from the list of current messages.
090: * If there's no matching message, <code>null</code> will be returned.
091: * @param filter the message filter
092: * @return the <code>Message</code>
093: */
094: public Message getMatchingMessage(Filter filter) {
095: for (int ii = 0; ii < currentMessages.size(); ii++) {
096: Message currentMessage = (Message) currentMessages.get(ii);
097: try {
098: if (filter.matches(currentMessage)) {
099: currentMessages.remove(ii);
100: return currentMessage;
101: }
102: } catch (JMSException exc) {
103: throw new NestedApplicationException(exc);
104: }
105: }
106: return null;
107: }
108:
109: /**
110: * Returns a <code>List</code> of all current messages.
111: * @return the <code>List</code> of messages
112: */
113: public List getCurrentMessageList() {
114: return Collections.unmodifiableList(currentMessages);
115: }
116:
117: /**
118: * Returns a <code>List</code> of all received messages.
119: * @return the <code>List</code> of messages
120: */
121: public List getReceivedMessageList() {
122: return Collections.unmodifiableList(receivedMessages);
123: }
124:
125: /**
126: * Adds a <code>Session</code>.
127: * @param session the session
128: */
129: public void addSession(Session session) {
130: sessions.add(session);
131: }
132:
133: /**
134: * Removes a <code>Session</code>.
135: * @param session the session
136: */
137: public void removeSession(Session session) {
138: sessions.remove(session);
139: }
140:
141: /**
142: * Return a <code>Set</code> of all sessions.
143: * @return a <code>Set</code> of all sessions
144: */
145: public Set sessionSet() {
146: return Collections.unmodifiableSet(sessions);
147: }
148:
149: protected void addReceivedMessage(Message message) {
150: receivedMessages.add(message);
151: }
152:
153: protected void addCurrentMessage(Message message) {
154: currentMessages.add(message);
155: }
156:
157: protected void acknowledgeMessage(Message message,
158: MockSession session) throws JMSException {
159: if (session.isAutoAcknowledge()) {
160: message.acknowledge();
161: }
162: }
163: }
|