View Javadoc

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;
19  import java.lang.reflect.Field;
20  import java.lang.reflect.Modifier;
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[] 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 name) {
59          super(name);
60      }
61  
62      @Override
63      protected void setUp() throws Exception {
64          super.setUp();
65          JULIAN = JulianChronology.getInstance();
66          ISO = ISOChronology.getInstance();
67          zone = DateTimeZone.getDefault();
68          DateTimeZone.setDefault(PARIS);
69      }
70  
71      @Override
72      protected void tearDown() throws Exception {
73          super.tearDown();
74          DateTimeZone.setDefault(zone);
75      }
76  
77      //-----------------------------------------------------------------------
78      public void testSingleton() throws Exception {
79          Class cls = ReadableDurationConverter.class;
80          assertEquals(false, Modifier.isPublic(cls.getModifiers()));
81          assertEquals(false, Modifier.isProtected(cls.getModifiers()));
82          assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
83          
84          Constructor con = cls.getDeclaredConstructor((Class[]) null);
85          assertEquals(1, cls.getDeclaredConstructors().length);
86          assertEquals(true, Modifier.isProtected(con.getModifiers()));
87          
88          Field fld = cls.getDeclaredField("INSTANCE");
89          assertEquals(false, Modifier.isPublic(fld.getModifiers()));
90          assertEquals(false, Modifier.isProtected(fld.getModifiers()));
91          assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
92      }
93  
94      //-----------------------------------------------------------------------
95      public void testSupportedType() throws Exception {
96          assertEquals(ReadableDuration.class, ReadableDurationConverter.INSTANCE.getSupportedType());
97      }
98  
99      //-----------------------------------------------------------------------
100     public void testGetDurationMillis_Object() throws Exception {
101         assertEquals(123L, ReadableDurationConverter.INSTANCE.getDurationMillis(new Duration(123L)));
102     }
103 
104     //-----------------------------------------------------------------------
105     public void testGetPeriodType_Object() throws Exception {
106         assertEquals(PeriodType.standard(),
107             ReadableDurationConverter.INSTANCE.getPeriodType(new Duration(123L)));
108     }
109 
110     public void testSetInto_Object() throws Exception {
111         MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime());
112         ReadableDurationConverter.INSTANCE.setInto(m, new Duration(
113             3L * DateTimeConstants.MILLIS_PER_DAY +
114             4L * DateTimeConstants.MILLIS_PER_MINUTE + 5L
115         ), null);
116         assertEquals(0, m.getYears());
117         assertEquals(0, m.getMonths());
118         assertEquals(0, m.getWeeks());
119         assertEquals(0, m.getDays());
120         assertEquals(3 * 24, m.getHours());
121         assertEquals(4, m.getMinutes());
122         assertEquals(0, m.getSeconds());
123         assertEquals(5, m.getMillis());
124     }
125 
126     //-----------------------------------------------------------------------
127     public void testToString() {
128         assertEquals("Converter[org.joda.time.ReadableDuration]", ReadableDurationConverter.INSTANCE.toString());
129     }
130 
131 }