TestCase2BasicP2P.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 2, basic P2P. Sends/received a specified number of messages to a specified route on the
033  * default direct exchange. Produces reports on the actual number of messages sent/received.
034  *
035  <p><table id="crc"><caption>CRC Card</caption>
036  <tr><th> Responsibilities <th> Collaborations
037  <tr><td> Supply the name of the test case that this implements.
038  <tr><td> Accept/Reject invites based on test parameters.
039  <tr><td> Adapt to assigned roles.
040  <tr><td> Send required number of test messages.
041  <tr><td> Generate test reports.
042  </table>
043  */
044 public class TestCase2BasicP2P implements TestClientControlledTest, MessageListener
045 {
046     /** Used for debugging. */
047     private static final Logger log = Logger.getLogger(TestCase2BasicP2P.class);
048 
049     /** Holds the count of test messages received. */
050     private int messageCount;
051 
052     /** The role to be played by the test. */
053     private Roles role;
054 
055     /** The number of test messages to send. */
056     private int numMessages;
057 
058     /** The connection to send the test messages on. */
059     private Connection connection;
060 
061     /** The controlSession to send the test messages on. */
062     private Session session;
063 
064     /** The producer to send the test messages with. */
065     MessageProducer producer;
066 
067     /**
068      * Should provide the name of the test case that this class implements. The exact names are defined in the
069      * interop testing spec.
070      *
071      @return The name of the test case that this implements.
072      */
073     public String getName()
074     {
075         log.debug("public String getName(): called");
076 
077         return "TC2_BasicP2P";
078     }
079 
080     /**
081      * Determines whether the test invite that matched this test case is acceptable.
082      *
083      @param inviteMessage The invitation to accept or reject.
084      *
085      @return <tt>true</tt> to accept the invitation, <tt>false</tt> to reject it.
086      *
087      @throws JMSException Any JMSException resulting from reading the message are allowed to fall through.
088      */
089     public boolean acceptInvite(Message inviteMessagethrows JMSException
090     {
091         log.debug("public boolean acceptInvite(Message inviteMessage = " + inviteMessage + "): called");
092 
093         // All invites are acceptable.
094         return true;
095     }
096 
097     /**
098      * Assigns the role to be played by this test case. The test parameters are fully specified in the
099      * assignment message. When this method return the test case will be ready to execute.
100      *
101      @param role              The role to be played; sender or receivers.
102      *
103      @param assignRoleMessage The role assingment message, contains the full test parameters.
104      *
105      @throws JMSException Any JMSException resulting from reading the message are allowed to fall through.
106      */
107     public void assignRole(Roles role, Message assignRoleMessagethrows JMSException
108     {
109         log.debug("public void assignRole(Roles role = " + role + ", Message assignRoleMessage = " + assignRoleMessage
110             "): called");
111 
112         // Reset the message count for a new test.
113         messageCount = 0;
114 
115         // Take note of the role to be played.
116         this.role = role;
117 
118         // Create a new connection to pass the test messages on.
119         connection = TestUtils.createConnection(TestClient.testContextProperties);
120         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
121 
122         // Extract and retain the test parameters.
123         numMessages = assignRoleMessage.getIntProperty("P2P_NUM_MESSAGES");
124         Destination sendDestination = session.createQueue(assignRoleMessage.getStringProperty("P2P_QUEUE_AND_KEY_NAME"));
125 
126         log.debug("numMessages = " + numMessages);
127         log.debug("sendDestination = " + sendDestination);
128         log.debug("role = " + role);
129 
130         switch (role)
131         {
132         // Check if the sender role is being assigned, and set up a message producer if so.
133         case SENDER:
134             producer = session.createProducer(sendDestination);
135             break;
136 
137         // Otherwise the receivers role is being assigned, so set this up to listen for messages.
138         case RECEIVER:
139             MessageConsumer consumer = session.createConsumer(sendDestination);
140             consumer.setMessageListener(this);
141             break;
142         }
143 
144         connection.start();
145     }
146 
147     /**
148      * Performs the test case actions. Returning from here, indicates that the sending role has completed its test.
149      *
150      @param numMessages The number of test messages to send.
151      *
152      @throws JMSException Any JMSException resulting from reading the message are allowed to fall through.
153      */
154     public void start(int numMessagesthrows JMSException
155     {
156         log.debug("public void start(): called");
157 
158         // Check that the sender role is being performed.
159         if (role.equals(Roles.SENDER))
160         {
161             Message testMessage = session.createTextMessage("test");
162 
163             for (int i = 0; i < this.numMessages; i++)
164             {
165                 producer.send(testMessage);
166 
167                 // Increment the message count.
168                 messageCount++;
169             }
170         }
171     }
172 
173     /**
174      * Gets a report on the actions performed by the test case in its assigned role.
175      *
176      @param session The controlSession to create the report message in.
177      *
178      @return The report message.
179      *
180      @throws JMSException Any JMSExceptions resulting from creating the report are allowed to fall through.
181      */
182     public Message getReport(Session sessionthrows JMSException
183     {
184         log.debug("public Message getReport(Session controlSession): called");
185 
186         // Close the test connection.
187         connection.close();
188 
189         // Generate a report message containing the count of the number of messages passed.
190         Message report = session.createMessage();
191         report.setStringProperty("CONTROL_TYPE""REPORT");
192         report.setIntProperty("MESSAGE_COUNT", messageCount);
193 
194         return report;
195     }
196 
197     /**
198      * Counts incoming test messages.
199      *
200      @param message The incoming test message.
201      */
202     public void onMessage(Message message)
203     {
204         log.debug("public void onMessage(Message message = " + message + "): called");
205 
206         // Increment the message count.
207         messageCount++;
208     }
209 }