1 16 package org.joda.time.format; 17 18 import java.util.Locale ; 19 import java.util.TimeZone ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.joda.time.DateTimeConstants; 25 import org.joda.time.DateTimeUtils; 26 import org.joda.time.DateTimeZone; 27 import org.joda.time.Period; 28 import org.joda.time.PeriodType; 29 30 35 public class TestPeriodFormat extends TestCase { 36 37 private static final Period PERIOD = new Period(1, 2, 3, 4, 5, 6, 7, 8); 38 private static final Period EMPTY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0); 39 private static final Period YEAR_DAY_PERIOD = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime()); 40 private static final Period EMPTY_YEAR_DAY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0, PeriodType.yearDayTime()); 41 private static final Period TIME_PERIOD = new Period(0, 0, 0, 0, 5, 6, 7, 8); 42 private static final Period DATE_PERIOD = new Period(1, 2, 3, 4, 0, 0, 0, 0); 43 44 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 45 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 46 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 47 48 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 49 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 50 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 51 366 + 365; 52 private long TEST_TIME_NOW = 54 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 55 56 private DateTimeZone originalDateTimeZone = null; 57 private TimeZone originalTimeZone = null; 58 private Locale originalLocale = null; 59 60 public static void main(String [] args) { 61 junit.textui.TestRunner.run(suite()); 62 } 63 64 public static TestSuite suite() { 65 return new TestSuite(TestPeriodFormat.class); 66 } 67 68 public TestPeriodFormat(String name) { 69 super(name); 70 } 71 72 protected void setUp() throws Exception { 73 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 74 originalDateTimeZone = DateTimeZone.getDefault(); 75 originalTimeZone = TimeZone.getDefault(); 76 originalLocale = Locale.getDefault(); 77 DateTimeZone.setDefault(LONDON); 78 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 79 Locale.setDefault(Locale.UK); 80 } 81 82 protected void tearDown() throws Exception { 83 DateTimeUtils.setCurrentMillisSystem(); 84 DateTimeZone.setDefault(originalDateTimeZone); 85 TimeZone.setDefault(originalTimeZone); 86 Locale.setDefault(originalLocale); 87 originalDateTimeZone = null; 88 originalTimeZone = null; 89 originalLocale = null; 90 } 91 92 public void testSubclassableConstructor() { 94 PeriodFormat f = new PeriodFormat() { 95 }; 97 assertNotNull(f); 98 } 99 100 public void testFormatStandard() { 102 Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8); 103 assertEquals("1 day, 5 hours, 6 minutes, 7 seconds and 8 milliseconds", PeriodFormat.getDefault().print(p)); 104 } 105 106 public void testFormatOneField() { 108 Period p = Period.days(2); 109 assertEquals("2 days", PeriodFormat.getDefault().print(p)); 110 } 111 112 public void testFormatTwoFields() { 114 Period p = Period.days(2).withHours(5); 115 assertEquals("2 days and 5 hours", PeriodFormat.getDefault().print(p)); 116 } 117 118 public void testParseOneField() { 120 Period p = Period.days(2); 121 assertEquals(p, PeriodFormat.getDefault().parsePeriod("2 days")); 122 } 123 124 public void testParseTwoFields() { 126 Period p = Period.days(2).withHours(5); 127 assertEquals(p, PeriodFormat.getDefault().parsePeriod("2 days and 5 hours")); 128 } 129 130 } 131 | Popular Tags |