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: import java.util.Locale;
023: import java.util.TimeZone;
024:
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: import org.joda.time.Chronology;
029: import org.joda.time.DateTimeConstants;
030: import org.joda.time.DateTimeUtils;
031: import org.joda.time.DateTimeZone;
032: import org.joda.time.MutableInterval;
033: import org.joda.time.MutablePeriod;
034: import org.joda.time.PeriodType;
035: import org.joda.time.TimeOfDay;
036: import org.joda.time.chrono.CopticChronology;
037: import org.joda.time.chrono.GJChronology;
038: import org.joda.time.chrono.ISOChronology;
039: import org.joda.time.chrono.JulianChronology;
040:
041: /**
042: * This class is a Junit unit test for NullConverter.
043: *
044: * @author Stephen Colebourne
045: */
046: public class TestNullConverter extends TestCase {
047:
048: private long TEST_TIME_NOW = 20 * DateTimeConstants.MILLIS_PER_DAY
049: + 10L * DateTimeConstants.MILLIS_PER_HOUR + 20L
050: * DateTimeConstants.MILLIS_PER_MINUTE + 30L
051: * DateTimeConstants.MILLIS_PER_SECOND + 40L;
052:
053: private static final DateTimeZone UTC = DateTimeZone.UTC;
054: private static final DateTimeZone PARIS = DateTimeZone
055: .forID("Europe/Paris");
056: private static final Chronology ISO_PARIS = ISOChronology
057: .getInstance(PARIS);
058: private static Chronology ISO;
059: private static Chronology JULIAN;
060:
061: private DateTimeZone zone = null;
062: private DateTimeZone originalDateTimeZone = null;
063: private TimeZone originalTimeZone = null;
064: private Locale originalLocale = null;
065:
066: public static void main(String[] args) {
067: junit.textui.TestRunner.run(suite());
068: }
069:
070: public static TestSuite suite() {
071: return new TestSuite(TestNullConverter.class);
072: }
073:
074: public TestNullConverter(String name) {
075: super (name);
076: }
077:
078: protected void setUp() throws Exception {
079: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
080: originalDateTimeZone = DateTimeZone.getDefault();
081: originalTimeZone = TimeZone.getDefault();
082: originalLocale = Locale.getDefault();
083: DateTimeZone.setDefault(DateTimeZone.forID("Europe/London"));
084: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
085: Locale.setDefault(Locale.UK);
086:
087: ISO = ISOChronology.getInstance();
088: JULIAN = JulianChronology.getInstance();
089: }
090:
091: protected void tearDown() throws Exception {
092: DateTimeUtils.setCurrentMillisSystem();
093: DateTimeZone.setDefault(originalDateTimeZone);
094: TimeZone.setDefault(originalTimeZone);
095: Locale.setDefault(originalLocale);
096: originalDateTimeZone = null;
097: originalTimeZone = null;
098: originalLocale = null;
099: }
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()
125: throws Exception {
126: assertEquals(TEST_TIME_NOW, NullConverter.INSTANCE
127: .getInstantMillis(null, JULIAN));
128: assertEquals(TEST_TIME_NOW, NullConverter.INSTANCE
129: .getInstantMillis(null, (Chronology) null));
130: }
131:
132: //-----------------------------------------------------------------------
133: public void testGetChronology_Object_Zone() throws Exception {
134: assertEquals(ISO_PARIS, NullConverter.INSTANCE.getChronology(
135: null, PARIS));
136: assertEquals(ISO, NullConverter.INSTANCE.getChronology(null,
137: (DateTimeZone) null));
138: }
139:
140: public void testGetChronology_Object_Chronology() throws Exception {
141: assertEquals(JULIAN, NullConverter.INSTANCE.getChronology(null,
142: JULIAN));
143: assertEquals(ISO, NullConverter.INSTANCE.getChronology(null,
144: (Chronology) null));
145: }
146:
147: //-----------------------------------------------------------------------
148: public void testGetPartialValues() throws Exception {
149: TimeOfDay tod = new TimeOfDay();
150: int[] expected = new int[] { 10 + 1, 20, 30, 40 }; // now
151: int[] actual = NullConverter.INSTANCE.getPartialValues(tod,
152: null, ISOChronology.getInstance());
153: assertEquals(true, Arrays.equals(expected, actual));
154: }
155:
156: //-----------------------------------------------------------------------
157: public void testGetDurationMillis_Object() throws Exception {
158: assertEquals(0L, NullConverter.INSTANCE.getDurationMillis(null));
159: }
160:
161: //-----------------------------------------------------------------------
162: public void testGetPeriodType_Object() throws Exception {
163: assertEquals(PeriodType.standard(), NullConverter.INSTANCE
164: .getPeriodType(null));
165: }
166:
167: public void testSetInto_Object() throws Exception {
168: MutablePeriod m = new MutablePeriod(PeriodType.millis());
169: NullConverter.INSTANCE.setInto(m, null, null);
170: assertEquals(0L, m.getMillis());
171: }
172:
173: //-----------------------------------------------------------------------
174: public void testIsReadableInterval_Object_Chronology()
175: throws Exception {
176: assertEquals(false, NullConverter.INSTANCE.isReadableInterval(
177: null, null));
178: }
179:
180: public void testSetInto_Object_Chronology1() throws Exception {
181: MutableInterval m = new MutableInterval(1000L, 2000L,
182: GJChronology.getInstance());
183: NullConverter.INSTANCE.setInto(m, null, null);
184: assertEquals(TEST_TIME_NOW, m.getStartMillis());
185: assertEquals(TEST_TIME_NOW, m.getEndMillis());
186: assertEquals(ISOChronology.getInstance(), m.getChronology());
187: }
188:
189: public void testSetInto_Object_Chronology2() throws Exception {
190: MutableInterval m = new MutableInterval(1000L, 2000L,
191: GJChronology.getInstance());
192: NullConverter.INSTANCE.setInto(m, null, CopticChronology
193: .getInstance());
194: assertEquals(TEST_TIME_NOW, m.getStartMillis());
195: assertEquals(TEST_TIME_NOW, m.getEndMillis());
196: assertEquals(CopticChronology.getInstance(), m.getChronology());
197: }
198:
199: //-----------------------------------------------------------------------
200: public void testToString() {
201: assertEquals("Converter[null]", NullConverter.INSTANCE
202: .toString());
203: }
204:
205: }
|