1 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 47 public class TestSerialization extends TestCase { 48 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 private long TEST_TIME_NOW = 66 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 67 68 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 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 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 public void testSerializedInstant() throws Exception { 132 Instant test = new Instant(); 133 loadAndCompare(test, "Instant.dat", false); 134 inlineCompare(test, false); 135 } 136 137 public void testSerializedDateTime() throws Exception { 138 DateTime test = new DateTime(); 139 loadAndCompare(test, "DateTime.dat", false); 140 inlineCompare(test, false); 141 } 142 143 public void testSerializedDateTimeProperty() throws Exception { 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 { 150 MutableDateTime test = new MutableDateTime(); 151 loadAndCompare(test, "MutableDateTime.dat", false); 152 inlineCompare(test, false); 153 } 154 155 public void testSerializedMutableDateTimeProperty() throws Exception { 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 { 162 DateMidnight test = new DateMidnight(); 163 loadAndCompare(test, "DateMidnight.dat", false); 164 inlineCompare(test, false); 165 } 166 167 public void testSerializedDateMidnightProperty() throws Exception { 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 { 174 YearMonthDay test = new YearMonthDay(); 175 loadAndCompare(test, "YearMonthDay.dat", false); 176 inlineCompare(test, false); 177 } 178 179 public void testSerializedTimeOfDay() throws Exception { 180 TimeOfDay test = new TimeOfDay(); 181 loadAndCompare(test, "TimeOfDay.dat", false); 182 inlineCompare(test, false); 183 } 184 185 public void testSerializedDateTimeZoneUTC() throws Exception { 186 DateTimeZone test = DateTimeZone.UTC; 187 loadAndCompare(test, "DateTimeZoneUTC.dat", true); 188 inlineCompare(test, true); 189 } 190 191 public void testSerializedDateTimeZone() throws Exception { 192 DateTimeZone test = DateTimeZone.forID("Europe/Paris"); 195 loadAndCompare(test, "DateTimeZone.dat", true); 196 inlineCompare(test, true); 197 } 198 199 public void testSerializedCopticChronology() throws Exception { 200 CopticChronology test = CopticChronology.getInstance(LONDON); 201 loadAndCompare(test, "CopticChronology.dat", true); 202 inlineCompare(test, true); 203 } 204 205 public void testSerializedISOChronology() throws Exception { 206 ISOChronology test = ISOChronology.getInstance(PARIS); 207 loadAndCompare(test, "ISOChronology.dat", true); 208 inlineCompare(test, true); 209 } 210 211 public void testSerializedGJChronology() throws Exception { 212 GJChronology test = GJChronology.getInstance(TOKYO); 213 loadAndCompare(test, "GJChronology.dat", true); 214 inlineCompare(test, true); 215 } 216 217 public void testSerializedGJChronologyChangedInternals() throws Exception { 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 { 224 GregorianChronology test = GregorianChronology.getInstance(PARIS); 225 loadAndCompare(test, "GregorianChronology.dat", true); 226 inlineCompare(test, true); 227 } 228 229 public void testSerializedJulianChronology() throws Exception { 230 JulianChronology test = JulianChronology.getInstance(PARIS); 231 loadAndCompare(test, "JulianChronology.dat", true); 232 inlineCompare(test, true); 233 } 234 235 public void testSerializedBuddhistChronology() throws Exception { 236 BuddhistChronology test = BuddhistChronology.getInstance(PARIS); 237 loadAndCompare(test, "BuddhistChronology.dat", true); 238 inlineCompare(test, true); 239 } 240 241 public void testSerializedPeriodType() throws Exception { 242 PeriodType test = PeriodType.dayTime(); 243 loadAndCompare(test, "PeriodType.dat", false); 244 inlineCompare(test, false); 245 } 246 247 public void testSerializedDateTimeFieldType() throws Exception { 248 DateTimeFieldType test = DateTimeFieldType.clockhourOfDay(); 249 loadAndCompare(test, "DateTimeFieldType.dat", true); 250 inlineCompare(test, true); 251 } 252 253 public void testSerializedUnsupportedDateTimeField() throws Exception { 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 test, String filename, boolean same) throws Exception { 262 FileInputStream fis = new FileInputStream ("src/testdata/" + filename); 263 ObjectInputStream ois = new ObjectInputStream (fis); 264 Object 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 test, boolean same) throws Exception { 274 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 275 ObjectOutputStream oos = new ObjectOutputStream (baos); 276 oos.writeObject(test); 277 oos.close(); 278 279 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 280 ObjectInputStream ois = new ObjectInputStream (bais); 281 Object obj = ois.readObject(); 282 ois.close(); 283 284 if (same) { 285 assertSame(test, obj); 286 } else { 287 assertEquals(test, obj); 288 } 289 } 290 291 431 private void store(Serializable test, String filename) throws Exception { 432 FileOutputStream fos = new FileOutputStream ("src/testdata/" + filename); 433 ObjectOutputStream oos = new ObjectOutputStream (fos); 434 try { 435 oos.writeObject(test); 436 } finally { 437 oos.close(); 438 } 439 oos.close(); 440 } 441 442 } 443 | Popular Tags |