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
046: .forID("Europe/Paris");
047: private static final Chronology ISO_PARIS = ISOChronology
048: .getInstance(PARIS);
049: private static Chronology JULIAN;
050: private static Chronology ISO;
051: private static Chronology BUDDHIST;
052:
053: private DateTimeZone zone = null;
054:
055: public static void main(String[] args) {
056: junit.textui.TestRunner.run(suite());
057: }
058:
059: public static TestSuite suite() {
060: return new TestSuite(TestReadablePartialConverter.class);
061: }
062:
063: public TestReadablePartialConverter(String name) {
064: super (name);
065: }
066:
067: protected void setUp() throws Exception {
068: JULIAN = JulianChronology.getInstance();
069: ISO = ISOChronology.getInstance();
070: BUDDHIST = BuddhistChronology.getInstance();
071: }
072:
073: //-----------------------------------------------------------------------
074: public void testSingleton() throws Exception {
075: Class cls = ReadablePartialConverter.class;
076: assertEquals(false, Modifier.isPublic(cls.getModifiers()));
077: assertEquals(false, Modifier.isProtected(cls.getModifiers()));
078: assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
079:
080: Constructor con = cls.getDeclaredConstructor((Class[]) null);
081: assertEquals(1, cls.getDeclaredConstructors().length);
082: assertEquals(true, Modifier.isProtected(con.getModifiers()));
083:
084: Field fld = cls.getDeclaredField("INSTANCE");
085: assertEquals(false, Modifier.isPublic(fld.getModifiers()));
086: assertEquals(false, Modifier.isProtected(fld.getModifiers()));
087: assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
088: }
089:
090: //-----------------------------------------------------------------------
091: public void testSupportedType() throws Exception {
092: assertEquals(ReadablePartial.class,
093: ReadablePartialConverter.INSTANCE.getSupportedType());
094: }
095:
096: //-----------------------------------------------------------------------
097: public void testGetChronology_Object_Zone() throws Exception {
098: assertEquals(ISO_PARIS, ReadablePartialConverter.INSTANCE
099: .getChronology(new TimeOfDay(123L), PARIS));
100: assertEquals(ISO, ReadablePartialConverter.INSTANCE
101: .getChronology(new TimeOfDay(123L), DateTimeZone
102: .getDefault()));
103: assertEquals(ISO,
104: ReadablePartialConverter.INSTANCE.getChronology(
105: new TimeOfDay(123L), (DateTimeZone) null));
106: }
107:
108: public void testGetChronology_Object_Chronology() throws Exception {
109: assertEquals(JULIAN, ReadablePartialConverter.INSTANCE
110: .getChronology(new TimeOfDay(123L, BUDDHIST), JULIAN));
111: assertEquals(JULIAN, ReadablePartialConverter.INSTANCE
112: .getChronology(new TimeOfDay(123L), JULIAN));
113: assertEquals(BUDDHIST.withUTC(),
114: ReadablePartialConverter.INSTANCE.getChronology(
115: new TimeOfDay(123L, BUDDHIST),
116: (Chronology) null));
117: }
118:
119: //-----------------------------------------------------------------------
120: public void testGetPartialValues() throws Exception {
121: TimeOfDay tod = new TimeOfDay();
122: int[] expected = new int[] { 1, 2, 3, 4 };
123: int[] actual = ReadablePartialConverter.INSTANCE
124: .getPartialValues(tod, new TimeOfDay(1, 2, 3, 4),
125: ISOChronology.getInstance(PARIS));
126: assertEquals(true, Arrays.equals(expected, actual));
127:
128: try {
129: ReadablePartialConverter.INSTANCE.getPartialValues(tod,
130: new YearMonthDay(2005, 6, 9), JULIAN);
131: fail();
132: } catch (IllegalArgumentException ex) {
133: }
134: try {
135: ReadablePartialConverter.INSTANCE.getPartialValues(tod,
136: new MockTOD(), JULIAN);
137: fail();
138: } catch (IllegalArgumentException ex) {
139: }
140: }
141:
142: static class MockTOD extends BasePartial {
143: protected DateTimeField getField(int index, Chronology chrono) {
144: switch (index) {
145: case 0:
146: return chrono.hourOfDay();
147: case 1:
148: return chrono.minuteOfHour();
149: case 2:
150: return chrono.year();
151: case 3:
152: return chrono.era();
153: }
154: return null;
155: }
156:
157: public int size() {
158: return 4;
159: }
160: }
161:
162: //-----------------------------------------------------------------------
163: public void testToString() {
164: assertEquals("Converter[org.joda.time.ReadablePartial]",
165: ReadablePartialConverter.INSTANCE.toString());
166: }
167:
168: }
|