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.format;
017:
018: import java.util.Locale;
019: import java.util.TimeZone;
020:
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.joda.time.DateTimeConstants;
025: import org.joda.time.DateTimeUtils;
026: import org.joda.time.DateTimeZone;
027: import org.joda.time.Period;
028: import org.joda.time.PeriodType;
029:
030: /**
031: * This class is a Junit unit test for PeriodFormat.
032: *
033: * @author Stephen Colebourne
034: */
035: public class TestPeriodFormatParsing extends TestCase {
036:
037: private static final Period PERIOD = new Period(1, 2, 3, 4, 5, 6,
038: 7, 8);
039: private static final Period EMPTY_PERIOD = new Period(0, 0, 0, 0,
040: 0, 0, 0, 0);
041: private static final Period YEAR_DAY_PERIOD = new Period(1, 0, 0,
042: 4, 5, 6, 7, 8, PeriodType.yearDayTime());
043: private static final Period EMPTY_YEAR_DAY_PERIOD = new Period(0,
044: 0, 0, 0, 0, 0, 0, 0, PeriodType.yearDayTime());
045: private static final Period TIME_PERIOD = new Period(0, 0, 0, 0, 5,
046: 6, 7, 8);
047: private static final Period DATE_PERIOD = new Period(1, 2, 3, 4, 0,
048: 0, 0, 0);
049:
050: private static final DateTimeZone PARIS = DateTimeZone
051: .forID("Europe/Paris");
052: private static final DateTimeZone LONDON = DateTimeZone
053: .forID("Europe/London");
054: private static final DateTimeZone TOKYO = DateTimeZone
055: .forID("Asia/Tokyo");
056:
057: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
058: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
059: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
060: + 365 + 365 + 366 + 365;
061: // 2002-06-09
062: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
063: + 31L + 9L - 1L)
064: * DateTimeConstants.MILLIS_PER_DAY;
065:
066: private DateTimeZone originalDateTimeZone = null;
067: private TimeZone originalTimeZone = null;
068: private Locale originalLocale = null;
069:
070: public static void main(String[] args) {
071: junit.textui.TestRunner.run(suite());
072: }
073:
074: public static TestSuite suite() {
075: return new TestSuite(TestPeriodFormatParsing.class);
076: }
077:
078: public TestPeriodFormatParsing(String name) {
079: super (name);
080: }
081:
082: protected void setUp() throws Exception {
083: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
084: originalDateTimeZone = DateTimeZone.getDefault();
085: originalTimeZone = TimeZone.getDefault();
086: originalLocale = Locale.getDefault();
087: DateTimeZone.setDefault(LONDON);
088: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
089: Locale.setDefault(Locale.UK);
090: }
091:
092: protected void tearDown() throws Exception {
093: DateTimeUtils.setCurrentMillisSystem();
094: DateTimeZone.setDefault(originalDateTimeZone);
095: TimeZone.setDefault(originalTimeZone);
096: Locale.setDefault(originalLocale);
097: originalDateTimeZone = null;
098: originalTimeZone = null;
099: originalLocale = null;
100: }
101:
102: //-----------------------------------------------------------------------
103: public void testParseStandard1() {
104: PeriodFormatter parser = PeriodFormat.getDefault();
105: Period p = parser.parsePeriod("6 years, 3 months and 2 days");
106: assertEquals(new Period(6, 3, 0, 2, 0, 0, 0, 0), p);
107: }
108:
109: public void testParseCustom1() {
110: PeriodFormatter formatter = new PeriodFormatterBuilder()
111: .printZeroAlways().appendHours().appendSuffix(":")
112: .minimumPrintedDigits(2).appendMinutes().toFormatter();
113:
114: Period p;
115:
116: p = new Period(47, 55, 0, 0);
117: assertEquals("47:55", formatter.print(p));
118: assertEquals(p, formatter.parsePeriod("47:55"));
119: assertEquals(p, formatter.parsePeriod("047:055"));
120:
121: p = new Period(7, 5, 0, 0);
122: assertEquals("7:05", formatter.print(p));
123: assertEquals(p, formatter.parsePeriod("7:05"));
124: assertEquals(p, formatter.parsePeriod("7:5"));
125: assertEquals(p, formatter.parsePeriod("07:05"));
126:
127: p = new Period(0, 5, 0, 0);
128: assertEquals("0:05", formatter.print(p));
129: assertEquals(p, formatter.parsePeriod("0:05"));
130: assertEquals(p, formatter.parsePeriod("0:5"));
131: assertEquals(p, formatter.parsePeriod("00:005"));
132: assertEquals(p, formatter.parsePeriod("0:005"));
133:
134: p = new Period(0, 0, 0, 0);
135: assertEquals("0:00", formatter.print(p));
136: assertEquals(p, formatter.parsePeriod("0:00"));
137: assertEquals(p, formatter.parsePeriod("0:0"));
138: assertEquals(p, formatter.parsePeriod("00:00"));
139: }
140:
141: }
|