KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > impl > MessageListImpl


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.systest.impl;
19
20 import org.apache.activemq.systest.AgentStopper;
21 import org.apache.activemq.systest.MessageList;
22
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.ObjectMessage JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.TextMessage JavaDoc;
29
30 import java.io.Serializable JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 import junit.framework.Assert;
37
38 /**
39  * A simple in-JVM implementation of a {@link MessageList}
40  *
41  * @version $Revision: 1.1 $
42  */

43 public class MessageListImpl extends Assert implements MessageList {
44
45     private int numberOfMessages;
46     private int charactersPerMessage;
47     private List payloads;
48     private String JavaDoc customHeader = "testHeader";
49     private boolean useTextMessage = true;
50     private int sentCounter;
51
52     public MessageListImpl(int numberOfMessages, int charactersPerMessage) {
53         this.numberOfMessages = numberOfMessages;
54         this.charactersPerMessage = charactersPerMessage;
55     }
56
57     public void start() throws Exception JavaDoc {
58         payloads = new ArrayList JavaDoc(numberOfMessages);
59         for (int i = 0; i < numberOfMessages; i++) {
60             payloads.add(createTextPayload(i, charactersPerMessage));
61         }
62     }
63
64     public void stop() throws Exception JavaDoc {
65         payloads = null;
66     }
67
68     public void stop(AgentStopper stopper) {
69         payloads = null;
70     }
71
72     public void assertMessagesCorrect(List actual) throws JMSException JavaDoc {
73         int actualSize = actual.size();
74
75         System.out.println("Consumed so far: " + actualSize + " message(s)");
76
77         assertTrue("Not enough messages received: " + actualSize + " when expected: " + getSize(), actualSize >= getSize());
78
79         for (int i = 0; i < actualSize; i++) {
80             assertMessageCorrect(i, actual);
81         }
82
83         assertEquals("Number of messages received", getSize(), actualSize);
84     }
85
86     public void assertMessageCorrect(int index, List actualMessages) throws JMSException JavaDoc {
87         Object JavaDoc expected = getPayloads().get(index);
88
89         Message JavaDoc message = (Message JavaDoc) actualMessages.get(index);
90
91         assertHeadersCorrect(message, index);
92
93         Object JavaDoc actual = getMessagePayload(message);
94         assertEquals("Message payload for message: " + index, expected, actual);
95     }
96
97     public void sendMessages(Session JavaDoc session, MessageProducer JavaDoc producer) throws JMSException JavaDoc {
98         int counter = 0;
99         for (Iterator JavaDoc iter = getPayloads().iterator(); iter.hasNext(); counter++) {
100             Object JavaDoc element = (Object JavaDoc) iter.next();
101             if (counter == sentCounter) {
102                 sentCounter++;
103                 Message JavaDoc message = createMessage(session, element, counter);
104                 appendHeaders(message, counter);
105                 producer.send(message);
106             }
107         }
108     }
109
110     public void sendMessages(Session JavaDoc session, MessageProducer JavaDoc producer, int percent) throws JMSException JavaDoc {
111         int numberOfMessages = (getPayloads().size() * percent) / 100;
112         int counter = 0;
113         int lastMessageId = sentCounter + numberOfMessages;
114         for (Iterator JavaDoc iter = getPayloads().iterator(); iter.hasNext() && counter < lastMessageId; counter++) {
115             Object JavaDoc element = (Object JavaDoc) iter.next();
116             if (counter == sentCounter) {
117                 sentCounter++;
118                 Message JavaDoc message = createMessage(session, element, counter);
119                 appendHeaders(message, counter);
120                 producer.send(message);
121             }
122         }
123     }
124
125     public int getSize() {
126         return getPayloads().size();
127     }
128
129     // Properties
130
// -------------------------------------------------------------------------
131
public String JavaDoc getCustomHeader() {
132         return customHeader;
133     }
134
135     public void setCustomHeader(String JavaDoc customHeader) {
136         this.customHeader = customHeader;
137     }
138
139     public List getPayloads() {
140         return payloads;
141     }
142
143     // Implementation methods
144
// -------------------------------------------------------------------------
145
protected void appendHeaders(Message JavaDoc message, int counter) throws JMSException JavaDoc {
146         message.setIntProperty(getCustomHeader(), counter);
147         message.setStringProperty("cheese", "Edam");
148     }
149
150     protected void assertHeadersCorrect(Message JavaDoc message, int index) throws JMSException JavaDoc {
151         assertEquals("String property 'cheese' for message: " + index, "Edam", message.getStringProperty("cheese"));
152         assertEquals("int property '" + getCustomHeader() + "' for message: " + index, index, message.getIntProperty(getCustomHeader()));
153     }
154
155     protected Object JavaDoc getMessagePayload(Message JavaDoc message) throws JMSException JavaDoc {
156         if (message instanceof TextMessage JavaDoc) {
157             TextMessage JavaDoc textMessage = (TextMessage JavaDoc) message;
158             return textMessage.getText();
159         }
160         else if (message instanceof ObjectMessage JavaDoc) {
161             ObjectMessage JavaDoc objectMessage = (ObjectMessage JavaDoc) message;
162             return objectMessage.getObject();
163         }
164         else {
165             return null;
166         }
167     }
168
169     protected Message JavaDoc createMessage(Session JavaDoc session, Object JavaDoc element, int counter) throws JMSException JavaDoc {
170         if (element instanceof String JavaDoc && useTextMessage) {
171             return session.createTextMessage((String JavaDoc) element);
172         }
173         else if (element == null) {
174             return session.createMessage();
175         }
176         else {
177             return session.createObjectMessage((Serializable JavaDoc) element);
178         }
179     }
180
181     protected Object JavaDoc createTextPayload(int messageCounter, int charactersPerMessage) {
182         return "Body of message: " + messageCounter + " sent at: " + new Date JavaDoc();
183     }
184 }
185
Popular Tags