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    import java.util.Arrays;
022    import java.util.Locale;
023    import java.util.TimeZone;
024    
025    import junit.framework.TestCase;
026    import junit.framework.TestSuite;
027    
028    import org.joda.time.Chronology;
029    import org.joda.time.DateTimeConstants;
030    import org.joda.time.DateTimeUtils;
031    import org.joda.time.DateTimeZone;
032    import org.joda.time.MutableInterval;
033    import org.joda.time.MutablePeriod;
034    import org.joda.time.PeriodType;
035    import org.joda.time.TimeOfDay;
036    import org.joda.time.chrono.CopticChronology;
037    import org.joda.time.chrono.GJChronology;
038    import org.joda.time.chrono.ISOChronology;
039    import org.joda.time.chrono.JulianChronology;
040    
041    /**
042     * This class is a Junit unit test for NullConverter.
043     *
044     * @author Stephen Colebourne
045     */
046    public class TestNullConverter extends TestCase {
047    
048        private long TEST_TIME_NOW =
049                20 * DateTimeConstants.MILLIS_PER_DAY
050                + 10L * DateTimeConstants.MILLIS_PER_HOUR
051                + 20L * DateTimeConstants.MILLIS_PER_MINUTE
052                + 30L * DateTimeConstants.MILLIS_PER_SECOND
053                + 40L;
054                
055        private static final DateTimeZone UTC = DateTimeZone.UTC;
056        private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
057        private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
058        private static Chronology ISO;
059        private static Chronology JULIAN;
060        
061        private DateTimeZone zone = null;
062        private DateTimeZone originalDateTimeZone = null;
063        private TimeZone originalTimeZone = null;
064        private Locale originalLocale = null;
065    
066        public static void main(String[] args) {
067            junit.textui.TestRunner.run(suite());
068        }
069    
070        public static TestSuite suite() {
071            return new TestSuite(TestNullConverter.class);
072        }
073    
074        public TestNullConverter(String name) {
075            super(name);
076        }
077    
078        protected void setUp() throws Exception {
079            DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
080            originalDateTimeZone = DateTimeZone.getDefault();
081            originalTimeZone = TimeZone.getDefault();
082            originalLocale = Locale.getDefault();
083            DateTimeZone.setDefault(DateTimeZone.forID("Europe/London"));
084            TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
085            Locale.setDefault(Locale.UK);
086            
087            ISO = ISOChronology.getInstance();
088            JULIAN = JulianChronology.getInstance();
089        }
090    
091        protected void tearDown() throws Exception {
092            DateTimeUtils.setCurrentMillisSystem();
093            DateTimeZone.setDefault(originalDateTimeZone);
094            TimeZone.setDefault(originalTimeZone);
095            Locale.setDefault(originalLocale);
096            originalDateTimeZone = null;
097            originalTimeZone = null;
098            originalLocale = null;
099        }
100    
101        //-----------------------------------------------------------------------
102        public void testSingleton() throws Exception {
103            Class cls = NullConverter.class;
104            assertEquals(false, Modifier.isPublic(cls.getModifiers()));
105            assertEquals(false, Modifier.isProtected(cls.getModifiers()));
106            assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
107            
108            Constructor con = cls.getDeclaredConstructor((Class[]) null);
109            assertEquals(1, cls.getDeclaredConstructors().length);
110            assertEquals(true, Modifier.isProtected(con.getModifiers()));
111            
112            Field fld = cls.getDeclaredField("INSTANCE");
113            assertEquals(false, Modifier.isPublic(fld.getModifiers()));
114            assertEquals(false, Modifier.isProtected(fld.getModifiers()));
115            assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
116        }
117    
118        //-----------------------------------------------------------------------
119        public void testSupportedType() throws Exception {
120            assertEquals(null, NullConverter.INSTANCE.getSupportedType());
121        }
122    
123        //-----------------------------------------------------------------------
124        public void testGetInstantMillis_Object_Chronology() throws Exception {
125            assertEquals(TEST_TIME_NOW, NullConverter.INSTANCE.getInstantMillis(null, JULIAN));
126            assertEquals(TEST_TIME_NOW, NullConverter.INSTANCE.getInstantMillis(null, (Chronology) null));
127        }
128    
129        //-----------------------------------------------------------------------
130        public void testGetChronology_Object_Zone() throws Exception {
131            assertEquals(ISO_PARIS, NullConverter.INSTANCE.getChronology(null, PARIS));
132            assertEquals(ISO, NullConverter.INSTANCE.getChronology(null, (DateTimeZone) null));
133        }
134    
135        public void testGetChronology_Object_Chronology() throws Exception {
136            assertEquals(JULIAN, NullConverter.INSTANCE.getChronology(null, JULIAN));
137            assertEquals(ISO, NullConverter.INSTANCE.getChronology(null, (Chronology) null));
138        }
139    
140        //-----------------------------------------------------------------------
141        public void testGetPartialValues() throws Exception {
142            TimeOfDay tod = new TimeOfDay();
143            int[] expected = new int[] {10 + 1, 20, 30, 40}; // now
144            int[] actual = NullConverter.INSTANCE.getPartialValues(tod, null, ISOChronology.getInstance());
145            assertEquals(true, Arrays.equals(expected, actual));
146        }
147    
148        //-----------------------------------------------------------------------
149        public void testGetDurationMillis_Object() throws Exception {
150            assertEquals(0L, NullConverter.INSTANCE.getDurationMillis(null));
151        }
152    
153        //-----------------------------------------------------------------------
154        public void testGetPeriodType_Object() throws Exception {
155            assertEquals(PeriodType.standard(),
156                NullConverter.INSTANCE.getPeriodType(null));
157        }
158    
159        public void testSetInto_Object() throws Exception {
160            MutablePeriod m = new MutablePeriod(PeriodType.millis());
161            NullConverter.INSTANCE.setInto(m, null, null);
162            assertEquals(0L, m.getMillis());
163        }
164    
165        //-----------------------------------------------------------------------
166        public void testIsReadableInterval_Object_Chronology() throws Exception {
167            assertEquals(false, NullConverter.INSTANCE.isReadableInterval(null, null));
168        }
169    
170        public void testSetInto_Object_Chronology1() throws Exception {
171            MutableInterval m = new MutableInterval(1000L, 2000L, GJChronology.getInstance());
172            NullConverter.INSTANCE.setInto(m, null, null);
173            assertEquals(TEST_TIME_NOW, m.getStartMillis());
174            assertEquals(TEST_TIME_NOW, m.getEndMillis());
175            assertEquals(ISOChronology.getInstance(), m.getChronology());
176        }
177    
178        public void testSetInto_Object_Chronology2() throws Exception {
179            MutableInterval m = new MutableInterval(1000L, 2000L, GJChronology.getInstance());
180            NullConverter.INSTANCE.setInto(m, null, CopticChronology.getInstance());
181            assertEquals(TEST_TIME_NOW, m.getStartMillis());
182            assertEquals(TEST_TIME_NOW, m.getEndMillis());
183            assertEquals(CopticChronology.getInstance(), m.getChronology());
184        }
185    
186        //-----------------------------------------------------------------------
187        public void testToString() {
188            assertEquals("Converter[null]", NullConverter.INSTANCE.toString());
189        }
190    
191    }