Move files in anticipation of move to modular system

This commit is contained in:
2016-07-03 12:28:35 +01:00
parent 72e82b41fe
commit 3cca0d9ba0
331 changed files with 5552 additions and 5587 deletions

View File

@@ -0,0 +1,17 @@
package junit;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
TestTcpSocketCommunication.class,
TestTcpChannelCommunication.class,
TestUdpUnicastCommunication.class,
TestUdpMulticastCommunication.class,
TestUdpDuplexCommunication.class
})
public class AllTests {}
// Should test start()/stop() of components, check implementation

View File

@@ -0,0 +1,94 @@
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.server.channel.TcpClient;
import base.server.channel.TcpServer;
import base.server.channel.TcpServerClient;
public class TestTcpChannelCommunication {
protected TestTcpServer server;
protected TestTcpClient client;
@Before
public void setUp() throws Exception {
server = new TestTcpServer(1234);
server.start();
client = new TestTcpClient(1234);
client.start();
}
@After
public void tearDown() throws Exception {
client.exit();
server.exit();
// Should add blocking stop and exit to worker
while (client.active() || server.active()) {
Thread.sleep(100);
}
}
@Test
public void testSendClientToServer() throws Exception {
String message = "test";
client.send(message.getBytes());
synchronized (server) {
server.wait(2000);
}
byte[] buffer = server.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
@Test
public void testSendServerToClient() throws Exception {
// If client can send, connection has been established
client.send("init".getBytes());
String message = "test";
server.send(message.getBytes());
synchronized (client) {
client.wait(2000);
}
byte[] buffer = client.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
class TestTcpServer extends TcpServer {
public byte[] buffer;
public TestTcpServer(int port) {
super(port);
}
public void input(TcpServerClient client, byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
class TestTcpClient extends TcpClient {
public byte[] buffer;
public TestTcpClient(int port) {
super(port);
}
protected void input(byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
}

View File

@@ -0,0 +1,93 @@
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.server.socket.TcpClient;
import base.server.socket.TcpServer;
import base.server.socket.TcpServerClient;
public class TestTcpSocketCommunication {
protected TestTcpServer server;
protected TestTcpClient client;
@Before
public void setUp() throws Exception {
server = new TestTcpServer(1234);
server.start();
client = new TestTcpClient(1234);
client.start();
}
@After
public void tearDown() throws Exception {
client.exit();
server.exit();
// Should add blocking stop and exit to worker
while (client.active() || server.active()) {
Thread.sleep(100);
}
}
@Test
public void testSendClientToServer() throws Exception {
String message = "test";
client.send(message.getBytes());
synchronized (server) {
server.wait(2000);
}
byte[] buffer = server.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
@Test
public void testSendServerToClient() throws Exception {
// If client can send, connection has been established
client.send("init".getBytes());
String message = "test";
server.send(message.getBytes());
synchronized (client) {
client.wait(2000);
}
byte[] buffer = client.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
class TestTcpServer extends TcpServer {
public byte[] buffer;
public TestTcpServer(int port) {
super(port);
}
public void input(TcpServerClient client, byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
class TestTcpClient extends TcpClient {
public byte[] buffer;
public TestTcpClient(int port) {
super(port);
}
protected void input(byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
}

View File

@@ -0,0 +1,96 @@
package junit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.net.UnknownHostException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import base.server.datagram.UdpDuplexAutoClient;
import base.server.datagram.UdpDuplexServer;
public class TestUdpDuplexCommunication {
protected TestUdpDuplexServer server;
protected TestUdpDuplexClient client;
@Before
public void setUp() throws Exception {
server = new TestUdpDuplexServer(1234, 1235);
server.start();
client = new TestUdpDuplexClient(1234, 1235);
client.start();
}
@After
public void tearDown() throws Exception {
client.exit();
server.exit();
// Should add blocking stop and exit to worker
while (client.active() || server.active()) {
Thread.sleep(1000);
}
}
@Test
public void testServerToClientCommunication() throws Exception {
String message = "test";
server.send(message.getBytes());
System.err.println("send");
synchronized (client) {
client.wait(2000);
}
byte[] buffer = client.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
@Test
public void testClientToServerCommunication() throws Exception {
// Let client discover server address
testServerToClientCommunication();
String message = "test";
client.send(message.getBytes());
System.err.println("send");
synchronized (server) {
server.wait(2000);
}
byte[] buffer = server.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
public class TestUdpDuplexServer extends UdpDuplexServer {
public byte[] buffer;
public TestUdpDuplexServer(int sendPort, int bindPort) {
super(sendPort, bindPort);
}
public void input(byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
class TestUdpDuplexClient extends UdpDuplexAutoClient {
public byte[] buffer;
public TestUdpDuplexClient(int bindPort, int sendPort) throws UnknownHostException {
super(bindPort, sendPort);
}
public void input(byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
}

View File

@@ -0,0 +1,63 @@
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.server.datagram.UdpMulticastClient;
import base.server.datagram.UdpMulticastServer;
public class TestUdpMulticastCommunication {
protected UdpMulticastServer server;
protected TestUdpMulticastClient client;
@Before
public void setUp() throws Exception {
server = new UdpMulticastServer(1234);
server.start();
client = new TestUdpMulticastClient(1234);
client.start();
}
@After
public void tearDown() throws Exception {
client.exit();
server.exit();
// Should add blocking stop and exit to worker
while (client.active() || server.active()) {
Thread.sleep(1000);
}
}
@Test
public void testServerToClientCommunication() throws Exception {
String message = "test";
server.send(message.getBytes());
System.err.println("send");
synchronized (client) {
client.wait(2000);
}
byte[] buffer = client.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
class TestUdpMulticastClient extends UdpMulticastClient {
public byte[] buffer;
public TestUdpMulticastClient(int port) {
super(port);
}
public void input(byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
}

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.server.datagram.UdpSender;
import base.server.datagram.UdpServer;
public class TestUdpUnicastCommunication {
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();
}
}
}
}

View File

@@ -0,0 +1,54 @@
package test;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class Test {
public static void main(String[] args) {
try {
new Test().start();
} catch (Throwable e) {
e.printStackTrace();
}
}
private void start() throws Throwable {
input((Object) new A());
input((Object) new B());
input((Object) new String[] {"a", "b"});
}
public void input(Object object) throws Throwable {
System.out.println("Object");
MethodType methodType = MethodType.methodType(void.class, object.getClass());
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle methodHandle = lookup.findVirtual(getClass(), "input", methodType);
try {
methodHandle.invoke(this, object);
} catch (NoSuchMethodException e) {
e.printStackTrace();
System.exit(1);
}
}
public void input(A object) {
System.out.println("A");
}
public void input(B object) {
System.out.println("B");
}
public void input(String[] object) {
System.out.println("String[]");
}
public class A {
}
public class B {
}
}

View File

@@ -0,0 +1,15 @@
package worker;
import worker.dummy.DummyWork;
public class TestDirectWork {
public static void main(String[] args) {
DummyWork work = new DummyWork(1);
work.setWork(100);
work.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {}
}
}

View File

@@ -0,0 +1,18 @@
package worker;
import worker.dummy.DummyIntervalWork;
import base.work.Work;
public class TestIntervalWork {
public static void main(String[] args) {
Work work = new DummyIntervalWork(500);
for (int i = 0; i < 10; ++i) {
work.start();
System.out.println("--");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
work.stop();
}
}
}

View File

@@ -0,0 +1,19 @@
package worker;
import worker.dummy.DummyListen;
public class TestListen {
public static void main(String[] args) {
DummyListen<Integer> listen = new DummyListen<Integer>(0);
listen.start();
for (int i = 0; i < 10; ++i) {
listen.add(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {}
}
}

View File

@@ -0,0 +1,38 @@
package worker;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import worker.dummy.DummyListen;
import base.worker.pool.ListenerPool;
public class TestPooledListen {
protected int id;
public TestPooledListen(int id) {
this.id = id;
}
public void input(Integer element) {
System.out.println("#" + id + ": " + element);
}
public static void main(String[] args) {
ListenerPool<Integer> listenerPool = new ListenerPool<Integer>(5);
List<DummyListen<Integer>> listenList = new ArrayList<DummyListen<Integer>>();
for (int i = 0; i < 20; ++i) {
DummyListen<Integer> listen = new DummyListen<Integer>(listenerPool, i + 1);
listenList.add(listen);
}
listenerPool.start();
System.out.println("Starting to give out elements!");
for (int i = 0; i < 100; ++i) {
DummyListen<Integer> randomListen = listenList.get((new Random()).nextInt(listenList.size()));
randomListen.add(i);
}
//listenerPool.await();
}
}

View File

@@ -0,0 +1,45 @@
package worker;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import worker.dummy.DummyWork;
import base.work.Work;
import base.worker.pool.WorkerPool;
public class TestPooledWork {
public static void main(String[] args) {
WorkerPool workerPool = new WorkerPool(3);
List<DummyWork> workList = new ArrayList<DummyWork>();
for (int i = 0; i < 10; ++i) {
DummyWork work = new DummyWork(workerPool, i + 1);
workList.add(work);
}
workerPool.start();
System.out.println("Starting work!");
ArrayList<Work> activeWorkList = new ArrayList<Work>();
for (int i = 0; i < 8; ++i) {
DummyWork work = workList.get((new Random()).nextInt(workList.size()));
work.setWork(1000);
work.start();
activeWorkList.add(work);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
int i = 0;
for (Work work : activeWorkList) {
if (++i > 5) {
break;
}
work.stop();
}
try {
Thread.sleep(100000);
} catch (InterruptedException e) {}
System.exit(0);
}
}

View File

@@ -0,0 +1,13 @@
package worker.dummy;
import base.worker.IntervalWork;
public class DummyIntervalWork extends IntervalWork {
public DummyIntervalWork(int interval) {
super(interval);
}
public void work() {
System.out.println(":-)");
}
}

View File

@@ -0,0 +1,26 @@
package worker.dummy;
import base.work.Listen;
import base.worker.pool.ListenerPool;
public class DummyListen<T> extends Listen<T> {
protected int id;
public DummyListen(ListenerPool<T> listenerPool, int id) {
super(listenerPool);
this.id = id;
}
public DummyListen(int id) {
super();
this.id = id;
}
public void input(Integer input) {
System.out.println("#" + id + ", input = " + input);
}
public void input(byte[] input) {
System.out.println("#" + id + ", input = " + new String(input).trim());
}
}

View File

@@ -0,0 +1,42 @@
package worker.dummy;
import base.exception.worker.ActivateException;
import base.exception.worker.DeactivateException;
import base.work.Work;
import base.worker.pool.WorkerPool;
public class DummyWork extends Work {
protected int id;
protected volatile int work;
public DummyWork(int id) {
super();
this.id = id;
}
public DummyWork(WorkerPool workerPool, int id) {
super(workerPool);
this.id = id;
}
public void setWork(int work) {
System.out.println("#" + id + ", set work @ " + work);
this.work = work;
}
public void work() {
System.out.println("#" + id + ", work = " + work);
if (--work < 1) {
stop();
}
sleep(300);
}
public void activate() throws ActivateException {
System.out.println("#" + id + ", activating...");
}
public void deactivate() throws DeactivateException {
System.out.println("#" + id + ", deactivating...");
}
}

View File

@@ -0,0 +1,4 @@
log4j.rootLogger=TRACE, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n