/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.test;
import EDU.oswego.cs.dl.util.concurrent.CountDown;
import org.codehaus.activemq.JmsTopicSendReceiveTest;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
/**
* @version $Revision: 1.6 $
*/
public class SendReceiveTest extends JmsTopicSendReceiveTest {
private CountDown countDown;
public void testSendReceive() throws Exception {
sendSomeMessagesAndCheckTheyAreAllReceived(1);
sendSomeMessagesAndCheckTheyAreAllReceived(2);
sendSomeMessagesAndCheckTheyAreAllReceived(3);
sendSomeMessagesAndCheckTheyAreAllReceived(4);
}
public void testSendReceiveReusingTheMessageInstance() throws Exception {
sendSomeMessagesReusingTheSameMessageAndCheckTheyAreAllReceived(1);
sendSomeMessagesReusingTheSameMessageAndCheckTheyAreAllReceived(2);
sendSomeMessagesReusingTheSameMessageAndCheckTheyAreAllReceived(3);
sendSomeMessagesReusingTheSameMessageAndCheckTheyAreAllReceived(4);
}
public void onMessage(Message message) {
if (countDown != null) {
countDown.release();
}
else {
System.out.println("Warning: no countDown available!");
}
}
protected void sendSomeMessagesAndCheckTheyAreAllReceived(int numberOfMessages) throws JMSException, InterruptedException {
countDown = new CountDown(numberOfMessages);
// send messages
for (int i = 0; i < numberOfMessages; i++) {
producer.send(producerDestination, session.createTextMessage("hi"));
}
// block until the listener has received everything it's expecting, or timeout
countDown.attempt(5000);
assertEquals("Undelivered messages for count: " + numberOfMessages, 0, countDown.currentCount());
}
protected void sendSomeMessagesReusingTheSameMessageAndCheckTheyAreAllReceived(int numberOfMessages) throws JMSException, InterruptedException {
countDown = new CountDown(numberOfMessages);
// send messages
TextMessage message = session.createTextMessage();
for (int i = 0; i < numberOfMessages; i++) {
message.setText("This is message: " + i);
producer.send(producerDestination, message);
}
// block until the listener has received everything it's expecting, or timeout
countDown.attempt(5000);
assertEquals("Undelivered messages for count: " + numberOfMessages, 0, countDown.currentCount());
}
}