1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.ObjectInputStream;
31 import java.io.ObjectOutputStream;
32 import java.io.OutputStream;
33 import java.io.Serializable;
34
35 import org.junit.jupiter.api.Test;
36
37
38
39
40
41
42
43
44
45
46
47 public abstract class AbstractObjectTest extends BulkTest {
48
49
50 public static final int COLLECTIONS_MAJOR_VERSION = 4;
51
52
53
54
55
56
57 public AbstractObjectTest(final String testName) {
58 super(testName);
59 }
60
61 protected String getCanonicalEmptyCollectionName(final Object object) {
62 final StringBuilder retval = new StringBuilder();
63 retval.append(TEST_DATA_PATH);
64 String colName = object.getClass().getName();
65 colName = colName.substring(colName.lastIndexOf(".") + 1);
66 retval.append(colName);
67 retval.append(".emptyCollection.version");
68 retval.append(getCompatibilityVersion());
69 retval.append(".obj");
70 return retval.toString();
71 }
72
73 protected String getCanonicalFullCollectionName(final Object object) {
74 final StringBuilder retval = new StringBuilder();
75 retval.append(TEST_DATA_PATH);
76 String colName = object.getClass().getName();
77 colName = colName.substring(colName.lastIndexOf(".") + 1);
78 retval.append(colName);
79 retval.append(".fullCollection.version");
80 retval.append(getCompatibilityVersion());
81 retval.append(".obj");
82 return retval.toString();
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public String getCompatibilityVersion() {
102 return "4";
103 }
104
105
106
107
108
109 public boolean isEqualsCheckable() {
110 return true;
111 }
112
113
114
115
116
117 public boolean isTestSerialization() {
118 return true;
119 }
120
121
122
123
124
125
126 public abstract Object makeObject();
127
128
129
130
131
132
133
134
135
136
137 protected Object readExternalFormFromBytes(final byte[] b) throws IOException, ClassNotFoundException {
138 final ByteArrayInputStream stream = new ByteArrayInputStream(b);
139 return readExternalFormFromStream(stream);
140 }
141
142
143
144
145
146
147
148
149
150
151
152 protected Object readExternalFormFromDisk(final String path) throws IOException, ClassNotFoundException {
153 try (FileInputStream stream = new FileInputStream(path)) {
154 return readExternalFormFromStream(stream);
155 }
156 }
157
158
159 private Object readExternalFormFromStream(final InputStream stream) throws IOException, ClassNotFoundException {
160 final ObjectInputStream oStream = new ObjectInputStream(stream);
161 return oStream.readObject();
162 }
163
164 protected Object serializeDeserialize(final Object obj) throws Exception {
165 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
166 final ObjectOutputStream out = new ObjectOutputStream(buffer);
167 out.writeObject(obj);
168 out.close();
169
170 final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
171 final Object dest = in.readObject();
172 in.close();
173
174 return dest;
175 }
176
177 protected boolean skipSerializedCanonicalTests() {
178 return Boolean.getBoolean("org.apache.commons.collections:with-clover");
179 }
180
181
182
183
184
185
186
187
188 public boolean supportsEmptyCollections() {
189 return true;
190 }
191
192
193
194
195
196
197
198
199 public boolean supportsFullCollections() {
200 return true;
201 }
202
203
204
205
206
207 @Test
208 public void testCanonicalEmptyCollectionExists() {
209 if (supportsEmptyCollections() && isTestSerialization() && !skipSerializedCanonicalTests()) {
210 final Object object = makeObject();
211 if (object instanceof Serializable) {
212 final String name = getCanonicalEmptyCollectionName(object);
213 assertTrue(
214 new File(name).exists(),
215 "Canonical empty collection (" + name + ") is not in SCM");
216 }
217 }
218 }
219
220
221
222
223
224 @Test
225 public void testCanonicalFullCollectionExists() {
226 if (supportsFullCollections() && isTestSerialization() && !skipSerializedCanonicalTests()) {
227 final Object object = makeObject();
228 if (object instanceof Serializable) {
229 final String name = getCanonicalFullCollectionName(object);
230 assertTrue(
231 new File(name).exists(),
232 "Canonical full collection (" + name + ") is not in SCM");
233 }
234 }
235 }
236
237 @Test
238 public void testEqualsNull() {
239 final Object obj = makeObject();
240 assertFalse(obj.equals(null));
241 }
242
243 @Test
244 public void testObjectEqualsSelf() {
245 final Object obj = makeObject();
246 assertEquals(obj, obj, "A Object should equal itself");
247 }
248
249 @Test
250 public void testObjectHashCodeEqualsContract() {
251 final Object obj1 = makeObject();
252 if (obj1.equals(obj1)) {
253 assertEquals(
254 obj1.hashCode(), obj1.hashCode(),
255 "[1] When two objects are equal, their hashCodes should be also.");
256 }
257 final Object obj2 = makeObject();
258 if (obj1.equals(obj2)) {
259 assertEquals(
260 obj1.hashCode(), obj2.hashCode(),
261 "[2] When two objects are equal, their hashCodes should be also.");
262 assertEquals(obj2, obj1, "When obj1.equals(obj2) is true, then obj2.equals(obj1) should also be true");
263 }
264 }
265
266 @Test
267 public void testObjectHashCodeEqualsSelfHashCode() {
268 final Object obj = makeObject();
269 assertEquals(obj.hashCode(), obj.hashCode(), "hashCode should be repeatable");
270 }
271
272 @Test
273 public void testSerializeDeserializeThenCompare() throws Exception {
274 final Object obj = makeObject();
275 if (obj instanceof Serializable && isTestSerialization()) {
276 final Object dest = serializeDeserialize(obj);
277 if (isEqualsCheckable()) {
278 assertEquals(obj, dest, "obj != deserialize(serialize(obj))");
279 }
280 }
281 }
282
283
284
285
286
287
288
289
290
291 @Test
292 public void testSimpleSerialization() throws Exception {
293 final Object o = makeObject();
294 if (o instanceof Serializable && isTestSerialization()) {
295 final byte[] object = writeExternalFormToBytes((Serializable) o);
296 readExternalFormFromBytes(object);
297 }
298 }
299
300
301
302
303
304
305
306
307
308 protected byte[] writeExternalFormToBytes(final Serializable o) throws IOException {
309 final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
310 writeExternalFormToStream(o, byteStream);
311 return byteStream.toByteArray();
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325
326 protected void writeExternalFormToDisk(final Serializable o, final String path) throws IOException {
327 try (FileOutputStream fileStream = new FileOutputStream(path)) {
328 writeExternalFormToStream(o, fileStream);
329 }
330 }
331
332 private void writeExternalFormToStream(final Serializable o, final OutputStream stream) throws IOException {
333 final ObjectOutputStream oStream = new ObjectOutputStream(stream);
334 oStream.writeObject(o);
335 }
336
337 }