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 TestISOPeriodFormatParsing 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(TestISOPeriodFormatParsing.class);
076: }
077:
078: public TestISOPeriodFormatParsing(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 = ISOPeriodFormat.standard();
105: Period p = parser.parsePeriod("P1Y2M3W4DT5H6M7.008S");
106: assertEquals(new Period(1, 2, 3, 4, 5, 6, 7, 8), p);
107: }
108:
109: //-----------------------------------------------------------------------
110: public void testParseStandard2() {
111: PeriodFormatter parser = ISOPeriodFormat.standard();
112: Period p = parser.parsePeriod("P0Y0M0W0DT5H6M7.008S");
113: assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 8), p);
114: }
115:
116: //-----------------------------------------------------------------------
117: public void testParseStandard3() {
118: PeriodFormatter parser = ISOPeriodFormat.standard();
119: Period p = parser.parsePeriod("P0DT5H6M7.008S");
120: assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 8), p);
121: }
122:
123: //-----------------------------------------------------------------------
124: public void testParseStandard4() {
125: PeriodFormatter parser = ISOPeriodFormat.standard();
126: Period p = parser.parsePeriod("P2Y3DT5H6M7.008S");
127: assertEquals(new Period(2, 0, 0, 3, 5, 6, 7, 8), p);
128: }
129:
130: //-----------------------------------------------------------------------
131: public void testParseStandard5() {
132: PeriodFormatter parser = ISOPeriodFormat.standard();
133: Period p = parser.parsePeriod("P2YT5H6M7.008S");
134: assertEquals(new Period(2, 0, 0, 0, 5, 6, 7, 8), p);
135: }
136:
137: //-----------------------------------------------------------------------
138: public void testParseStandard6() {
139: PeriodFormatter parser = ISOPeriodFormat.standard();
140: Period p = parser.parsePeriod("PT5H6M7.008S");
141: assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 8), p);
142: }
143:
144: //-----------------------------------------------------------------------
145: public void testParseStandard7() {
146: PeriodFormatter parser = ISOPeriodFormat.standard();
147: Period p = parser.parsePeriod("P1Y2M3W4D");
148: assertEquals(new Period(1, 2, 3, 4, 0, 0, 0, 0), p);
149: }
150:
151: //-----------------------------------------------------------------------
152: public void testParseStandard8() {
153: PeriodFormatter parser = ISOPeriodFormat.standard();
154: Period p = parser.parsePeriod("PT5H6M7S");
155: assertEquals(new Period(0, 0, 0, 0, 5, 6, 7, 0), p);
156: }
157:
158: //-----------------------------------------------------------------------
159: public void testParseStandard9() {
160: PeriodFormatter parser = ISOPeriodFormat.standard();
161: Period p = parser.parsePeriod("PT0S");
162: assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 0), p);
163: }
164:
165: //-----------------------------------------------------------------------
166: public void testParseStandard10() {
167: PeriodFormatter parser = ISOPeriodFormat.standard();
168: Period p = parser.parsePeriod("P0D");
169: assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 0), p);
170: }
171:
172: //-----------------------------------------------------------------------
173: public void testParseStandard11() {
174: PeriodFormatter parser = ISOPeriodFormat.standard();
175: Period p = parser.parsePeriod("P0Y");
176: assertEquals(new Period(0, 0, 0, 0, 0, 0, 0, 0), p);
177: }
178:
179: //-----------------------------------------------------------------------
180: public void testParseStandardFail1() {
181: PeriodFormatter parser = ISOPeriodFormat.standard();
182: try {
183: parser.parsePeriod("P1Y2S");
184: fail();
185: } catch (IllegalArgumentException ex) {
186: }
187: }
188:
189: //-----------------------------------------------------------------------
190: public void testParseStandardFail2() {
191: PeriodFormatter parser = ISOPeriodFormat.standard();
192: try {
193: parser.parsePeriod("PS");
194: fail();
195: } catch (IllegalArgumentException ex) {
196: }
197: }
198:
199: //-----------------------------------------------------------------------
200: public void testParseStandardFail3() {
201: PeriodFormatter parser = ISOPeriodFormat.standard();
202: try {
203: parser.parsePeriod("PTS");
204: fail();
205: } catch (IllegalArgumentException ex) {
206: }
207: }
208:
209: //-----------------------------------------------------------------------
210: public void testParseStandardFail4() {
211: PeriodFormatter parser = ISOPeriodFormat.standard();
212: try {
213: parser.parsePeriod("PXS");
214: fail();
215: } catch (IllegalArgumentException ex) {
216: }
217: }
218:
219: }
|