Replace simple test code for UDP unicast communication by junit test
This commit is contained in:
@@ -11,12 +11,17 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class UdpSender implements Sender {
|
public class UdpSender implements Sender {
|
||||||
|
protected static final String HOST = "localhost";
|
||||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
protected DatagramSocket datagramSocket;
|
protected DatagramSocket datagramSocket;
|
||||||
protected InetAddress inetAddress;
|
protected InetAddress inetAddress;
|
||||||
protected int port;
|
protected int port;
|
||||||
|
|
||||||
|
public UdpSender(int port) throws UnknownHostException {
|
||||||
|
this(HOST, port);
|
||||||
|
}
|
||||||
|
|
||||||
public UdpSender(String host, int port) throws UnknownHostException{
|
public UdpSender(String host, int port) throws UnknownHostException{
|
||||||
inetAddress = InetAddress.getByName(host);
|
inetAddress = InetAddress.getByName(host);
|
||||||
logger.debug(host);
|
logger.debug(host);
|
||||||
|
|||||||
@@ -5,10 +5,8 @@ import java.net.DatagramPacket;
|
|||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import base.exception.worker.ActivateException;
|
import base.exception.worker.ActivateException;
|
||||||
import base.work.Listen;
|
|
||||||
import base.work.Work;
|
import base.work.Work;
|
||||||
|
|
||||||
public abstract class UdpServer extends Work {
|
public abstract class UdpServer extends Work {
|
||||||
@@ -17,7 +15,6 @@ public abstract class UdpServer extends Work {
|
|||||||
protected int port;
|
protected int port;
|
||||||
protected int bufferSize;
|
protected int bufferSize;
|
||||||
protected DatagramSocket diagramSocket;
|
protected DatagramSocket diagramSocket;
|
||||||
protected ArrayList<Listen<byte[]>> listenList = new ArrayList<Listen<byte[]>>();
|
|
||||||
|
|
||||||
public UdpServer(int port) {
|
public UdpServer(int port) {
|
||||||
this(port, BUFFER_SIZE);
|
this(port, BUFFER_SIZE);
|
||||||
@@ -53,19 +50,8 @@ public abstract class UdpServer extends Work {
|
|||||||
stop();
|
stop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
listen(buffer);
|
input(buffer);
|
||||||
/*for (Listen<byte[]> listen : listenList) {
|
|
||||||
listen.add(buffer);
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void listen(byte[] buffer);
|
protected abstract void input(byte[] buffer);
|
||||||
|
|
||||||
/*public void register(Listen<byte[]> listen) {
|
|
||||||
listenList.add(listen);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove(Listen<Object> listen) {
|
|
||||||
listenList.remove(listen);
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
32
java/base/src/main/java/test/server/dummy/DummyWriter.java
Normal file
32
java/base/src/main/java/test/server/dummy/DummyWriter.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,8 @@ import org.junit.runners.Suite.SuiteClasses;
|
|||||||
@RunWith(Suite.class)
|
@RunWith(Suite.class)
|
||||||
@SuiteClasses({
|
@SuiteClasses({
|
||||||
TestTcpSocketCommunication.class,
|
TestTcpSocketCommunication.class,
|
||||||
TestTcpChannelCommunication.class
|
TestTcpChannelCommunication.class,
|
||||||
|
TestUdpCommunication.class
|
||||||
})
|
})
|
||||||
|
|
||||||
public class AllTests {}
|
public class AllTests {}
|
||||||
|
|||||||
61
java/base/src/test/java/junit/TestUdpCommunication.java
Normal file
61
java/base/src/test/java/junit/TestUdpCommunication.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user