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.convert;
017    
018    import java.lang.reflect.Constructor;
019    import java.lang.reflect.Field;
020    import java.lang.reflect.Modifier;
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.DateTimeZone;
028    import org.joda.time.Duration;
029    import org.joda.time.PeriodType;
030    import org.joda.time.MutablePeriod;
031    import org.joda.time.ReadableDuration;
032    import org.joda.time.chrono.ISOChronology;
033    import org.joda.time.chrono.JulianChronology;
034    
035    /**
036     * This class is a Junit unit test for ReadableDurationConverter.
037     *
038     * @author Stephen Colebourne
039     */
040    public class TestReadableDurationConverter 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 Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
045        private static Chronology JULIAN;
046        private static Chronology ISO;
047        
048        private DateTimeZone zone = null;
049    
050        public static void main(String[] args) {
051            junit.textui.TestRunner.run(suite());
052        }
053    
054        public static TestSuite suite() {
055            return new TestSuite(TestReadableDurationConverter.class);
056        }
057    
058        public TestReadableDurationConverter(String name) {
059            super(name);
060        }
061    
062        @Override
063        protected void setUp() throws Exception {
064            super.setUp();
065            JULIAN = JulianChronology.getInstance();
066            ISO = ISOChronology.getInstance();
067            zone = DateTimeZone.getDefault();
068            DateTimeZone.setDefault(PARIS);
069        }
070    
071        @Override
072        protected void tearDown() throws Exception {
073            super.tearDown();
074            DateTimeZone.setDefault(zone);
075        }
076    
077        //-----------------------------------------------------------------------
078        public void testSingleton() throws Exception {
079            Class cls = ReadableDurationConverter.class;
080            assertEquals(false, Modifier.isPublic(cls.getModifiers()));
081            assertEquals(false, Modifier.isProtected(cls.getModifiers()));
082            assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
083            
084            Constructor con = cls.getDeclaredConstructor((Class[]) null);
085            assertEquals(1, cls.getDeclaredConstructors().length);
086            assertEquals(true, Modifier.isProtected(con.getModifiers()));
087            
088            Field fld = cls.getDeclaredField("INSTANCE");
089            assertEquals(false, Modifier.isPublic(fld.getModifiers()));
090            assertEquals(false, Modifier.isProtected(fld.getModifiers()));
091            assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
092        }
093    
094        //-----------------------------------------------------------------------
095        public void testSupportedType() throws Exception {
096            assertEquals(ReadableDuration.class, ReadableDurationConverter.INSTANCE.getSupportedType());
097        }
098    
099        //-----------------------------------------------------------------------
100        public void testGetDurationMillis_Object() throws Exception {
101            assertEquals(123L, ReadableDurationConverter.INSTANCE.getDurationMillis(new Duration(123L)));
102        }
103    
104        //-----------------------------------------------------------------------
105        public void testGetPeriodType_Object() throws Exception {
106            assertEquals(PeriodType.standard(),
107                ReadableDurationConverter.INSTANCE.getPeriodType(new Duration(123L)));
108        }
109    
110        public void testSetInto_Object() throws Exception {
111            MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime());
112            ReadableDurationConverter.INSTANCE.setInto(m, new Duration(
113                3L * DateTimeConstants.MILLIS_PER_DAY +
114                4L * DateTimeConstants.MILLIS_PER_MINUTE + 5L
115            ), null);
116            assertEquals(0, m.getYears());
117            assertEquals(0, m.getMonths());
118            assertEquals(0, m.getWeeks());
119            assertEquals(0, m.getDays());
120            assertEquals(3 * 24, m.getHours());
121            assertEquals(4, m.getMinutes());
122            assertEquals(0, m.getSeconds());
123            assertEquals(5, m.getMillis());
124        }
125    
126        //-----------------------------------------------------------------------
127        public void testToString() {
128            assertEquals("Converter[org.joda.time.ReadableDuration]", ReadableDurationConverter.INSTANCE.toString());
129        }
130    
131    }