001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.interop.clienttestcases;
022
023 import org.apache.log4j.Logger;
024
025 import org.apache.qpid.test.framework.TestUtils;
026 import org.apache.qpid.test.framework.distributedtesting.TestClient;
027 import org.apache.qpid.test.framework.distributedtesting.TestClientControlledTest;
028
029 import javax.jms.*;
030
031 /**
032 * Implements test case 3, basic pub/sub. Sends/received a specified number of messages to a specified route on the
033 * default topic exchange, using the specified number of receivers connections. Produces reports on the actual number of
034 * messages sent/received.
035 *
036 * <p><table id="crc"><caption>CRC Card</caption>
037 * <tr><th> Responsibilities <th> Collaborations
038 * <tr><td> Supply the name of the test case that this implements.
039 * <tr><td> Accept/Reject invites based on test parameters.
040 * <tr><td> Adapt to assigned roles.
041 * <tr><td> Send required number of test messages using pub/sub.
042 * <tr><td> Generate test reports.
043 * </table>
044 */
045 public class TestCase3BasicPubSub implements TestClientControlledTest, MessageListener
046 {
047 /** Used for debugging. */
048 private static final Logger log = Logger.getLogger(TestCase3BasicPubSub.class);
049
050 /** Holds the count of test messages received. */
051 private int messageCount;
052
053 /** The role to be played by the test. */
054 private Roles role;
055
056 /** The number of test messages to send. */
057 private int numMessages;
058
059 /** The connections to send/receive the test messages on. */
060 private Connection[] connection;
061
062 /** The sessions to send/receive the test messages on. */
063 private Session[] session;
064
065 /** The producer to send the test messages with. */
066 MessageProducer producer;
067
068 /**
069 * Should provide the name of the test case that this class implements. The exact names are defined in the
070 * interop testing spec.
071 *
072 * @return The name of the test case that this implements.
073 */
074 public String getName()
075 {
076 log.debug("public String getName(): called");
077
078 return "TC3_BasicPubSub";
079 }
080
081 /**
082 * Determines whether the test invite that matched this test case is acceptable.
083 *
084 * @param inviteMessage The invitation to accept or reject.
085 *
086 * @return <tt>true</tt> to accept the invitation, <tt>false</tt> to reject it.
087 *
088 * @throws javax.jms.JMSException Any JMSException resulting from reading the message are allowed to fall through.
089 */
090 public boolean acceptInvite(Message inviteMessage) throws JMSException
091 {
092 log.debug("public boolean acceptInvite(Message inviteMessage = " + inviteMessage + "): called");
093
094 // All invites are acceptable.
095 return true;
096 }
097
098 /**
099 * Assigns the role to be played by this test case. The test parameters are fully specified in the
100 * assignment message. When this method return the test case will be ready to execute.
101 *
102 * @param role The role to be played; sender or receivers.
103 *
104 * @param assignRoleMessage The role assingment message, contains the full test parameters.
105 *
106 * @throws JMSException Any JMSException resulting from reading the message are allowed to fall through.
107 */
108 public void assignRole(Roles role, Message assignRoleMessage) throws JMSException
109 {
110 log.debug("public void assignRole(Roles role = " + role + ", Message assignRoleMessage = " + assignRoleMessage
111 + "): called");
112
113 // Reset the message count for a new test.
114 messageCount = 0;
115
116 // Take note of the role to be played.
117 this.role = role;
118
119 // Extract and retain the test parameters.
120 numMessages = assignRoleMessage.getIntProperty("PUBSUB_NUM_MESSAGES");
121 int numReceivers = assignRoleMessage.getIntProperty("PUBSUB_NUM_RECEIVERS");
122 String sendKey = assignRoleMessage.getStringProperty("PUBSUB_KEY");
123
124 log.debug("numMessages = " + numMessages);
125 log.debug("numReceivers = " + numReceivers);
126 log.debug("sendKey = " + sendKey);
127 log.debug("role = " + role);
128
129 switch (role)
130 {
131 // Check if the sender role is being assigned, and set up a single message producer if so.
132 case SENDER:
133 // Create a new connection to pass the test messages on.
134 connection = new Connection[1];
135 session = new Session[1];
136
137 connection[0] = TestUtils.createConnection(TestClient.testContextProperties);
138 session[0] = connection[0].createSession(false, Session.AUTO_ACKNOWLEDGE);
139
140 // Extract and retain the test parameters.
141 Destination sendDestination = session[0].createTopic(sendKey);
142
143 producer = session[0].createProducer(sendDestination);
144 break;
145
146 // Otherwise the receivers role is being assigned, so set this up to listen for messages on the required number
147 // of receivers connections.
148 case RECEIVER:
149 // Create the required number of receivers connections.
150 connection = new Connection[numReceivers];
151 session = new Session[numReceivers];
152
153 for (int i = 0; i < numReceivers; i++)
154 {
155 connection[i] = TestUtils.createConnection(TestClient.testContextProperties);
156 session[i] = connection[i].createSession(false, Session.AUTO_ACKNOWLEDGE);
157
158 sendDestination = session[i].createTopic(sendKey);
159
160 MessageConsumer consumer = session[i].createConsumer(sendDestination);
161 consumer.setMessageListener(this);
162 }
163
164 break;
165 }
166
167 // Start all the connection dispatcher threads running.
168 for (Connection conn : connection)
169 {
170 conn.start();
171 }
172 }
173
174 /**
175 * Performs the test case actions. Returning from here, indicates that the sending role has completed its test.
176 *
177 * @param numMessages The number of test messages to send.
178 *
179 * @throws JMSException Any JMSException resulting from reading the message are allowed to fall through.
180 */
181 public void start(int numMessages) throws JMSException
182 {
183 log.debug("public void start(): called");
184
185 // Check that the sender role is being performed.
186 if (role.equals(Roles.SENDER))
187 {
188 Message testMessage = session[0].createTextMessage("test");
189
190 for (int i = 0; i < this.numMessages; i++)
191 {
192 producer.send(testMessage);
193
194 // Increment the message count.
195 messageCount++;
196 }
197 }
198 }
199
200 /**
201 * Gets a report on the actions performed by the test case in its assigned role.
202 *
203 * @param session The controlSession to create the report message in.
204 *
205 * @return The report message.
206 *
207 * @throws JMSException Any JMSExceptions resulting from creating the report are allowed to fall through.
208 */
209 public Message getReport(Session session) throws JMSException
210 {
211 log.debug("public Message getReport(Session controlSession): called");
212
213 // Close the test connections.
214 for (Connection conn : connection)
215 {
216 conn.close();
217 }
218
219 // Generate a report message containing the count of the number of messages passed.
220 Message report = session.createMessage();
221 report.setStringProperty("CONTROL_TYPE", "REPORT");
222 report.setIntProperty("MESSAGE_COUNT", messageCount);
223
224 return report;
225 }
226
227 /**
228 * Counts incoming test messages.
229 *
230 * @param message The incoming test message.
231 */
232 public void onMessage(Message message)
233 {
234 log.debug("public void onMessage(Message message = " + message + "): called");
235
236 // Increment the message count.
237 messageCount++;
238 }
239 }
|