You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UISerialization.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.ui;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.ObjectInputStream;
  21. import java.io.ObjectOutputStream;
  22. import java.io.PrintWriter;
  23. import java.io.Serializable;
  24. import java.io.StringWriter;
  25. import java.util.Date;
  26. import com.vaadin.server.VaadinRequest;
  27. import com.vaadin.shared.ui.ContentMode;
  28. import com.vaadin.tests.components.AbstractReindeerTestUI;
  29. import com.vaadin.tests.util.Log;
  30. import com.vaadin.ui.Button;
  31. import com.vaadin.ui.Button.ClickEvent;
  32. import com.vaadin.ui.Button.ClickListener;
  33. import com.vaadin.ui.Label;
  34. public class UISerialization extends AbstractReindeerTestUI {
  35. private Log log = new Log(5);
  36. @Override
  37. protected void setup(VaadinRequest request) {
  38. addComponent(log);
  39. addComponent(new Button("Serialize UI", new ClickListener() {
  40. @Override
  41. public void buttonClick(ClickEvent event) {
  42. Date d = new Date();
  43. try {
  44. byte[] result = serialize(UISerialization.this);
  45. long elapsed = new Date().getTime() - d.getTime();
  46. log.log("Serialized UI in " + elapsed + "ms into "
  47. + result.length + " bytes");
  48. Object diffStateBefore = getConnectorTracker()
  49. .getDiffState(UISerialization.this);
  50. UISerialization app = (UISerialization) deserialize(result);
  51. log.log("Deserialized UI in " + elapsed + "ms");
  52. Object diffStateAfter = getConnectorTracker()
  53. .getDiffState(UISerialization.this);
  54. if (diffStateBefore.equals(diffStateAfter)) {
  55. log.log("Diff states match, size: "
  56. + diffStateBefore.toString().length());
  57. } else {
  58. log.log("Diff states do not match");
  59. }
  60. } catch (Exception e) {
  61. log.log("Exception caught: " + e.getMessage());
  62. StringWriter sw = new StringWriter();
  63. e.printStackTrace(new PrintWriter(sw));
  64. addComponent(
  65. new Label(sw.toString(), ContentMode.PREFORMATTED));
  66. }
  67. }
  68. }));
  69. }
  70. protected void serializeInstance(Class<?> cls)
  71. throws InstantiationException, IllegalAccessException, IOException {
  72. serialize((Serializable) cls.newInstance());
  73. }
  74. protected byte[] serialize(Serializable serializable) throws IOException {
  75. ByteArrayOutputStream os = new ByteArrayOutputStream();
  76. ObjectOutputStream oos;
  77. oos = new ObjectOutputStream(os);
  78. oos.writeObject(serializable);
  79. return os.toByteArray();
  80. }
  81. protected Object deserialize(byte[] result) {
  82. ByteArrayInputStream is = new ByteArrayInputStream(result);
  83. ObjectInputStream ois;
  84. try {
  85. ois = new ObjectInputStream(is);
  86. return ois.readObject();
  87. } catch (Exception e) {
  88. throw new RuntimeException("Deserialization failed", e);
  89. }
  90. }
  91. @Override
  92. protected String getTestDescription() {
  93. // TODO Auto-generated method stub
  94. return null;
  95. }
  96. @Override
  97. protected Integer getTicketNumber() {
  98. // TODO Auto-generated method stub
  99. return null;
  100. }
  101. }