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 package com.cedarsoft.serialization;
33
34 import org.junit.*;
35 import org.xml.sax.SAXException;
36
37 import javax.annotation.Nonnull;
38 import java.io.ByteArrayInputStream;
39 import java.io.ByteArrayOutputStream;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import static org.junit.Assert.*;
45
46
47
48
49
50
51
52 @Deprecated
53 public abstract class AbstractSerializerMultiTest<T> {
54 @Test
55 public void testSerializer() throws Exception {
56 Serializer<T> serializer = getSerializer();
57
58 Iterable<? extends T> objectsToSerialize = createObjectsToSerialize();
59
60
61 List<? extends byte[]> serialized = serialize( serializer, objectsToSerialize );
62
63
64 verifySerialized( serialized );
65
66
67 List<T> deserialized = new ArrayList<T>();
68 for ( byte[] currentSerialized : serialized ) {
69 deserialized.add( serializer.deserialize( new ByteArrayInputStream( currentSerialized ) ) );
70 }
71
72 verifyDeserialized( deserialized );
73 }
74
75 @Nonnull
76 private List<? extends byte[]> serialize( @Nonnull Serializer<T> serializer, @Nonnull Iterable<? extends T> objectsToSerialize ) throws IOException {
77 List<byte[]> serialized = new ArrayList<byte[]>();
78
79 int index = 0;
80 for ( T objectToSerialize : objectsToSerialize ) {
81 try {
82 serialized.add( serialize( serializer, objectToSerialize ) );
83 index++;
84 } catch ( IOException e ) {
85 throw new IOException( "Serialization failed for (" + index + ") <" + objectsToSerialize + ">", e );
86 }
87 }
88 return serialized;
89 }
90
91 @Nonnull
92 protected byte[] serialize( @Nonnull Serializer<T> serializer, @Nonnull T objectToSerialize ) throws IOException {
93 ByteArrayOutputStream out = new ByteArrayOutputStream();
94 serializer.serialize( objectToSerialize, out );
95 return out.toByteArray();
96 }
97
98
99
100
101
102
103 @Nonnull
104 protected abstract Serializer<T> getSerializer() throws Exception;
105
106
107
108
109
110
111
112
113 protected abstract void verifySerialized( @Nonnull List<? extends byte[]> serialized ) throws Exception;
114
115
116
117
118
119
120 @Nonnull
121 protected abstract Iterable<? extends T> createObjectsToSerialize() throws Exception;
122
123
124
125
126
127
128
129 protected void verifyDeserialized( @Nonnull List<? extends T> deserialized ) throws Exception {
130 int index = 0;
131 for ( T currentExpected : createObjectsToSerialize() ) {
132 assertEquals( deserialized.get( index ), currentExpected );
133 index++;
134 }
135 }
136 }