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.DateTimeZone;
27  import org.joda.time.PeriodType;
28  import org.joda.time.MutablePeriod;
29  import org.joda.time.ReadablePeriod;
30  import org.joda.time.Period;
31  import org.joda.time.chrono.ISOChronology;
32  import org.joda.time.chrono.JulianChronology;
33  
34  /**
35   * This class is a Junit unit test for ReadablePeriodConverter.
36   *
37   * @author Stephen Colebourne
38   */
39  public class TestReadablePeriodConverter extends TestCase {
40  
41      private static final DateTimeZone UTC = DateTimeZone.UTC;
42      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
43      private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
44      private static Chronology JULIAN;
45      private static Chronology ISO;
46      
47      private DateTimeZone zone = null;
48  
49      public static void main(String[] args) {
50          junit.textui.TestRunner.run(suite());
51      }
52  
53      public static TestSuite suite() {
54          return new TestSuite(TestReadablePeriodConverter.class);
55      }
56  
57      public TestReadablePeriodConverter(String name) {
58          super(name);
59      }
60  
61      protected void setUp() throws Exception {
62          JULIAN = JulianChronology.getInstance();
63          ISO = ISOChronology.getInstance();
64      }
65  
66      //-----------------------------------------------------------------------
67      public void testSingleton() throws Exception {
68          Class cls = ReadablePeriodConverter.class;
69          assertEquals(false, Modifier.isPublic(cls.getModifiers()));
70          assertEquals(false, Modifier.isProtected(cls.getModifiers()));
71          assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
72          
73          Constructor con = cls.getDeclaredConstructor((Class[]) null);
74          assertEquals(1, cls.getDeclaredConstructors().length);
75          assertEquals(true, Modifier.isProtected(con.getModifiers()));
76          
77          Field fld = cls.getDeclaredField("INSTANCE");
78          assertEquals(false, Modifier.isPublic(fld.getModifiers()));
79          assertEquals(false, Modifier.isProtected(fld.getModifiers()));
80          assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
81      }
82  
83      //-----------------------------------------------------------------------
84      public void testSupportedType() throws Exception {
85          assertEquals(ReadablePeriod.class, ReadablePeriodConverter.INSTANCE.getSupportedType());
86      }
87  
88      //-----------------------------------------------------------------------
89      public void testGetPeriodType_Object() throws Exception {
90          assertEquals(PeriodType.standard(),
91              ReadablePeriodConverter.INSTANCE.getPeriodType(new Period(123L, PeriodType.standard())));
92          assertEquals(PeriodType.yearMonthDayTime(),
93              ReadablePeriodConverter.INSTANCE.getPeriodType(new Period(123L, PeriodType.yearMonthDayTime())));
94      }
95  
96      public void testSetInto_Object() throws Exception {
97          MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime());
98          ReadablePeriodConverter.INSTANCE.setInto(m, new Period(0, 0, 0, 3, 0, 4, 0, 5), null);
99          assertEquals(0, m.getYears());
100         assertEquals(0, m.getMonths());
101         assertEquals(0, m.getWeeks());
102         assertEquals(3, m.getDays());
103         assertEquals(0, m.getHours());
104         assertEquals(4, m.getMinutes());
105         assertEquals(0, m.getSeconds());
106         assertEquals(5, m.getMillis());
107     }
108 
109     //-----------------------------------------------------------------------
110     public void testToString() {
111         assertEquals("Converter[org.joda.time.ReadablePeriod]", ReadablePeriodConverter.INSTANCE.toString());
112     }
113 
114 }