KickJava   Java API By Example, From Geeks To Geeks.

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


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 TestISOPeriodFormat 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 =
54             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
55
56     private DateTimeZone originalDateTimeZone = null;
57     private TimeZone JavaDoc originalTimeZone = null;
58     private Locale JavaDoc originalLocale = null;
59
60     public static void main(String JavaDoc[] args) {
61         junit.textui.TestRunner.run(suite());
62     }
63
64     public static TestSuite suite() {
65         return new TestSuite(TestISOPeriodFormat.class);
66     }
67
68     public TestISOPeriodFormat(String JavaDoc name) {
69         super(name);
70     }
71
72     protected void setUp() throws Exception JavaDoc {
73         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
74         originalDateTimeZone = DateTimeZone.getDefault();
75         originalTimeZone = TimeZone.getDefault();
76         originalLocale = Locale.getDefault();
77         DateTimeZone.setDefault(LONDON);
78         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
79         Locale.setDefault(Locale.UK);
80     }
81
82     protected void tearDown() throws Exception JavaDoc {
83         DateTimeUtils.setCurrentMillisSystem();
84         DateTimeZone.setDefault(originalDateTimeZone);
85         TimeZone.setDefault(originalTimeZone);
86         Locale.setDefault(originalLocale);
87         originalDateTimeZone = null;
88         originalTimeZone = null;
89         originalLocale = null;
90     }
91
92     //-----------------------------------------------------------------------
93
public void testSubclassableConstructor() {
94         ISOPeriodFormat f = new ISOPeriodFormat() {
95             // test constructor is protected
96
};
97         assertNotNull(f);
98     }
99
100     //-----------------------------------------------------------------------
101
public void testFormatStandard() {
102         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
103         assertEquals("P1Y2M3W4DT5H6M7.008S", ISOPeriodFormat.standard().print(p));
104         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
105         assertEquals("P1Y2M3W4DT5H6M7S", ISOPeriodFormat.standard().print(p));
106         
107         p = new Period(0);
108         assertEquals("PT0S", ISOPeriodFormat.standard().print(p));
109         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
110         assertEquals("PT0M", ISOPeriodFormat.standard().print(p));
111         
112         assertEquals("P1Y4DT5H6M7.008S", ISOPeriodFormat.standard().print(YEAR_DAY_PERIOD));
113         assertEquals("PT0S", ISOPeriodFormat.standard().print(EMPTY_YEAR_DAY_PERIOD));
114         assertEquals("P1Y2M3W4D", ISOPeriodFormat.standard().print(DATE_PERIOD));
115         assertEquals("PT5H6M7.008S", ISOPeriodFormat.standard().print(TIME_PERIOD));
116     }
117
118     //-----------------------------------------------------------------------
119
public void testFormatAlternate() {
120         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
121         assertEquals("P00010204T050607.008", ISOPeriodFormat.alternate().print(p));
122         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
123         assertEquals("P00010204T050607", ISOPeriodFormat.alternate().print(p));
124         
125         p = new Period(0);
126         assertEquals("P00000000T000000", ISOPeriodFormat.alternate().print(p));
127         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
128         assertEquals("P00000000T000000", ISOPeriodFormat.alternate().print(p));
129         
130         assertEquals("P00010004T050607.008", ISOPeriodFormat.alternate().print(YEAR_DAY_PERIOD));
131         assertEquals("P00000000T000000", ISOPeriodFormat.alternate().print(EMPTY_YEAR_DAY_PERIOD));
132         assertEquals("P00010204T000000", ISOPeriodFormat.alternate().print(DATE_PERIOD));
133         assertEquals("P00000000T050607.008", ISOPeriodFormat.alternate().print(TIME_PERIOD));
134     }
135
136     //-----------------------------------------------------------------------
137
public void testFormatAlternateExtended() {
138         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
139         assertEquals("P0001-02-04T05:06:07.008", ISOPeriodFormat.alternateExtended().print(p));
140         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
141         assertEquals("P0001-02-04T05:06:07", ISOPeriodFormat.alternateExtended().print(p));
142         
143         p = new Period(0);
144         assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat.alternateExtended().print(p));
145         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
146         assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat.alternateExtended().print(p));
147         
148         assertEquals("P0001-00-04T05:06:07.008", ISOPeriodFormat.alternateExtended().print(YEAR_DAY_PERIOD));
149         assertEquals("P0000-00-00T00:00:00", ISOPeriodFormat.alternateExtended().print(EMPTY_YEAR_DAY_PERIOD));
150         assertEquals("P0001-02-04T00:00:00", ISOPeriodFormat.alternateExtended().print(DATE_PERIOD));
151         assertEquals("P0000-00-00T05:06:07.008", ISOPeriodFormat.alternateExtended().print(TIME_PERIOD));
152     }
153
154     //-----------------------------------------------------------------------
155
public void testFormatAlternateWithWeeks() {
156         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
157         assertEquals("P0001W0304T050607.008", ISOPeriodFormat.alternateWithWeeks().print(p));
158         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
159         assertEquals("P0001W0304T050607", ISOPeriodFormat.alternateWithWeeks().print(p));
160         
161         p = new Period(0);
162         assertEquals("P0000W0000T000000", ISOPeriodFormat.alternateWithWeeks().print(p));
163         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
164         assertEquals("P0000W0000T000000", ISOPeriodFormat.alternateWithWeeks().print(p));
165         
166         assertEquals("P0001W0004T050607.008", ISOPeriodFormat.alternateWithWeeks().print(YEAR_DAY_PERIOD));
167         assertEquals("P0000W0000T000000", ISOPeriodFormat.alternateWithWeeks().print(EMPTY_YEAR_DAY_PERIOD));
168         assertEquals("P0001W0304T000000", ISOPeriodFormat.alternateWithWeeks().print(DATE_PERIOD));
169         assertEquals("P0000W0000T050607.008", ISOPeriodFormat.alternateWithWeeks().print(TIME_PERIOD));
170     }
171
172     //-----------------------------------------------------------------------
173
public void testFormatAlternateExtendedWithWeeks() {
174         Period p = new Period(1, 2, 3, 4, 5, 6 ,7, 8);
175         assertEquals("P0001-W03-04T05:06:07.008", ISOPeriodFormat.alternateExtendedWithWeeks().print(p));
176         p = new Period(1, 2, 3, 4, 5, 6 ,7, 0);
177         assertEquals("P0001-W03-04T05:06:07", ISOPeriodFormat.alternateExtendedWithWeeks().print(p));
178         
179         p = new Period(0);
180         assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat.alternateExtendedWithWeeks().print(p));
181         p = new Period(0, PeriodType.standard().withMillisRemoved().withSecondsRemoved());
182         assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat.alternateExtendedWithWeeks().print(p));
183         
184         assertEquals("P0001-W00-04T05:06:07.008", ISOPeriodFormat.alternateExtendedWithWeeks().print(YEAR_DAY_PERIOD));
185         assertEquals("P0000-W00-00T00:00:00", ISOPeriodFormat.alternateExtendedWithWeeks().print(EMPTY_YEAR_DAY_PERIOD));
186         assertEquals("P0001-W03-04T00:00:00", ISOPeriodFormat.alternateExtendedWithWeeks().print(DATE_PERIOD));
187         assertEquals("P0000-W00-00T05:06:07.008", ISOPeriodFormat.alternateExtendedWithWeeks().print(TIME_PERIOD));
188     }
189
190 }
191
Popular Tags