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