]> source.dussan.org Git - vaadin-framework.git/commitdiff
Serialize and deserialize diff state manually (#9717, #9767) 16/16/1
authorArtur Signell <artur@vaadin.com>
Fri, 28 Sep 2012 17:14:48 +0000 (20:14 +0300)
committerArtur Signell <artur@vaadin.com>
Fri, 28 Sep 2012 17:14:52 +0000 (20:14 +0300)
Extended UISerialization test to both serialize and deserialize and also validate ConnectorTracker (de)serialization

Change-Id: Ifb8228bd56ec3635e4267e78160eef14dd9ff318

server/src/com/vaadin/server/AbstractCommunicationManager.java
server/src/com/vaadin/ui/ConnectorTracker.java
uitest/src/com/vaadin/tests/components/ui/UISerialization.html
uitest/src/com/vaadin/tests/components/ui/UISerialization.java

index 8ea0b88b747350124c6b8524cdd55ed4fed4405c..b7b97cbefdc9eaf8eab0b595f17e0f0acb5f5e80 100644 (file)
@@ -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();
     }
index 3fb83eeb9268e2e54804a9dc60c57866baaee74f..ddb02129d40747993c13591f853a7a8fa5397b99 100644 (file)
@@ -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);
+            }
+        }
+
+    }
 }
index 2e62166cb8eb66fc21edccf538a6959d9f5dca8e..1eb6dffcc093b37e17b6657d44aa5210f62ce44c 100644 (file)
@@ -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>
 <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>
index ebb3ff63333bf3319986d3b8711db569db300ccf..927d00a3887d800859af1d10a5f9654528681999 100644 (file)
  */
 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