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