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
042: .forID("Europe/Paris");
043: private static final Chronology ISO_PARIS = ISOChronology
044: .getInstance(PARIS);
045: private static Chronology ISO;
046: private static Chronology JULIAN;
047: private static Chronology COPTIC;
048:
049: public static void main(String[] args) {
050: junit.textui.TestRunner.run(suite());
051: }
052:
053: public static TestSuite suite() {
054: return new TestSuite(TestDateConverter.class);
055: }
056:
057: public TestDateConverter(String name) {
058: super (name);
059: }
060:
061: protected void setUp() throws Exception {
062: JULIAN = JulianChronology.getInstance();
063: COPTIC = CopticChronology.getInstance();
064: ISO = ISOChronology.getInstance();
065: }
066:
067: //-----------------------------------------------------------------------
068: public void testSingleton() throws Exception {
069: Class cls = DateConverter.class;
070: assertEquals(false, Modifier.isPublic(cls.getModifiers()));
071: assertEquals(false, Modifier.isProtected(cls.getModifiers()));
072: assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
073:
074: Constructor con = cls.getDeclaredConstructor((Class[]) null);
075: assertEquals(1, cls.getDeclaredConstructors().length);
076: assertEquals(true, Modifier.isProtected(con.getModifiers()));
077:
078: Field fld = cls.getDeclaredField("INSTANCE");
079: assertEquals(false, Modifier.isPublic(fld.getModifiers()));
080: assertEquals(false, Modifier.isProtected(fld.getModifiers()));
081: assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
082: }
083:
084: //-----------------------------------------------------------------------
085: public void testSupportedType() throws Exception {
086: assertEquals(Date.class, DateConverter.INSTANCE
087: .getSupportedType());
088: }
089:
090: //-----------------------------------------------------------------------
091: public void testGetInstantMillis_Object_Chronology()
092: throws Exception {
093: Date date = new Date(123L);
094: long millis = DateConverter.INSTANCE.getInstantMillis(date,
095: JULIAN);
096: assertEquals(123L, millis);
097: assertEquals(123L, DateConverter.INSTANCE.getInstantMillis(
098: date, (Chronology) null));
099: }
100:
101: //-----------------------------------------------------------------------
102: public void testGetChronology_Object_Zone() throws Exception {
103: assertEquals(ISO_PARIS, DateConverter.INSTANCE.getChronology(
104: new Date(123L), PARIS));
105: assertEquals(ISO, DateConverter.INSTANCE.getChronology(
106: new Date(123L), (DateTimeZone) null));
107: }
108:
109: public void testGetChronology_Object_Chronology() throws Exception {
110: assertEquals(JULIAN, DateConverter.INSTANCE.getChronology(
111: new Date(123L), JULIAN));
112: assertEquals(ISO, DateConverter.INSTANCE.getChronology(
113: new Date(123L), (Chronology) null));
114: }
115:
116: //-----------------------------------------------------------------------
117: public void testGetPartialValues() throws Exception {
118: TimeOfDay tod = new TimeOfDay();
119: int[] expected = COPTIC.get(tod, 12345678L);
120: int[] actual = DateConverter.INSTANCE.getPartialValues(tod,
121: new Date(12345678L), COPTIC);
122: assertEquals(true, Arrays.equals(expected, actual));
123: }
124:
125: //-----------------------------------------------------------------------
126: public void testToString() {
127: assertEquals("Converter[java.util.Date]",
128: DateConverter.INSTANCE.toString());
129: }
130:
131: }
|