View Javadoc

1   /*
2    *  Copyright 2001-2005 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.convert;
17  
18  import java.lang.reflect.Constructor;
19  import java.lang.reflect.Field;
20  import java.lang.reflect.Modifier;
21  import java.util.Arrays;
22  
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.joda.time.Chronology;
27  import org.joda.time.DateTime;
28  import org.joda.time.DateTimeZone;
29  import org.joda.time.Instant;
30  import org.joda.time.MutableDateTime;
31  import org.joda.time.ReadableInstant;
32  import org.joda.time.TimeOfDay;
33  import org.joda.time.chrono.ISOChronology;
34  import org.joda.time.chrono.JulianChronology;
35  
36  /**
37   * This class is a Junit unit test for ReadableInstantConverter.
38   *
39   * @author Stephen Colebourne
40   */
41  public class TestReadableInstantConverter extends TestCase {
42  
43      private static final DateTimeZone UTC = DateTimeZone.UTC;
44      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
45      private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
46      private static Chronology JULIAN;
47      private static Chronology ISO;
48      
49      private DateTimeZone zone = null;
50  
51      public static void main(String[] args) {
52          junit.textui.TestRunner.run(suite());
53      }
54  
55      public static TestSuite suite() {
56          return new TestSuite(TestReadableInstantConverter.class);
57      }
58  
59      public TestReadableInstantConverter(String name) {
60          super(name);
61      }
62  
63      protected void setUp() throws Exception {
64          JULIAN = JulianChronology.getInstance();
65          ISO = ISOChronology.getInstance();
66      }
67  
68      //-----------------------------------------------------------------------
69      public void testSingleton() throws Exception {
70          Class cls = ReadableInstantConverter.class;
71          assertEquals(false, Modifier.isPublic(cls.getModifiers()));
72          assertEquals(false, Modifier.isProtected(cls.getModifiers()));
73          assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
74          
75          Constructor con = cls.getDeclaredConstructor((Class[]) null);
76          assertEquals(1, cls.getDeclaredConstructors().length);
77          assertEquals(true, Modifier.isProtected(con.getModifiers()));
78          
79          Field fld = cls.getDeclaredField("INSTANCE");
80          assertEquals(false, Modifier.isPublic(fld.getModifiers()));
81          assertEquals(false, Modifier.isProtected(fld.getModifiers()));
82          assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
83      }
84  
85      //-----------------------------------------------------------------------
86      public void testSupportedType() throws Exception {
87          assertEquals(ReadableInstant.class, ReadableInstantConverter.INSTANCE.getSupportedType());
88      }
89  
90      //-----------------------------------------------------------------------
91      public void testGetInstantMillis_Object_Chronology() throws Exception {
92          assertEquals(123L, ReadableInstantConverter.INSTANCE.getInstantMillis(new Instant(123L), JULIAN));
93          assertEquals(123L, ReadableInstantConverter.INSTANCE.getInstantMillis(new DateTime(123L), JULIAN));
94          assertEquals(123L, ReadableInstantConverter.INSTANCE.getInstantMillis(new Instant(123L), (Chronology) null));
95          assertEquals(123L, ReadableInstantConverter.INSTANCE.getInstantMillis(new DateTime(123L), (Chronology) null));
96      }
97  
98      //-----------------------------------------------------------------------
99      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 }