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.Locale;
23  import java.util.TimeZone;
24  
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  import org.joda.time.Chronology;
29  import org.joda.time.DateTimeConstants;
30  import org.joda.time.DateTimeUtils;
31  import org.joda.time.DateTimeZone;
32  import org.joda.time.MutableInterval;
33  import org.joda.time.MutablePeriod;
34  import org.joda.time.PeriodType;
35  import org.joda.time.TimeOfDay;
36  import org.joda.time.chrono.CopticChronology;
37  import org.joda.time.chrono.GJChronology;
38  import org.joda.time.chrono.ISOChronology;
39  import org.joda.time.chrono.JulianChronology;
40  
41  /**
42   * This class is a Junit unit test for NullConverter.
43   *
44   * @author Stephen Colebourne
45   */
46  public class TestNullConverter extends TestCase {
47  
48      private long TEST_TIME_NOW =
49              20 * DateTimeConstants.MILLIS_PER_DAY
50              + 10L * DateTimeConstants.MILLIS_PER_HOUR
51              + 20L * DateTimeConstants.MILLIS_PER_MINUTE
52              + 30L * DateTimeConstants.MILLIS_PER_SECOND
53              + 40L;
54              
55      private static final DateTimeZone UTC = DateTimeZone.UTC;
56      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
57      private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
58      private static Chronology ISO;
59      private static Chronology JULIAN;
60      
61      private DateTimeZone zone = null;
62      private DateTimeZone originalDateTimeZone = null;
63      private TimeZone originalTimeZone = null;
64      private Locale originalLocale = null;
65  
66      public static void main(String[] args) {
67          junit.textui.TestRunner.run(suite());
68      }
69  
70      public static TestSuite suite() {
71          return new TestSuite(TestNullConverter.class);
72      }
73  
74      public TestNullConverter(String name) {
75          super(name);
76      }
77  
78      protected void setUp() throws Exception {
79          DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
80          originalDateTimeZone = DateTimeZone.getDefault();
81          originalTimeZone = TimeZone.getDefault();
82          originalLocale = Locale.getDefault();
83          DateTimeZone.setDefault(DateTimeZone.forID("Europe/London"));
84          TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
85          Locale.setDefault(Locale.UK);
86          
87          ISO = ISOChronology.getInstance();
88          JULIAN = JulianChronology.getInstance();
89      }
90  
91      protected void tearDown() throws Exception {
92          DateTimeUtils.setCurrentMillisSystem();
93          DateTimeZone.setDefault(originalDateTimeZone);
94          TimeZone.setDefault(originalTimeZone);
95          Locale.setDefault(originalLocale);
96          originalDateTimeZone = null;
97          originalTimeZone = null;
98          originalLocale = null;
99      }
100 
101     //-----------------------------------------------------------------------
102     public void testSingleton() throws Exception {
103         Class cls = NullConverter.class;
104         assertEquals(false, Modifier.isPublic(cls.getModifiers()));
105         assertEquals(false, Modifier.isProtected(cls.getModifiers()));
106         assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
107         
108         Constructor con = cls.getDeclaredConstructor((Class[]) null);
109         assertEquals(1, cls.getDeclaredConstructors().length);
110         assertEquals(true, Modifier.isProtected(con.getModifiers()));
111         
112         Field fld = cls.getDeclaredField("INSTANCE");
113         assertEquals(false, Modifier.isPublic(fld.getModifiers()));
114         assertEquals(false, Modifier.isProtected(fld.getModifiers()));
115         assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
116     }
117 
118     //-----------------------------------------------------------------------
119     public void testSupportedType() throws Exception {
120         assertEquals(null, NullConverter.INSTANCE.getSupportedType());
121     }
122 
123     //-----------------------------------------------------------------------
124     public void testGetInstantMillis_Object_Chronology() throws Exception {
125         assertEquals(TEST_TIME_NOW, NullConverter.INSTANCE.getInstantMillis(null, JULIAN));
126         assertEquals(TEST_TIME_NOW, NullConverter.INSTANCE.getInstantMillis(null, (Chronology) null));
127     }
128 
129     //-----------------------------------------------------------------------
130     public void testGetChronology_Object_Zone() throws Exception {
131         assertEquals(ISO_PARIS, NullConverter.INSTANCE.getChronology(null, PARIS));
132         assertEquals(ISO, NullConverter.INSTANCE.getChronology(null, (DateTimeZone) null));
133     }
134 
135     public void testGetChronology_Object_Chronology() throws Exception {
136         assertEquals(JULIAN, NullConverter.INSTANCE.getChronology(null, JULIAN));
137         assertEquals(ISO, NullConverter.INSTANCE.getChronology(null, (Chronology) null));
138     }
139 
140     //-----------------------------------------------------------------------
141     public void testGetPartialValues() throws Exception {
142         TimeOfDay tod = new TimeOfDay();
143         int[] expected = new int[] {10 + 1, 20, 30, 40}; // now
144         int[] actual = NullConverter.INSTANCE.getPartialValues(tod, null, ISOChronology.getInstance());
145         assertEquals(true, Arrays.equals(expected, actual));
146     }
147 
148     //-----------------------------------------------------------------------
149     public void testGetDurationMillis_Object() throws Exception {
150         assertEquals(0L, NullConverter.INSTANCE.getDurationMillis(null));
151     }
152 
153     //-----------------------------------------------------------------------
154     public void testGetPeriodType_Object() throws Exception {
155         assertEquals(PeriodType.standard(),
156             NullConverter.INSTANCE.getPeriodType(null));
157     }
158 
159     public void testSetInto_Object() throws Exception {
160         MutablePeriod m = new MutablePeriod(PeriodType.millis());
161         NullConverter.INSTANCE.setInto(m, null, null);
162         assertEquals(0L, m.getMillis());
163     }
164 
165     //-----------------------------------------------------------------------
166     public void testIsReadableInterval_Object_Chronology() throws Exception {
167         assertEquals(false, NullConverter.INSTANCE.isReadableInterval(null, null));
168     }
169 
170     public void testSetInto_Object_Chronology1() throws Exception {
171         MutableInterval m = new MutableInterval(1000L, 2000L, GJChronology.getInstance());
172         NullConverter.INSTANCE.setInto(m, null, null);
173         assertEquals(TEST_TIME_NOW, m.getStartMillis());
174         assertEquals(TEST_TIME_NOW, m.getEndMillis());
175         assertEquals(ISOChronology.getInstance(), m.getChronology());
176     }
177 
178     public void testSetInto_Object_Chronology2() throws Exception {
179         MutableInterval m = new MutableInterval(1000L, 2000L, GJChronology.getInstance());
180         NullConverter.INSTANCE.setInto(m, null, CopticChronology.getInstance());
181         assertEquals(TEST_TIME_NOW, m.getStartMillis());
182         assertEquals(TEST_TIME_NOW, m.getEndMillis());
183         assertEquals(CopticChronology.getInstance(), m.getChronology());
184     }
185 
186     //-----------------------------------------------------------------------
187     public void testToString() {
188         assertEquals("Converter[null]", NullConverter.INSTANCE.toString());
189     }
190 
191 }