From: Niki Roo Date: Sat, 2 Dec 2017 19:18:29 +0000 (+0100) Subject: Version 3.1.6: fix Bridge, Serialiser, Progress: X-Git-Tag: nikiroo-utils-3.1.6^0 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=f4053377fa15da2f11e82955bfab86e673fa371c;p=nikiroo-utils.git Version 3.1.6: fix Bridge, Serialiser, Progress: - Serialiser bug with custom objects and string inside another custom object fixed - Bridge default maxPrintSize parameter fixed - Progress/ProgressBar synchronisation issues fixed --- diff --git a/VERSION b/VERSION index 3ad0595..9cec716 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.5 +3.1.6 diff --git a/changelog.md b/changelog.md index e4354b8..6552145 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # nikiroo-utils +## Version 3.1.6 + +- Fix Serialiser issue with custom objects and String in a custom object +- Fix Progress/ProgressBar synchronisation issues +- Fix Bridge default maxPrintSize parameter + ## Version 3.1.5 - Fix Cache with no-parent file diff --git a/src/be/nikiroo/utils/Progress.java b/src/be/nikiroo/utils/Progress.java index 38ce29f..6f05110 100644 --- a/src/be/nikiroo/utils/Progress.java +++ b/src/be/nikiroo/utils/Progress.java @@ -6,7 +6,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; /** * Progress reporting system, possibly nested. @@ -318,8 +317,10 @@ public class Progress { * * @return the children (Who will think of the children??) */ - public Set getChildren() { - return children.keySet(); + public List getChildren() { + synchronized (getLock()) { + return new ArrayList(children.keySet()); + } } /** @@ -359,7 +360,9 @@ public class Progress { * the listener */ public void addProgressListener(ProgressListener l) { - this.listeners.add(l); + synchronized (getLock()) { + this.listeners.add(l); + } } /** @@ -371,7 +374,9 @@ public class Progress { * @return TRUE if it was found (and removed) */ public boolean removeProgressListener(ProgressListener l) { - return this.listeners.remove(l); + synchronized (getLock()) { + return this.listeners.remove(l); + } } /** @@ -408,9 +413,12 @@ public class Progress { public void progress(Progress pg, String name) { synchronized (getLock()) { double total = relativeLocalProgress; - for (Entry entry : children.entrySet()) { - total += (entry.getValue() / (max - min)) - * entry.getKey().getRelativeProgress(); + synchronized (getLock()) { + for (Entry entry : children + .entrySet()) { + total += (entry.getValue() / (max - min)) + * entry.getKey().getRelativeProgress(); + } } setRelativeProgress(pg, name, total); @@ -418,7 +426,9 @@ public class Progress { } }); - this.children.put(progress, weight); + synchronized (getLock()) { + this.children.put(progress, weight); + } } /** diff --git a/src/be/nikiroo/utils/serial/CustomSerializer.java b/src/be/nikiroo/utils/serial/CustomSerializer.java index 0bafb86..be89316 100644 --- a/src/be/nikiroo/utils/serial/CustomSerializer.java +++ b/src/be/nikiroo/utils/serial/CustomSerializer.java @@ -23,7 +23,7 @@ public abstract class CustomSerializer { public boolean encode(StringBuilder builder, Object value) { int prev = builder.length(); String customString = toString(value); - builder.append("custom:").append(getType()).append(":"); + builder.append("custom^").append(getType()).append("^"); if (!SerialUtils.encode(builder, customString)) { builder.delete(prev, builder.length()); return false; @@ -37,23 +37,23 @@ public abstract class CustomSerializer { } public static boolean isCustom(String encodedValue) { - int pos1 = encodedValue.indexOf(':'); - int pos2 = encodedValue.indexOf(':', pos1 + 1); + int pos1 = encodedValue.indexOf('^'); + int pos2 = encodedValue.indexOf('^', pos1 + 1); - return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom:"); + return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom^"); } public static String typeOf(String encodedValue) { - int pos1 = encodedValue.indexOf(':'); - int pos2 = encodedValue.indexOf(':', pos1 + 1); + int pos1 = encodedValue.indexOf('^'); + int pos2 = encodedValue.indexOf('^', pos1 + 1); String type = encodedValue.substring(pos1 + 1, pos2); return type; } public static String contentOf(String encodedValue) { - int pos1 = encodedValue.indexOf(':'); - int pos2 = encodedValue.indexOf(':', pos1 + 1); + int pos1 = encodedValue.indexOf('^'); + int pos2 = encodedValue.indexOf('^', pos1 + 1); String encodedContent = encodedValue.substring(pos2 + 1); return encodedContent; diff --git a/src/be/nikiroo/utils/serial/Importer.java b/src/be/nikiroo/utils/serial/Importer.java index 8fba42d..e7285c4 100644 --- a/src/be/nikiroo/utils/serial/Importer.java +++ b/src/be/nikiroo/utils/serial/Importer.java @@ -154,7 +154,8 @@ public class Importer { if (line.endsWith(":")) { // field value is compound currentFieldName = line.substring(0, line.length() - 1); - } else if (line.startsWith(":") || !line.contains(":")) { + } else if (line.startsWith(":") || !line.contains(":") + || line.startsWith("\"") || CustomSerializer.isCustom(line)) { // not a field value but a direct value me = SerialUtils.decode(line); } else { diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java index decf575..a1105a9 100644 --- a/src/be/nikiroo/utils/serial/SerialUtils.java +++ b/src/be/nikiroo/utils/serial/SerialUtils.java @@ -7,6 +7,7 @@ import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.UnknownFormatConversionException; @@ -37,6 +38,7 @@ import be.nikiroo.utils.ImageUtils; *
  • Enum (any enum whose name and value is known by the caller)
  • *
  • java.awt.image.BufferedImage (as a {@link CustomSerializer})
  • *
  • An array of the above (as a {@link CustomSerializer})
  • + *
  • URL
  • * * * @author niki @@ -106,6 +108,30 @@ public class SerialUtils { } }); + // URL: + customTypes.put("java.net.URL", new CustomSerializer() { + @Override + protected String toString(Object value) { + if (value != null) { + return ((URL) value).toString(); + } + return null; + } + + @Override + protected Object fromString(String content) throws IOException { + if (content != null) { + return new URL(content); + } + return null; + } + + @Override + protected String getType() { + return "java.net.URL"; + } + }); + // Images (this is currently the only supported image type by default) customTypes.put("java.awt.image.BufferedImage", new CustomSerializer() { @Override diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClient.java b/src/be/nikiroo/utils/serial/server/ConnectActionClient.java index 626b62f..db06a9f 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClient.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClient.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.Socket; +import java.net.UnknownHostException; import be.nikiroo.utils.Version; @@ -44,7 +45,12 @@ abstract class ConnectActionClient { * TRUE for an SSL connection, FALSE for plain text * * @throws IOException - * in case of I/O error when creating the socket + * in case of I/O error + * @throws UnknownHostException + * if the host is not known + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ConnectActionClient(String host, int port, boolean ssl) throws IOException { @@ -64,7 +70,12 @@ abstract class ConnectActionClient { * the client version * * @throws IOException - * in case of I/O error when creating the socket + * in case of I/O error + * @throws UnknownHostException + * if the host is not known + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ConnectActionClient(String host, int port, boolean ssl, Version version) throws IOException { diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java b/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java index e9673ac..dd6f917 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.Socket; +import java.net.UnknownHostException; import be.nikiroo.utils.Version; @@ -39,7 +40,12 @@ public class ConnectActionClientObject extends ConnectActionClient { * TRUE for an SSL connection, FALSE for plain text * * @throws IOException - * in case of I/O error when creating the socket + * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ConnectActionClientObject(String host, int port, boolean ssl) throws IOException { @@ -59,7 +65,12 @@ public class ConnectActionClientObject extends ConnectActionClient { * the client version * * @throws IOException - * in case of I/O error when creating the socket + * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ConnectActionClientObject(String host, int port, boolean ssl, Version version) throws IOException { diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java b/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java index f9402cb..8b5ec2a 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.Socket; +import java.net.UnknownHostException; import be.nikiroo.utils.Version; @@ -39,7 +40,12 @@ public class ConnectActionClientString extends ConnectActionClient { * TRUE for an SSL connection, FALSE for plain text * * @throws IOException - * in case of I/O error when creating the socket + * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ConnectActionClientString(String host, int port, boolean ssl) throws IOException { @@ -59,7 +65,12 @@ public class ConnectActionClientString extends ConnectActionClient { * the client version * * @throws IOException - * in case of I/O error when creating the socket + * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ConnectActionClientString(String host, int port, boolean ssl, Version version) throws IOException { diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index be847d0..2022469 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -3,6 +3,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; @@ -62,6 +63,11 @@ abstract class Server implements Runnable { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public Server(int port, boolean ssl) throws IOException { this((String) null, port, ssl); @@ -80,6 +86,11 @@ abstract class Server implements Runnable { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public Server(String name, int port, boolean ssl) throws IOException { this.name = name; @@ -180,7 +191,8 @@ abstract class Server implements Runnable { } try { - tracer.trace(name + ": server starting on port " + port); + tracer.trace(name + ": server starting on port " + port + " (" + + (ssl ? "SSL" : "plain text") + ")"); while (started && !exiting) { count(1); @@ -334,6 +346,11 @@ abstract class Server implements Runnable { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the host is not known + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ static Socket createSocket(String host, int port, boolean ssl) throws IOException { @@ -360,6 +377,11 @@ abstract class Server implements Runnable { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ static ServerSocket createSocketServer(int port, boolean ssl) throws IOException { diff --git a/src/be/nikiroo/utils/serial/server/ServerBridge.java b/src/be/nikiroo/utils/serial/server/ServerBridge.java index 4aa9c41..9ccae1c 100644 --- a/src/be/nikiroo/utils/serial/server/ServerBridge.java +++ b/src/be/nikiroo/utils/serial/server/ServerBridge.java @@ -3,6 +3,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.lang.reflect.Array; import java.net.Socket; +import java.net.UnknownHostException; import be.nikiroo.utils.StringUtils; import be.nikiroo.utils.TraceHandler; @@ -45,6 +46,11 @@ public class ServerBridge extends Server { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ServerBridge(int port, boolean ssl, String forwardToHost, int forwardToPort, boolean forwardToSsl) throws IOException { @@ -73,6 +79,11 @@ public class ServerBridge extends Server { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ServerBridge(String name, int port, boolean ssl, String forwardToHost, int forwardToPort, boolean forwardToSsl) @@ -113,30 +124,34 @@ public class ServerBridge extends Server { onClientContact(clientVersion); final ConnectActionServerString bridge = this; - new ConnectActionClientString(forwardToHost, forwardToPort, - forwardToSsl, clientVersion) { - @Override - public void action(final Version serverVersion) - throws Exception { - onServerContact(serverVersion); + try { + 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); - } + for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge + .rec()) { + onRec(clientVersion, fromClient); + String fromServer = send(fromClient); + onSend(serverVersion, fromServer); + bridge.send(fromServer); + } - getTraceHandler().trace("=== DONE", 1); - getTraceHandler().trace("", 1); - } + getTraceHandler().trace("=== DONE", 1); + getTraceHandler().trace("", 1); + } - @Override - protected void onError(Exception e) { - ServerBridge.this.onError(e); - } - }.connect(); + @Override + protected void onError(Exception e) { + ServerBridge.this.onError(e); + } + }.connect(); + } catch (Exception e) { + ServerBridge.this.onError(e); + } } }; } @@ -187,6 +202,15 @@ public class ServerBridge extends Server { trace("<<< SERVER (" + serverVersion + ")", data); } + @Override + public void run() { + getTraceHandler().trace( + getName() + ": will forward to " + forwardToHost + ":" + + forwardToPort + " (" + + (forwardToSsl ? "SSL" : "plain text") + ")"); + super.run(); + } + /** * Trace the data with the given prefix. * @@ -305,7 +329,7 @@ public class ServerBridge extends Server { if (args.length > 6) { traceLevel = Integer.parseInt(args[i++]); } - int maxPrintSize = 1; + int maxPrintSize = 0; if (args.length > 7) { maxPrintSize = Integer.parseInt(args[i++]); } diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index dec8473..53606ed 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.Socket; +import java.net.UnknownHostException; import be.nikiroo.utils.Version; @@ -28,6 +29,11 @@ abstract public class ServerObject extends Server { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ServerObject(int port, boolean ssl) throws IOException { super(port, ssl); @@ -46,6 +52,11 @@ abstract public class ServerObject extends Server { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ServerObject(String name, int port, boolean ssl) throws IOException { super(name, port, ssl); diff --git a/src/be/nikiroo/utils/serial/server/ServerString.java b/src/be/nikiroo/utils/serial/server/ServerString.java index c19e0ae..3185c6f 100644 --- a/src/be/nikiroo/utils/serial/server/ServerString.java +++ b/src/be/nikiroo/utils/serial/server/ServerString.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.Socket; +import java.net.UnknownHostException; import be.nikiroo.utils.Version; @@ -28,6 +29,11 @@ abstract public class ServerString extends Server { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ServerString(int port, boolean ssl) throws IOException { super(port, ssl); @@ -46,6 +52,11 @@ abstract public class ServerString extends Server { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ServerString(String name, int port, boolean ssl) throws IOException { super(name, port, ssl); diff --git a/src/be/nikiroo/utils/test/SerialServerTest.java b/src/be/nikiroo/utils/test/SerialServerTest.java index 67ba161..a34d30e 100644 --- a/src/be/nikiroo/utils/test/SerialServerTest.java +++ b/src/be/nikiroo/utils/test/SerialServerTest.java @@ -1,5 +1,7 @@ package be.nikiroo.utils.test; +import java.net.URL; + import be.nikiroo.utils.Version; import be.nikiroo.utils.serial.server.ConnectActionClientObject; import be.nikiroo.utils.serial.server.ConnectActionClientString; @@ -44,6 +46,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); port = br.getPort(); assertEquals( @@ -95,7 +98,6 @@ class SerialServerTest extends TestLauncher { @Override protected void onError(Exception e) { - super.onError(e); err[0] = e; } }; @@ -107,6 +109,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); port = br.getPort(); br.start(); } @@ -159,7 +162,6 @@ class SerialServerTest extends TestLauncher { @Override protected void onError(Exception e) { - super.onError(e); err[0] = e; } }; @@ -171,6 +173,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); port = br.getPort(); br.start(); } @@ -224,7 +227,6 @@ class SerialServerTest extends TestLauncher { @Override protected void onError(Exception e) { - super.onError(e); err[0] = e; } }; @@ -236,6 +238,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); port = br.getPort(); br.start(); } @@ -309,11 +312,8 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl) { - @Override - protected void onError(Exception e) { - } - }; + br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); port = br.getPort(); br.start(); } @@ -364,7 +364,6 @@ class SerialServerTest extends TestLauncher { @Override protected void onError(Exception e) { - super.onError(e); err[0] = e; } }; @@ -376,6 +375,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); port = br.getPort(); br.start(); } @@ -428,7 +428,6 @@ class SerialServerTest extends TestLauncher { @Override protected void onError(Exception e) { - super.onError(e); err[0] = e; } }; @@ -440,6 +439,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); port = br.getPort(); br.start(); } @@ -474,6 +474,78 @@ class SerialServerTest extends TestLauncher { } }); + series.addTest(new TestCase("Object array of URLs " + 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 new Object[] { "ACK" }; + } + + @Override + protected void onError(Exception e) { + err[0] = e; + } + }; + + int port = server.getPort(); + + server.start(); + + ServerBridge br = null; + if (bridge) { + br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); + port = br.getPort(); + br.start(); + } + + try { + try { + new ConnectActionClientObject(null, port, ssl) { + @Override + public void action(Version serverVersion) + throws Exception { + recd[0] = send(new Object[] { + "key", + new URL( + "https://example.com/from_client"), + "https://example.com/from_client" }); + } + }.connect(); + } finally { + server.stop(); + } + } finally { + if (br != null) { + br.stop(); + } + } + + if (err[0] != null) { + fail("An exception was thrown: " + err[0].getMessage()); + } + + Object[] sento = (Object[]) (sent[0]); + Object[] recdo = (Object[]) (recd[0]); + + assertEquals("key", sento[0]); + assertEquals("https://example.com/from_client", + ((URL) sento[1]).toString()); + assertEquals("https://example.com/from_client", sento[2]); + assertEquals("ACK", recdo[0]); + } + }); + series.addTest(new TestCase("Multiple call from client " + ssls) { final Object[] sent = new Object[3]; final Object[] recd = new Object[3]; @@ -493,7 +565,6 @@ class SerialServerTest extends TestLauncher { @Override protected void onError(Exception e) { - super.onError(e); err[0] = e; } }; @@ -505,6 +576,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { br = new ServerBridge(0, ssl, "", port, ssl); + br.setTraceHandler(null); port = br.getPort(); br.start(); } diff --git a/src/be/nikiroo/utils/test/SerialTest.java b/src/be/nikiroo/utils/test/SerialTest.java index b203c2a..b272804 100644 --- a/src/be/nikiroo/utils/test/SerialTest.java +++ b/src/be/nikiroo/utils/test/SerialTest.java @@ -1,5 +1,7 @@ package be.nikiroo.utils.test; +import java.net.URL; + import be.nikiroo.utils.serial.Exporter; import be.nikiroo.utils.serial.Importer; @@ -28,6 +30,53 @@ class SerialTest extends TestLauncher { } }); + addTest(new TestCase("URL Import/Export") { + @Override + public void test() throws Exception { + URL data = new URL("https://fanfan.be/"); + String encoded = new Exporter().append(data).toString(false); + Object redata = new Importer().read(encoded).getValue(); + String reencoded = new Exporter().append(redata) + .toString(false); + + assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), + reencoded.replaceAll("@[0-9]*", "@REF")); + } + }); + + addTest(new TestCase("URL-String Import/Export") { + @Override + public void test() throws Exception { + String data = new URL("https://fanfan.be/").toString(); + String encoded = new Exporter().append(data).toString(false); + Object redata = new Importer().read(encoded).getValue(); + String reencoded = new Exporter().append(redata) + .toString(false); + + assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), + reencoded.replaceAll("@[0-9]*", "@REF")); + assertEquals(data, redata); + } + }); + + addTest(new TestCase("URL/URL-String arrays Import/Export") { + @Override + public void test() throws Exception { + final String url = "https://fanfan.be/"; + + Object[] data = new Object[] { new URL(url), url }; + String encoded = new Exporter().append(data).toString(false); + Object redata = new Importer().read(encoded).getValue(); + String reencoded = new Exporter().append(redata) + .toString(false); + + assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), + reencoded.replaceAll("@[0-9]*", "@REF")); + assertEquals(data[0], ((Object[]) redata)[0]); + assertEquals(data[1], ((Object[]) redata)[1]); + } + }); + addTest(new TestCase("Import/Export with nested objects") { @Override public void test() throws Exception { diff --git a/src/be/nikiroo/utils/ui/ProgressBar.java b/src/be/nikiroo/utils/ui/ProgressBar.java index 2c91c70..219cde9 100644 --- a/src/be/nikiroo/utils/ui/ProgressBar.java +++ b/src/be/nikiroo/utils/ui/ProgressBar.java @@ -28,6 +28,7 @@ public class ProgressBar extends JPanel { private List actionListeners; private List updateListeners; private Progress pg; + private Object lock = new Object(); public ProgressBar() { bars = new HashMap(); @@ -68,6 +69,7 @@ public class ProgressBar extends JPanel { bar.setValue(pg.getProgress()); bar.setString(pg.getName()); + synchronized (lock) { for (Progress pgChild : getChildrenAsOrderedList(pg)) { JProgressBar barChild = bars .get(pgChild); @@ -83,12 +85,15 @@ public class ProgressBar extends JPanel { barChild.setValue(pgChild.getProgress()); barChild.setString(pgChild.getName()); } - + if (ProgressBar.this.pg == null) { bars.clear(); } else { bars = newBars; - + } + } + + if (ProgressBar.this.pg != null) { if (pg.isDone()) { pg.removeProgressListener(l); for (ActionListener listener : actionListeners) { @@ -138,33 +143,38 @@ public class ProgressBar extends JPanel { // only named ones private List getChildrenAsOrderedList(Progress pg) { List children = new ArrayList(); - for (Progress child : pg.getChildren()) { + + synchronized (lock) { + for (Progress child : pg.getChildren()) { if (child.getName() != null && !child.getName().isEmpty()) { children.add(child); } children.addAll(getChildrenAsOrderedList(child)); } - + } + return children; } private void update() { - invalidate(); - removeAll(); - - if (pg != null) { - setLayout(new GridLayout(bars.size(), 1)); - add(bars.get(pg), 0); - for (Progress child : getChildrenAsOrderedList(pg)) { - JProgressBar jbar = bars.get(child); - if (jbar != null) { - add(jbar); + synchronized (lock) { + invalidate(); + removeAll(); + + if (pg != null) { + setLayout(new GridLayout(bars.size(), 1)); + add(bars.get(pg), 0); + for (Progress child : getChildrenAsOrderedList(pg)) { + JProgressBar jbar = bars.get(child); + if (jbar != null) { + add(jbar); + } } } - } - validate(); - repaint(); + validate(); + repaint(); + } for (ActionListener listener : updateListeners) { listener.actionPerformed(new ActionEvent(this, 0, "update"));