KickJava   Java API By Example, From Geeks To Geeks.

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


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 PeriodFormat.
32  *
33  * @author Stephen Colebourne
34  */

35 public class TestPeriodFormat 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(TestPeriodFormat.class);
66     }
67
68     public TestPeriodFormat(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         PeriodFormat f = new PeriodFormat() {
95             // test constructor is protected
96
};
97         assertNotNull(f);
98     }
99
100     //-----------------------------------------------------------------------
101
public void testFormatStandard() {
102         Period p = new Period(0, 0, 0, 1, 5, 6 ,7, 8);
103         assertEquals("1 day, 5 hours, 6 minutes, 7 seconds and 8 milliseconds", PeriodFormat.getDefault().print(p));
104     }
105
106     //-----------------------------------------------------------------------
107
public void testFormatOneField() {
108         Period p = Period.days(2);
109         assertEquals("2 days", PeriodFormat.getDefault().print(p));
110     }
111
112     //-----------------------------------------------------------------------
113
public void testFormatTwoFields() {
114         Period p = Period.days(2).withHours(5);
115         assertEquals("2 days and 5 hours", PeriodFormat.getDefault().print(p));
116     }
117
118     //-----------------------------------------------------------------------
119
public void testParseOneField() {
120         Period p = Period.days(2);
121         assertEquals(p, PeriodFormat.getDefault().parsePeriod("2 days"));
122     }
123
124     //-----------------------------------------------------------------------
125
public void testParseTwoFields() {
126         Period p = Period.days(2).withHours(5);
127         assertEquals(p, PeriodFormat.getDefault().parsePeriod("2 days and 5 hours"));
128     }
129
130 }
131
Popular Tags