TestCase4P2PMessageSize.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 4, P2P messages with message size. Sends/received a specified number of messages to a specified
033  * route on the default direct exchange, of a specified size. Produces reports on the actual number of messages
034  * 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.
042  <tr><td> Generate test reports.
043  </table>
044  */
045 public class TestCase4P2PMessageSize implements TestClientControlledTest, MessageListener
046 {
047     /** Used for debugging. */
048     private static final Logger log = Logger.getLogger(TestCase4P2PMessageSize.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 connection to send the test messages on. */
063     private Connection connection;
064 
065     /** The controlSession to send 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 "TC4_P2PMessageSize";
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 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 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         // Create a new connection to pass the test messages on.
123         connection = TestUtils.createConnection(TestClient.testContextProperties);
124         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
125 
126         // Extract and retain the test parameters.
127         numMessages = assignRoleMessage.getIntProperty("P2P_NUM_MESSAGES");
128         messageSize = assignRoleMessage.getIntProperty("messageSize");
129         Destination sendDestination = session.createQueue(assignRoleMessage.getStringProperty("P2P_QUEUE_AND_KEY_NAME"));
130 
131         log.debug("numMessages = " + numMessages);
132         log.debug("sendDestination = " + sendDestination);
133         log.debug("role = " + role);
134 
135         switch (role)
136         {
137         // Check if the sender role is being assigned, and set up a message producer if so.
138         case SENDER:
139             producer = session.createProducer(sendDestination);
140             break;
141 
142         // Otherwise the receivers role is being assigned, so set this up to listen for messages.
143         case RECEIVER:
144             MessageConsumer consumer = session.createConsumer(sendDestination);
145             consumer.setMessageListener(this);
146             break;
147         }
148 
149         connection.start();
150     }
151 
152     /**
153      * Performs the test case actions. Returning from here, indicates that the sending role has completed its test.
154      *
155      @param numMessages The number of test messages to send.
156      *
157      @throws JMSException Any JMSException resulting from reading the message are allowed to fall through.
158      */
159     public void start(int numMessagesthrows JMSException
160     {
161         log.debug("public void start(): called");
162 
163         // Check that the sender role is being performed.
164         if (role.equals(Roles.SENDER))
165         {
166             Message testMessage = TestUtils.createTestMessageOfSize(session, messageSize);
167 
168             for (int i = 0; i < this.numMessages; i++)
169             {
170                 producer.send(testMessage);
171 
172                 // Increment the message count.
173                 messageCount++;
174             }
175         }
176     }
177 
178     /**
179      * Gets a report on the actions performed by the test case in its assigned role.
180      *
181      @param session The controlSession to create the report message in.
182      *
183      @return The report message.
184      *
185      @throws JMSException Any JMSExceptions resulting from creating the report are allowed to fall through.
186      */
187     public Message getReport(Session sessionthrows JMSException
188     {
189         log.debug("public Message getReport(Session controlSession): called");
190 
191         // Close the test connection.
192         connection.close();
193 
194         // Generate a report message containing the count of the number of messages passed.
195         Message report = session.createMessage();
196         report.setStringProperty("CONTROL_TYPE""REPORT");
197         report.setIntProperty("MESSAGE_COUNT", messageCount);
198 
199         return report;
200     }
201 
202     /**
203      * Counts incoming test messages.
204      *
205      @param message The incoming test message.
206      */
207     public void onMessage(Message message)
208     {
209         log.debug("public void onMessage(Message message = " + message + "): called");
210 
211         // Increment the message count.
212         messageCount++;
213     }
214 }