aboutsummaryrefslogtreecommitdiffstats
path: root/compatibility-server/src/test/java/com/vaadin/v7/tests/server/SerializationTest.java
blob: 2130fc493af02e82f7f45411490e53615fda2623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.vaadin.v7.tests.server;

import static org.junit.Assert.assertNotNull;

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 org.junit.Test;

import com.vaadin.server.VaadinSession;
import com.vaadin.v7.data.Item;
import com.vaadin.v7.data.Property;
import com.vaadin.v7.data.util.IndexedContainer;
import com.vaadin.v7.data.util.MethodProperty;
import com.vaadin.v7.data.validator.RegexpValidator;
import com.vaadin.v7.ui.Form;

public class SerializationTest {

    @Test
    public void testValidators() throws Exception {
        RegexpValidator validator = new RegexpValidator(".*", "Error");
        validator.validate("aaa");
        RegexpValidator validator2 = serializeAndDeserialize(validator);
        validator2.validate("aaa");
    }

    @Test
    public void testForm() throws Exception {
        Form f = new Form();
        String propertyId = "My property";
        f.addItemProperty(propertyId,
                new MethodProperty<Object>(new Data(), "dummyGetterAndSetter"));
        f.replaceWithSelect(propertyId, new Object[] { "a", "b", null },
                new String[] { "Item a", "ITem b", "Null item" });

        serializeAndDeserialize(f);

    }

    @Test
    public void testIndedexContainerItemIds() throws Exception {
        IndexedContainer ic = new IndexedContainer();
        ic.addContainerProperty("prop1", String.class, null);
        Object id = ic.addItem();
        ic.getItem(id).getItemProperty("prop1").setValue("1");

        Item item2 = ic.addItem("item2");
        item2.getItemProperty("prop1").setValue("2");

        serializeAndDeserialize(ic);
    }

    @Test
    public void testMethodPropertyGetter() throws Exception {
        MethodProperty<?> mp = new MethodProperty<Object>(new Data(),
                "dummyGetter");
        serializeAndDeserialize(mp);
    }

    @Test
    public void testMethodPropertyGetterAndSetter() throws Exception {
        MethodProperty<?> mp = new MethodProperty<Object>(new Data(),
                "dummyGetterAndSetter");
        serializeAndDeserialize(mp);
    }

    @Test
    public void testMethodPropertyInt() throws Exception {
        MethodProperty<?> mp = new MethodProperty<Object>(new Data(),
                "dummyInt");
        serializeAndDeserialize(mp);
    }

    @Test
    public void testVaadinSession() throws Exception {
        VaadinSession session = new VaadinSession(null);

        session = serializeAndDeserialize(session);

        assertNotNull(
                "Pending access queue was not recreated after deserialization",
                session.getPendingAccessQueue());
    }

    private static <S extends Serializable> S serializeAndDeserialize(S s)
            throws IOException, ClassNotFoundException {
        // Serialize and deserialize

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bs);
        out.writeObject(s);
        byte[] data = bs.toByteArray();
        ObjectInputStream in = new ObjectInputStream(
                new ByteArrayInputStream(data));
        @SuppressWarnings("unchecked")
        S s2 = (S) in.readObject();

        // using special toString(Object) method to avoid calling
        // Property.toString(), which will be temporarily disabled
        // TODO This is hilariously broken (#12723)
        if (s.equals(s2)) {
            System.out.println(toString(s) + " equals " + toString(s2));
        } else {
            System.out.println(toString(s) + " does NOT equal " + toString(s2));
        }

        return s2;
    }

    private static String toString(Object o) {
        if (o instanceof Property) {
            return String.valueOf(((Property<?>) o).getValue());
        } else {
            return String.valueOf(o);
        }
    }

    public static class Data implements Serializable {
        private String dummyGetter;
        private String dummyGetterAndSetter;
        private int dummyInt;

        public String getDummyGetterAndSetter() {
            return dummyGetterAndSetter;
        }

        public void setDummyGetterAndSetter(String dummyGetterAndSetter) {
            this.dummyGetterAndSetter = dummyGetterAndSetter;
        }

        public int getDummyInt() {
            return dummyInt;
        }

        public void setDummyInt(int dummyInt) {
            this.dummyInt = dummyInt;
        }

        public String getDummyGetter() {
            return dummyGetter;
        }
    }
}