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 ISOPeriodFormat.
032: *
033: * @author Stephen Colebourne
034: */
035: public class TestISOPeriodFormat 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(TestISOPeriodFormat.class);
076: }
077:
078: public TestISOPeriodFormat(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: ISOPeriodFormat f = new ISOPeriodFormat() {
105: // test constructor is protected
106: };
107: assertNotNull(f);
108: }
109:
110: //-----------------------------------------------------------------------
111: public void testFormatStandard() {
112: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
113: assertEquals("P1Y2M3W4DT5H6M7.008S", ISOPeriodFormat.standard()
114: .print(p));
115: p = new Period(1, 2, 3, 4, 5, 6, 7, 0);
116: assertEquals("P1Y2M3W4DT5H6M7S", ISOPeriodFormat.standard()
117: .print(p));
118:
119: p = new Period(0);
120: assertEquals("PT0S", ISOPeriodFormat.standard().print(p));
121: p = new Period(0, PeriodType.standard().withMillisRemoved()
122: .withSecondsRemoved());
123: assertEquals("PT0M", ISOPeriodFormat.standard().print(p));
124:
125: assertEquals("P1Y4DT5H6M7.008S", ISOPeriodFormat.standard()
126: .print(YEAR_DAY_PERIOD));
127: assertEquals("PT0S", ISOPeriodFormat.standard().print(
128: EMPTY_YEAR_DAY_PERIOD));
129: assertEquals("P1Y2M3W4D", ISOPeriodFormat.standard().print(
130: DATE_PERIOD));
131: assertEquals("PT5H6M7.008S", ISOPeriodFormat.standard().print(
132: TIME_PERIOD));
133: }
134:
135: //-----------------------------------------------------------------------
136: public void testFormatAlternate() {
137: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
138: assertEquals("P00010204T050607.008", ISOPeriodFormat
139: .alternate().print(p));
140: p = new Period(1, 2, 3, 4, 5, 6, 7, 0);
141: assertEquals("P00010204T050607", ISOPeriodFormat.alternate()
142: .print(p));
143:
144: p = new Period(0);
145: assertEquals("P00000000T000000", ISOPeriodFormat.alternate()
146: .print(p));
147: p = new Period(0, PeriodType.standard().withMillisRemoved()
148: .withSecondsRemoved());
149: assertEquals("P00000000T000000", ISOPeriodFormat.alternate()
150: .print(p));
151:
152: assertEquals("P00010004T050607.008", ISOPeriodFormat
153: .alternate().print(YEAR_DAY_PERIOD));
154: assertEquals("P00000000T000000", ISOPeriodFormat.alternate()
155: .print(EMPTY_YEAR_DAY_PERIOD));
156: assertEquals("P00010204T000000", ISOPeriodFormat.alternate()
157: .print(DATE_PERIOD));
158: assertEquals("P00000000T050607.008", ISOPeriodFormat
159: .alternate().print(TIME_PERIOD));
160: }
161:
162: //-----------------------------------------------------------------------
163: public void testFormatAlternateExtended() {
164: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
165: assertEquals("P0001-02-04T05:06:07.008", ISOPeriodFormat
166: .alternateExtended().print(p));
167: p = new Period(1, 2, 3, 4, 5, 6, 7, 0);
168: assertEquals("P0001-02-04T05:06:07", ISOPeriodFormat
169: .alternateExtended().print(p));
170:
171: p = new Period(0);
172: assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat
173: .alternateExtended().print(p));
174: p = new Period(0, PeriodType.standard().withMillisRemoved()
175: .withSecondsRemoved());
176: assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat
177: .alternateExtended().print(p));
178:
179: assertEquals("P0001-00-04T05:06:07.008", ISOPeriodFormat
180: .alternateExtended().print(YEAR_DAY_PERIOD));
181: assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat
182: .alternateExtended().print(EMPTY_YEAR_DAY_PERIOD));
183: assertEquals("P0001-02-04T00:00:00", ISOPeriodFormat
184: .alternateExtended().print(DATE_PERIOD));
185: assertEquals("P0000-00-00T05:06:07.008", ISOPeriodFormat
186: .alternateExtended().print(TIME_PERIOD));
187: }
188:
189: //-----------------------------------------------------------------------
190: public void testFormatAlternateWithWeeks() {
191: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
192: assertEquals("P0001W0304T050607.008", ISOPeriodFormat
193: .alternateWithWeeks().print(p));
194: p = new Period(1, 2, 3, 4, 5, 6, 7, 0);
195: assertEquals("P0001W0304T050607", ISOPeriodFormat
196: .alternateWithWeeks().print(p));
197:
198: p = new Period(0);
199: assertEquals("P0000W0000T000000", ISOPeriodFormat
200: .alternateWithWeeks().print(p));
201: p = new Period(0, PeriodType.standard().withMillisRemoved()
202: .withSecondsRemoved());
203: assertEquals("P0000W0000T000000", ISOPeriodFormat
204: .alternateWithWeeks().print(p));
205:
206: assertEquals("P0001W0004T050607.008", ISOPeriodFormat
207: .alternateWithWeeks().print(YEAR_DAY_PERIOD));
208: assertEquals("P0000W0000T000000", ISOPeriodFormat
209: .alternateWithWeeks().print(EMPTY_YEAR_DAY_PERIOD));
210: assertEquals("P0001W0304T000000", ISOPeriodFormat
211: .alternateWithWeeks().print(DATE_PERIOD));
212: assertEquals("P0000W0000T050607.008", ISOPeriodFormat
213: .alternateWithWeeks().print(TIME_PERIOD));
214: }
215:
216: //-----------------------------------------------------------------------
217: public void testFormatAlternateExtendedWithWeeks() {
218: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
219: assertEquals("P0001-W03-04T05:06:07.008", ISOPeriodFormat
220: .alternateExtendedWithWeeks().print(p));
221: p = new Period(1, 2, 3, 4, 5, 6, 7, 0);
222: assertEquals("P0001-W03-04T05:06:07", ISOPeriodFormat
223: .alternateExtendedWithWeeks().print(p));
224:
225: p = new Period(0);
226: assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat
227: .alternateExtendedWithWeeks().print(p));
228: p = new Period(0, PeriodType.standard().withMillisRemoved()
229: .withSecondsRemoved());
230: assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat
231: .alternateExtendedWithWeeks().print(p));
232:
233: assertEquals("P0001-W00-04T05:06:07.008", ISOPeriodFormat
234: .alternateExtendedWithWeeks().print(YEAR_DAY_PERIOD));
235: assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat
236: .alternateExtendedWithWeeks().print(
237: EMPTY_YEAR_DAY_PERIOD));
238: assertEquals("P0001-W03-04T00:00:00", ISOPeriodFormat
239: .alternateExtendedWithWeeks().print(DATE_PERIOD));
240: assertEquals("P0000-W00-00T05:06:07.008", ISOPeriodFormat
241: .alternateExtendedWithWeeks().print(TIME_PERIOD));
242: }
243:
244: }
|