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.

SerializationTest.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.vaadin.v7.tests.server;
  2. import static org.junit.Assert.assertNotNull;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.Serializable;
  9. import org.junit.Test;
  10. import com.vaadin.server.VaadinSession;
  11. import com.vaadin.v7.data.Item;
  12. import com.vaadin.v7.data.Property;
  13. import com.vaadin.v7.data.util.IndexedContainer;
  14. import com.vaadin.v7.data.util.MethodProperty;
  15. import com.vaadin.v7.data.validator.RegexpValidator;
  16. public class SerializationTest {
  17. @Test
  18. public void testValidators() throws Exception {
  19. RegexpValidator validator = new RegexpValidator(".*", "Error");
  20. validator.validate("aaa");
  21. RegexpValidator validator2 = serializeAndDeserialize(validator);
  22. validator2.validate("aaa");
  23. }
  24. @Test
  25. public void testIndedexContainerItemIds() throws Exception {
  26. IndexedContainer ic = new IndexedContainer();
  27. ic.addContainerProperty("prop1", String.class, null);
  28. Object id = ic.addItem();
  29. ic.getItem(id).getItemProperty("prop1").setValue("1");
  30. Item item2 = ic.addItem("item2");
  31. item2.getItemProperty("prop1").setValue("2");
  32. serializeAndDeserialize(ic);
  33. }
  34. @Test
  35. public void testMethodPropertyGetter() throws Exception {
  36. MethodProperty<?> mp = new MethodProperty<Object>(new Data(),
  37. "dummyGetter");
  38. serializeAndDeserialize(mp);
  39. }
  40. @Test
  41. public void testMethodPropertyGetterAndSetter() throws Exception {
  42. MethodProperty<?> mp = new MethodProperty<Object>(new Data(),
  43. "dummyGetterAndSetter");
  44. serializeAndDeserialize(mp);
  45. }
  46. @Test
  47. public void testMethodPropertyInt() throws Exception {
  48. MethodProperty<?> mp = new MethodProperty<Object>(new Data(),
  49. "dummyInt");
  50. serializeAndDeserialize(mp);
  51. }
  52. @Test
  53. public void testVaadinSession() throws Exception {
  54. VaadinSession session = new VaadinSession(null);
  55. session = serializeAndDeserialize(session);
  56. assertNotNull(
  57. "Pending access queue was not recreated after deserialization",
  58. session.getPendingAccessQueue());
  59. }
  60. private static <S extends Serializable> S serializeAndDeserialize(S s)
  61. throws IOException, ClassNotFoundException {
  62. // Serialize and deserialize
  63. ByteArrayOutputStream bs = new ByteArrayOutputStream();
  64. ObjectOutputStream out = new ObjectOutputStream(bs);
  65. out.writeObject(s);
  66. byte[] data = bs.toByteArray();
  67. ObjectInputStream in = new ObjectInputStream(
  68. new ByteArrayInputStream(data));
  69. @SuppressWarnings("unchecked")
  70. S s2 = (S) in.readObject();
  71. // using special toString(Object) method to avoid calling
  72. // Property.toString(), which will be temporarily disabled
  73. // TODO This is hilariously broken (#12723)
  74. if (s.equals(s2)) {
  75. System.out.println(toString(s) + " equals " + toString(s2));
  76. } else {
  77. System.out.println(toString(s) + " does NOT equal " + toString(s2));
  78. }
  79. return s2;
  80. }
  81. private static String toString(Object o) {
  82. if (o instanceof Property) {
  83. return String.valueOf(((Property<?>) o).getValue());
  84. } else {
  85. return String.valueOf(o);
  86. }
  87. }
  88. public static class Data implements Serializable {
  89. private String dummyGetter;
  90. private String dummyGetterAndSetter;
  91. private int dummyInt;
  92. public String getDummyGetterAndSetter() {
  93. return dummyGetterAndSetter;
  94. }
  95. public void setDummyGetterAndSetter(String dummyGetterAndSetter) {
  96. this.dummyGetterAndSetter = dummyGetterAndSetter;
  97. }
  98. public int getDummyInt() {
  99. return dummyInt;
  100. }
  101. public void setDummyInt(int dummyInt) {
  102. this.dummyInt = dummyInt;
  103. }
  104. public String getDummyGetter() {
  105. return dummyGetter;
  106. }
  107. }
  108. }