Added test case for UDP

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@383984 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-03-07 19:52:50 +00:00
parent 187f884b58
commit 05b81ba8be
2 changed files with 171 additions and 0 deletions

View File

@ -0,0 +1,128 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* 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.apache.activemq.transport.udp;
import org.apache.activemq.command.Command;
import org.apache.activemq.command.ConsumerInfo;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportListener;
import java.io.IOException;
import junit.framework.TestCase;
/**
*
* @version $Revision$
*/
public abstract class UdpTestSupport extends TestCase implements TransportListener {
protected abstract Transport createConsumer() throws Exception;
protected abstract Transport createProducer() throws Exception;
protected Transport producer;
protected Transport consumer;
protected Object lock = new Object();
protected Command receivedCommand;
public void testSendingSmallMessage() throws Exception {
ConsumerInfo expected = new ConsumerInfo();
expected.setSelector("Cheese");
try {
producer.oneway(expected);
Command received = assertCommandReceived();
assertTrue("Should have received a ConsumerInfo but was: " + received, received instanceof ConsumerInfo);
ConsumerInfo actual = (ConsumerInfo) received;
assertEquals("Selector", expected.getSelector(), actual.getSelector());
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
fail("Failed to send to transport: " + e);
}
}
protected void setUp() throws Exception {
consumer = createConsumer();
producer = createProducer();
consumer.setTransportListener(this);
producer.setTransportListener(new TransportListener() {
public void onCommand(Command command) {
}
public void onException(IOException error) {
}
public void transportInterupted() {
}
public void transportResumed() {
}
});
consumer.start();
producer.start();
}
protected void tearDown() throws Exception {
if (producer != null) {
producer.stop();
}
if (consumer != null) {
consumer.stop();
}
}
public void onCommand(Command command) {
System.out.println("### Received command: " + command);
synchronized (lock) {
receivedCommand = command;
lock.notifyAll();
}
}
public void onException(IOException error) {
System.out.println("### Received error: " + error);
}
public void transportInterupted() {
System.out.println("### Transport interrupted");
}
public void transportResumed() {
System.out.println("### Transport resumed");
}
protected Command assertCommandReceived() throws InterruptedException {
Command answer = null;
synchronized (lock) {
lock.wait(5000);
answer = receivedCommand;
}
assertNotNull("Should have received a Command by now!", answer);
return answer;
}
}

View File

@ -0,0 +1,43 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* 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.apache.activemq.transport.udp;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportFactory;
import java.net.URI;
/**
*
* @version $Revision$
*/
public class UdpTransportTest extends UdpTestSupport {
protected String producerURI = "udp://localhost:8830";
protected String consumerURI = "udp://localhost:8831?port=8830";
protected Transport createProducer() throws Exception {
System.out.println("Producer using URI: " + producerURI);
return TransportFactory.connect(new URI(producerURI));
}
protected Transport createConsumer() throws Exception {
System.out.println("Consumer using URI: " + consumerURI);
return TransportFactory.connect(new URI(consumerURI));
}
}