KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > convert > TestReadableDurationConverter


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.convert;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Field JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
21
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import org.joda.time.Chronology;
26 import org.joda.time.DateTimeConstants;
27 import org.joda.time.DateTimeZone;
28 import org.joda.time.Duration;
29 import org.joda.time.PeriodType;
30 import org.joda.time.MutablePeriod;
31 import org.joda.time.ReadableDuration;
32 import org.joda.time.chrono.ISOChronology;
33 import org.joda.time.chrono.JulianChronology;
34
35 /**
36  * This class is a Junit unit test for ReadableDurationConverter.
37  *
38  * @author Stephen Colebourne
39  */

40 public class TestReadableDurationConverter extends TestCase {
41
42     private static final DateTimeZone UTC = DateTimeZone.UTC;
43     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
44     private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
45     private static Chronology JULIAN;
46     private static Chronology ISO;
47     
48     private DateTimeZone zone = null;
49
50     public static void main(String JavaDoc[] args) {
51         junit.textui.TestRunner.run(suite());
52     }
53
54     public static TestSuite suite() {
55         return new TestSuite(TestReadableDurationConverter.class);
56     }
57
58     public TestReadableDurationConverter(String JavaDoc name) {
59         super(name);
60     }
61
62     protected void setUp() throws Exception JavaDoc {
63         JULIAN = JulianChronology.getInstance();
64         ISO = ISOChronology.getInstance();
65     }
66
67     //-----------------------------------------------------------------------
68
public void testSingleton() throws Exception JavaDoc {
69         Class JavaDoc cls = ReadableDurationConverter.class;
70         assertEquals(false, Modifier.isPublic(cls.getModifiers()));
71         assertEquals(false, Modifier.isProtected(cls.getModifiers()));
72         assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
73         
74         Constructor JavaDoc con = cls.getDeclaredConstructor((Class JavaDoc[]) null);
75         assertEquals(1, cls.getDeclaredConstructors().length);
76         assertEquals(true, Modifier.isProtected(con.getModifiers()));
77         
78         Field JavaDoc fld = cls.getDeclaredField("INSTANCE");
79         assertEquals(false, Modifier.isPublic(fld.getModifiers()));
80         assertEquals(false, Modifier.isProtected(fld.getModifiers()));
81         assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
82     }
83
84     //-----------------------------------------------------------------------
85
public void testSupportedType() throws Exception JavaDoc {
86         assertEquals(ReadableDuration.class, ReadableDurationConverter.INSTANCE.getSupportedType());
87     }
88
89     //-----------------------------------------------------------------------
90
public void testGetDurationMillis_Object() throws Exception JavaDoc {
91         assertEquals(123L, ReadableDurationConverter.INSTANCE.getDurationMillis(new Duration(123L)));
92     }
93
94     //-----------------------------------------------------------------------
95
public void testGetPeriodType_Object() throws Exception JavaDoc {
96         assertEquals(PeriodType.standard(),
97             ReadableDurationConverter.INSTANCE.getPeriodType(new Duration(123L)));
98     }
99
100     public void testSetInto_Object() throws Exception JavaDoc {
101         MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime());
102         ReadableDurationConverter.INSTANCE.setInto(m, new Duration(
103             3L * DateTimeConstants.MILLIS_PER_DAY +
104             4L * DateTimeConstants.MILLIS_PER_MINUTE + 5L
105         ), null);
106         assertEquals(0, m.getYears());
107         assertEquals(0, m.getMonths());
108         assertEquals(0, m.getWeeks());
109         assertEquals(0, m.getDays());
110         assertEquals(3 * 24, m.getHours());
111         assertEquals(4, m.getMinutes());
112         assertEquals(0, m.getSeconds());
113         assertEquals(5, m.getMillis());
114     }
115
116     //-----------------------------------------------------------------------
117
public void testToString() {
118         assertEquals("Converter[org.joda.time.ReadableDuration]", ReadableDurationConverter.INSTANCE.toString());
119     }
120
121 }
122
Popular Tags