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 023 import junit.framework.TestCase; 024 import junit.framework.TestSuite; 025 026 import org.joda.time.Chronology; 027 import org.joda.time.DateTimeField; 028 import org.joda.time.DateTimeZone; 029 import org.joda.time.ReadablePartial; 030 import org.joda.time.TimeOfDay; 031 import org.joda.time.YearMonthDay; 032 import org.joda.time.base.BasePartial; 033 import org.joda.time.chrono.BuddhistChronology; 034 import org.joda.time.chrono.ISOChronology; 035 import org.joda.time.chrono.JulianChronology; 036 037 /** 038 * This class is a Junit unit test for ReadablePartialConverter. 039 * 040 * @author Stephen Colebourne 041 */ 042 public class TestReadablePartialConverter extends TestCase { 043 044 private static final DateTimeZone UTC = DateTimeZone.UTC; 045 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 046 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 047 private static Chronology JULIAN; 048 private static Chronology ISO; 049 private static Chronology BUDDHIST; 050 051 private DateTimeZone zone = null; 052 053 public static void main(String[] args) { 054 junit.textui.TestRunner.run(suite()); 055 } 056 057 public static TestSuite suite() { 058 return new TestSuite(TestReadablePartialConverter.class); 059 } 060 061 public TestReadablePartialConverter(String name) { 062 super(name); 063 } 064 065 protected void setUp() throws Exception { 066 JULIAN = JulianChronology.getInstance(); 067 ISO = ISOChronology.getInstance(); 068 BUDDHIST = BuddhistChronology.getInstance(); 069 } 070 071 //----------------------------------------------------------------------- 072 public void testSingleton() throws Exception { 073 Class cls = ReadablePartialConverter.class; 074 assertEquals(false, Modifier.isPublic(cls.getModifiers())); 075 assertEquals(false, Modifier.isProtected(cls.getModifiers())); 076 assertEquals(false, Modifier.isPrivate(cls.getModifiers())); 077 078 Constructor con = cls.getDeclaredConstructor((Class[]) null); 079 assertEquals(1, cls.getDeclaredConstructors().length); 080 assertEquals(true, Modifier.isProtected(con.getModifiers())); 081 082 Field fld = cls.getDeclaredField("INSTANCE"); 083 assertEquals(false, Modifier.isPublic(fld.getModifiers())); 084 assertEquals(false, Modifier.isProtected(fld.getModifiers())); 085 assertEquals(false, Modifier.isPrivate(fld.getModifiers())); 086 } 087 088 //----------------------------------------------------------------------- 089 public void testSupportedType() throws Exception { 090 assertEquals(ReadablePartial.class, ReadablePartialConverter.INSTANCE.getSupportedType()); 091 } 092 093 //----------------------------------------------------------------------- 094 public void testGetChronology_Object_Zone() throws Exception { 095 assertEquals(ISO_PARIS, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L), PARIS)); 096 assertEquals(ISO, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L), DateTimeZone.getDefault())); 097 assertEquals(ISO, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L), (DateTimeZone) null)); 098 } 099 100 public void testGetChronology_Object_Chronology() throws Exception { 101 assertEquals(JULIAN, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L, BUDDHIST), JULIAN)); 102 assertEquals(JULIAN, ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L), JULIAN)); 103 assertEquals(BUDDHIST.withUTC(), ReadablePartialConverter.INSTANCE.getChronology(new TimeOfDay(123L, BUDDHIST), (Chronology) null)); 104 } 105 106 //----------------------------------------------------------------------- 107 public void testGetPartialValues() throws Exception { 108 TimeOfDay tod = new TimeOfDay(); 109 int[] expected = new int[] {1, 2, 3, 4}; 110 int[] actual = ReadablePartialConverter.INSTANCE.getPartialValues(tod, new TimeOfDay(1, 2, 3, 4), ISOChronology.getInstance(PARIS)); 111 assertEquals(true, Arrays.equals(expected, actual)); 112 113 try { 114 ReadablePartialConverter.INSTANCE.getPartialValues(tod, new YearMonthDay(2005, 6, 9), JULIAN); 115 fail(); 116 } catch (IllegalArgumentException ex) {} 117 try { 118 ReadablePartialConverter.INSTANCE.getPartialValues(tod, new MockTOD(), JULIAN); 119 fail(); 120 } catch (IllegalArgumentException ex) {} 121 } 122 123 static class MockTOD extends BasePartial { 124 protected DateTimeField getField(int index, Chronology chrono) { 125 switch (index) { 126 case 0: 127 return chrono.hourOfDay(); 128 case 1: 129 return chrono.minuteOfHour(); 130 case 2: 131 return chrono.year(); 132 case 3: 133 return chrono.era(); 134 } 135 return null; 136 } 137 public int size() { 138 return 4; 139 } 140 } 141 142 //----------------------------------------------------------------------- 143 public void testToString() { 144 assertEquals("Converter[org.joda.time.ReadablePartial]", ReadablePartialConverter.INSTANCE.toString()); 145 } 146 147 }