From 41f2e9e753bb12372160a8cb2e8d2f68d7318d96 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 28 Sep 2012 20:14:48 +0300 Subject: [PATCH] Serialize and deserialize diff state manually (#9717, #9767) Extended UISerialization test to both serialize and deserialize and also validate ConnectorTracker (de)serialization Change-Id: Ifb8228bd56ec3635e4267e78160eef14dd9ff318 --- .../server/AbstractCommunicationManager.java | 2 +- .../src/com/vaadin/ui/ConnectorTracker.java | 45 +++++++++++++++++-- .../tests/components/ui/UISerialization.html | 13 +++++- .../tests/components/ui/UISerialization.java | 25 +++++++++++ 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/server/src/com/vaadin/server/AbstractCommunicationManager.java b/server/src/com/vaadin/server/AbstractCommunicationManager.java index 8ea0b88b74..b7b97cbefd 100644 --- a/server/src/com/vaadin/server/AbstractCommunicationManager.java +++ b/server/src/com/vaadin/server/AbstractCommunicationManager.java @@ -1279,7 +1279,7 @@ public abstract class AbstractCommunicationManager implements Serializable { stateType, uI.getConnectorTracker()); if (supportsDiffState) { connectorTracker.setDiffState(connector, - encodeResult.getEncodedValue()); + (JSONObject) encodeResult.getEncodedValue()); } return (JSONObject) encodeResult.getDiff(); } diff --git a/server/src/com/vaadin/ui/ConnectorTracker.java b/server/src/com/vaadin/ui/ConnectorTracker.java index 3fb83eeb92..ddb02129d4 100644 --- a/server/src/com/vaadin/ui/ConnectorTracker.java +++ b/server/src/com/vaadin/ui/ConnectorTracker.java @@ -15,6 +15,7 @@ */ package com.vaadin.ui; +import java.io.IOException; import java.io.Serializable; import java.util.Collection; import java.util.HashMap; @@ -25,6 +26,9 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import org.json.JSONException; +import org.json.JSONObject; + import com.vaadin.server.AbstractClientConnector; import com.vaadin.server.AbstractCommunicationManager; import com.vaadin.server.ClientConnector; @@ -59,7 +63,7 @@ public class ConnectorTracker implements Serializable { private boolean writingResponse = false; private UI uI; - private transient Map diffStates = new HashMap(); + private transient Map diffStates = new HashMap(); /** * Gets a logger for this class @@ -409,11 +413,11 @@ public class ConnectorTracker implements Serializable { return dirtyConnectors; } - public Object getDiffState(ClientConnector connector) { + public JSONObject getDiffState(ClientConnector connector) { return diffStates.get(connector); } - public void setDiffState(ClientConnector connector, Object diffState) { + public void setDiffState(ClientConnector connector, JSONObject diffState) { diffStates.put(connector, diffState); } @@ -457,4 +461,39 @@ public class ConnectorTracker implements Serializable { } this.writingResponse = writingResponse; } + + /* Special serialization to JSONObjects which are not serializable */ + private void writeObject(java.io.ObjectOutputStream out) throws IOException { + out.defaultWriteObject(); + // Convert JSONObjects in diff state to String representation as + // JSONObject is not serializable + HashMap stringDiffStates = new HashMap( + diffStates.size()); + for (ClientConnector key : diffStates.keySet()) { + stringDiffStates.put(key, diffStates.get(key).toString()); + } + out.writeObject(stringDiffStates); + }; + + /* Special serialization to JSONObjects which are not serializable */ + private void readObject(java.io.ObjectInputStream in) throws IOException, + ClassNotFoundException { + in.defaultReadObject(); + + // Read String versions of JSONObjects and parse into JSONObjects as + // JSONObject is not serializable + diffStates = new HashMap(); + @SuppressWarnings("unchecked") + HashMap stringDiffStates = (HashMap) in + .readObject(); + diffStates = new HashMap(); + for (ClientConnector key : stringDiffStates.keySet()) { + try { + diffStates.put(key, new JSONObject(stringDiffStates.get(key))); + } catch (JSONException e) { + throw new IOException(e); + } + } + + } } diff --git a/uitest/src/com/vaadin/tests/components/ui/UISerialization.html b/uitest/src/com/vaadin/tests/components/ui/UISerialization.html index 2e62166cb8..1eb6dffcc0 100644 --- a/uitest/src/com/vaadin/tests/components/ui/UISerialization.html +++ b/uitest/src/com/vaadin/tests/components/ui/UISerialization.html @@ -13,7 +13,7 @@ open - /run/com.vaadin.tests.components.ui.UISerialization?debug + /run/com.vaadin.tests.components.ui.UISerialization?restartApplication @@ -24,9 +24,18 @@ assertText vaadin=runcomvaadintestscomponentsuiUISerialization::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VLabel[0] + 3. Diff states match, size: * + + + assertText + vaadin=runcomvaadintestscomponentsuiUISerialization::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VLabel[0] + 2. Deserialized UI in *ms + + + assertText + vaadin=runcomvaadintestscomponentsuiUISerialization::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VVerticalLayout[0]/VOrderedLayout$Slot[2]/VLabel[0] 1. Serialized UI in *ms into * bytes - diff --git a/uitest/src/com/vaadin/tests/components/ui/UISerialization.java b/uitest/src/com/vaadin/tests/components/ui/UISerialization.java index ebb3ff6333..927d00a388 100644 --- a/uitest/src/com/vaadin/tests/components/ui/UISerialization.java +++ b/uitest/src/com/vaadin/tests/components/ui/UISerialization.java @@ -15,8 +15,10 @@ */ package com.vaadin.tests.components.ui; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Date; @@ -69,6 +71,18 @@ public class UISerialization extends AbstractTestUI { long elapsed = new Date().getTime() - d.getTime(); log.log("Serialized UI in " + elapsed + "ms into " + result.length + " bytes"); + Object diffStateBefore = getConnectorTracker().getDiffState( + UISerialization.this); + UISerialization app = (UISerialization) deserialize(result); + log.log("Deserialized UI in " + elapsed + "ms"); + Object diffStateAfter = getConnectorTracker().getDiffState( + UISerialization.this); + if (diffStateBefore.equals(diffStateAfter)) { + log.log("Diff states match, size: " + + diffStateBefore.toString().length()); + } else { + log.log("Diff states do not match"); + } } })); @@ -112,6 +126,17 @@ public class UISerialization extends AbstractTestUI { } } + protected Object deserialize(byte[] result) { + ByteArrayInputStream is = new ByteArrayInputStream(result); + ObjectInputStream ois; + try { + ois = new ObjectInputStream(is); + return ois.readObject(); + } catch (Exception e) { + throw new RuntimeException("Deserialization failed", e); + } + } + @Override protected String getTestDescription() { // TODO Auto-generated method stub -- 2.39.5