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:
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: import org.joda.time.Chronology;
026: import org.joda.time.DateTimeConstants;
027: import org.joda.time.DateTimeZone;
028: import org.joda.time.Duration;
029: import org.joda.time.PeriodType;
030: import org.joda.time.MutablePeriod;
031: import org.joda.time.ReadableDuration;
032: import org.joda.time.chrono.ISOChronology;
033: import org.joda.time.chrono.JulianChronology;
034:
035: /**
036: * This class is a Junit unit test for ReadableDurationConverter.
037: *
038: * @author Stephen Colebourne
039: */
040: public class TestReadableDurationConverter extends TestCase {
041:
042: private static final DateTimeZone UTC = DateTimeZone.UTC;
043: private static final DateTimeZone PARIS = DateTimeZone
044: .forID("Europe/Paris");
045: private static final Chronology ISO_PARIS = ISOChronology
046: .getInstance(PARIS);
047: private static Chronology JULIAN;
048: private static Chronology ISO;
049:
050: private DateTimeZone zone = null;
051:
052: public static void main(String[] args) {
053: junit.textui.TestRunner.run(suite());
054: }
055:
056: public static TestSuite suite() {
057: return new TestSuite(TestReadableDurationConverter.class);
058: }
059:
060: public TestReadableDurationConverter(String name) {
061: super (name);
062: }
063:
064: protected void setUp() throws Exception {
065: JULIAN = JulianChronology.getInstance();
066: ISO = ISOChronology.getInstance();
067: }
068:
069: //-----------------------------------------------------------------------
070: public void testSingleton() throws Exception {
071: Class cls = ReadableDurationConverter.class;
072: assertEquals(false, Modifier.isPublic(cls.getModifiers()));
073: assertEquals(false, Modifier.isProtected(cls.getModifiers()));
074: assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
075:
076: Constructor con = cls.getDeclaredConstructor((Class[]) null);
077: assertEquals(1, cls.getDeclaredConstructors().length);
078: assertEquals(true, Modifier.isProtected(con.getModifiers()));
079:
080: Field fld = cls.getDeclaredField("INSTANCE");
081: assertEquals(false, Modifier.isPublic(fld.getModifiers()));
082: assertEquals(false, Modifier.isProtected(fld.getModifiers()));
083: assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
084: }
085:
086: //-----------------------------------------------------------------------
087: public void testSupportedType() throws Exception {
088: assertEquals(ReadableDuration.class,
089: ReadableDurationConverter.INSTANCE.getSupportedType());
090: }
091:
092: //-----------------------------------------------------------------------
093: public void testGetDurationMillis_Object() throws Exception {
094: assertEquals(123L, ReadableDurationConverter.INSTANCE
095: .getDurationMillis(new Duration(123L)));
096: }
097:
098: //-----------------------------------------------------------------------
099: public void testGetPeriodType_Object() throws Exception {
100: assertEquals(PeriodType.standard(),
101: ReadableDurationConverter.INSTANCE
102: .getPeriodType(new Duration(123L)));
103: }
104:
105: public void testSetInto_Object() throws Exception {
106: MutablePeriod m = new MutablePeriod(PeriodType
107: .yearMonthDayTime());
108: ReadableDurationConverter.INSTANCE.setInto(m, new Duration(3L
109: * DateTimeConstants.MILLIS_PER_DAY + 4L
110: * DateTimeConstants.MILLIS_PER_MINUTE + 5L), null);
111: assertEquals(0, m.getYears());
112: assertEquals(0, m.getMonths());
113: assertEquals(0, m.getWeeks());
114: assertEquals(0, m.getDays());
115: assertEquals(3 * 24, m.getHours());
116: assertEquals(4, m.getMinutes());
117: assertEquals(0, m.getSeconds());
118: assertEquals(5, m.getMillis());
119: }
120:
121: //-----------------------------------------------------------------------
122: public void testToString() {
123: assertEquals("Converter[org.joda.time.ReadableDuration]",
124: ReadableDurationConverter.INSTANCE.toString());
125: }
126:
127: }
|