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.forID("Europe/Paris");
044        private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
045        private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
046        private static final DateTimeZone NEWYORK = DateTimeZone.forID("America/New_York");
047        private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
048        private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
049        private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS);
050    
051        long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 
052                         366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 
053                         365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
054                         366 + 365;
055        // 2002-06-09
056        private long TEST_TIME_NOW =
057                (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
058    
059        private DateTimeZone originalDateTimeZone = null;
060        private TimeZone originalTimeZone = null;
061        private Locale originalLocale = null;
062        private PeriodFormatter f = null;
063    
064        public static void main(String[] args) {
065            junit.textui.TestRunner.run(suite());
066        }
067    
068        public static TestSuite suite() {
069            return new TestSuite(TestPeriodFormatter.class);
070        }
071    
072        public TestPeriodFormatter(String name) {
073            super(name);
074        }
075    
076        protected void setUp() throws Exception {
077            DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
078            originalDateTimeZone = DateTimeZone.getDefault();
079            originalTimeZone = TimeZone.getDefault();
080            originalLocale = Locale.getDefault();
081            DateTimeZone.setDefault(LONDON);
082            TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
083            Locale.setDefault(Locale.UK);
084            f = ISOPeriodFormat.standard();
085        }
086    
087        protected void tearDown() throws Exception {
088            DateTimeUtils.setCurrentMillisSystem();
089            DateTimeZone.setDefault(originalDateTimeZone);
090            TimeZone.setDefault(originalTimeZone);
091            Locale.setDefault(originalLocale);
092            originalDateTimeZone = null;
093            originalTimeZone = null;
094            originalLocale = null;
095            f = null;
096        }
097    
098        //-----------------------------------------------------------------------
099        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 {
106            Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
107            StringBuffer buf = new StringBuffer();
108            f.printTo(buf, p);
109            assertEquals("P1Y2M3W4DT5H6M7.008S", buf.toString());
110            
111            buf = new StringBuffer();
112            try {
113                f.printTo(buf, null);
114                fail();
115            } catch (IllegalArgumentException ex) {}
116        }
117    
118        //-----------------------------------------------------------------------
119        public void testPrint_writerMethods() throws Exception {
120            Period p = new Period(1, 2, 3, 4, 5, 6, 7, 8);
121            CharArrayWriter out = new CharArrayWriter();
122            f.printTo(out, p);
123            assertEquals("P1Y2M3W4DT5H6M7.008S", out.toString());
124            
125            out = new CharArrayWriter();
126            try {
127                f.printTo(out, null);
128                fail();
129            } catch (IllegalArgumentException 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 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 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 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 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 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 ex) {}
228            
229            assertEquals(~0, f.parseInto(result, "ABC", 0));
230        }
231    
232    }