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    
023    import junit.framework.TestCase;
024    import junit.framework.TestSuite;
025    
026    import org.joda.time.Chronology;
027    import org.joda.time.DateTimeZone;
028    import org.joda.time.TimeOfDay;
029    import org.joda.time.chrono.ISOChronology;
030    import org.joda.time.chrono.JulianChronology;
031    
032    /**
033     * This class is a Junit unit test for LongConverter.
034     *
035     * @author Stephen Colebourne
036     */
037    public class TestLongConverter extends TestCase {
038    
039        private static final DateTimeZone UTC = DateTimeZone.UTC;
040        private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
041        private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
042        private static Chronology JULIAN;
043        private static Chronology ISO;
044        
045        private DateTimeZone zone = null;
046    
047        public static void main(String[] args) {
048            junit.textui.TestRunner.run(suite());
049        }
050    
051        public static TestSuite suite() {
052            return new TestSuite(TestLongConverter.class);
053        }
054    
055        public TestLongConverter(String name) {
056            super(name);
057        }
058    
059        protected void setUp() throws Exception {
060            JULIAN = JulianChronology.getInstance();
061            ISO = ISOChronology.getInstance();
062        }
063    
064        //-----------------------------------------------------------------------
065        public void testSingleton() throws Exception {
066            Class cls = LongConverter.class;
067            assertEquals(false, Modifier.isPublic(cls.getModifiers()));
068            assertEquals(false, Modifier.isProtected(cls.getModifiers()));
069            assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
070            
071            Constructor con = cls.getDeclaredConstructor((Class[]) null);
072            assertEquals(1, cls.getDeclaredConstructors().length);
073            assertEquals(true, Modifier.isProtected(con.getModifiers()));
074            
075            Field fld = cls.getDeclaredField("INSTANCE");
076            assertEquals(false, Modifier.isPublic(fld.getModifiers()));
077            assertEquals(false, Modifier.isProtected(fld.getModifiers()));
078            assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
079        }
080    
081        //-----------------------------------------------------------------------
082        public void testSupportedType() throws Exception {
083            assertEquals(Long.class, LongConverter.INSTANCE.getSupportedType());
084        }
085    
086        //-----------------------------------------------------------------------
087        public void testGetInstantMillis_Object_Chronology() throws Exception {
088            assertEquals(123L, LongConverter.INSTANCE.getInstantMillis(new Long(123L), JULIAN));
089            assertEquals(123L, LongConverter.INSTANCE.getInstantMillis(new Long(123L), (Chronology) null));
090        }
091    
092        //-----------------------------------------------------------------------
093        public void testGetChronology_Object_Zone() throws Exception {
094            assertEquals(ISO_PARIS, LongConverter.INSTANCE.getChronology(new Long(123L), PARIS));
095            assertEquals(ISO, LongConverter.INSTANCE.getChronology(new Long(123L), (DateTimeZone) null));
096        }
097    
098        public void testGetChronology_Object_Chronology() throws Exception {
099            assertEquals(JULIAN, LongConverter.INSTANCE.getChronology(new Long(123L), JULIAN));
100            assertEquals(ISO, LongConverter.INSTANCE.getChronology(new Long(123L), (Chronology) null));
101        }
102    
103        //-----------------------------------------------------------------------
104        public void testGetPartialValues() throws Exception {
105            TimeOfDay tod = new TimeOfDay();
106            int[] expected = ISOChronology.getInstance().get(tod, 12345678L);
107            int[] actual = LongConverter.INSTANCE.getPartialValues(tod, new Long(12345678L), ISOChronology.getInstance());
108            assertEquals(true, Arrays.equals(expected, actual));
109        }
110    
111        //-----------------------------------------------------------------------
112        public void testGetDurationMillis_Object() throws Exception {
113            assertEquals(123L, LongConverter.INSTANCE.getDurationMillis(new Long(123L)));
114        }
115    
116        //-----------------------------------------------------------------------
117        public void testToString() {
118            assertEquals("Converter[java.lang.Long]", LongConverter.INSTANCE.toString());
119        }
120    
121    }