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