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  import java.util.Date;
23  
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  import org.joda.time.Chronology;
28  import org.joda.time.DateTimeZone;
29  import org.joda.time.TimeOfDay;
30  import org.joda.time.chrono.CopticChronology;
31  import org.joda.time.chrono.ISOChronology;
32  import org.joda.time.chrono.JulianChronology;
33  
34  /**
35   * This class is a Junit unit test for DateConverter.
36   *
37   * @author Stephen Colebourne
38   */
39  public class TestDateConverter extends TestCase {
40  
41      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
42      private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
43      private static Chronology ISO;
44      private static Chronology JULIAN;
45      private static Chronology COPTIC;
46      
47      public static void main(String[] args) {
48          junit.textui.TestRunner.run(suite());
49      }
50  
51      public static TestSuite suite() {
52          return new TestSuite(TestDateConverter.class);
53      }
54  
55      public TestDateConverter(String name) {
56          super(name);
57      }
58  
59      protected void setUp() throws Exception {
60          JULIAN = JulianChronology.getInstance();
61          COPTIC = CopticChronology.getInstance();
62          ISO = ISOChronology.getInstance();
63      }
64  
65      //-----------------------------------------------------------------------
66      public void testSingleton() throws Exception {
67          Class cls = DateConverter.class;
68          assertEquals(false, Modifier.isPublic(cls.getModifiers()));
69          assertEquals(false, Modifier.isProtected(cls.getModifiers()));
70          assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
71          
72          Constructor con = cls.getDeclaredConstructor((Class[]) null);
73          assertEquals(1, cls.getDeclaredConstructors().length);
74          assertEquals(true, Modifier.isProtected(con.getModifiers()));
75          
76          Field fld = cls.getDeclaredField("INSTANCE");
77          assertEquals(false, Modifier.isPublic(fld.getModifiers()));
78          assertEquals(false, Modifier.isProtected(fld.getModifiers()));
79          assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
80      }
81  
82      //-----------------------------------------------------------------------
83      public void testSupportedType() throws Exception {
84          assertEquals(Date.class, DateConverter.INSTANCE.getSupportedType());
85      }
86  
87      //-----------------------------------------------------------------------
88      public void testGetInstantMillis_Object_Chronology() throws Exception {
89          Date date = new Date(123L);
90          long millis = DateConverter.INSTANCE.getInstantMillis(date, JULIAN);
91          assertEquals(123L, millis);
92          assertEquals(123L, DateConverter.INSTANCE.getInstantMillis(date, (Chronology) null));
93      }
94  
95      //-----------------------------------------------------------------------
96      public void testGetChronology_Object_Zone() throws Exception {
97          assertEquals(ISO_PARIS, DateConverter.INSTANCE.getChronology(new Date(123L), PARIS));
98          assertEquals(ISO, DateConverter.INSTANCE.getChronology(new Date(123L), (DateTimeZone) null));
99      }
100 
101     public void testGetChronology_Object_Chronology() throws Exception {
102         assertEquals(JULIAN, DateConverter.INSTANCE.getChronology(new Date(123L), JULIAN));
103         assertEquals(ISO, DateConverter.INSTANCE.getChronology(new Date(123L), (Chronology) null));
104     }
105 
106     //-----------------------------------------------------------------------
107     public void testGetPartialValues() throws Exception {
108         TimeOfDay tod = new TimeOfDay();
109         int[] expected = COPTIC.get(tod, 12345678L);
110         int[] actual = DateConverter.INSTANCE.getPartialValues(tod, new Date(12345678L), COPTIC);
111         assertEquals(true, Arrays.equals(expected, actual));
112     }
113 
114     //-----------------------------------------------------------------------
115     public void testToString() {
116         assertEquals("Converter[java.util.Date]", DateConverter.INSTANCE.toString());
117     }
118 
119 }