1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutputStream;
24 import java.io.Serializable;
25 import java.util.Locale;
26 import java.util.TimeZone;
27
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31 import org.joda.time.chrono.BuddhistChronology;
32 import org.joda.time.chrono.CopticChronology;
33 import org.joda.time.chrono.GJChronology;
34 import org.joda.time.chrono.GregorianChronology;
35 import org.joda.time.chrono.ISOChronology;
36 import org.joda.time.chrono.JulianChronology;
37 import org.joda.time.field.DelegatedDurationField;
38 import org.joda.time.field.MillisDurationField;
39 import org.joda.time.field.UnsupportedDateTimeField;
40 import org.joda.time.field.UnsupportedDurationField;
41
42
43
44
45
46
47 public class TestSerialization extends TestCase {
48
49
50
51 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
52 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
53 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
54
55 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
56 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
57 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
58 366 + 365;
59 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
60 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
61 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
62 366 + 365 + 365;
63
64
65 private long TEST_TIME_NOW =
66 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
67
68
69 private long TEST_TIME1 =
70 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY
71 + 12L * DateTimeConstants.MILLIS_PER_HOUR
72 + 24L * DateTimeConstants.MILLIS_PER_MINUTE;
73
74
75 private long TEST_TIME2 =
76 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY
77 + 14L * DateTimeConstants.MILLIS_PER_HOUR
78 + 28L * DateTimeConstants.MILLIS_PER_MINUTE;
79
80 private static class MockDelegatedDurationField extends DelegatedDurationField implements Serializable {
81 private static final long serialVersionUID = 1878496002811998493L;
82 public MockDelegatedDurationField() {
83 super(MillisDurationField.INSTANCE);
84 }
85 }
86
87 private DateTimeZone originalDateTimeZone = null;
88 private TimeZone originalTimeZone = null;
89 private Locale originalLocale = null;
90
91 public static void main(String[] args) {
92 junit.textui.TestRunner.run(suite());
93 }
94
95 public static TestSuite suite() {
96 return new TestSuite(TestSerialization.class);
97 }
98
99 public TestSerialization(String name) {
100 super(name);
101 }
102
103 protected void setUp() throws Exception {
104 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
105 originalDateTimeZone = DateTimeZone.getDefault();
106 originalTimeZone = TimeZone.getDefault();
107 originalLocale = Locale.getDefault();
108 DateTimeZone.setDefault(LONDON);
109 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
110 Locale.setDefault(Locale.UK);
111 }
112
113 protected void tearDown() throws Exception {
114 DateTimeUtils.setCurrentMillisSystem();
115 DateTimeZone.setDefault(originalDateTimeZone);
116 TimeZone.setDefault(originalTimeZone);
117 Locale.setDefault(originalLocale);
118 originalDateTimeZone = null;
119 originalTimeZone = null;
120 originalLocale = null;
121 }
122
123
124 public void testTest() {
125 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString());
126 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString());
127 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString());
128 }
129
130
131 public void testSerializedInstant() throws Exception {
132 Instant test = new Instant();
133 loadAndCompare(test, "Instant", false);
134 inlineCompare(test, false);
135 }
136
137 public void testSerializedDateTime() throws Exception {
138 DateTime test = new DateTime();
139 loadAndCompare(test, "DateTime", false);
140 inlineCompare(test, false);
141 }
142
143 public void testSerializedDateTimeProperty() throws Exception {
144 DateTime.Property test = new DateTime().hourOfDay();
145 loadAndCompare(test, "DateTimeProperty", false);
146 inlineCompare(test, false);
147 }
148
149 public void testSerializedMutableDateTime() throws Exception {
150 MutableDateTime test = new MutableDateTime();
151 loadAndCompare(test, "MutableDateTime", false);
152 inlineCompare(test, false);
153 }
154
155 public void testSerializedMutableDateTimeProperty() throws Exception {
156 MutableDateTime.Property test = new MutableDateTime().hourOfDay();
157 loadAndCompare(test, "MutableDateTimeProperty", false);
158 inlineCompare(test, false);
159 }
160
161 public void testSerializedDateMidnight() throws Exception {
162 DateMidnight test = new DateMidnight();
163 loadAndCompare(test, "DateMidnight", false);
164 inlineCompare(test, false);
165 }
166
167 public void testSerializedDateMidnightProperty() throws Exception {
168 DateMidnight.Property test = new DateMidnight().monthOfYear();
169 loadAndCompare(test, "DateMidnightProperty", false);
170 inlineCompare(test, false);
171 }
172
173 public void testSerializedLocalDate() throws Exception {
174 LocalDate test = new LocalDate();
175 loadAndCompare(test, "LocalDate", false);
176 inlineCompare(test, false);
177 }
178
179 public void testSerializedLocalDateBuddhist() throws Exception {
180 LocalDate test = new LocalDate(BuddhistChronology.getInstanceUTC());
181 loadAndCompare(test, "LocalDateBuddhist", false);
182 inlineCompare(test, false);
183 }
184
185 public void testSerializedLocalTime() throws Exception {
186 LocalTime test = new LocalTime();
187 loadAndCompare(test, "LocalTime", false);
188 inlineCompare(test, false);
189 }
190
191 public void testSerializedLocalDateTime() throws Exception {
192 LocalDateTime test = new LocalDateTime();
193 loadAndCompare(test, "LocalDateTime", false);
194 inlineCompare(test, false);
195 }
196
197 public void testSerializedYearMonthDay() throws Exception {
198 YearMonthDay test = new YearMonthDay();
199 loadAndCompare(test, "YearMonthDay", false);
200 inlineCompare(test, false);
201 }
202
203 public void testSerializedTimeOfDay() throws Exception {
204 TimeOfDay test = new TimeOfDay();
205 loadAndCompare(test, "TimeOfDay", false);
206 inlineCompare(test, false);
207 }
208
209 public void testSerializedDateTimeZoneUTC() throws Exception {
210 DateTimeZone test = DateTimeZone.UTC;
211 loadAndCompare(test, "DateTimeZoneUTC", true);
212 inlineCompare(test, true);
213 }
214
215 public void testSerializedDateTimeZone() throws Exception {
216
217
218 DateTimeZone test = DateTimeZone.forID("Europe/Paris");
219 loadAndCompare(test, "DateTimeZone", true);
220 inlineCompare(test, true);
221 }
222
223 public void testDuration() throws Exception {
224 Duration test = Duration.millis(12345);
225 loadAndCompare(test, "Duration", false);
226 inlineCompare(test, false);
227 }
228
229 public void testSerializedCopticChronology() throws Exception {
230 CopticChronology test = CopticChronology.getInstance(LONDON);
231 loadAndCompare(test, "CopticChronology", true);
232 inlineCompare(test, true);
233 }
234
235 public void testSerializedISOChronology() throws Exception {
236 ISOChronology test = ISOChronology.getInstance(PARIS);
237 loadAndCompare(test, "ISOChronology", true);
238 inlineCompare(test, true);
239 }
240
241 public void testSerializedGJChronology() throws Exception {
242 GJChronology test = GJChronology.getInstance(TOKYO);
243 loadAndCompare(test, "GJChronology", true);
244 inlineCompare(test, true);
245 }
246
247 public void testSerializedGJChronologyChangedInternals() throws Exception {
248 GJChronology test = GJChronology.getInstance(PARIS, 123L, 2);
249 loadAndCompare(test, "GJChronologyChangedInternals", true);
250 inlineCompare(test, true);
251 }
252
253 public void testSerializedGregorianChronology() throws Exception {
254 GregorianChronology test = GregorianChronology.getInstance(PARIS);
255 loadAndCompare(test, "GregorianChronology", true);
256 inlineCompare(test, true);
257 }
258
259 public void testSerializedJulianChronology() throws Exception {
260 JulianChronology test = JulianChronology.getInstance(PARIS);
261 loadAndCompare(test, "JulianChronology", true);
262 inlineCompare(test, true);
263 }
264
265 public void testSerializedBuddhistChronology() throws Exception {
266 BuddhistChronology test = BuddhistChronology.getInstance(PARIS);
267 loadAndCompare(test, "BuddhistChronology", true);
268 inlineCompare(test, true);
269 }
270
271 public void testSerializedPeriodType() throws Exception {
272 PeriodType test = PeriodType.dayTime();
273 loadAndCompare(test, "PeriodType", false);
274 inlineCompare(test, false);
275 }
276
277 public void testSerializedDateTimeFieldType() throws Exception {
278 DateTimeFieldType test = DateTimeFieldType.clockhourOfDay();
279 loadAndCompare(test, "DateTimeFieldType", true);
280 inlineCompare(test, true);
281 }
282
283 public void testSerializedUnsupportedDateTimeField() throws Exception {
284 UnsupportedDateTimeField test = UnsupportedDateTimeField.getInstance(
285 DateTimeFieldType.year(),
286 UnsupportedDurationField.getInstance(DurationFieldType.years()));
287 loadAndCompare(test, "UnsupportedDateTimeField", true);
288 inlineCompare(test, true);
289 }
290
291 private void loadAndCompare(Serializable test, String filename, boolean same) throws Exception {
292 FileInputStream fis = new FileInputStream("src/test/resources/" + filename + ".dat");
293 ObjectInputStream ois = new ObjectInputStream(fis);
294 Object obj = ois.readObject();
295 ois.close();
296 if (same) {
297 assertSame(test, obj);
298 } else {
299 assertEquals(test, obj);
300 }
301
302
303
304
305
306
307
308
309
310
311
312
313
314 }
315
316 public void inlineCompare(Serializable test, boolean same) throws Exception {
317 ByteArrayOutputStream baos = new ByteArrayOutputStream();
318 ObjectOutputStream oos = new ObjectOutputStream(baos);
319 oos.writeObject(test);
320 oos.close();
321
322 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
323 ObjectInputStream ois = new ObjectInputStream(bais);
324 Object obj = ois.readObject();
325 ois.close();
326
327 if (same) {
328 assertSame(test, obj);
329 } else {
330 assertEquals(test, obj);
331 }
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499 private void store(Serializable test, String filename) throws Exception {
500 FileOutputStream fos = new FileOutputStream("src/test/resources/" + filename);
501 ObjectOutputStream oos = new ObjectOutputStream(fos);
502 try {
503 oos.writeObject(test);
504 } finally {
505 oos.close();
506 }
507 oos.close();
508 }
509
510 }