TestCase5PubSubMessageSize.java
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 5, pub/sub with message size. Sends/received a specified number of messages to a specified route
033  * on the default topic exchange, using the specified number of receivers connections, and the specified message size.
034  * Produces reports on the actual number of 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 TestCase5PubSubMessageSize implements TestClientControlledTest, MessageListener
046 {
047     /** Used for debugging. */
048     private static final Logger log = Logger.getLogger(TestCase5PubSubMessageSize.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 size of the test messages to send. */
060     private int messageSize;
061 
062     /** The connections to send/receive the test messages on. */
063     private Connection[] connection;
064 
065     /** The sessions to send/receive the test messages on. */
066     private Session[] session;
067 
068     /** The producer to send the test messages with. */
069     MessageProducer producer;
070 
071     /**
072      * Should provide the name of the test case that this class implements. The exact names are defined in the
073      * interop testing spec.
074      *
075      @return The name of the test case that this implements.
076      */
077     public String getName()
078     {
079         log.debug("public String getName(): called");
080 
081         return "TC5_PubSubMessageSize";
082     }
083 
084     /**
085      * Determines whether the test invite that matched this test case is acceptable.
086      *
087      @param inviteMessage The invitation to accept or reject.
088      *
089      @return <tt>true</tt> to accept the invitation, <tt>false</tt> to reject it.
090      *
091      @throws javax.jms.JMSException Any JMSException resulting from reading the message are allowed to fall through.
092      */
093     public boolean acceptInvite(Message inviteMessagethrows JMSException
094     {
095         log.debug("public boolean acceptInvite(Message inviteMessage = " + inviteMessage + "): called");
096 
097         // All invites are acceptable.
098         return true;
099     }
100 
101     /**
102      * Assigns the role to be played by this test case. The test parameters are fully specified in the
103      * assignment message. When this method return the test case will be ready to execute.
104      *
105      @param role              The role to be played; sender or receivers.
106      *
107      @param assignRoleMessage The role assingment message, contains the full test parameters.
108      *
109      @throws javax.jms.JMSException Any JMSException resulting from reading the message are allowed to fall through.
110      */
111     public void assignRole(Roles role, Message assignRoleMessagethrows JMSException
112     {
113         log.debug("public void assignRole(Roles role = " + role + ", Message assignRoleMessage = " + assignRoleMessage
114             "): called");
115 
116         // Reset the message count for a new test.
117         messageCount = 0;
118 
119         // Take note of the role to be played.
120         this.role = role;
121 
122         // Extract and retain the test parameters.
123         numMessages = assignRoleMessage.getIntProperty("PUBSUB_NUM_MESSAGES");
124         messageSize = assignRoleMessage.getIntProperty("messageSize");
125         int numReceivers = assignRoleMessage.getIntProperty("PUBSUB_NUM_RECEIVERS");
126         String sendKey = assignRoleMessage.getStringProperty("PUBSUB_KEY");
127 
128         log.debug("numMessages = " + numMessages);
129         log.debug("numReceivers = " + numReceivers);
130         log.debug("sendKey = " + sendKey);
131         log.debug("role = " + role);
132 
133         switch (role)
134         {
135         // Check if the sender role is being assigned, and set up a single message producer if so.
136         case SENDER:
137             // Create a new connection to pass the test messages on.
138             connection = new Connection[1];
139             session = new Session[1];
140 
141             connection[0= TestUtils.createConnection(TestClient.testContextProperties);
142             session[0= connection[0].createSession(false, Session.AUTO_ACKNOWLEDGE);
143 
144             // Extract and retain the test parameters.
145             Destination sendDestination = session[0].createTopic(sendKey);
146 
147             producer = session[0].createProducer(sendDestination);
148             break;
149 
150         // Otherwise the receivers role is being assigned, so set this up to listen for messages on the required number
151         // of receivers connections.
152         case RECEIVER:
153             // Create the required number of receivers connections.
154             connection = new Connection[numReceivers];
155             session = new Session[numReceivers];
156 
157             for (int i = 0; i < numReceivers; i++)
158             {
159                 connection[i= TestUtils.createConnection(TestClient.testContextProperties);
160                 session[i= connection[i].createSession(false, Session.AUTO_ACKNOWLEDGE);
161 
162                 sendDestination = session[i].createTopic(sendKey);
163 
164                 MessageConsumer consumer = session[i].createConsumer(sendDestination);
165                 consumer.setMessageListener(this);
166             }
167 
168             break;
169         }
170 
171         // Start all the connection dispatcher threads running.
172         for (Connection conn : connection)
173         {
174             conn.start();
175         }
176     }
177 
178     /**
179      * Performs the test case actions. Returning from here, indicates that the sending role has completed its test.
180      *
181      @param numMessages The number of test messages to send.
182      *
183      @throws JMSException Any JMSException resulting from reading the message are allowed to fall through.
184      */
185     public void start(int numMessagesthrows JMSException
186     {
187         log.debug("public void start(): called");
188 
189         // Check that the sender role is being performed.
190         if (role.equals(Roles.SENDER))
191         {
192             Message testMessage = TestUtils.createTestMessageOfSize(session[0], messageSize);
193 
194             for (int i = 0; i < this.numMessages; i++)
195             {
196                 producer.send(testMessage);
197 
198                 // Increment the message count.
199                 messageCount++;
200             }
201         }
202     }
203 
204     /**
205      * Gets a report on the actions performed by the test case in its assigned role.
206      *
207      @param session The controlSession to create the report message in.
208      *
209      @return The report message.
210      *
211      @throws javax.jms.JMSException Any JMSExceptions resulting from creating the report are allowed to fall through.
212      */
213     public Message getReport(Session sessionthrows JMSException
214     {
215         log.debug("public Message getReport(Session controlSession): called");
216 
217         // Close the test connections.
218         for (Connection conn : connection)
219         {
220             conn.close();
221         }
222 
223         // Generate a report message containing the count of the number of messages passed.
224         Message report = session.createMessage();
225         report.setStringProperty("CONTROL_TYPE""REPORT");
226         report.setIntProperty("MESSAGE_COUNT", messageCount);
227 
228         return report;
229     }
230 
231     /**
232      * Counts incoming test messages.
233      *
234      @param message The incoming test message.
235      */
236     public void onMessage(Message message)
237     {
238         log.debug("public void onMessage(Message message = " + message + "): called");
239 
240         // Increment the message count.
241         messageCount++;
242     }
243 }