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 TestPeriodFormat 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(TestPeriodFormat.class);
076: }
077:
078: public TestPeriodFormat(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 testSubclassableConstructor() {
104: PeriodFormat f = new PeriodFormat() {
105: // test constructor is protected
106: };
107: assertNotNull(f);
108: }
109:
110: //-----------------------------------------------------------------------
111: public void testFormatStandard() {
112: Period p = new Period(0, 0, 0, 1, 5, 6, 7, 8);
113: assertEquals(
114: "1 day, 5 hours, 6 minutes, 7 seconds and 8 milliseconds",
115: PeriodFormat.getDefault().print(p));
116: }
117:
118: //-----------------------------------------------------------------------
119: public void testFormatOneField() {
120: Period p = Period.days(2);
121: assertEquals("2 days", PeriodFormat.getDefault().print(p));
122: }
123:
124: //-----------------------------------------------------------------------
125: public void testFormatTwoFields() {
126: Period p = Period.days(2).withHours(5);
127: assertEquals("2 days and 5 hours", PeriodFormat.getDefault()
128: .print(p));
129: }
130:
131: //-----------------------------------------------------------------------
132: public void testParseOneField() {
133: Period p = Period.days(2);
134: assertEquals(p, PeriodFormat.getDefault().parsePeriod("2 days"));
135: }
136:
137: //-----------------------------------------------------------------------
138: public void testParseTwoFields() {
139: Period p = Period.days(2).withHours(5);
140: assertEquals(p, PeriodFormat.getDefault().parsePeriod(
141: "2 days and 5 hours"));
142: }
143:
144: }
|