001: package com.mockrunner.jms;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.List;
006:
007: import com.mockrunner.mock.jms.MockBytesMessage;
008: import com.mockrunner.mock.jms.MockMapMessage;
009: import com.mockrunner.mock.jms.MockMessage;
010: import com.mockrunner.mock.jms.MockObjectMessage;
011: import com.mockrunner.mock.jms.MockStreamMessage;
012: import com.mockrunner.mock.jms.MockTextMessage;
013:
014: /**
015: * Can be used to create and access all type of messages.
016: * The create methods are usually called by
017: * {@link com.mockrunner.mock.jms.MockSession}.
018: */
019: public class MessageManager {
020: private List messages;
021: private List byteMessages;
022: private List mapMessages;
023: private List textMessages;
024: private List streamMessages;
025: private List objectMessages;
026:
027: public MessageManager() {
028: messages = new ArrayList();
029: byteMessages = new ArrayList();
030: mapMessages = new ArrayList();
031: textMessages = new ArrayList();
032: streamMessages = new ArrayList();
033: objectMessages = new ArrayList();
034: }
035:
036: /**
037: * Creates a new <code>Message</code>. Usually this method is called
038: * by {@link com.mockrunner.mock.jms.MockSession#createMessage}.
039: * @return the created <code>Message</code>
040: */
041: public MockMessage createMessage() {
042: MockMessage message = new MockMessage();
043: messages.add(message);
044: return message;
045: }
046:
047: /**
048: * Returns a <code>Message</code> by its index or
049: * <code>null</code>, if no such <code>Message</code> is
050: * present.
051: * @param index the index of the <code>Message</code>
052: * @return the <code>Message</code>
053: */
054: public MockMessage getMessage(int index) {
055: if (messages.size() <= index || index < 0)
056: return null;
057: return (MockMessage) messages.get(index);
058: }
059:
060: /**
061: * Returns the list of <code>Message</code> objects.
062: * @return the <code>Message</code> list
063: */
064: public List getMessageList() {
065: return Collections.unmodifiableList(messages);
066: }
067:
068: /**
069: * Creates a new <code>BytesMessage</code>. Usually this method is called
070: * by {@link com.mockrunner.mock.jms.MockSession#createBytesMessage}.
071: * @return the created <code>BytesMessage</code>
072: */
073: public MockBytesMessage createBytesMessage() {
074: MockBytesMessage message = new MockBytesMessage();
075: byteMessages.add(message);
076: return message;
077: }
078:
079: /**
080: * Returns a <code>BytesMessage</code> by its index or
081: * <code>null</code>, if no such <code>BytesMessage</code> is
082: * present.
083: * @param index the index of the <code>BytesMessage</code>
084: * @return the <code>BytesMessage</code>
085: */
086: public MockBytesMessage getBytesMessage(int index) {
087: if (byteMessages.size() <= index || index < 0)
088: return null;
089: return (MockBytesMessage) byteMessages.get(index);
090: }
091:
092: /**
093: * Returns the list of <code>BytesMessage</code> objects.
094: * @return the <code>BytesMessage</code> list
095: */
096: public List getBytesMessageList() {
097: return Collections.unmodifiableList(byteMessages);
098: }
099:
100: /**
101: * Creates a new <code>MapMessage</code>. Usually this method is called
102: * by {@link com.mockrunner.mock.jms.MockSession#createMapMessage}.
103: * @return the created <code>MapMessage</code>
104: */
105: public MockMapMessage createMapMessage() {
106: MockMapMessage message = new MockMapMessage();
107: mapMessages.add(message);
108: return message;
109: }
110:
111: /**
112: * Returns a <code>MapMessage</code> by its index or
113: * <code>null</code>, if no such <code>MapMessage</code> is
114: * present.
115: * @param index the index of the <code>MapMessage</code>
116: * @return the <code>MapMessage</code>
117: */
118: public MockMapMessage getMapMessage(int index) {
119: if (mapMessages.size() <= index || index < 0)
120: return null;
121: return (MockMapMessage) mapMessages.get(index);
122: }
123:
124: /**
125: * Returns the list of <code>MapMessage</code> objects.
126: * @return the <code>MapMessage</code> list
127: */
128: public List getMapMessageList() {
129: return Collections.unmodifiableList(mapMessages);
130: }
131:
132: /**
133: * Creates a new <code>TextMessage</code>. Usually this method is called
134: * by {@link com.mockrunner.mock.jms.MockSession#createTextMessage}.
135: * @return the created <code>TextMessage</code>
136: */
137: public MockTextMessage createTextMessage(String text) {
138: MockTextMessage message = new MockTextMessage(text);
139: textMessages.add(message);
140: return message;
141: }
142:
143: /**
144: * Returns a <code>TextMessage</code> by its index or
145: * <code>null</code>, if no such <code>TextMessage</code> is
146: * present.
147: * @param index the index of the <code>TextMessage</code>
148: * @return the <code>TextMessage</code>
149: */
150: public MockTextMessage getTextMessage(int index) {
151: if (textMessages.size() <= index || index < 0)
152: return null;
153: return (MockTextMessage) textMessages.get(index);
154: }
155:
156: /**
157: * Returns the list of <code>TextMessage</code> objects.
158: * @return the <code>TextMessage</code> list
159: */
160: public List getTextMessageList() {
161: return Collections.unmodifiableList(textMessages);
162: }
163:
164: /**
165: * Creates a new <code>StreamMessage</code>. Usually this method is called
166: * by {@link com.mockrunner.mock.jms.MockSession#createStreamMessage}.
167: * @return the created <code>StreamMessage</code>
168: */
169: public MockStreamMessage createStreamMessage() {
170: MockStreamMessage message = new MockStreamMessage();
171: streamMessages.add(message);
172: return message;
173: }
174:
175: /**
176: * Returns a <code>StreamMessage</code> by its index or
177: * <code>null</code>, if no such <code>StreamMessage</code> is
178: * present.
179: * @param index the index of the <code>StreamMessage</code>
180: * @return the <code>StreamMessage</code>
181: */
182: public MockStreamMessage getStreamMessage(int index) {
183: if (streamMessages.size() <= index || index < 0)
184: return null;
185: return (MockStreamMessage) streamMessages.get(index);
186: }
187:
188: /**
189: * Returns the list of <code>StreamMessage</code> objects.
190: * @return the <code>StreamMessage</code> list
191: */
192: public List getStreamMessageList() {
193: return Collections.unmodifiableList(streamMessages);
194: }
195:
196: /**
197: * Creates a new <code>ObjectMessage</code>. Usually this method is called
198: * by {@link com.mockrunner.mock.jms.MockSession#createObjectMessage}.
199: * @return the created <code>ObjectMessage</code>
200: */
201: public MockObjectMessage createObjectMessage(
202: java.io.Serializable object) {
203: MockObjectMessage message = new MockObjectMessage(object);
204: objectMessages.add(message);
205: return message;
206: }
207:
208: /**
209: * Returns a <code>ObjectMessage</code> by its index or
210: * <code>null</code>, if no such <code>ObjectMessage</code> is
211: * present.
212: * @param index the index of the <code>ObjectMessage</code>
213: * @return the <code>ObjectMessage</code>
214: */
215: public MockObjectMessage getObjectMessage(int index) {
216: if (objectMessages.size() <= index || index < 0)
217: return null;
218: return (MockObjectMessage) objectMessages.get(index);
219: }
220:
221: /**
222: * Returns the list of <code>ObjectMessage</code> objects.
223: * @return the <code>ObjectMessage</code> list
224: */
225: public List getObjectMessageList() {
226: return Collections.unmodifiableList(objectMessages);
227: }
228: }
|