*/
package com.vaadin.ui;
+import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
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;
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
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);
}
}
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);
+ }
+ }
+
+ }
}
</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>
*/
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;
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");
+ }
}
}));
}
}
+ 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