aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2012-09-28 20:14:48 +0300
committerArtur Signell <artur@vaadin.com>2012-09-28 20:14:52 +0300
commit41f2e9e753bb12372160a8cb2e8d2f68d7318d96 (patch)
tree247095ab903542fc41fa591847581f0a74d7c2f5
parentca59c93fb4dbc112e5deb5f5a4b10c0c730a4be4 (diff)
downloadvaadin-framework-41f2e9e753bb12372160a8cb2e8d2f68d7318d96.tar.gz
vaadin-framework-41f2e9e753bb12372160a8cb2e8d2f68d7318d96.zip
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
-rw-r--r--server/src/com/vaadin/server/AbstractCommunicationManager.java2
-rw-r--r--server/src/com/vaadin/ui/ConnectorTracker.java45
-rw-r--r--uitest/src/com/vaadin/tests/components/ui/UISerialization.html13
-rw-r--r--uitest/src/com/vaadin/tests/components/ui/UISerialization.java25
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<ClientConnector, Object> diffStates = new HashMap<ClientConnector, Object>();
+ private transient Map<ClientConnector, JSONObject> diffStates = new HashMap<ClientConnector, JSONObject>();
/**
* 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<ClientConnector, String> stringDiffStates = new HashMap<ClientConnector, String>(
+ 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<ClientConnector, JSONObject>();
+ @SuppressWarnings("unchecked")
+ HashMap<ClientConnector, String> stringDiffStates = (HashMap<ClientConnector, String>) in
+ .readObject();
+ diffStates = new HashMap<ClientConnector, JSONObject>();
+ 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 @@
</thead><tbody>
<tr>
<td>open</td>
- <td>/run/com.vaadin.tests.components.ui.UISerialization?debug</td>
+ <td>/run/com.vaadin.tests.components.ui.UISerialization?restartApplication</td>
<td></td>
</tr>
<tr>
@@ -24,9 +24,18 @@
<tr>
<td>assertText</td>
<td>vaadin=runcomvaadintestscomponentsuiUISerialization::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VLabel[0]</td>
+ <td>3. Diff states match, size: *</td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestscomponentsuiUISerialization::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VLabel[0]</td>
+ <td>2. Deserialized UI in *ms</td>
+</tr>
+<tr>
+ <td>assertText</td>
+ <td>vaadin=runcomvaadintestscomponentsuiUISerialization::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[0]/VVerticalLayout[0]/VOrderedLayout$Slot[2]/VLabel[0]</td>
<td>1. Serialized UI in *ms into * bytes</td>
</tr>
-
</tbody></table>
</body>
</html>
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