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