Replace simple test code for UDP unicast communication by junit test

This commit is contained in:
2015-06-15 23:20:59 +01:00
parent 6290eb98da
commit 8e410c263a
8 changed files with 103 additions and 65 deletions

View File

@@ -11,12 +11,17 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UdpSender implements Sender {
protected Logger logger = LoggerFactory.getLogger(getClass());
protected static final String HOST = "localhost";
protected Logger logger = LoggerFactory.getLogger(getClass());
protected DatagramSocket datagramSocket;
protected InetAddress inetAddress;
protected int port;
public UdpSender(int port) throws UnknownHostException {
this(HOST, port);
}
public UdpSender(String host, int port) throws UnknownHostException{
inetAddress = InetAddress.getByName(host);
logger.debug(host);

View File

@@ -5,10 +5,8 @@ import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import base.exception.worker.ActivateException;
import base.work.Listen;
import base.work.Work;
public abstract class UdpServer extends Work {
@@ -17,7 +15,6 @@ public abstract class UdpServer extends Work {
protected int port;
protected int bufferSize;
protected DatagramSocket diagramSocket;
protected ArrayList<Listen<byte[]>> listenList = new ArrayList<Listen<byte[]>>();
public UdpServer(int port) {
this(port, BUFFER_SIZE);
@@ -53,19 +50,8 @@ public abstract class UdpServer extends Work {
stop();
return;
}
listen(buffer);
/*for (Listen<byte[]> listen : listenList) {
listen.add(buffer);
}*/
input(buffer);
}
protected abstract void listen(byte[] buffer);
/*public void register(Listen<byte[]> listen) {
listenList.add(listen);
}
public void remove(Listen<Object> listen) {
listenList.remove(listen);
}*/
protected abstract void input(byte[] buffer);
}

View File

@@ -1,24 +0,0 @@
package test.server;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import test.server.dummy.DummyUdpServer;
import base.sender.UdpSender;
import base.server.datagram.UdpServer;
public class TestUdpCommunication {
public static void main(String[] args) {
// Test Client > Server
UdpServer server = new DummyUdpServer(1234);
server.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {}
try {
UdpSender sender = new UdpSender("255.255.255.255", 1234);
sender.send("Ciao!".getBytes(Charset.defaultCharset()));
} catch (UnknownHostException e) {}
}
}

View File

@@ -1,10 +0,0 @@
package test.server.dummy;
import base.work.Listen;
public class DummyListen extends Listen<byte[]> {
public void receive(byte[] buffer) {
String output = new String(buffer).trim();
System.out.println("Client: message read: " + output);
}
}

View File

@@ -1,13 +0,0 @@
package test.server.dummy;
import base.server.datagram.UdpServer;
public class DummyUdpServer extends UdpServer {
public DummyUdpServer(int port) {
super(port);
}
protected void listen(byte[] buffer) {
logger.debug("Server: " + new String(buffer).trim());
}
}

View File

@@ -0,0 +1,32 @@
package test.server.dummy;
import java.io.IOException;
import base.sender.Sender;
import base.work.Work;
public class DummyWriter extends Work implements Sender {
private Sender sender;
public DummyWriter(Sender sender) {
this.sender = sender;
}
public void work() {
System.out.println("Client sending messages to server...");
String [] messages = new String[] {"Time goes fast.", "What now?", "Bye."};
try {
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i]);
send(new String(messages[i]).getBytes());
sleep(200);
}
stop();
} catch (Exception e) {}
}
public void send(byte[] buffer) throws IOException {
sender.send(buffer);
}
}

View File

@@ -7,7 +7,8 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
TestTcpSocketCommunication.class,
TestTcpChannelCommunication.class
TestTcpChannelCommunication.class,
TestUdpCommunication.class
})
public class AllTests {}

View File

@@ -0,0 +1,61 @@
package junit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import base.sender.UdpSender;
import base.server.datagram.UdpServer;
public class TestUdpCommunication {
protected TestUdpServer server;
protected UdpSender sender;
@Before
public void setUp() throws Exception {
server = new TestUdpServer(1234);
server.start();
sender = new UdpSender(1234);
}
@After
public void tearDown() throws Exception {
server.exit();
// Should add blocking stop and exit to worker
while (server.active()) {
Thread.sleep(100);
}
}
@Test
public void testSendClientToServer() throws Exception {
String message = "test";
sender.send(message.getBytes());
synchronized (server) {
server.wait(2000);
}
byte[] buffer = server.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
class TestUdpServer extends UdpServer {
public byte[] buffer;
public TestUdpServer(int port) {
super(port);
}
@Override
protected void input(byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
}