KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > format > TestISOPeriodFormatParsing


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.format;
17
18 import java.util.Locale JavaDoc;
19 import java.util.TimeZone JavaDoc;
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 /**
31  * This class is a Junit unit test for ISOPeriodFormat.
32  *
33  * @author Stephen Colebourne
34  */

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     // 2002-06-09
53
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 JavaDoc originalTimeZone = null;
57     private Locale JavaDoc originalLocale = null;
58
59     public static void main(String JavaDoc[] 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 JavaDoc name) {
68         super(name);
69     }
70
71     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
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     //-----------------------------------------------------------------------
92
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     //-----------------------------------------------------------------------
99
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     //-----------------------------------------------------------------------
106
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     //-----------------------------------------------------------------------
113
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     //-----------------------------------------------------------------------
120
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     //-----------------------------------------------------------------------
127
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     //-----------------------------------------------------------------------
134
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     //-----------------------------------------------------------------------
141
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     //-----------------------------------------------------------------------
148
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     //-----------------------------------------------------------------------
155
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     //-----------------------------------------------------------------------
162
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     //-----------------------------------------------------------------------
169
public void testParseStandardFail1() {
170         PeriodFormatter parser = ISOPeriodFormat.standard();
171         try {
172             parser.parsePeriod("P1Y2S");
173             fail();
174         } catch (IllegalArgumentException JavaDoc ex) {}
175     }
176
177     //-----------------------------------------------------------------------
178
public void testParseStandardFail2() {
179         PeriodFormatter parser = ISOPeriodFormat.standard();
180         try {
181             parser.parsePeriod("PS");
182             fail();
183         } catch (IllegalArgumentException JavaDoc ex) {}
184     }
185
186     //-----------------------------------------------------------------------
187
public void testParseStandardFail3() {
188         PeriodFormatter parser = ISOPeriodFormat.standard();
189         try {
190             parser.parsePeriod("PTS");
191             fail();
192         } catch (IllegalArgumentException JavaDoc ex) {}
193     }
194
195     //-----------------------------------------------------------------------
196
public void testParseStandardFail4() {
197         PeriodFormatter parser = ISOPeriodFormat.standard();
198         try {
199             parser.parsePeriod("PXS");
200             fail();
201         } catch (IllegalArgumentException JavaDoc ex) {}
202     }
203
204 }
205
Popular Tags