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.DateTime;
028: import org.joda.time.DateTimeZone;
029: import org.joda.time.Instant;
030: import org.joda.time.MutableDateTime;
031: import org.joda.time.ReadableInstant;
032: import org.joda.time.TimeOfDay;
033: import org.joda.time.chrono.ISOChronology;
034: import org.joda.time.chrono.JulianChronology;
035:
036: /**
037: * This class is a Junit unit test for ReadableInstantConverter.
038: *
039: * @author Stephen Colebourne
040: */
041: public class TestReadableInstantConverter extends TestCase {
042:
043: private static final DateTimeZone UTC = DateTimeZone.UTC;
044: private static final DateTimeZone PARIS = DateTimeZone
045: .forID("Europe/Paris");
046: private static final Chronology ISO_PARIS = ISOChronology
047: .getInstance(PARIS);
048: private static Chronology JULIAN;
049: private static Chronology ISO;
050:
051: private DateTimeZone zone = null;
052:
053: public static void main(String[] args) {
054: junit.textui.TestRunner.run(suite());
055: }
056:
057: public static TestSuite suite() {
058: return new TestSuite(TestReadableInstantConverter.class);
059: }
060:
061: public TestReadableInstantConverter(String name) {
062: super (name);
063: }
064:
065: protected void setUp() throws Exception {
066: JULIAN = JulianChronology.getInstance();
067: ISO = ISOChronology.getInstance();
068: }
069:
070: //-----------------------------------------------------------------------
071: public void testSingleton() throws Exception {
072: Class cls = ReadableInstantConverter.class;
073: assertEquals(false, Modifier.isPublic(cls.getModifiers()));
074: assertEquals(false, Modifier.isProtected(cls.getModifiers()));
075: assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
076:
077: Constructor con = cls.getDeclaredConstructor((Class[]) null);
078: assertEquals(1, cls.getDeclaredConstructors().length);
079: assertEquals(true, Modifier.isProtected(con.getModifiers()));
080:
081: Field fld = cls.getDeclaredField("INSTANCE");
082: assertEquals(false, Modifier.isPublic(fld.getModifiers()));
083: assertEquals(false, Modifier.isProtected(fld.getModifiers()));
084: assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
085: }
086:
087: //-----------------------------------------------------------------------
088: public void testSupportedType() throws Exception {
089: assertEquals(ReadableInstant.class,
090: ReadableInstantConverter.INSTANCE.getSupportedType());
091: }
092:
093: //-----------------------------------------------------------------------
094: public void testGetInstantMillis_Object_Chronology()
095: throws Exception {
096: assertEquals(123L, ReadableInstantConverter.INSTANCE
097: .getInstantMillis(new Instant(123L), JULIAN));
098: assertEquals(123L, ReadableInstantConverter.INSTANCE
099: .getInstantMillis(new DateTime(123L), JULIAN));
100: assertEquals(123L, ReadableInstantConverter.INSTANCE
101: .getInstantMillis(new Instant(123L), (Chronology) null));
102: assertEquals(123L,
103: ReadableInstantConverter.INSTANCE.getInstantMillis(
104: new DateTime(123L), (Chronology) null));
105: }
106:
107: //-----------------------------------------------------------------------
108: public void testGetChronology_Object_Zone() throws Exception {
109: assertEquals(ISO_PARIS, ReadableInstantConverter.INSTANCE
110: .getChronology(new Instant(123L), PARIS));
111: assertEquals(ISO_PARIS, ReadableInstantConverter.INSTANCE
112: .getChronology(new DateTime(123L), PARIS));
113: assertEquals(ISO, ReadableInstantConverter.INSTANCE
114: .getChronology(new Instant(123L), DateTimeZone
115: .getDefault()));
116: assertEquals(ISO, ReadableInstantConverter.INSTANCE
117: .getChronology(new DateTime(123L), DateTimeZone
118: .getDefault()));
119: assertEquals(ISO, ReadableInstantConverter.INSTANCE
120: .getChronology(new Instant(123L), (DateTimeZone) null));
121: assertEquals(ISO, ReadableInstantConverter.INSTANCE
122: .getChronology(new DateTime(123L), (DateTimeZone) null));
123:
124: assertEquals(ISO_PARIS, ReadableInstantConverter.INSTANCE
125: .getChronology(new DateTime(123L,
126: new MockBadChronology()), PARIS));
127:
128: MutableDateTime mdt = new MutableDateTime() {
129: public Chronology getChronology() {
130: return null; // bad
131: }
132: };
133: assertEquals(ISO_PARIS, ReadableInstantConverter.INSTANCE
134: .getChronology(mdt, PARIS));
135: }
136:
137: public void testGetChronology_Object_nullChronology()
138: throws Exception {
139: assertEquals(ISO.withUTC(), ReadableInstantConverter.INSTANCE
140: .getChronology(new Instant(123L), (Chronology) null));
141: assertEquals(ISO, ReadableInstantConverter.INSTANCE
142: .getChronology(new DateTime(123L), (Chronology) null));
143:
144: MutableDateTime mdt = new MutableDateTime() {
145: public Chronology getChronology() {
146: return null; // bad
147: }
148: };
149: assertEquals(ISO, ReadableInstantConverter.INSTANCE
150: .getChronology(mdt, (Chronology) null));
151: }
152:
153: public void testGetChronology_Object_Chronology() throws Exception {
154: assertEquals(JULIAN, ReadableInstantConverter.INSTANCE
155: .getChronology(new Instant(123L), JULIAN));
156: assertEquals(JULIAN, ReadableInstantConverter.INSTANCE
157: .getChronology(new DateTime(123L), JULIAN));
158: }
159:
160: //-----------------------------------------------------------------------
161: public void testGetPartialValues() throws Exception {
162: TimeOfDay tod = new TimeOfDay();
163: int[] expected = ISOChronology.getInstance()
164: .get(tod, 12345678L);
165: int[] actual = ReadableInstantConverter.INSTANCE
166: .getPartialValues(tod, new Instant(12345678L),
167: ISOChronology.getInstance());
168: assertEquals(true, Arrays.equals(expected, actual));
169: }
170:
171: //-----------------------------------------------------------------------
172: public void testToString() {
173: assertEquals("Converter[org.joda.time.ReadableInstant]",
174: ReadableInstantConverter.INSTANCE.toString());
175: }
176:
177: }
|