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.Interval;
28  import org.joda.time.MutableInterval;
29  import org.joda.time.MutablePeriod;
30  import org.joda.time.PeriodType;
31  import org.joda.time.ReadableInterval;
32  import org.joda.time.chrono.BuddhistChronology;
33  import org.joda.time.chrono.CopticChronology;
34  import org.joda.time.chrono.GJChronology;
35  import org.joda.time.chrono.ISOChronology;
36  import org.joda.time.chrono.JulianChronology;
37  
38  /**
39   * This class is a JUnit test for ReadableIntervalConverter.
40   *
41   * @author Stephen Colebourne
42   */
43  public class TestReadableIntervalConverter extends TestCase {
44  
45      private static final DateTimeZone UTC = DateTimeZone.UTC;
46      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
47      private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
48      private static Chronology JULIAN;
49      private static Chronology ISO;
50      
51      private DateTimeZone zone = null;
52  
53      public static void main(String[] args) {
54          junit.textui.TestRunner.run(suite());
55      }
56  
57      public static TestSuite suite() {
58          return new TestSuite(TestReadableIntervalConverter.class);
59      }
60  
61      public TestReadableIntervalConverter(String name) {
62          super(name);
63      }
64  
65      protected void setUp() throws Exception {
66          JULIAN = JulianChronology.getInstance();
67          ISO = ISOChronology.getInstance();
68      }
69  
70      //-----------------------------------------------------------------------
71      public void testSingleton() throws Exception {
72          Class cls = ReadableIntervalConverter.class;
73          assertEquals(false, Modifier.isPublic(cls.getModifiers()));
74          assertEquals(false, Modifier.isProtected(cls.getModifiers()));
75          assertEquals(false, Modifier.isPrivate(cls.getModifiers()));
76          
77          Constructor con = cls.getDeclaredConstructor((Class[]) null);
78          assertEquals(1, cls.getDeclaredConstructors().length);
79          assertEquals(true, Modifier.isProtected(con.getModifiers()));
80          
81          Field fld = cls.getDeclaredField("INSTANCE");
82          assertEquals(false, Modifier.isPublic(fld.getModifiers()));
83          assertEquals(false, Modifier.isProtected(fld.getModifiers()));
84          assertEquals(false, Modifier.isPrivate(fld.getModifiers()));
85      }
86  
87      //-----------------------------------------------------------------------
88      public void testSupportedType() throws Exception {
89          assertEquals(ReadableInterval.class, ReadableIntervalConverter.INSTANCE.getSupportedType());
90      }
91  
92      //-----------------------------------------------------------------------
93      public void testGetDurationMillis_Object() throws Exception {
94          Interval i = new Interval(100L, 223L);
95          assertEquals(123L, ReadableIntervalConverter.INSTANCE.getDurationMillis(i));
96      }
97  
98      //-----------------------------------------------------------------------
99      public void testGetPeriodType_Object() throws Exception {
100         Interval i = new Interval(100L, 223L);
101         assertEquals(PeriodType.standard(),
102             ReadableIntervalConverter.INSTANCE.getPeriodType(i));
103     }
104 
105     public void testSetIntoPeriod_Object1() throws Exception {
106         Interval i = new Interval(100L, 223L);
107         MutablePeriod m = new MutablePeriod(PeriodType.millis());
108         ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
109         assertEquals(0, m.getYears());
110         assertEquals(0, m.getMonths());
111         assertEquals(0, m.getWeeks());
112         assertEquals(0, m.getDays());
113         assertEquals(0, m.getHours());
114         assertEquals(0, m.getMinutes());
115         assertEquals(0, m.getSeconds());
116         assertEquals(123, m.getMillis());
117     }
118 
119     public void testSetIntoPeriod_Object2() throws Exception {
120         Interval i = new Interval(100L, 223L);
121         MutablePeriod m = new MutablePeriod(PeriodType.millis());
122         ReadableIntervalConverter.INSTANCE.setInto(m, i, CopticChronology.getInstance());
123         assertEquals(0, m.getYears());
124         assertEquals(0, m.getMonths());
125         assertEquals(0, m.getWeeks());
126         assertEquals(0, m.getDays());
127         assertEquals(0, m.getHours());
128         assertEquals(0, m.getMinutes());
129         assertEquals(0, m.getSeconds());
130         assertEquals(123, m.getMillis());
131     }
132 
133     //-----------------------------------------------------------------------
134     public void testIsReadableInterval_Object_Chronology() throws Exception {
135         Interval i = new Interval(1234L, 5678L);
136         assertEquals(true, ReadableIntervalConverter.INSTANCE.isReadableInterval(i, null));
137     }
138 
139     public void testSetIntoInterval_Object1() throws Exception {
140         Interval i = new Interval(0L, 123L, CopticChronology.getInstance());
141         MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
142         ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
143         assertEquals(0L, m.getStartMillis());
144         assertEquals(123L, m.getEndMillis());
145         assertEquals(CopticChronology.getInstance(), m.getChronology());
146     }
147 
148     public void testSetIntoInterval_Object2() throws Exception {
149         Interval i = new Interval(0L, 123L, CopticChronology.getInstance());
150         MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
151         ReadableIntervalConverter.INSTANCE.setInto(m, i, GJChronology.getInstance());
152         assertEquals(0L, m.getStartMillis());
153         assertEquals(123L, m.getEndMillis());
154         assertEquals(GJChronology.getInstance(), m.getChronology());
155     }
156 
157     public void testSetIntoInterval_Object3() throws Exception {
158         MutableInterval i = new MutableInterval(0L, 123L) {
159             public Chronology getChronology() {
160                 return null; // bad
161             }
162         };
163         MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
164         ReadableIntervalConverter.INSTANCE.setInto(m, i, GJChronology.getInstance());
165         assertEquals(0L, m.getStartMillis());
166         assertEquals(123L, m.getEndMillis());
167         assertEquals(GJChronology.getInstance(), m.getChronology());
168     }
169 
170     public void testSetIntoInterval_Object4() throws Exception {
171         MutableInterval i = new MutableInterval(0L, 123L) {
172             public Chronology getChronology() {
173                 return null; // bad
174             }
175         };
176         MutableInterval m = new MutableInterval(-1000L, 1000L, BuddhistChronology.getInstance());
177         ReadableIntervalConverter.INSTANCE.setInto(m, i, null);
178         assertEquals(0L, m.getStartMillis());
179         assertEquals(123L, m.getEndMillis());
180         assertEquals(ISOChronology.getInstance(), m.getChronology());
181     }
182 
183     //-----------------------------------------------------------------------
184     public void testToString() {
185         assertEquals("Converter[org.joda.time.ReadableInterval]", ReadableIntervalConverter.INSTANCE.toString());
186     }
187 
188 }