From: Niki Roo Date: Wed, 29 Nov 2017 17:17:26 +0000 (+0100) Subject: Version 3.1.0: ServerBridge X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=8537d55a7dacf9f528ea9453b03d2391ea348846;p=fanfix-jexer.git Version 3.1.0: ServerBridge --- diff --git a/VERSION b/VERSION index 4a36342..fd2a018 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.0.0 +3.1.0 diff --git a/changelog.md b/changelog.md index 1df57c7..07ebcd4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # nikiroo-utils +## Version 3.1.0 + +- New ServerBridge (including tests) + ## Version 3.0.0 - jDoc diff --git a/src/be/nikiroo/utils/TraceHandler.java b/src/be/nikiroo/utils/TraceHandler.java index 6541a8b..daa3743 100644 --- a/src/be/nikiroo/utils/TraceHandler.java +++ b/src/be/nikiroo/utils/TraceHandler.java @@ -7,9 +7,9 @@ package be.nikiroo.utils; * @author niki */ public class TraceHandler { - private boolean showErrors; - private boolean showTraces; - private boolean showErrorDetails; + private final boolean showErrors; + private final boolean showErrorDetails; + private final int traceLevel; /** * Create a default {@link TraceHandler} that will print errors on stderr @@ -27,13 +27,38 @@ public class TraceHandler { * @param showErrorDetails * show more details when printing errors * @param showTraces - * show traces on stdout + * show level 1 traces on stdout, or no traces at all */ public TraceHandler(boolean showErrors, boolean showErrorDetails, boolean showTraces) { + this(showErrors, showErrorDetails, showTraces ? 1 : 0); + } + + /** + * Create a default {@link TraceHandler}. + * + * @param showErrors + * show errors on stderr + * @param showErrorDetails + * show more details when printing errors + * @param traceLevel + * show traces of this level or lower (0 means "no traces", + * higher means more traces) + */ + public TraceHandler(boolean showErrors, boolean showErrorDetails, + int traceLevel) { this.showErrors = showErrors; this.showErrorDetails = showErrorDetails; - this.showTraces = showTraces; + this.traceLevel = Math.max(traceLevel, 0); + } + + /** + * The trace level of this {@link TraceHandler}. + * + * @return the level + */ + public int getTraceLevel() { + return traceLevel; } /** @@ -67,13 +92,32 @@ public class TraceHandler { /** * A trace happened, show it. *

