KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestSerialization


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.TimeZone JavaDoc;
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  * This class is a Junit unit test for serialization.
44  *
45  * @author Stephen Colebourne
46  */

47 public class TestSerialization extends TestCase {
48     // Test in 2002/03 as time zones are more well known
49
// (before the late 90's they were all over the place)
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     // 2002-06-09
65
private long TEST_TIME_NOW =
66             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
67             
68     // 2002-04-05
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     // 2003-05-06
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 JavaDoc {
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 JavaDoc originalTimeZone = null;
89     private Locale JavaDoc originalLocale = null;
90
91     public static void main(String JavaDoc[] 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 JavaDoc name) {
100         super(name);
101     }
102
103     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
132         Instant test = new Instant();
133         loadAndCompare(test, "Instant.dat", false);
134         inlineCompare(test, false);
135     }
136
137     public void testSerializedDateTime() throws Exception JavaDoc {
138         DateTime test = new DateTime();
139         loadAndCompare(test, "DateTime.dat", false);
140         inlineCompare(test, false);
141     }
142
143     public void testSerializedDateTimeProperty() throws Exception JavaDoc {
144         DateTime.Property test = new DateTime().hourOfDay();
145         loadAndCompare(test, "DateTimeProperty.dat", false);
146         inlineCompare(test, false);
147     }
148
149     public void testSerializedMutableDateTime() throws Exception JavaDoc {
150         MutableDateTime test = new MutableDateTime();
151         loadAndCompare(test, "MutableDateTime.dat", false);
152         inlineCompare(test, false);
153     }
154
155     public void testSerializedMutableDateTimeProperty() throws Exception JavaDoc {
156         MutableDateTime.Property test = new MutableDateTime().hourOfDay();
157         loadAndCompare(test, "MutableDateTimeProperty.dat", false);
158         inlineCompare(test, false);
159     }
160
161     public void testSerializedDateMidnight() throws Exception JavaDoc {
162         DateMidnight test = new DateMidnight();
163         loadAndCompare(test, "DateMidnight.dat", false);
164         inlineCompare(test, false);
165     }
166
167     public void testSerializedDateMidnightProperty() throws Exception JavaDoc {
168         DateMidnight.Property test = new DateMidnight().monthOfYear();
169         loadAndCompare(test, "DateMidnightProperty.dat", false);
170         inlineCompare(test, false);
171     }
172
173     public void testSerializedYearMonthDay() throws Exception JavaDoc {
174         YearMonthDay test = new YearMonthDay();
175         loadAndCompare(test, "YearMonthDay.dat", false);
176         inlineCompare(test, false);
177     }
178
179     public void testSerializedTimeOfDay() throws Exception JavaDoc {
180         TimeOfDay test = new TimeOfDay();
181         loadAndCompare(test, "TimeOfDay.dat", false);
182         inlineCompare(test, false);
183     }
184
185     public void testSerializedDateTimeZoneUTC() throws Exception JavaDoc {
186         DateTimeZone test = DateTimeZone.UTC;
187         loadAndCompare(test, "DateTimeZoneUTC.dat", true);
188         inlineCompare(test, true);
189     }
190
191     public void testSerializedDateTimeZone() throws Exception JavaDoc {
192         // have to re-get the zone, as TestDateTimeZone may have
193
// changed the cache, or a SoftReference may have got cleared
194
DateTimeZone test = DateTimeZone.forID("Europe/Paris");
195         loadAndCompare(test, "DateTimeZone.dat", true);
196         inlineCompare(test, true);
197     }
198
199     public void testSerializedCopticChronology() throws Exception JavaDoc {
200         CopticChronology test = CopticChronology.getInstance(LONDON);
201         loadAndCompare(test, "CopticChronology.dat", true);
202         inlineCompare(test, true);
203     }
204
205     public void testSerializedISOChronology() throws Exception JavaDoc {
206         ISOChronology test = ISOChronology.getInstance(PARIS);
207         loadAndCompare(test, "ISOChronology.dat", true);
208         inlineCompare(test, true);
209     }
210
211     public void testSerializedGJChronology() throws Exception JavaDoc {
212         GJChronology test = GJChronology.getInstance(TOKYO);
213         loadAndCompare(test, "GJChronology.dat", true);
214         inlineCompare(test, true);
215     }
216
217     public void testSerializedGJChronologyChangedInternals() throws Exception JavaDoc {
218         GJChronology test = GJChronology.getInstance(PARIS, 123L, 2);
219         loadAndCompare(test, "GJChronologyChangedInternals.dat", true);
220         inlineCompare(test, true);
221     }
222
223     public void testSerializedGregorianChronology() throws Exception JavaDoc {
224         GregorianChronology test = GregorianChronology.getInstance(PARIS);
225         loadAndCompare(test, "GregorianChronology.dat", true);
226         inlineCompare(test, true);
227     }
228
229     public void testSerializedJulianChronology() throws Exception JavaDoc {
230         JulianChronology test = JulianChronology.getInstance(PARIS);
231         loadAndCompare(test, "JulianChronology.dat", true);
232         inlineCompare(test, true);
233     }
234
235     public void testSerializedBuddhistChronology() throws Exception JavaDoc {
236         BuddhistChronology test = BuddhistChronology.getInstance(PARIS);
237         loadAndCompare(test, "BuddhistChronology.dat", true);
238         inlineCompare(test, true);
239     }
240
241     public void testSerializedPeriodType() throws Exception JavaDoc {
242         PeriodType test = PeriodType.dayTime();
243         loadAndCompare(test, "PeriodType.dat", false);
244         inlineCompare(test, false);
245     }
246
247     public void testSerializedDateTimeFieldType() throws Exception JavaDoc {
248         DateTimeFieldType test = DateTimeFieldType.clockhourOfDay();
249         loadAndCompare(test, "DateTimeFieldType.dat", true);
250         inlineCompare(test, true);
251     }
252
253     public void testSerializedUnsupportedDateTimeField() throws Exception JavaDoc {
254         UnsupportedDateTimeField test = UnsupportedDateTimeField.getInstance(
255                 DateTimeFieldType.year(),
256                 UnsupportedDurationField.getInstance(DurationFieldType.years()));
257         loadAndCompare(test, "UnsupportedDateTimeField.dat", true);
258         inlineCompare(test, true);
259     }
260
261     private void loadAndCompare(Serializable JavaDoc test, String JavaDoc filename, boolean same) throws Exception JavaDoc {
262         FileInputStream JavaDoc fis = new FileInputStream JavaDoc("src/testdata/" + filename);
263         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
264         Object JavaDoc obj = ois.readObject();
265         ois.close();
266         if (same) {
267             assertSame(test, obj);
268         } else {
269             assertEquals(test, obj);
270         }
271     }
272
273     public void inlineCompare(Serializable JavaDoc test, boolean same) throws Exception JavaDoc {
274         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
275         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
276         oos.writeObject(test);
277         oos.close();
278         
279         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
280         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
281         Object JavaDoc obj = ois.readObject();
282         ois.close();
283         
284         if (same) {
285             assertSame(test, obj);
286         } else {
287             assertEquals(test, obj);
288         }
289     }
290
291 // //-----------------------------------------------------------------------
292
// public void testStoreSerializedInstant() throws Exception {
293
// Instant test = new Instant();
294
// store(test, "Instant.dat");
295
// }
296
//
297
// public void testStoreSerializedDateTime() throws Exception {
298
// DateTime test = new DateTime();
299
// store(test, "DateTime.dat");
300
// }
301
//
302
// public void testStoreSerializedMutableDateTime() throws Exception {
303
// MutableDateTime test = new MutableDateTime();
304
// store(test, "MutableDateTime.dat");
305
// }
306
//
307
// public void testStoreSerializedDateMidnight() throws Exception {
308
// DateMidnight test = new DateMidnight();
309
// store(test, "DateMidnight.dat");
310
// }
311
//
312
// public void testStoreSerializedYearMonthDay() throws Exception {
313
// YearMonthDay test = new YearMonthDay();
314
// store(test, "YearMonthDay.dat");
315
// }
316
//
317
// public void testStoreSerializedYearMonthDayProperty() throws Exception {
318
// YearMonthDay.Property test = new YearMonthDay().monthOfYear();
319
// store(test, "YearMonthDayProperty.dat");
320
// }
321
//
322
// public void testStoreSerializedTimeOfDay() throws Exception {
323
// TimeOfDay test = new TimeOfDay();
324
// store(test, "TimeOfDay.dat");
325
// }
326
//
327
// public void testStoreSerializedTimeOfDayProperty() throws Exception {
328
// TimeOfDay.Property test = new TimeOfDay().hourOfDay();
329
// store(test, "TimeOfDayProperty.dat");
330
// }
331
//
332
// public void testStoreSerializedDateTimeZoneUTC() throws Exception {
333
// DateTimeZone test = DateTimeZone.UTC;
334
// store(test, "DateTimeZoneUTC.dat");
335
// }
336
//
337
// public void testStoreSerializedDateTimeZone() throws Exception {
338
// DateTimeZone test = PARIS;
339
// store(test, "DateTimeZone.dat");
340
// }
341
//
342
// public void testStoreSerializedCopticChronology() throws Exception {
343
// CopticChronology test = CopticChronology.getInstance(LONDON);
344
// store(test, "CopticChronology.dat");
345
// }
346
//
347
// public void testStoreSerializedISOChronology() throws Exception {
348
// ISOChronology test = ISOChronology.getInstance(PARIS);
349
// store(test, "ISOChronology.dat");
350
// }
351
//
352
// public void testStoreSerializedGJChronology() throws Exception {
353
// GJChronology test = GJChronology.getInstance(TOKYO);
354
// store(test, "GJChronology.dat");
355
// }
356
//
357
// // Format changed in v1.2 - min days in first week not deserialized in v1.0/1.1
358
// public void testStoreSerializedGJChronologyChangedInternals() throws Exception {
359
// GJChronology test = GJChronology.getInstance(PARIS, 123L, 2);
360
// store(test, "GJChronologyChangedInternals.dat");
361
// }
362
//
363
// public void testStoreSerializedGregorianChronology() throws Exception {
364
// GregorianChronology test = GregorianChronology.getInstance(PARIS);
365
// store(test, "GregorianChronology.dat");
366
// }
367
//
368
// public void testStoreSerializedJulianChronology() throws Exception {
369
// JulianChronology test = JulianChronology.getInstance(PARIS);
370
// store(test, "JulianChronology.dat");
371
// }
372
//
373
// public void testStoreSerializedBuddhistChronology() throws Exception {
374
// BuddhistChronology test = BuddhistChronology.getInstance(PARIS);
375
// store(test, "BuddhistChronology.dat");
376
// }
377
//
378
// public void testStoreSerializedPeriodType() throws Exception {
379
// PeriodType test = PeriodType.dayTime();
380
// store(test, "PeriodType.dat");
381
// }
382
//
383
// public void testStoreSerializedDateTimeFieldType() throws Exception {
384
// DateTimeFieldType test = DateTimeFieldType.clockhourOfDay();
385
// store(test, "DateTimeFieldType.dat");
386
// }
387
//
388
// public void testStoreSerializedUnsupportedDateTimeField() throws Exception {
389
// UnsupportedDateTimeField test = UnsupportedDateTimeField.getInstance(
390
// DateTimeFieldType.year(),
391
// UnsupportedDurationField.getInstance(DurationFieldType.years()));
392
// store(test, "UnsupportedDateTimeField.dat");
393
// }
394
//
395
// public void testStoreSerializedDurationFieldType() throws Exception {
396
// DurationFieldType test = DurationFieldType.MINUTES_TYPE;
397
// store(test, "DurationFieldType.dat");
398
// }
399
//
400
// public void testStoreSerializedMillisDurationField() throws Exception {
401
// MillisDurationField test = (MillisDurationField) MillisDurationField.INSTANCE;
402
// store(test, "MillisDurationField.dat");
403
// }
404
//
405
// public void testStoreSerializedDelegatedDurationField() throws Exception {
406
// DelegatedDurationField test = new MockDelegatedDurationField();
407
// store(test, "DelegatedDurationField.dat");
408
// }
409
//
410
// public void testStoreSerializedUnsupportedDurationField() throws Exception {
411
// UnsupportedDurationField test = UnsupportedDurationField.getInstance(DurationFieldType.eras());
412
// store(test, "UnsupportedDurationField.dat");
413
// }
414
//
415
// format changed (properly defined) in v1.1
416
// public void testStoreSerializedDateTimeProperty() throws Exception {
417
// DateTime.Property test = new DateTime().hourOfDay();
418
// store(test, "DateTimeProperty.dat");
419
// }
420
//
421
// public void testStoreSerializedMutableDateTimeProperty() throws Exception {
422
// MutableDateTime.Property test = new MutableDateTime().hourOfDay();
423
// store(test, "MutableDateTimeProperty.dat");
424
// }
425
//
426
// public void testStoreSerializedDateMidnightProperty() throws Exception {
427
// DateMidnight.Property test = new DateMidnight().monthOfYear();
428
// store(test, "DateMidnightProperty.dat");
429
// }
430

431     private void store(Serializable JavaDoc test, String JavaDoc filename) throws Exception JavaDoc {
432         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("src/testdata/" + filename);
433         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
434         try {
435             oos.writeObject(test);
436         } finally {
437             oos.close();
438         }
439         oos.close();
440     }
441
442 }
443
Popular Tags