001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.time;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.io.Serializable;
025: import java.util.Locale;
026: import java.util.TimeZone;
027:
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030:
031: import org.joda.time.chrono.BuddhistChronology;
032: import org.joda.time.chrono.CopticChronology;
033: import org.joda.time.chrono.GJChronology;
034: import org.joda.time.chrono.GregorianChronology;
035: import org.joda.time.chrono.ISOChronology;
036: import org.joda.time.chrono.JulianChronology;
037: import org.joda.time.field.DelegatedDurationField;
038: import org.joda.time.field.MillisDurationField;
039: import org.joda.time.field.UnsupportedDateTimeField;
040: import org.joda.time.field.UnsupportedDurationField;
041:
042: /**
043: * This class is a Junit unit test for serialization.
044: *
045: * @author Stephen Colebourne
046: */
047: public class TestSerialization extends TestCase {
048: // Test in 2002/03 as time zones are more well known
049: // (before the late 90's they were all over the place)
050:
051: private static final DateTimeZone PARIS = DateTimeZone
052: .forID("Europe/Paris");
053: private static final DateTimeZone LONDON = DateTimeZone
054: .forID("Europe/London");
055: private static final DateTimeZone TOKYO = DateTimeZone
056: .forID("Asia/Tokyo");
057:
058: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
059: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
060: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
061: + 365 + 365 + 366 + 365;
062: long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
063: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
064: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
065: + 365 + 365 + 366 + 365 + 365;
066:
067: // 2002-06-09
068: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
069: + 31L + 9L - 1L)
070: * DateTimeConstants.MILLIS_PER_DAY;
071:
072: // 2002-04-05
073: private long TEST_TIME1 = (y2002days + 31L + 28L + 31L + 5L - 1L)
074: * DateTimeConstants.MILLIS_PER_DAY + 12L
075: * DateTimeConstants.MILLIS_PER_HOUR + 24L
076: * DateTimeConstants.MILLIS_PER_MINUTE;
077:
078: // 2003-05-06
079: private long TEST_TIME2 = (y2003days + 31L + 28L + 31L + 30L + 6L - 1L)
080: * DateTimeConstants.MILLIS_PER_DAY
081: + 14L
082: * DateTimeConstants.MILLIS_PER_HOUR
083: + 28L
084: * DateTimeConstants.MILLIS_PER_MINUTE;
085:
086: private static class MockDelegatedDurationField extends
087: DelegatedDurationField implements Serializable {
088: private static final long serialVersionUID = 1878496002811998493L;
089:
090: public MockDelegatedDurationField() {
091: super (MillisDurationField.INSTANCE);
092: }
093: }
094:
095: private DateTimeZone originalDateTimeZone = null;
096: private TimeZone originalTimeZone = null;
097: private Locale originalLocale = null;
098:
099: public static void main(String[] args) {
100: junit.textui.TestRunner.run(suite());
101: }
102:
103: public static TestSuite suite() {
104: return new TestSuite(TestSerialization.class);
105: }
106:
107: public TestSerialization(String name) {
108: super (name);
109: }
110:
111: protected void setUp() throws Exception {
112: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
113: originalDateTimeZone = DateTimeZone.getDefault();
114: originalTimeZone = TimeZone.getDefault();
115: originalLocale = Locale.getDefault();
116: DateTimeZone.setDefault(LONDON);
117: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
118: Locale.setDefault(Locale.UK);
119: }
120:
121: protected void tearDown() throws Exception {
122: DateTimeUtils.setCurrentMillisSystem();
123: DateTimeZone.setDefault(originalDateTimeZone);
124: TimeZone.setDefault(originalTimeZone);
125: Locale.setDefault(originalLocale);
126: originalDateTimeZone = null;
127: originalTimeZone = null;
128: originalLocale = null;
129: }
130:
131: //-----------------------------------------------------------------------
132: public void testTest() {
133: assertEquals("2002-06-09T00:00:00.000Z", new Instant(
134: TEST_TIME_NOW).toString());
135: assertEquals("2002-04-05T12:24:00.000Z",
136: new Instant(TEST_TIME1).toString());
137: assertEquals("2003-05-06T14:28:00.000Z",
138: new Instant(TEST_TIME2).toString());
139: }
140:
141: //-----------------------------------------------------------------------
142: public void testSerializedInstant() throws Exception {
143: Instant test = new Instant();
144: loadAndCompare(test, "Instant.dat", false);
145: inlineCompare(test, false);
146: }
147:
148: public void testSerializedDateTime() throws Exception {
149: DateTime test = new DateTime();
150: loadAndCompare(test, "DateTime.dat", false);
151: inlineCompare(test, false);
152: }
153:
154: public void testSerializedDateTimeProperty() throws Exception {
155: DateTime.Property test = new DateTime().hourOfDay();
156: loadAndCompare(test, "DateTimeProperty.dat", false);
157: inlineCompare(test, false);
158: }
159:
160: public void testSerializedMutableDateTime() throws Exception {
161: MutableDateTime test = new MutableDateTime();
162: loadAndCompare(test, "MutableDateTime.dat", false);
163: inlineCompare(test, false);
164: }
165:
166: public void testSerializedMutableDateTimeProperty()
167: throws Exception {
168: MutableDateTime.Property test = new MutableDateTime()
169: .hourOfDay();
170: loadAndCompare(test, "MutableDateTimeProperty.dat", false);
171: inlineCompare(test, false);
172: }
173:
174: public void testSerializedDateMidnight() throws Exception {
175: DateMidnight test = new DateMidnight();
176: loadAndCompare(test, "DateMidnight.dat", false);
177: inlineCompare(test, false);
178: }
179:
180: public void testSerializedDateMidnightProperty() throws Exception {
181: DateMidnight.Property test = new DateMidnight().monthOfYear();
182: loadAndCompare(test, "DateMidnightProperty.dat", false);
183: inlineCompare(test, false);
184: }
185:
186: public void testSerializedYearMonthDay() throws Exception {
187: YearMonthDay test = new YearMonthDay();
188: loadAndCompare(test, "YearMonthDay.dat", false);
189: inlineCompare(test, false);
190: }
191:
192: public void testSerializedTimeOfDay() throws Exception {
193: TimeOfDay test = new TimeOfDay();
194: loadAndCompare(test, "TimeOfDay.dat", false);
195: inlineCompare(test, false);
196: }
197:
198: public void testSerializedDateTimeZoneUTC() throws Exception {
199: DateTimeZone test = DateTimeZone.UTC;
200: loadAndCompare(test, "DateTimeZoneUTC.dat", true);
201: inlineCompare(test, true);
202: }
203:
204: public void testSerializedDateTimeZone() throws Exception {
205: // have to re-get the zone, as TestDateTimeZone may have
206: // changed the cache, or a SoftReference may have got cleared
207: DateTimeZone test = DateTimeZone.forID("Europe/Paris");
208: loadAndCompare(test, "DateTimeZone.dat", true);
209: inlineCompare(test, true);
210: }
211:
212: public void testSerializedCopticChronology() throws Exception {
213: CopticChronology test = CopticChronology.getInstance(LONDON);
214: loadAndCompare(test, "CopticChronology.dat", true);
215: inlineCompare(test, true);
216: }
217:
218: public void testSerializedISOChronology() throws Exception {
219: ISOChronology test = ISOChronology.getInstance(PARIS);
220: loadAndCompare(test, "ISOChronology.dat", true);
221: inlineCompare(test, true);
222: }
223:
224: public void testSerializedGJChronology() throws Exception {
225: GJChronology test = GJChronology.getInstance(TOKYO);
226: loadAndCompare(test, "GJChronology.dat", true);
227: inlineCompare(test, true);
228: }
229:
230: public void testSerializedGJChronologyChangedInternals()
231: throws Exception {
232: GJChronology test = GJChronology.getInstance(PARIS, 123L, 2);
233: loadAndCompare(test, "GJChronologyChangedInternals.dat", true);
234: inlineCompare(test, true);
235: }
236:
237: public void testSerializedGregorianChronology() throws Exception {
238: GregorianChronology test = GregorianChronology
239: .getInstance(PARIS);
240: loadAndCompare(test, "GregorianChronology.dat", true);
241: inlineCompare(test, true);
242: }
243:
244: public void testSerializedJulianChronology() throws Exception {
245: JulianChronology test = JulianChronology.getInstance(PARIS);
246: loadAndCompare(test, "JulianChronology.dat", true);
247: inlineCompare(test, true);
248: }
249:
250: public void testSerializedBuddhistChronology() throws Exception {
251: BuddhistChronology test = BuddhistChronology.getInstance(PARIS);
252: loadAndCompare(test, "BuddhistChronology.dat", true);
253: inlineCompare(test, true);
254: }
255:
256: public void testSerializedPeriodType() throws Exception {
257: PeriodType test = PeriodType.dayTime();
258: loadAndCompare(test, "PeriodType.dat", false);
259: inlineCompare(test, false);
260: }
261:
262: public void testSerializedDateTimeFieldType() throws Exception {
263: DateTimeFieldType test = DateTimeFieldType.clockhourOfDay();
264: loadAndCompare(test, "DateTimeFieldType.dat", true);
265: inlineCompare(test, true);
266: }
267:
268: public void testSerializedUnsupportedDateTimeField()
269: throws Exception {
270: UnsupportedDateTimeField test = UnsupportedDateTimeField
271: .getInstance(DateTimeFieldType.year(),
272: UnsupportedDurationField
273: .getInstance(DurationFieldType.years()));
274: loadAndCompare(test, "UnsupportedDateTimeField.dat", true);
275: inlineCompare(test, true);
276: }
277:
278: private void loadAndCompare(Serializable test, String filename,
279: boolean same) throws Exception {
280: FileInputStream fis = new FileInputStream("src/testdata/"
281: + filename);
282: ObjectInputStream ois = new ObjectInputStream(fis);
283: Object obj = ois.readObject();
284: ois.close();
285: if (same) {
286: assertSame(test, obj);
287: } else {
288: assertEquals(test, obj);
289: }
290: }
291:
292: public void inlineCompare(Serializable test, boolean same)
293: throws Exception {
294: ByteArrayOutputStream baos = new ByteArrayOutputStream();
295: ObjectOutputStream oos = new ObjectOutputStream(baos);
296: oos.writeObject(test);
297: oos.close();
298:
299: ByteArrayInputStream bais = new ByteArrayInputStream(baos
300: .toByteArray());
301: ObjectInputStream ois = new ObjectInputStream(bais);
302: Object obj = ois.readObject();
303: ois.close();
304:
305: if (same) {
306: assertSame(test, obj);
307: } else {
308: assertEquals(test, obj);
309: }
310: }
311:
312: // //-----------------------------------------------------------------------
313: // public void testStoreSerializedInstant() throws Exception {
314: // Instant test = new Instant();
315: // store(test, "Instant.dat");
316: // }
317: //
318: // public void testStoreSerializedDateTime() throws Exception {
319: // DateTime test = new DateTime();
320: // store(test, "DateTime.dat");
321: // }
322: //
323: // public void testStoreSerializedMutableDateTime() throws Exception {
324: // MutableDateTime test = new MutableDateTime();
325: // store(test, "MutableDateTime.dat");
326: // }
327: //
328: // public void testStoreSerializedDateMidnight() throws Exception {
329: // DateMidnight test = new DateMidnight();
330: // store(test, "DateMidnight.dat");
331: // }
332: //
333: // public void testStoreSerializedYearMonthDay() throws Exception {
334: // YearMonthDay test = new YearMonthDay();
335: // store(test, "YearMonthDay.dat");
336: // }
337: //
338: // public void testStoreSerializedYearMonthDayProperty() throws Exception {
339: // YearMonthDay.Property test = new YearMonthDay().monthOfYear();
340: // store(test, "YearMonthDayProperty.dat");
341: // }
342: //
343: // public void testStoreSerializedTimeOfDay() throws Exception {
344: // TimeOfDay test = new TimeOfDay();
345: // store(test, "TimeOfDay.dat");
346: // }
347: //
348: // public void testStoreSerializedTimeOfDayProperty() throws Exception {
349: // TimeOfDay.Property test = new TimeOfDay().hourOfDay();
350: // store(test, "TimeOfDayProperty.dat");
351: // }
352: //
353: // public void testStoreSerializedDateTimeZoneUTC() throws Exception {
354: // DateTimeZone test = DateTimeZone.UTC;
355: // store(test, "DateTimeZoneUTC.dat");
356: // }
357: //
358: // public void testStoreSerializedDateTimeZone() throws Exception {
359: // DateTimeZone test = PARIS;
360: // store(test, "DateTimeZone.dat");
361: // }
362: //
363: // public void testStoreSerializedCopticChronology() throws Exception {
364: // CopticChronology test = CopticChronology.getInstance(LONDON);
365: // store(test, "CopticChronology.dat");
366: // }
367: //
368: // public void testStoreSerializedISOChronology() throws Exception {
369: // ISOChronology test = ISOChronology.getInstance(PARIS);
370: // store(test, "ISOChronology.dat");
371: // }
372: //
373: // public void testStoreSerializedGJChronology() throws Exception {
374: // GJChronology test = GJChronology.getInstance(TOKYO);
375: // store(test, "GJChronology.dat");
376: // }
377: //
378: // // Format changed in v1.2 - min days in first week not deserialized in v1.0/1.1
379: // public void testStoreSerializedGJChronologyChangedInternals() throws Exception {
380: // GJChronology test = GJChronology.getInstance(PARIS, 123L, 2);
381: // store(test, "GJChronologyChangedInternals.dat");
382: // }
383: //
384: // public void testStoreSerializedGregorianChronology() throws Exception {
385: // GregorianChronology test = GregorianChronology.getInstance(PARIS);
386: // store(test, "GregorianChronology.dat");
387: // }
388: //
389: // public void testStoreSerializedJulianChronology() throws Exception {
390: // JulianChronology test = JulianChronology.getInstance(PARIS);
391: // store(test, "JulianChronology.dat");
392: // }
393: //
394: // public void testStoreSerializedBuddhistChronology() throws Exception {
395: // BuddhistChronology test = BuddhistChronology.getInstance(PARIS);
396: // store(test, "BuddhistChronology.dat");
397: // }
398: //
399: // public void testStoreSerializedPeriodType() throws Exception {
400: // PeriodType test = PeriodType.dayTime();
401: // store(test, "PeriodType.dat");
402: // }
403: //
404: // public void testStoreSerializedDateTimeFieldType() throws Exception {
405: // DateTimeFieldType test = DateTimeFieldType.clockhourOfDay();
406: // store(test, "DateTimeFieldType.dat");
407: // }
408: //
409: // public void testStoreSerializedUnsupportedDateTimeField() throws Exception {
410: // UnsupportedDateTimeField test = UnsupportedDateTimeField.getInstance(
411: // DateTimeFieldType.year(),
412: // UnsupportedDurationField.getInstance(DurationFieldType.years()));
413: // store(test, "UnsupportedDateTimeField.dat");
414: // }
415: //
416: // public void testStoreSerializedDurationFieldType() throws Exception {
417: // DurationFieldType test = DurationFieldType.MINUTES_TYPE;
418: // store(test, "DurationFieldType.dat");
419: // }
420: //
421: // public void testStoreSerializedMillisDurationField() throws Exception {
422: // MillisDurationField test = (MillisDurationField) MillisDurationField.INSTANCE;
423: // store(test, "MillisDurationField.dat");
424: // }
425: //
426: // public void testStoreSerializedDelegatedDurationField() throws Exception {
427: // DelegatedDurationField test = new MockDelegatedDurationField();
428: // store(test, "DelegatedDurationField.dat");
429: // }
430: //
431: // public void testStoreSerializedUnsupportedDurationField() throws Exception {
432: // UnsupportedDurationField test = UnsupportedDurationField.getInstance(DurationFieldType.eras());
433: // store(test, "UnsupportedDurationField.dat");
434: // }
435: //
436: // format changed (properly defined) in v1.1
437: // public void testStoreSerializedDateTimeProperty() throws Exception {
438: // DateTime.Property test = new DateTime().hourOfDay();
439: // store(test, "DateTimeProperty.dat");
440: // }
441: //
442: // public void testStoreSerializedMutableDateTimeProperty() throws Exception {
443: // MutableDateTime.Property test = new MutableDateTime().hourOfDay();
444: // store(test, "MutableDateTimeProperty.dat");
445: // }
446: //
447: // public void testStoreSerializedDateMidnightProperty() throws Exception {
448: // DateMidnight.Property test = new DateMidnight().monthOfYear();
449: // store(test, "DateMidnightProperty.dat");
450: // }
451:
452: private void store(Serializable test, String filename)
453: throws Exception {
454: FileOutputStream fos = new FileOutputStream("src/testdata/"
455: + filename);
456: ObjectOutputStream oos = new ObjectOutputStream(fos);
457: try {
458: oos.writeObject(test);
459: } finally {
460: oos.close();
461: }
462: oos.close();
463: }
464:
465: }
|