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.DateTimeZone;
027    import org.joda.time.Interval;
028    import org.joda.time.MutableInterval;
029    import org.joda.time.MutablePeriod;
030    import org.joda.time.PeriodType;
031    import org.joda.time.ReadableInterval;
032    import org.joda.time.chrono.BuddhistChronology;
033    import org.joda.time.chrono.CopticChronology;
034    import org.joda.time.chrono.GJChronology;
035    import org.joda.time.chrono.ISOChronology;
036    import org.joda.time.chrono.JulianChronology;
037    
038    /**
039     * This class is a JUnit test for ReadableIntervalConverter.
040     *
041     * @author Stephen Colebourne
042     */
043    public class TestReadableIntervalConverter extends TestCase {
044    
045        private static final DateTimeZone UTC = DateTimeZone.UTC;
046        private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
047        private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
048        private static Chronology JULIAN;
049        private static Chronology ISO;
050        
051        private DateTimeZone zone = null;
052    
053        public static void main(String[] args) {
054            junit.textui.TestRunner.run(suite());
055        }
056    
057        public static TestSuite suite() {
058            return new TestSuite(TestReadableIntervalConverter.class);
059        }
060    
061        public TestReadableIntervalConverter(String name) {
062            super(name);
063        }
064    
065        protected void setUp() throws Exception {
066            JULIAN = JulianChronology.getInstance();
067            ISO = ISOChronology.getInstance();
068        }
069    
070        //-----------------------------------------------------------------------
071        public void testSingleton() throws Exception {
072            Class cls = ReadableIntervalConverter.class;
073            assertEquals(false, Modifier.isPublic(cls.getModifiers()));
074            assertEquals(false, Modifier.isProtected(cls.getModifiers()));
075            assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
076            
077            Constructor con = cls.getDeclaredConstructor((Class[]) null);
078            assertEquals(1, cls.getDeclaredConstructors().length);
079            assertEquals(true, Modifier.isProtected(con.getModifiers()));
080            
081            Field fld = cls.getDeclaredField("INSTANCE");
082            assertEquals(false, Modifier.isPublic(fld.getModifiers()));
083            assertEquals(false, Modifier.isProtected(fld.getModifiers()));
084            assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
085        }
086    
087        //-----------------------------------------------------------------------
088        public void testSupportedType() throws Exception {
089            assertEquals(ReadableInterval.class, ReadableIntervalConverter.INSTANCE.getSupportedType());
090        }
091    
092        //-----------------------------------------------------------------------
093        public void testGetDurationMillis_Object() throws Exception {
094            Interval i = new Interval(100L, 223L);
095            assertEquals(123L, ReadableIntervalConverter.INSTANCE.getDurationMillis(i));
096        }
097    
098        //-----------------------------------------------------------------------
099        public void testGetPeriodType_Object() throws Exception {
100            Interval i = new Interval(100L, 223L);
101            assertEquals(PeriodType.standard(),
102                ReadableIntervalConverter.INSTANCE.getPeriodType(i));
103        }
104    
105        public void testSetIntoPeriod_Object1() throws Exception {
106            Interval i = new Interval(100L, 223L);
107            MutablePeriod m = new MutablePeriod(PeriodType.millis());
108            ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
109            assertEquals(0, m.getYears());
110            assertEquals(0, m.getMonths());
111            assertEquals(0, m.getWeeks());
112            assertEquals(0, m.getDays());
113            assertEquals(0, m.getHours());
114            assertEquals(0, m.getMinutes());
115            assertEquals(0, m.getSeconds());
116            assertEquals(123, m.getMillis());
117        }
118    
119        public void testSetIntoPeriod_Object2() throws Exception {
120            Interval i = new Interval(100L, 223L);
121            MutablePeriod m = new MutablePeriod(PeriodType.millis());
122            ReadableIntervalConverter.INSTANCE.setInto(m, i, CopticChronology.getInstance());
123            assertEquals(0, m.getYears());
124            assertEquals(0, m.getMonths());
125            assertEquals(0, m.getWeeks());
126            assertEquals(0, m.getDays());
127            assertEquals(0, m.getHours());
128            assertEquals(0, m.getMinutes());
129            assertEquals(0, m.getSeconds());
130            assertEquals(123, m.getMillis());
131        }
132    
133        //-----------------------------------------------------------------------
134        public void testIsReadableInterval_Object_Chronology() throws Exception {
135            Interval i = new Interval(1234L, 5678L);
136            assertEquals(true, ReadableIntervalConverter.INSTANCE.isReadableInterval(i, null));
137        }
138    
139        public void testSetIntoInterval_Object1() throws Exception {
140            Interval i = new Interval(0L, 123L, CopticChronology.getInstance());
141            MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
142            ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
143            assertEquals(0L, m.getStartMillis());
144            assertEquals(123L, m.getEndMillis());
145            assertEquals(CopticChronology.getInstance(), m.getChronology());
146        }
147    
148        public void testSetIntoInterval_Object2() throws Exception {
149            Interval i = new Interval(0L, 123L, CopticChronology.getInstance());
150            MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
151            ReadableIntervalConverter.INSTANCE.setInto(m, i, GJChronology.getInstance());
152            assertEquals(0L, m.getStartMillis());
153            assertEquals(123L, m.getEndMillis());
154            assertEquals(GJChronology.getInstance(), m.getChronology());
155        }
156    
157        public void testSetIntoInterval_Object3() throws Exception {
158            MutableInterval i = new MutableInterval(0L, 123L) {
159                public Chronology getChronology() {
160                    return null; // bad
161                }
162            };
163            MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
164            ReadableIntervalConverter.INSTANCE.setInto(m, i, GJChronology.getInstance());
165            assertEquals(0L, m.getStartMillis());
166            assertEquals(123L, m.getEndMillis());
167            assertEquals(GJChronology.getInstance(), m.getChronology());
168        }
169    
170        public void testSetIntoInterval_Object4() throws Exception {
171            MutableInterval i = new MutableInterval(0L, 123L) {
172                public Chronology getChronology() {
173                    return null; // bad
174                }
175            };
176            MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
177            ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
178            assertEquals(0L, m.getStartMillis());
179            assertEquals(123L, m.getEndMillis());
180            assertEquals(ISOChronology.getInstance(), m.getChronology());
181        }
182    
183        //-----------------------------------------------------------------------
184        public void testToString() {
185            assertEquals("Converter[org.joda.time.ReadableInterval]", ReadableIntervalConverter.INSTANCE.toString());
186        }
187    
188    }