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.Interval;
028: import org.joda.time.MutableInterval;
029: import org.joda.time.MutablePeriod;
030: import org.joda.time.PeriodType;
031: import org.joda.time.ReadableInterval;
032: import org.joda.time.chrono.BuddhistChronology;
033: import org.joda.time.chrono.CopticChronology;
034: import org.joda.time.chrono.GJChronology;
035: import org.joda.time.chrono.ISOChronology;
036: import org.joda.time.chrono.JulianChronology;
037:
038: /**
039: * This class is a JUnit test for ReadableIntervalConverter.
040: *
041: * @author Stephen Colebourne
042: */
043: public class TestReadableIntervalConverter extends TestCase {
044:
045: private static final DateTimeZone UTC = DateTimeZone.UTC;
046: private static final DateTimeZone PARIS = DateTimeZone
047: .forID("Europe/Paris");
048: private static final Chronology ISO_PARIS = ISOChronology
049: .getInstance(PARIS);
050: private static Chronology JULIAN;
051: private static Chronology ISO;
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(TestReadableIntervalConverter.class);
061: }
062:
063: public TestReadableIntervalConverter(String name) {
064: super (name);
065: }
066:
067: protected void setUp() throws Exception {
068: JULIAN = JulianChronology.getInstance();
069: ISO = ISOChronology.getInstance();
070: }
071:
072: //-----------------------------------------------------------------------
073: public void testSingleton() throws Exception {
074: Class cls = ReadableIntervalConverter.class;
075: assertEquals(false, Modifier.isPublic(cls.getModifiers()));
076: assertEquals(false, Modifier.isProtected(cls.getModifiers()));
077: assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
078:
079: Constructor con = cls.getDeclaredConstructor((Class[]) null);
080: assertEquals(1, cls.getDeclaredConstructors().length);
081: assertEquals(true, Modifier.isProtected(con.getModifiers()));
082:
083: Field fld = cls.getDeclaredField("INSTANCE");
084: assertEquals(false, Modifier.isPublic(fld.getModifiers()));
085: assertEquals(false, Modifier.isProtected(fld.getModifiers()));
086: assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
087: }
088:
089: //-----------------------------------------------------------------------
090: public void testSupportedType() throws Exception {
091: assertEquals(ReadableInterval.class,
092: ReadableIntervalConverter.INSTANCE.getSupportedType());
093: }
094:
095: //-----------------------------------------------------------------------
096: public void testGetDurationMillis_Object() throws Exception {
097: Interval i = new Interval(100L, 223L);
098: assertEquals(123L, ReadableIntervalConverter.INSTANCE
099: .getDurationMillis(i));
100: }
101:
102: //-----------------------------------------------------------------------
103: public void testGetPeriodType_Object() throws Exception {
104: Interval i = new Interval(100L, 223L);
105: assertEquals(PeriodType.standard(),
106: ReadableIntervalConverter.INSTANCE.getPeriodType(i));
107: }
108:
109: public void testSetIntoPeriod_Object1() throws Exception {
110: Interval i = new Interval(100L, 223L);
111: MutablePeriod m = new MutablePeriod(PeriodType.millis());
112: ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
113: assertEquals(0, m.getYears());
114: assertEquals(0, m.getMonths());
115: assertEquals(0, m.getWeeks());
116: assertEquals(0, m.getDays());
117: assertEquals(0, m.getHours());
118: assertEquals(0, m.getMinutes());
119: assertEquals(0, m.getSeconds());
120: assertEquals(123, m.getMillis());
121: }
122:
123: public void testSetIntoPeriod_Object2() throws Exception {
124: Interval i = new Interval(100L, 223L);
125: MutablePeriod m = new MutablePeriod(PeriodType.millis());
126: ReadableIntervalConverter.INSTANCE.setInto(m, i,
127: CopticChronology.getInstance());
128: assertEquals(0, m.getYears());
129: assertEquals(0, m.getMonths());
130: assertEquals(0, m.getWeeks());
131: assertEquals(0, m.getDays());
132: assertEquals(0, m.getHours());
133: assertEquals(0, m.getMinutes());
134: assertEquals(0, m.getSeconds());
135: assertEquals(123, m.getMillis());
136: }
137:
138: //-----------------------------------------------------------------------
139: public void testIsReadableInterval_Object_Chronology()
140: throws Exception {
141: Interval i = new Interval(1234L, 5678L);
142: assertEquals(true, ReadableIntervalConverter.INSTANCE
143: .isReadableInterval(i, null));
144: }
145:
146: public void testSetIntoInterval_Object1() throws Exception {
147: Interval i = new Interval(0L, 123L, CopticChronology
148: .getInstance());
149: MutableInterval m = new MutableInterval(-1000L, 1000L,
150: BuddhistChronology.getInstance());
151: ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
152: assertEquals(0L, m.getStartMillis());
153: assertEquals(123L, m.getEndMillis());
154: assertEquals(CopticChronology.getInstance(), m.getChronology());
155: }
156:
157: public void testSetIntoInterval_Object2() throws Exception {
158: Interval i = new Interval(0L, 123L, CopticChronology
159: .getInstance());
160: MutableInterval m = new MutableInterval(-1000L, 1000L,
161: BuddhistChronology.getInstance());
162: ReadableIntervalConverter.INSTANCE.setInto(m, i, GJChronology
163: .getInstance());
164: assertEquals(0L, m.getStartMillis());
165: assertEquals(123L, m.getEndMillis());
166: assertEquals(GJChronology.getInstance(), m.getChronology());
167: }
168:
169: public void testSetIntoInterval_Object3() throws Exception {
170: MutableInterval i = new MutableInterval(0L, 123L) {
171: public Chronology getChronology() {
172: return null; // bad
173: }
174: };
175: MutableInterval m = new MutableInterval(-1000L, 1000L,
176: BuddhistChronology.getInstance());
177: ReadableIntervalConverter.INSTANCE.setInto(m, i, GJChronology
178: .getInstance());
179: assertEquals(0L, m.getStartMillis());
180: assertEquals(123L, m.getEndMillis());
181: assertEquals(GJChronology.getInstance(), m.getChronology());
182: }
183:
184: public void testSetIntoInterval_Object4() throws Exception {
185: MutableInterval i = new MutableInterval(0L, 123L) {
186: public Chronology getChronology() {
187: return null; // bad
188: }
189: };
190: MutableInterval m = new MutableInterval(-1000L, 1000L,
191: BuddhistChronology.getInstance());
192: ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
193: assertEquals(0L, m.getStartMillis());
194: assertEquals(123L, m.getEndMillis());
195: assertEquals(ISOChronology.getInstance(), m.getChronology());
196: }
197:
198: //-----------------------------------------------------------------------
199: public void testToString() {
200: assertEquals("Converter[org.joda.time.ReadableInterval]",
201: ReadableIntervalConverter.INSTANCE.toString());
202: }
203:
204: }
|