- * Will only be effective if {@link TraceHandler#showTraces} is true. + * By default, will only be effective if {@link TraceHandler#showTraces} is + * true + *

+ * A call to this method is equivalent to a call to + * {@link TraceHandler#trace(String, int)} with a level of 1. * * @param message * the trace message */ public void trace(String message) { - if (showTraces) { + trace(message, 1); + } + + /** + * A trace happened, show it. + *

+ * By default, will only be effective if {@link TraceHandler#showTraces} is + * true and the level is lower or equal to {@link TraceHandler#traceLevel}. + * + * @param message + * the trace message + * @param level + * the trace level + */ + public void trace(String message, int level) { + if (traceLevel > 0 && level <= traceLevel) { System.out.println(message); } } diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index 36806d6..be847d0 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -184,8 +184,17 @@ abstract class Server implements Runnable { while (started && !exiting) { count(1); - Socket s = ss.accept(); - createConnectActionServer(s).connectAsync(); + final Socket s = ss.accept(); + new Thread(new Runnable() { + @Override + public void run() { + try { + createConnectActionServer(s).connect(); + } finally { + count(-1); + } + } + }).start(); } // Will be covered by @link{Server#stop(long)} for timeouts @@ -283,19 +292,6 @@ abstract class Server implements Runnable { } } - /** - * This method will be called on errors. - *

- * By default, it will only call the trace handler (so you may want to call - * super {@link Server#onError} if you override it). - * - * @param e - * the error - */ - protected void onError(Exception e) { - tracer.error(e); - } - /** * Change the number of currently serviced actions. * @@ -304,13 +300,26 @@ abstract class Server implements Runnable { * * @return the current number after this operation */ - int count(int change) { + private int count(int change) { synchronized (counterLock) { counter += change; return counter; } } + /** + * This method will be called on errors. + *

+ * By default, it will only call the trace handler (so you may want to call + * super {@link Server#onError} if you override it). + * + * @param e + * the error + */ + protected void onError(Exception e) { + tracer.error(e); + } + /** * Create a {@link Socket}. * diff --git a/src/be/nikiroo/utils/serial/server/ServerBridge.java b/src/be/nikiroo/utils/serial/server/ServerBridge.java new file mode 100644 index 0000000..786d018 --- /dev/null +++ b/src/be/nikiroo/utils/serial/server/ServerBridge.java @@ -0,0 +1,252 @@ +package be.nikiroo.utils.serial.server; + +import java.io.IOException; +import java.net.Socket; + +import be.nikiroo.utils.TraceHandler; +import be.nikiroo.utils.Version; +import be.nikiroo.utils.serial.Importer; + +/** + * This class implements a simple server that can bridge two other + * {@link Server}s. + *

+ * It can, of course, inspect the data that goes through it (by default, it + * prints traces of the data). + *

+ * Note: this {@link ServerBridge} has to be discarded after use (cannot be + * started twice). + * + * @author niki + */ +public class ServerBridge extends Server { + private final String forwardToHost; + private final int forwardToPort; + private final boolean forwardToSsl; + + /** + * Create a new server that will start listening on the network when + * {@link ServerBridge#start()} is called. + * + * @param port + * the port to listen on, or 0 to assign any unallocated port + * found (which can later on be queried via + * {@link ServerBridge#getPort()} + * @param ssl + * use a SSL connection (or not) + * + * @throws IOException + * in case of I/O error + */ + public ServerBridge(int port, boolean ssl, String forwardToHost, + int forwardToPort, boolean forwardToSsl) throws IOException { + super(port, ssl); + this.forwardToHost = forwardToHost; + this.forwardToPort = forwardToPort; + this.forwardToSsl = forwardToSsl; + } + + /** + * Create a new server that will start listening on the network when + * {@link ServerBridge#start()} is called. + * + * @param name + * the server name (only used for debug info and traces) + * @param port + * the port to listen on + * @param ssl + * use a SSL connection (or not) + * + * @throws IOException + * in case of I/O error + */ + public ServerBridge(String name, int port, boolean ssl, + String forwardToHost, int forwardToPort, boolean forwardToSsl) + throws IOException { + super(name, port, ssl); + this.forwardToHost = forwardToHost; + this.forwardToPort = forwardToPort; + this.forwardToSsl = forwardToSsl; + } + + /** + * The traces handler for this {@link Server}. + *

+ *

+ * + * @param tracer + * the new traces handler + */ + @Override + public void setTraceHandler(TraceHandler tracer) { + super.setTraceHandler(tracer); + } + + @Override + protected ConnectActionServer createConnectActionServer(Socket s) { + return new ConnectActionServerString(s) { + @Override + public void action(final Version clientVersion) throws Exception { + onClientContact(clientVersion); + final ConnectActionServerString bridge = this; + + new ConnectActionClientString(forwardToHost, forwardToPort, + forwardToSsl, clientVersion) { + @Override + public void action(final Version serverVersion) + throws Exception { + onServerContact(serverVersion); + + for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge + .rec()) { + onRec(clientVersion, fromClient); + String fromServer = send(fromClient); + onSend(serverVersion, fromServer); + bridge.send(fromServer); + } + } + }.connect(); + } + }; + } + + /** + * This is the method that is called each time a client contact us. + * + * @param clientVersion + * the client version + * @param data + * the data sent by the client + */ + protected void onClientContact(Version clientVersion) { + getTraceHandler().trace("CLIENT " + clientVersion); + } + + /** + * This is the method that is called each time a client contact us. + * + * @param serverVersion + * the server version + * @param data + * the data sent by the client + */ + protected void onServerContact(Version serverVersion) { + getTraceHandler().trace("SERVER " + serverVersion); + } + + /** + * This is the method that is called each time a client contact us. + * + * @param clientVersion + * the client version + * @param data + * the data sent by the client + */ + protected void onRec(Version clientVersion, String data) { + trace("<<< CLIENT (" + clientVersion + ")", data); + } + + /** + * This is the method that is called each time the forwarded server contact + * us. + * + * @param serverVersion + * the client version + * @param data + * the data sent by the client + */ + protected void onSend(Version serverVersion, String data) { + trace(">>> SERVER (" + serverVersion + ")", data); + } + + /** + * Trace the data with the given prefix. + * + * @param prefix + * the prefix (client, server, version...) + * @param data + * the data to trace + */ + private void trace(String prefix, String data) { + getTraceHandler().trace(prefix + ": " + data.length() + " characters", + 1); + + if (getTraceHandler().getTraceLevel() >= 2) { + try { + Object obj = new Importer().read(data).getValue(); + if (obj == null) { + getTraceHandler().trace("NULL", 3); + getTraceHandler().trace("NULL", 4); + } else { + getTraceHandler().trace("(" + obj.getClass() + ")", 3); + getTraceHandler().trace("" + obj.toString(), 4); + } + } catch (Exception e) { + getTraceHandler().trace("(not an object)", 3); + getTraceHandler().trace(data, 4); + } + + getTraceHandler().trace("", 4); + } + } + + /** + * Start a bridge between 2 servers. + * + * @param args + * an array containing: + * + */ + public static void main(String[] args) { + final TraceHandler tracer = new TraceHandler(true, false, 0); + try { + if (args.length < 6) { + tracer.error("Invalid syntax.\n" + + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level])\n" + + "\tname: the bridge name\n" + + "\tport: the bridge port\n" + + "\tssl: TRUE for an SSL bridge, FALSE for plain text\n" + + "\tfhost: the forward server host\n" + + "\tfport: the forward server port\n" + + "\tfssl: TRUE for an SSL forward server, FALSE for plain text\n" + + "\ttrace level: the optional trace level (default is 1)\n"); + return; + } + + int i = 0; + String name = args[i++]; + int port = Integer.parseInt(args[i++]); + boolean ssl = Boolean.parseBoolean(args[i++]); + String fhost = args[i++]; + int fport = Integer.parseInt(args[i++]); + boolean fssl = Boolean.parseBoolean(args[i++]); + + int traceLevel = 1; + if (args.length > 6) { + traceLevel = Integer.parseInt(args[i++]); + } + + ServerBridge bridge = new ServerBridge(name, port, ssl, fhost, + fport, fssl); + bridge.setTraceHandler(new TraceHandler(true, true, traceLevel)); + bridge.run(); + } catch (Exception e) { + tracer.error(e); + } + } +} diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index 6ff81f5..1559ae4 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -73,15 +73,6 @@ abstract public class ServerObject extends Server { + ": client has data no more, stopping connection"); } } - - @Override - public void connect() { - try { - super.connect(); - } finally { - count(-1); - } - } }; } diff --git a/src/be/nikiroo/utils/serial/server/ServerString.java b/src/be/nikiroo/utils/serial/server/ServerString.java index 4a185da..186afa0 100644 --- a/src/be/nikiroo/utils/serial/server/ServerString.java +++ b/src/be/nikiroo/utils/serial/server/ServerString.java @@ -56,35 +56,19 @@ abstract public class ServerString extends Server { return new ConnectActionServerString(s) { @Override public void action(Version clientVersion) throws Exception { - try { - for (String data = rec(); data != null; data = rec()) { - String rep = null; - try { - rep = onRequest(this, clientVersion, data); - } catch (Exception e) { - onError(e); - } - - if (rep == null) { - rep = ""; - } + for (String data = rec(); data != null; data = rec()) { + String rep = null; + try { + rep = onRequest(this, clientVersion, data); + } catch (Exception e) { + onError(e); + } - send(rep); + if (rep == null) { + rep = ""; } - } catch (NullPointerException e) { - // Client has no data any more, we quit - getTraceHandler() - .trace(getName() - + ": client has data no more, stopping connection"); - } - } - @Override - public void connect() { - try { - super.connect(); - } finally { - count(-1); + send(rep); } } }; diff --git a/src/be/nikiroo/utils/test/SerialServerTest.java b/src/be/nikiroo/utils/test/SerialServerTest.java new file mode 100644 index 0000000..885ee8c --- /dev/null +++ b/src/be/nikiroo/utils/test/SerialServerTest.java @@ -0,0 +1,542 @@ +package be.nikiroo.utils.test; + +import be.nikiroo.utils.Version; +import be.nikiroo.utils.serial.server.ConnectActionClientObject; +import be.nikiroo.utils.serial.server.ConnectActionClientString; +import be.nikiroo.utils.serial.server.ConnectActionServerObject; +import be.nikiroo.utils.serial.server.ConnectActionServerString; +import be.nikiroo.utils.serial.server.ServerBridge; +import be.nikiroo.utils.serial.server.ServerObject; +import be.nikiroo.utils.serial.server.ServerString; + +class SerialServerTest extends TestLauncher { + private TestLauncher createServerStringTestCases(final String[] args, + final boolean ssl, final boolean bridge) { + final String ssls = (ssl ? "(ssl)" : "(plain text)"); + final String bridges = (bridge ? " with bridge" : ""); + TestLauncher series = new TestLauncher( + "ServerString " + ssls + bridges, args); + + series.addTest(new TestCase("Simple connection " + ssls) { + @Override + public void test() throws Exception { + final String[] rec = new String[1]; + + ServerString server = new ServerString(this.getName(), 0, ssl) { + @Override + protected String onRequest( + ConnectActionServerString action, + Version clientVersion, String data) + throws Exception { + return null; + } + }; + + int port = server.getPort(); + assertEquals("A port should have been assigned", true, port > 0); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + + port = br.getPort(); + assertEquals( + "A port should have been assigned to the bridge", + true, port > 0); + + br.start(); + } + + try { + try { + new ConnectActionClientObject(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + rec[0] = "ok"; + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + assertNotNull("The client action was not run", rec[0]); + assertEquals("ok", rec[0]); + } + }); + + series.addTest(new TestCase("Simple exchange " + ssls) { + final String[] sent = new String[1]; + final String[] recd = new String[1]; + final Exception[] err = new Exception[1]; + + @Override + public void test() throws Exception { + ServerString server = new ServerString(this.getName(), 0, ssl) { + @Override + protected String onRequest( + ConnectActionServerString action, + Version clientVersion, String data) + throws Exception { + sent[0] = data; + return "pong"; + } + + @Override + protected void onError(Exception e) { + super.onError(e); + err[0] = e; + } + }; + + int port = server.getPort(); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + port = br.getPort(); + br.start(); + } + + try { + try { + new ConnectActionClientString(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + recd[0] = send("ping"); + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + if (err[0] != null) { + fail("An exception was thrown: " + err[0].getMessage()); + } + + assertEquals("ping", sent[0]); + assertEquals("pong", recd[0]); + } + }); + + series.addTest(new TestCase("Multiple exchanges " + ssls) { + final String[] sent = new String[3]; + final String[] recd = new String[3]; + final Exception[] err = new Exception[1]; + + @Override + public void test() throws Exception { + ServerString server = new ServerString(this.getName(), 0, ssl) { + @Override + protected String onRequest( + ConnectActionServerString action, + Version clientVersion, String data) + throws Exception { + sent[0] = data; + action.send("pong"); + sent[1] = action.rec(); + return "pong2"; + } + + @Override + protected void onError(Exception e) { + super.onError(e); + err[0] = e; + } + }; + + int port = server.getPort(); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + port = br.getPort(); + br.start(); + } + + try { + try { + new ConnectActionClientString(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + recd[0] = send("ping"); + recd[1] = send("ping2"); + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + if (err[0] != null) { + fail("An exception was thrown: " + err[0].getMessage()); + } + + assertEquals("ping", sent[0]); + assertEquals("pong", recd[0]); + assertEquals("ping2", sent[1]); + assertEquals("pong2", recd[1]); + } + }); + + series.addTest(new TestCase("Multiple call from client " + ssls) { + final String[] sent = new String[3]; + final String[] recd = new String[3]; + final Exception[] err = new Exception[1]; + + @Override + public void test() throws Exception { + ServerString server = new ServerString(this.getName(), 0, ssl) { + @Override + protected String onRequest( + ConnectActionServerString action, + Version clientVersion, String data) + throws Exception { + sent[Integer.parseInt(data)] = data; + return "" + (Integer.parseInt(data) * 2); + } + + @Override + protected void onError(Exception e) { + super.onError(e); + err[0] = e; + } + }; + + int port = server.getPort(); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + port = br.getPort(); + br.start(); + } + + try { + try { + new ConnectActionClientString(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + for (int i = 0; i < 3; i++) { + recd[i] = send("" + i); + } + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + if (err[0] != null) { + fail("An exception was thrown: " + err[0].getMessage()); + } + + assertEquals("0", sent[0]); + assertEquals("0", recd[0]); + assertEquals("1", sent[1]); + assertEquals("2", recd[1]); + assertEquals("2", sent[2]); + assertEquals("4", recd[2]); + } + }); + + return series; + } + + private TestLauncher createServerObjectTestCases(final String[] args, + final boolean ssl, final boolean bridge) { + final String ssls = (ssl ? "(ssl)" : "(plain text)"); + final String bridges = (bridge ? " with bridge" : ""); + TestLauncher series = new TestLauncher( + "ServerObject " + ssls + bridges, args); + + series.addTest(new TestCase("Simple connection " + ssls) { + @Override + public void test() throws Exception { + final Object[] rec = new Object[1]; + + ServerObject server = new ServerObject(this.getName(), 0, ssl) { + @Override + protected Object onRequest( + ConnectActionServerObject action, + Version clientVersion, Object data) + throws Exception { + return null; + } + }; + + int port = server.getPort(); + assertEquals("A port should have been assigned", true, port > 0); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + port = br.getPort(); + br.start(); + } + + try { + try { + new ConnectActionClientObject(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + rec[0] = true; + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + assertNotNull("The client action was not run", rec[0]); + assertEquals(true, (boolean) ((Boolean) rec[0])); + } + }); + + series.addTest(new TestCase("Simple exchange " + ssls) { + final Object[] sent = new Object[1]; + final Object[] recd = new Object[1]; + final Exception[] err = new Exception[1]; + + @Override + public void test() throws Exception { + ServerObject server = new ServerObject(this.getName(), 0, ssl) { + @Override + protected Object onRequest( + ConnectActionServerObject action, + Version clientVersion, Object data) + throws Exception { + sent[0] = data; + return "pong"; + } + + @Override + protected void onError(Exception e) { + super.onError(e); + err[0] = e; + } + }; + + int port = server.getPort(); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + port = br.getPort(); + br.start(); + } + + try { + try { + new ConnectActionClientObject(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + recd[0] = send("ping"); + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + if (err[0] != null) { + fail("An exception was thrown: " + err[0].getMessage()); + } + + assertEquals("ping", sent[0]); + assertEquals("pong", recd[0]); + } + }); + + series.addTest(new TestCase("Multiple exchanges " + ssls) { + final Object[] sent = new Object[3]; + final Object[] recd = new Object[3]; + final Exception[] err = new Exception[1]; + + @Override + public void test() throws Exception { + ServerObject server = new ServerObject(this.getName(), 0, ssl) { + @Override + protected Object onRequest( + ConnectActionServerObject action, + Version clientVersion, Object data) + throws Exception { + sent[0] = data; + action.send("pong"); + sent[1] = action.rec(); + return "pong2"; + } + + @Override + protected void onError(Exception e) { + super.onError(e); + err[0] = e; + } + }; + + int port = server.getPort(); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + port = br.getPort(); + br.start(); + } + + try { + try { + new ConnectActionClientObject(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + recd[0] = send("ping"); + recd[1] = send("ping2"); + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + if (err[0] != null) { + fail("An exception was thrown: " + err[0].getMessage()); + } + + assertEquals("ping", sent[0]); + assertEquals("pong", recd[0]); + assertEquals("ping2", sent[1]); + assertEquals("pong2", recd[1]); + } + }); + + series.addTest(new TestCase("Multiple call from client " + ssls) { + final Object[] sent = new Object[3]; + final Object[] recd = new Object[3]; + final Exception[] err = new Exception[1]; + + @Override + public void test() throws Exception { + ServerObject server = new ServerObject(this.getName(), 0, ssl) { + @Override + protected Object onRequest( + ConnectActionServerObject action, + Version clientVersion, Object data) + throws Exception { + sent[(Integer) data] = data; + return ((Integer) data) * 2; + } + + @Override + protected void onError(Exception e) { + super.onError(e); + err[0] = e; + } + }; + + int port = server.getPort(); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + port = br.getPort(); + br.start(); + } + + try { + try { + new ConnectActionClientObject(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + for (int i = 0; i < 3; i++) { + recd[i] = send(i); + } + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + if (err[0] != null) { + fail("An exception was thrown: " + err[0].getMessage()); + } + + assertEquals(0, sent[0]); + assertEquals(0, recd[0]); + assertEquals(1, sent[1]); + assertEquals(2, recd[1]); + assertEquals(2, sent[2]); + assertEquals(4, recd[2]); + } + }); + + return series; + } + + public SerialServerTest(String[] args) { + super("SerialServer test", args); + + for (boolean ssl : new Boolean[] { false, true }) { + for (boolean bridge : new Boolean[] { false, true }) { + addSeries(createServerObjectTestCases(args, ssl, bridge)); + addSeries(createServerStringTestCases(args, ssl, bridge)); + } + } + } +} diff --git a/src/be/nikiroo/utils/test/SerialTest.java b/src/be/nikiroo/utils/test/SerialTest.java index b1b5c8b..bb62144 100644 --- a/src/be/nikiroo/utils/test/SerialTest.java +++ b/src/be/nikiroo/utils/test/SerialTest.java @@ -1,425 +1,12 @@ package be.nikiroo.utils.test; -import be.nikiroo.utils.Version; import be.nikiroo.utils.serial.Exporter; import be.nikiroo.utils.serial.Importer; -import be.nikiroo.utils.serial.server.ConnectActionClientObject; -import be.nikiroo.utils.serial.server.ConnectActionClientString; -import be.nikiroo.utils.serial.server.ConnectActionServerObject; -import be.nikiroo.utils.serial.server.ConnectActionServerString; -import be.nikiroo.utils.serial.server.ServerObject; -import be.nikiroo.utils.serial.server.ServerString; class SerialTest extends TestLauncher { - private SerialTest() { - super("Serial test", null); - } - - private TestLauncher createServerStringTestCases(final String[] args, - final boolean ssl) { - final String ssls = (ssl ? "(ssl)" : "(plain text)"); - TestLauncher series = new TestLauncher("Client/Server " + ssls, args); - - series.addTest(new TestCase("Simple connection " + ssls) { - @Override - public void test() throws Exception { - final String[] rec = new String[1]; - - ServerString server = new ServerString(this.getName(), 0, ssl) { - @Override - protected String onRequest( - ConnectActionServerString action, - Version clientVersion, String data) - throws Exception { - return null; - } - }; - - assertEquals("A port should have been assigned", true, - server.getPort() > 0); - - server.start(); - - try { - new ConnectActionClientObject(null, server.getPort(), ssl) { - @Override - public void action(Version serverVersion) - throws Exception { - rec[0] = "ok"; - } - }.connect(); - } finally { - server.stop(); - } - - assertNotNull("The client action was not run", rec[0]); - assertEquals("ok", rec[0]); - } - }); - - series.addTest(new TestCase("Simple exchange " + ssls) { - final String[] sent = new String[1]; - final String[] recd = new String[1]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, ssl) { - @Override - protected String onRequest( - ConnectActionServerString action, - Version clientVersion, String data) - throws Exception { - sent[0] = data; - return "pong"; - } - - @Override - protected void onError(Exception e) { - super.onError(e); - err[0] = e; - } - }; - - server.start(); - - try { - new ConnectActionClientString(null, server.getPort(), ssl) { - @Override - public void action(Version serverVersion) - throws Exception { - recd[0] = send("ping"); - } - }.connect(); - } finally { - server.stop(); - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage()); - } - - assertEquals("ping", sent[0]); - assertEquals("pong", recd[0]); - } - }); - - series.addTest(new TestCase("Multiple exchanges " + ssls) { - final String[] sent = new String[3]; - final String[] recd = new String[3]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, ssl) { - @Override - protected String onRequest( - ConnectActionServerString action, - Version clientVersion, String data) - throws Exception { - sent[0] = data; - action.send("pong"); - sent[1] = action.rec(); - return "pong2"; - } - - @Override - protected void onError(Exception e) { - super.onError(e); - err[0] = e; - } - }; - - server.start(); - - try { - new ConnectActionClientString(null, server.getPort(), ssl) { - @Override - public void action(Version serverVersion) - throws Exception { - recd[0] = send("ping"); - recd[1] = send("ping2"); - } - }.connect(); - } finally { - server.stop(); - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage()); - } - - assertEquals("ping", sent[0]); - assertEquals("pong", recd[0]); - assertEquals("ping2", sent[1]); - assertEquals("pong2", recd[1]); - } - }); - - series.addTest(new TestCase("Multiple call from client " + ssls) { - final String[] sent = new String[3]; - final String[] recd = new String[3]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, ssl) { - @Override - protected String onRequest( - ConnectActionServerString action, - Version clientVersion, String data) - throws Exception { - sent[Integer.parseInt(data)] = data; - return "" + (Integer.parseInt(data) * 2); - } - - @Override - protected void onError(Exception e) { - super.onError(e); - err[0] = e; - } - }; - - server.start(); - - try { - new ConnectActionClientString(null, server.getPort(), ssl) { - @Override - public void action(Version serverVersion) - throws Exception { - for (int i = 0; i < 3; i++) { - recd[i] = send("" + i); - } - } - }.connect(); - } finally { - server.stop(); - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage()); - } - - assertEquals("0", sent[0]); - assertEquals("0", recd[0]); - assertEquals("1", sent[1]); - assertEquals("2", recd[1]); - assertEquals("2", sent[2]); - assertEquals("4", recd[2]); - } - }); - - return series; - } - - private TestLauncher createServerObjectTestCases(final String[] args, - final boolean ssl) { - final String ssls = (ssl ? "(ssl)" : "(plain text)"); - TestLauncher series = new TestLauncher("Client/Server " + ssls, args); - - series.addTest(new TestCase("Simple connection " + ssls) { - @Override - public void test() throws Exception { - final Object[] rec = new Object[1]; - - ServerObject server = new ServerObject(this.getName(), 0, ssl) { - @Override - protected Object onRequest( - ConnectActionServerObject action, - Version clientVersion, Object data) - throws Exception { - return null; - } - }; - - assertEquals("A port should have been assigned", true, - server.getPort() > 0); - - server.start(); - - try { - new ConnectActionClientObject(null, server.getPort(), ssl) { - @Override - public void action(Version serverVersion) - throws Exception { - rec[0] = true; - } - }.connect(); - } finally { - server.stop(); - } - - assertNotNull("The client action was not run", rec[0]); - assertEquals(true, (boolean) ((Boolean) rec[0])); - } - }); - - series.addTest(new TestCase("Simple exchange " + ssls) { - final Object[] sent = new Object[1]; - final Object[] recd = new Object[1]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, ssl) { - @Override - protected Object onRequest( - ConnectActionServerObject action, - Version clientVersion, Object data) - throws Exception { - sent[0] = data; - return "pong"; - } - - @Override - protected void onError(Exception e) { - super.onError(e); - err[0] = e; - } - }; - - server.start(); - - try { - new ConnectActionClientObject(null, server.getPort(), ssl) { - @Override - public void action(Version serverVersion) - throws Exception { - recd[0] = send("ping"); - } - }.connect(); - } finally { - server.stop(); - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage()); - } - - assertEquals("ping", sent[0]); - assertEquals("pong", recd[0]); - } - }); - - series.addTest(new TestCase("Multiple exchanges " + ssls) { - final Object[] sent = new Object[3]; - final Object[] recd = new Object[3]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, ssl) { - @Override - protected Object onRequest( - ConnectActionServerObject action, - Version clientVersion, Object data) - throws Exception { - sent[0] = data; - action.send("pong"); - sent[1] = action.rec(); - return "pong2"; - } - - @Override - protected void onError(Exception e) { - super.onError(e); - err[0] = e; - } - }; - - server.start(); - - try { - new ConnectActionClientObject(null, server.getPort(), ssl) { - @Override - public void action(Version serverVersion) - throws Exception { - recd[0] = send("ping"); - recd[1] = send("ping2"); - } - }.connect(); - } finally { - server.stop(); - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage()); - } - - assertEquals("ping", sent[0]); - assertEquals("pong", recd[0]); - assertEquals("ping2", sent[1]); - assertEquals("pong2", recd[1]); - } - }); - - series.addTest(new TestCase("Multiple call from client " + ssls) { - final Object[] sent = new Object[3]; - final Object[] recd = new Object[3]; - final Exception[] err = new Exception[1]; - - @Override - public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, ssl) { - @Override - protected Object onRequest( - ConnectActionServerObject action, - Version clientVersion, Object data) - throws Exception { - sent[(Integer) data] = data; - return ((Integer) data) * 2; - } - - @Override - protected void onError(Exception e) { - super.onError(e); - err[0] = e; - } - }; - - server.start(); - - try { - new ConnectActionClientObject(null, server.getPort(), ssl) { - @Override - public void action(Version serverVersion) - throws Exception { - for (int i = 0; i < 3; i++) { - recd[i] = send(i); - } - } - }.connect(); - } finally { - server.stop(); - } - - if (err[0] != null) { - fail("An exception was thrown: " + err[0].getMessage()); - } - - assertEquals(0, sent[0]); - assertEquals(0, recd[0]); - assertEquals(1, sent[1]); - assertEquals(2, recd[1]); - assertEquals(2, sent[2]); - assertEquals(4, recd[2]); - } - }); - - return series; - } - public SerialTest(String[] args) { super("Serial test", args); - addSeries(createServerObjectTestCases(args, false)); - - addSeries(createServerObjectTestCases(args, true)); - - addSeries(createServerStringTestCases(args, false)); - - addSeries(createServerStringTestCases(args, true)); - addTest(new TestCase("Simple class Import/Export") { @Override public void test() throws Exception { diff --git a/src/be/nikiroo/utils/test/Test.java b/src/be/nikiroo/utils/test/Test.java index 1682955..4f20182 100644 --- a/src/be/nikiroo/utils/test/Test.java +++ b/src/be/nikiroo/utils/test/Test.java @@ -21,6 +21,7 @@ public class Test extends TestLauncher { addSeries(new IOUtilsTest(args)); addSeries(new VersionTest(args)); addSeries(new SerialTest(args)); + addSeries(new SerialServerTest(args)); addSeries(new StringUtilsTest(args)); }