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