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.io.CharArrayWriter;
019: import java.util.Locale;
020: import java.util.TimeZone;
021:
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: import org.joda.time.Chronology;
026: import org.joda.time.DateTimeConstants;
027: import org.joda.time.DateTimeUtils;
028: import org.joda.time.DateTimeZone;
029: import org.joda.time.MutablePeriod;
030: import org.joda.time.Period;
031: import org.joda.time.PeriodType;
032: import org.joda.time.chrono.BuddhistChronology;
033: import org.joda.time.chrono.ISOChronology;
034:
035: /**
036: * This class is a Junit unit test for Period Formating.
037: *
038: * @author Stephen Colebourne
039: */
040: public class TestPeriodFormatter extends TestCase {
041:
042: private static final DateTimeZone UTC = DateTimeZone.UTC;
043: private static final DateTimeZone PARIS = DateTimeZone
044: .forID("Europe/Paris");
045: private static final DateTimeZone LONDON = DateTimeZone
046: .forID("Europe/London");
047: private static final DateTimeZone TOKYO = DateTimeZone
048: .forID("Asia/Tokyo");
049: private static final DateTimeZone NEWYORK = DateTimeZone
050: .forID("America/New_York");
051: private static final Chronology ISO_UTC = ISOChronology
052: .getInstanceUTC();
053: private static final Chronology ISO_PARIS = ISOChronology
054: .getInstance(PARIS);
055: private static final Chronology BUDDHIST_PARIS = BuddhistChronology
056: .getInstance(PARIS);
057:
058: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
059: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
060: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
061: + 365 + 365 + 366 + 365;
062: // 2002-06-09
063: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
064: + 31L + 9L - 1L)
065: * DateTimeConstants.MILLIS_PER_DAY;
066:
067: private DateTimeZone originalDateTimeZone = null;
068: private TimeZone originalTimeZone = null;
069: private Locale originalLocale = null;
070: private PeriodFormatter f = null;
071:
072: public static void main(String[] args) {
073: junit.textui.TestRunner.run(suite());
074: }
075:
076: public static TestSuite suite() {
077: return new TestSuite(TestPeriodFormatter.class);
078: }
079:
080: public TestPeriodFormatter(String name) {
081: super (name);
082: }
083:
084: protected void setUp() throws Exception {
085: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
086: originalDateTimeZone = DateTimeZone.getDefault();
087: originalTimeZone = TimeZone.getDefault();
088: originalLocale = Locale.getDefault();
089: DateTimeZone.setDefault(LONDON);
090: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
091: Locale.setDefault(Locale.UK);
092: f = ISOPeriodFormat.standard();
093: }
094:
095: protected void tearDown() throws Exception {
096: DateTimeUtils.setCurrentMillisSystem();
097: DateTimeZone.setDefault(originalDateTimeZone);
098: TimeZone.setDefault(originalTimeZone);
099: Locale.setDefault(originalLocale);
100: originalDateTimeZone = null;
101: originalTimeZone = null;
102: originalLocale = null;
103: f = null;
104: }
105:
106: //-----------------------------------------------------------------------
107: public void testPrint_simple() {
108: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
109: assertEquals("P1Y2M3W4DT5H6M7.008S", f.print(p));
110: }
111:
112: //-----------------------------------------------------------------------
113: public void testPrint_bufferMethods() throws Exception {
114: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
115: StringBuffer buf = new StringBuffer();
116: f.printTo(buf, p);
117: assertEquals("P1Y2M3W4DT5H6M7.008S", buf.toString());
118:
119: buf = new StringBuffer();
120: try {
121: f.printTo(buf, null);
122: fail();
123: } catch (IllegalArgumentException ex) {
124: }
125: }
126:
127: //-----------------------------------------------------------------------
128: public void testPrint_writerMethods() throws Exception {
129: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
130: CharArrayWriter out = new CharArrayWriter();
131: f.printTo(out, p);
132: assertEquals("P1Y2M3W4DT5H6M7.008S", out.toString());
133:
134: out = new CharArrayWriter();
135: try {
136: f.printTo(out, null);
137: fail();
138: } catch (IllegalArgumentException ex) {
139: }
140: }
141:
142: //-----------------------------------------------------------------------
143: public void testWithGetLocaleMethods() {
144: PeriodFormatter f2 = f.withLocale(Locale.FRENCH);
145: assertEquals(Locale.FRENCH, f2.getLocale());
146: assertSame(f2, f2.withLocale(Locale.FRENCH));
147:
148: f2 = f.withLocale(null);
149: assertEquals(null, f2.getLocale());
150: assertSame(f2, f2.withLocale(null));
151: }
152:
153: public void testWithGetParseTypeMethods() {
154: PeriodFormatter f2 = f.withParseType(PeriodType.dayTime());
155: assertEquals(PeriodType.dayTime(), f2.getParseType());
156: assertSame(f2, f2.withParseType(PeriodType.dayTime()));
157:
158: f2 = f.withParseType(null);
159: assertEquals(null, f2.getParseType());
160: assertSame(f2, f2.withParseType(null));
161: }
162:
163: public void testPrinterParserMethods() {
164: Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
165: PeriodFormatter f2 = new PeriodFormatter(f.getPrinter(), f
166: .getParser());
167: assertEquals(f.getPrinter(), f2.getPrinter());
168: assertEquals(f.getParser(), f2.getParser());
169: assertEquals(true, f2.isPrinter());
170: assertEquals(true, f2.isParser());
171: assertNotNull(f2.print(p));
172: assertNotNull(f2.parsePeriod("P1Y2M3W4DT5H6M7.008S"));
173:
174: f2 = new PeriodFormatter(f.getPrinter(), null);
175: assertEquals(f.getPrinter(), f2.getPrinter());
176: assertEquals(null, f2.getParser());
177: assertEquals(true, f2.isPrinter());
178: assertEquals(false, f2.isParser());
179: assertNotNull(f2.print(p));
180: try {
181: assertNotNull(f2.parsePeriod("P1Y2M3W4DT5H6M7.008S"));
182: fail();
183: } catch (UnsupportedOperationException ex) {
184: }
185:
186: f2 = new PeriodFormatter(null, f.getParser());
187: assertEquals(null, f2.getPrinter());
188: assertEquals(f.getParser(), f2.getParser());
189: assertEquals(false, f2.isPrinter());
190: assertEquals(true, f2.isParser());
191: try {
192: f2.print(p);
193: fail();
194: } catch (UnsupportedOperationException ex) {
195: }
196: assertNotNull(f2.parsePeriod("P1Y2M3W4DT5H6M7.008S"));
197: }
198:
199: //-----------------------------------------------------------------------
200: public void testParsePeriod_simple() {
201: Period expect = new Period(1, 2, 3, 4, 5, 6, 7, 8);
202: assertEquals(expect, f.parsePeriod("P1Y2M3W4DT5H6M7.008S"));
203:
204: try {
205: f.parsePeriod("ABC");
206: fail();
207: } catch (IllegalArgumentException ex) {
208: }
209: }
210:
211: public void testParsePeriod_parseType() {
212: Period expect = new Period(0, 0, 0, 4, 5, 6, 7, 8, PeriodType
213: .dayTime());
214: assertEquals(expect, f.withParseType(PeriodType.dayTime())
215: .parsePeriod("P4DT5H6M7.008S"));
216: try {
217: f.withParseType(PeriodType.dayTime()).parsePeriod(
218: "P3W4DT5H6M7.008S");
219: fail();
220: } catch (IllegalArgumentException ex) {
221: }
222: }
223:
224: //-----------------------------------------------------------------------
225: public void testParseMutablePeriod_simple() {
226: MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
227: assertEquals(expect, f
228: .parseMutablePeriod("P1Y2M3W4DT5H6M7.008S"));
229:
230: try {
231: f.parseMutablePeriod("ABC");
232: fail();
233: } catch (IllegalArgumentException ex) {
234: }
235: }
236:
237: //-----------------------------------------------------------------------
238: public void testParseInto_simple() {
239: MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8);
240: MutablePeriod result = new MutablePeriod();
241: assertEquals(20, f.parseInto(result, "P1Y2M3W4DT5H6M7.008S", 0));
242: assertEquals(expect, result);
243:
244: try {
245: f.parseInto(null, "P1Y2M3W4DT5H6M7.008S", 0);
246: fail();
247: } catch (IllegalArgumentException ex) {
248: }
249:
250: assertEquals(~0, f.parseInto(result, "ABC", 0));
251: }
252:
253: }
|