Skip to content

Commit 5fb51d8

Browse files
javier-godoymlopezFC
authored andcommitted
test: add test for TerminalHistory serialization
1 parent e278fb4 commit 5fb51d8

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

src/test/java/com/flowingcode/vaadin/addons/xterm/test/SerializationTest.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.xterm.test;
2121

22+
import static org.hamcrest.Matchers.is;
23+
import static org.hamcrest.Matchers.notNullValue;
24+
import static org.junit.Assert.assertThat;
25+
import com.flowingcode.vaadin.addons.xterm.TerminalHistory;
2226
import com.flowingcode.vaadin.addons.xterm.XTerm;
2327
import java.io.ByteArrayInputStream;
2428
import java.io.ByteArrayOutputStream;
2529
import java.io.IOException;
2630
import java.io.ObjectInputStream;
2731
import java.io.ObjectOutputStream;
32+
import java.lang.reflect.Method;
33+
import java.util.ListIterator;
34+
import java.util.stream.IntStream;
2835
import org.junit.Test;
2936

3037
public class SerializationTest {
3138

32-
private void testSerializationOf(Object obj) throws IOException, ClassNotFoundException {
39+
private <T> T shake(T obj) throws IOException, ClassNotFoundException {
3340
ByteArrayOutputStream baos = new ByteArrayOutputStream();
3441

3542
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
@@ -40,10 +47,56 @@ private void testSerializationOf(Object obj) throws IOException, ClassNotFoundEx
4047
new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
4148
obj.getClass().cast(in.readObject());
4249
}
50+
51+
return obj;
4352
}
4453

4554
@Test
4655
public void testSerialization() throws ClassNotFoundException, IOException {
47-
testSerializationOf(new XTerm());
56+
shake(new XTerm());
57+
}
58+
59+
@SuppressWarnings("serial")
60+
private final static class TestTerminalHistory extends TerminalHistory {
61+
public TestTerminalHistory(XTerm terminal) {
62+
super(terminal);
63+
}
64+
65+
@SuppressWarnings("unchecked")
66+
public ListIterator<String> listIterator() {
67+
try {
68+
Method method = TerminalHistory.class.getDeclaredMethod("listIterator");
69+
method.setAccessible(true);
70+
return (ListIterator<String>) method.invoke(this);
71+
} catch (Exception e) {
72+
throw new RuntimeException(e);
73+
}
74+
}
4875
}
76+
77+
@Test
78+
public void testTerminalHistorySerialization() throws ClassNotFoundException, IOException {
79+
// prepare
80+
TestTerminalHistory history = new TestTerminalHistory(new XTerm());
81+
82+
assertThat(history.listIterator(), is(notNullValue()));
83+
assertThat(history.listIterator().nextIndex(), is(0));
84+
85+
shake(history);
86+
assertThat(history.listIterator(), is(notNullValue()));
87+
assertThat(history.listIterator().previousIndex(), is(-1));
88+
89+
int n = 5;
90+
IntStream.range(0, n).mapToObj(Integer::toString).forEach(history::add);
91+
assertThat(history.listIterator().nextIndex(), is(n));
92+
93+
history.listIterator().previous();
94+
history.listIterator().previous();
95+
assertThat(history.listIterator().nextIndex(), is(n - 2));
96+
97+
// assert
98+
shake(history);
99+
assertThat(history.listIterator().nextIndex(), is(n - 2));
100+
}
101+
49102
}

0 commit comments

Comments
 (0)