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 TestISOPeriodFormatParsing 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 = (y2002days + 31L + 28L + 31L + 30L + 31L + 9L - 1L) * DateTimeConstants.MILLIS_PER_DAY; 54 55 private DateTimeZone originalDateTimeZone = null; 56 private TimeZone originalTimeZone = null; 57 private Locale originalLocale = null; 58 59 public static void main(String [] args) { 60 junit.textui.TestRunner.run(suite()); 61 } 62 63 public static TestSuite suite() { 64 return new TestSuite(TestISOPeriodFormatParsing.class); 65 } 66 67 public TestISOPeriodFormatParsing(String name) { 68 super(name); 69 } 70 71 protected void setUp() throws Exception { 72 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 73 originalDateTimeZone = DateTimeZone.getDefault(); 74 originalTimeZone = TimeZone.getDefault(); 75 originalLocale = Locale.getDefault(); 76 DateTimeZone.setDefault(LONDON); 77 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 78 Locale.setDefault(Locale.UK); 79 } 80 81 protected void tearDown() throws Exception { 82 DateTimeUtils.setCurrentMillisSystem(); 83 DateTimeZone.setDefault(originalDateTimeZone); 84 TimeZone.setDefault(originalTimeZone); 85 Locale.setDefault(originalLocale); 86 originalDateTimeZone = null; 87 originalTimeZone = null; 88 originalLocale = null; 89 } 90 91 public void testParseStandard1() { 93 PeriodFormatter parser = ISOPeriodFormat.standard(); 94 Period p = parser.parsePeriod("P1Y2M3W4DT5H6M7.008S"); 95 assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), p); 96 } 97 98 public void testParseStandard2() { 100 PeriodFormatter parser = ISOPeriodFormat.standard(); 101 Period p = parser.parsePeriod("P0Y0M0W0DT5H6M7.008S"); 102 assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 8), p); 103 } 104 105 public void testParseStandard3() { 107 PeriodFormatter parser = ISOPeriodFormat.standard(); 108 Period p = parser.parsePeriod("P0DT5H6M7.008S"); 109 assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 8), p); 110 } 111 112 public void testParseStandard4() { 114 PeriodFormatter parser = ISOPeriodFormat.standard(); 115 Period p = parser.parsePeriod("P2Y3DT5H6M7.008S"); 116 assertEquals(new Period(2, 0, 0, 3, 5, 6, 7, 8), p); 117 } 118 119 public void testParseStandard5() { 121 PeriodFormatter parser = ISOPeriodFormat.standard(); 122 Period p = parser.parsePeriod("P2YT5H6M7.008S"); 123 assertEquals(new Period(2, 0, 0, 0, 5, 6, 7, 8), p); 124 } 125 126 public void testParseStandard6() { 128 PeriodFormatter parser = ISOPeriodFormat.standard(); 129 Period p = parser.parsePeriod("PT5H6M7.008S"); 130 assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 8), p); 131 } 132 133 public void testParseStandard7() { 135 PeriodFormatter parser = ISOPeriodFormat.standard(); 136 Period p = parser.parsePeriod("P1Y2M3W4D"); 137 assertEquals(new Period(1, 2, 3, 4, 0, 0, 0, 0), p); 138 } 139 140 public void testParseStandard8() { 142 PeriodFormatter parser = ISOPeriodFormat.standard(); 143 Period p = parser.parsePeriod("PT5H6M7S"); 144 assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 0), p); 145 } 146 147 public void testParseStandard9() { 149 PeriodFormatter parser = ISOPeriodFormat.standard(); 150 Period p = parser.parsePeriod("PT0S"); 151 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 0), p); 152 } 153 154 public void testParseStandard10() { 156 PeriodFormatter parser = ISOPeriodFormat.standard(); 157 Period p = parser.parsePeriod("P0D"); 158 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 0), p); 159 } 160 161 public void testParseStandard11() { 163 PeriodFormatter parser = ISOPeriodFormat.standard(); 164 Period p = parser.parsePeriod("P0Y"); 165 assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 0), p); 166 } 167 168 public void testParseStandardFail1() { 170 PeriodFormatter parser = ISOPeriodFormat.standard(); 171 try { 172 parser.parsePeriod("P1Y2S"); 173 fail(); 174 } catch (IllegalArgumentException ex) {} 175 } 176 177 public void testParseStandardFail2() { 179 PeriodFormatter parser = ISOPeriodFormat.standard(); 180 try { 181 parser.parsePeriod("PS"); 182 fail(); 183 } catch (IllegalArgumentException ex) {} 184 } 185 186 public void testParseStandardFail3() { 188 PeriodFormatter parser = ISOPeriodFormat.standard(); 189 try { 190 parser.parsePeriod("PTS"); 191 fail(); 192 } catch (IllegalArgumentException ex) {} 193 } 194 195 public void testParseStandardFail4() { 197 PeriodFormatter parser = ISOPeriodFormat.standard(); 198 try { 199 parser.parsePeriod("PXS"); 200 fail(); 201 } catch (IllegalArgumentException ex) {} 202 } 203 204 } 205 | Popular Tags |