1 16 package org.joda.time; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.util.Locale ; 23 import java.util.TimeZone ; 24 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 import org.joda.time.base.BasePeriod; 29 30 35 public class TestMutablePeriod_Basics extends TestCase { 36 39 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 40 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 41 42 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 43 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 44 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 45 366 + 365; 46 long y2003days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 47 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 48 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 49 366 + 365 + 365; 50 51 private long TEST_TIME_NOW = 53 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 54 55 private long TEST_TIME1 = 57 (y2002days + 31L + 28L + 31L + 5L -1L) * DateTimeConstants.MILLIS_PER_DAY 58 + 12L * DateTimeConstants.MILLIS_PER_HOUR 59 + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 60 61 private long TEST_TIME2 = 63 (y2003days + 31L + 28L + 31L + 30L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 64 + 14L * DateTimeConstants.MILLIS_PER_HOUR 65 + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 66 67 private DateTimeZone originalDateTimeZone = null; 68 private TimeZone originalTimeZone = null; 69 private Locale originalLocale = null; 70 71 public static void main(String [] args) { 72 junit.textui.TestRunner.run(suite()); 73 } 74 75 public static TestSuite suite() { 76 return new TestSuite(TestMutablePeriod_Basics.class); 77 } 78 79 public TestMutablePeriod_Basics(String name) { 80 super(name); 81 } 82 83 protected void setUp() throws Exception { 84 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 85 originalDateTimeZone = DateTimeZone.getDefault(); 86 originalTimeZone = TimeZone.getDefault(); 87 originalLocale = Locale.getDefault(); 88 DateTimeZone.setDefault(LONDON); 89 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 90 Locale.setDefault(Locale.UK); 91 } 92 93 protected void tearDown() throws Exception { 94 DateTimeUtils.setCurrentMillisSystem(); 95 DateTimeZone.setDefault(originalDateTimeZone); 96 TimeZone.setDefault(originalTimeZone); 97 Locale.setDefault(originalLocale); 98 originalDateTimeZone = null; 99 originalTimeZone = null; 100 originalLocale = null; 101 } 102 103 public void testTest() { 105 assertEquals("2002-06-09T00:00:00.000Z", new Instant(TEST_TIME_NOW).toString()); 106 assertEquals("2002-04-05T12:24:00.000Z", new Instant(TEST_TIME1).toString()); 107 assertEquals("2003-05-06T14:28:00.000Z", new Instant(TEST_TIME2).toString()); 108 } 109 110 public void testGetPeriodType() { 112 MutablePeriod test = new MutablePeriod(); 113 assertEquals(PeriodType.standard(), test.getPeriodType()); 114 } 115 116 public void testGetMethods() { 117 MutablePeriod test = new MutablePeriod(); 118 assertEquals(0, test.getYears()); 119 assertEquals(0, test.getMonths()); 120 assertEquals(0, test.getDays()); 121 assertEquals(0, test.getWeeks()); 122 assertEquals(0, test.getDays()); 123 assertEquals(0, test.getHours()); 124 assertEquals(0, test.getMinutes()); 125 assertEquals(0, test.getSeconds()); 126 assertEquals(0, test.getMillis()); 127 } 128 129 public void testEqualsHashCode() { 130 MutablePeriod test1 = new MutablePeriod(123L); 131 MutablePeriod test2 = new MutablePeriod(123L); 132 assertEquals(true, test1.equals(test2)); 133 assertEquals(true, test2.equals(test1)); 134 assertEquals(true, test1.equals(test1)); 135 assertEquals(true, test2.equals(test2)); 136 assertEquals(true, test1.hashCode() == test2.hashCode()); 137 assertEquals(true, test1.hashCode() == test1.hashCode()); 138 assertEquals(true, test2.hashCode() == test2.hashCode()); 139 140 MutablePeriod test3 = new MutablePeriod(321L); 141 assertEquals(false, test1.equals(test3)); 142 assertEquals(false, test2.equals(test3)); 143 assertEquals(false, test3.equals(test1)); 144 assertEquals(false, test3.equals(test2)); 145 assertEquals(false, test1.hashCode() == test3.hashCode()); 146 assertEquals(false, test2.hashCode() == test3.hashCode()); 147 148 assertEquals(false, test1.equals("Hello")); 149 assertEquals(true, test1.equals(new MockMutablePeriod(123L))); 150 assertEquals(false, test1.equals(new Period(123L, PeriodType.dayTime()))); 151 } 152 153 class MockMutablePeriod extends BasePeriod { 154 public MockMutablePeriod(long value) { 155 super(value, null, null); 156 } 157 } 158 159 public void testSerialization() throws Exception { 161 MutablePeriod test = new MutablePeriod(123L); 162 163 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 164 ObjectOutputStream oos = new ObjectOutputStream (baos); 165 oos.writeObject(test); 166 byte[] bytes = baos.toByteArray(); 167 oos.close(); 168 169 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 170 ObjectInputStream ois = new ObjectInputStream (bais); 171 MutablePeriod result = (MutablePeriod) ois.readObject(); 172 ois.close(); 173 174 assertEquals(test, result); 175 } 176 177 402 public void testToString() { 404 MutablePeriod test = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8); 405 assertEquals("P1Y2M3W4DT5H6M7.008S", test.toString()); 406 407 test = new MutablePeriod(0, 0, 0, 0, 0, 0, 0, 0); 408 assertEquals("PT0S", test.toString()); 409 410 test = new MutablePeriod(12345L); 411 assertEquals("PT12.345S", test.toString()); 412 } 413 414 public void testToPeriod() { 416 MutablePeriod test = new MutablePeriod(123L); 417 Period result = test.toPeriod(); 418 assertEquals(test, result); 419 } 420 421 public void testToMutablePeriod() { 422 MutablePeriod test = new MutablePeriod(123L); 423 MutablePeriod result = test.toMutablePeriod(); 424 assertEquals(test, result); 425 } 426 427 433 public void testToDurationFrom() { 434 MutablePeriod test = new MutablePeriod(123L); 435 assertEquals(new Duration(123L), test.toDurationFrom(new Instant(0L))); 436 } 437 438 public void testCopy() { 440 MutablePeriod test = new MutablePeriod(123L); 441 MutablePeriod copy = test.copy(); 442 assertEquals(test.getPeriodType(), copy.getPeriodType()); 443 assertEquals(test, copy); 444 } 445 446 public void testClone() { 448 MutablePeriod test = new MutablePeriod(123L); 449 MutablePeriod copy = (MutablePeriod) test.clone(); 450 assertEquals(test.getPeriodType(), copy.getPeriodType()); 451 assertEquals(test, copy); 452 } 453 454 } 455 | Popular Tags |