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