KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.CharArrayWriter JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.TimeZone JavaDoc;
21
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import org.joda.time.Chronology;
26 import org.joda.time.DateTimeConstants;
27 import org.joda.time.DateTimeUtils;
28 import org.joda.time.DateTimeZone;
29 import org.joda.time.MutablePeriod;
30 import org.joda.time.Period;
31 import org.joda.time.PeriodType;
32 import org.joda.time.chrono.BuddhistChronology;
33 import org.joda.time.chrono.ISOChronology;
34
35 /**
36  * This class is a Junit unit test for Period Formating.
37  *
38  * @author Stephen Colebourne
39  */

40 public class TestPeriodFormatter extends TestCase {
41
42     private static final DateTimeZone UTC = DateTimeZone.UTC;
43     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
44     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
45     private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
46     private static final DateTimeZone NEWYORK = DateTimeZone.forID("America/New_York");
47     private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
48     private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
49     private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS);
50
51     long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
52                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
53                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
54                      366 + 365;
55     // 2002-06-09
56
private long TEST_TIME_NOW =
57             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
58
59     private DateTimeZone originalDateTimeZone = null;
60     private TimeZone JavaDoc originalTimeZone = null;
61     private Locale JavaDoc originalLocale = null;
62     private PeriodFormatter f = null;
63
64     public static void main(String JavaDoc[] args) {
65         junit.textui.TestRunner.run(suite());
66     }
67
68     public static TestSuite suite() {
69         return new TestSuite(TestPeriodFormatter.class);
70     }
71
72     public TestPeriodFormatter(String JavaDoc name) {
73         super(name);
74     }
75
76     protected void setUp() throws Exception JavaDoc {
77         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
78         originalDateTimeZone = DateTimeZone.getDefault();
79         originalTimeZone = TimeZone.getDefault();
80         originalLocale = Locale.getDefault();
81         DateTimeZone.setDefault(LONDON);
82         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
83         Locale.setDefault(Locale.UK);
84         f = ISOPeriodFormat.standard();
85     }
86
87     protected void tearDown() throws Exception JavaDoc {
88         DateTimeUtils.setCurrentMillisSystem();
89         DateTimeZone.setDefault(originalDateTimeZone);
90         TimeZone.setDefault(originalTimeZone);
91         Locale.setDefault(originalLocale);
92         originalDateTimeZone = null;
93         originalTimeZone = null;
94         originalLocale = null;
95         f = null;
96     }
97
98     //-----------------------------------------------------------------------
99
public void testPrint_simple() {
100         Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
101         assertEquals("P1Y2M3W4DT5H6M7.008S", f.print(p));
102     }
103
104     //-----------------------------------------------------------------------
105
public void testPrint_bufferMethods() throws Exception JavaDoc {
106         Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
107         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
108         f.printTo(buf, p);
109         assertEquals("P1Y2M3W4DT5H6M7.008S", buf.toString());
110         
111         buf = new StringBuffer JavaDoc();
112         try {
113             f.printTo(buf, null);
114             fail();
115         } catch (IllegalArgumentException JavaDoc ex) {}
116     }
117
118     //-----------------------------------------------------------------------
119
public void testPrint_writerMethods() throws Exception JavaDoc {
120         Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
121         CharArrayWriter JavaDoc out = new CharArrayWriter JavaDoc();
122         f.printTo(out, p);
123         assertEquals("P1Y2M3W4DT5H6M7.008S", out.toString());
124         
125         out = new CharArrayWriter JavaDoc();
126         try {
127             f.printTo(out, null);
128             fail();
129         } catch (IllegalArgumentException JavaDoc ex) {}
130     }
131
132     //-----------------------------------------------------------------------
133
public void testWithGetLocaleMethods() {
134         PeriodFormatter f2 = f.withLocale(Locale.FRENCH);
135         assertEquals(Locale.FRENCH, f2.getLocale());
136         assertSame(f2, f2.withLocale(Locale.FRENCH));
137         
138         f2 = f.withLocale(null);
139         assertEquals(null, f2.getLocale());
140         assertSame(f2, f2.withLocale(null));
141     }
142
143     public void testWithGetParseTypeMethods() {
144         PeriodFormatter f2 = f.withParseType(PeriodType.dayTime());
145         assertEquals(PeriodType.dayTime(), f2.getParseType());
146         assertSame(f2, f2.withParseType(PeriodType.dayTime()));
147         
148         f2 = f.withParseType(null);
149         assertEquals(null, f2.getParseType());
150         assertSame(f2, f2.withParseType(null));
151     }
152
153     public void testPrinterParserMethods() {
154         Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
155         PeriodFormatter f2 = new PeriodFormatter(f.getPrinter(), f.getParser());
156         assertEquals(f.getPrinter(), f2.getPrinter());
157         assertEquals(f.getParser(), f2.getParser());
158         assertEquals(true, f2.isPrinter());
159         assertEquals(true, f2.isParser());
160         assertNotNull(f2.print(p));
161         assertNotNull(f2.parsePeriod("P1Y2M3W4DT5H6M7.008S"));
162         
163         f2 = new PeriodFormatter(f.getPrinter(), null);
164         assertEquals(f.getPrinter(), f2.getPrinter());
165         assertEquals(null, f2.getParser());
166         assertEquals(true, f2.isPrinter());
167         assertEquals(false, f2.isParser());
168         assertNotNull(f2.print(p));
169         try {
170             assertNotNull(f2.parsePeriod("P1Y2M3W4DT5H6M7.008S"));
171             fail();
172         } catch (UnsupportedOperationException JavaDoc ex) {}
173         
174         f2 = new PeriodFormatter(null, f.getParser());
175         assertEquals(null, f2.getPrinter());
176         assertEquals(f.getParser(), f2.getParser());
177         assertEquals(false, f2.isPrinter());
178         assertEquals(true, f2.isParser());
179         try {
180             f2.print(p);
181             fail();
182         } catch (UnsupportedOperationException JavaDoc ex) {}
183         assertNotNull(f2.parsePeriod("P1Y2M3W4DT5H6M7.008S"));
184     }
185
186     //-----------------------------------------------------------------------
187
public void testParsePeriod_simple() {
188         Period expect = new Period(1, 2, 3, 4, 5, 6, 7, 8);
189         assertEquals(expect, f.parsePeriod("P1Y2M3W4DT5H6M7.008S"));
190         
191         try {
192             f.parsePeriod("ABC");
193             fail();
194         } catch (IllegalArgumentException JavaDoc ex) {}
195     }
196
197     public void testParsePeriod_parseType() {
198         Period expect = new Period(0, 0, 0, 4, 5, 6, 7, 8, PeriodType.dayTime());
199         assertEquals(expect, f.withParseType(PeriodType.dayTime()).parsePeriod("P4DT5H6M7.008S"));
200         try {
201             f.withParseType(PeriodType.dayTime()).parsePeriod("P3W4DT5H6M7.008S");
202             fail();
203         } catch (IllegalArgumentException JavaDoc ex) {}
204     }
205
206     //-----------------------------------------------------------------------
207
public void testParseMutablePeriod_simple() {
208         MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
209         assertEquals(expect, f.parseMutablePeriod("P1Y2M3W4DT5H6M7.008S"));
210         
211         try {
212             f.parseMutablePeriod("ABC");
213             fail();
214         } catch (IllegalArgumentException JavaDoc ex) {}
215     }
216
217     //-----------------------------------------------------------------------
218
public void testParseInto_simple() {
219         MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
220         MutablePeriod result = new MutablePeriod();
221         assertEquals(20, f.parseInto(result, "P1Y2M3W4DT5H6M7.008S", 0));
222         assertEquals(expect, result);
223         
224         try {
225             f.parseInto(null, "P1Y2M3W4DT5H6M7.008S", 0);
226             fail();
227         } catch (IllegalArgumentException JavaDoc ex) {}
228         
229         assertEquals(~0, f.parseInto(result, "ABC", 0));
230     }
231
232 }
233
Popular Tags