View Javadoc

1   /*
2    *  Copyright 2001-2006 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;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.ByteArrayOutputStream;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  /**
27   * This class is a Junit unit test for Hours.
28   *
29   * @author Stephen Colebourne
30   */
31  public class TestHours extends TestCase {
32      // Test in 2002/03 as time zones are more well known
33      // (before the late 90's they were all over the place)
34      private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
35  
36      public static void main(String[] args) {
37          junit.textui.TestRunner.run(suite());
38      }
39  
40      public static TestSuite suite() {
41          return new TestSuite(TestHours.class);
42      }
43  
44      public TestHours(String name) {
45          super(name);
46      }
47  
48      protected void setUp() throws Exception {
49      }
50  
51      protected void tearDown() throws Exception {
52      }
53  
54      //-----------------------------------------------------------------------
55      public void testConstants() {
56          assertEquals(0, Hours.ZERO.getHours());
57          assertEquals(1, Hours.ONE.getHours());
58          assertEquals(2, Hours.TWO.getHours());
59          assertEquals(3, Hours.THREE.getHours());
60          assertEquals(4, Hours.FOUR.getHours());
61          assertEquals(5, Hours.FIVE.getHours());
62          assertEquals(6, Hours.SIX.getHours());
63          assertEquals(7, Hours.SEVEN.getHours());
64          assertEquals(8, Hours.EIGHT.getHours());
65          assertEquals(Integer.MAX_VALUE, Hours.MAX_VALUE.getHours());
66          assertEquals(Integer.MIN_VALUE, Hours.MIN_VALUE.getHours());
67      }
68  
69      //-----------------------------------------------------------------------
70      public void testFactory_hours_int() {
71          assertSame(Hours.ZERO, Hours.hours(0));
72          assertSame(Hours.ONE, Hours.hours(1));
73          assertSame(Hours.TWO, Hours.hours(2));
74          assertSame(Hours.THREE, Hours.hours(3));
75          assertSame(Hours.FOUR, Hours.hours(4));
76          assertSame(Hours.FIVE, Hours.hours(5));
77          assertSame(Hours.SIX, Hours.hours(6));
78          assertSame(Hours.SEVEN, Hours.hours(7));
79          assertSame(Hours.EIGHT, Hours.hours(8));
80          assertSame(Hours.MAX_VALUE, Hours.hours(Integer.MAX_VALUE));
81          assertSame(Hours.MIN_VALUE, Hours.hours(Integer.MIN_VALUE));
82          assertEquals(-1, Hours.hours(-1).getHours());
83          assertEquals(9, Hours.hours(9).getHours());
84      }
85  
86      //-----------------------------------------------------------------------
87      public void testFactory_hoursBetween_RInstant() {
88          DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
89          DateTime end1 = new DateTime(2006, 6, 9, 15, 0, 0, 0, PARIS);
90          DateTime end2 = new DateTime(2006, 6, 9, 18, 0, 0, 0, PARIS);
91          
92          assertEquals(3, Hours.hoursBetween(start, end1).getHours());
93          assertEquals(0, Hours.hoursBetween(start, start).getHours());
94          assertEquals(0, Hours.hoursBetween(end1, end1).getHours());
95          assertEquals(-3, Hours.hoursBetween(end1, start).getHours());
96          assertEquals(6, Hours.hoursBetween(start, end2).getHours());
97      }
98  
99      public void testFactory_hoursBetween_RPartial() {
100         LocalTime start = new LocalTime(12, 0);
101         LocalTime end1 = new LocalTime(15, 0);
102         TimeOfDay end2 = new TimeOfDay(18, 0);
103         
104         assertEquals(3, Hours.hoursBetween(start, end1).getHours());
105         assertEquals(0, Hours.hoursBetween(start, start).getHours());
106         assertEquals(0, Hours.hoursBetween(end1, end1).getHours());
107         assertEquals(-3, Hours.hoursBetween(end1, start).getHours());
108         assertEquals(6, Hours.hoursBetween(start, end2).getHours());
109     }
110 
111     public void testFactory_hoursIn_RInterval() {
112         DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
113         DateTime end1 = new DateTime(2006, 6, 9, 15, 0, 0, 0, PARIS);
114         DateTime end2 = new DateTime(2006, 6, 9, 18, 0, 0, 0, PARIS);
115         
116         assertEquals(0, Hours.hoursIn((ReadableInterval) null).getHours());
117         assertEquals(3, Hours.hoursIn(new Interval(start, end1)).getHours());
118         assertEquals(0, Hours.hoursIn(new Interval(start, start)).getHours());
119         assertEquals(0, Hours.hoursIn(new Interval(end1, end1)).getHours());
120         assertEquals(6, Hours.hoursIn(new Interval(start, end2)).getHours());
121     }
122 
123     public void testFactory_standardHoursIn_RPeriod() {
124         assertEquals(0, Hours.standardHoursIn((ReadablePeriod) null).getHours());
125         assertEquals(0, Hours.standardHoursIn(Period.ZERO).getHours());
126         assertEquals(1, Hours.standardHoursIn(new Period(0, 0, 0, 0, 1, 0, 0, 0)).getHours());
127         assertEquals(123, Hours.standardHoursIn(Period.hours(123)).getHours());
128         assertEquals(-987, Hours.standardHoursIn(Period.hours(-987)).getHours());
129         assertEquals(1, Hours.standardHoursIn(Period.minutes(119)).getHours());
130         assertEquals(2, Hours.standardHoursIn(Period.minutes(120)).getHours());
131         assertEquals(2, Hours.standardHoursIn(Period.minutes(121)).getHours());
132         assertEquals(48, Hours.standardHoursIn(Period.days(2)).getHours());
133         try {
134             Hours.standardHoursIn(Period.months(1));
135             fail();
136         } catch (IllegalArgumentException ex) {
137             // expeceted
138         }
139     }
140 
141     public void testFactory_parseHours_String() {
142         assertEquals(0, Hours.parseHours((String) null).getHours());
143         assertEquals(0, Hours.parseHours("PT0H").getHours());
144         assertEquals(1, Hours.parseHours("PT1H").getHours());
145         assertEquals(-3, Hours.parseHours("PT-3H").getHours());
146         assertEquals(2, Hours.parseHours("P0Y0M0DT2H").getHours());
147         assertEquals(2, Hours.parseHours("PT2H0M").getHours());
148         try {
149             Hours.parseHours("P1Y1D");
150             fail();
151         } catch (IllegalArgumentException ex) {
152             // expeceted
153         }
154         try {
155             Hours.parseHours("P1DT1H");
156             fail();
157         } catch (IllegalArgumentException ex) {
158             // expeceted
159         }
160     }
161 
162     //-----------------------------------------------------------------------
163     public void testGetMethods() {
164         Hours test = Hours.hours(20);
165         assertEquals(20, test.getHours());
166     }
167 
168     public void testGetFieldType() {
169         Hours test = Hours.hours(20);
170         assertEquals(DurationFieldType.hours(), test.getFieldType());
171     }
172 
173     public void testGetPeriodType() {
174         Hours test = Hours.hours(20);
175         assertEquals(PeriodType.hours(), test.getPeriodType());
176     }
177 
178     //-----------------------------------------------------------------------
179     public void testIsGreaterThan() {
180         assertEquals(true, Hours.THREE.isGreaterThan(Hours.TWO));
181         assertEquals(false, Hours.THREE.isGreaterThan(Hours.THREE));
182         assertEquals(false, Hours.TWO.isGreaterThan(Hours.THREE));
183         assertEquals(true, Hours.ONE.isGreaterThan(null));
184         assertEquals(false, Hours.hours(-1).isGreaterThan(null));
185     }
186 
187     public void testIsLessThan() {
188         assertEquals(false, Hours.THREE.isLessThan(Hours.TWO));
189         assertEquals(false, Hours.THREE.isLessThan(Hours.THREE));
190         assertEquals(true, Hours.TWO.isLessThan(Hours.THREE));
191         assertEquals(false, Hours.ONE.isLessThan(null));
192         assertEquals(true, Hours.hours(-1).isLessThan(null));
193     }
194 
195     //-----------------------------------------------------------------------
196     public void testToString() {
197         Hours test = Hours.hours(20);
198         assertEquals("PT20H", test.toString());
199         
200         test = Hours.hours(-20);
201         assertEquals("PT-20H", test.toString());
202     }
203 
204     //-----------------------------------------------------------------------
205     public void testSerialization() throws Exception {
206         Hours test = Hours.SEVEN;
207         
208         ByteArrayOutputStream baos = new ByteArrayOutputStream();
209         ObjectOutputStream oos = new ObjectOutputStream(baos);
210         oos.writeObject(test);
211         byte[] bytes = baos.toByteArray();
212         oos.close();
213         
214         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
215         ObjectInputStream ois = new ObjectInputStream(bais);
216         Hours result = (Hours) ois.readObject();
217         ois.close();
218         
219         assertSame(test, result);
220     }
221 
222     //-----------------------------------------------------------------------
223     public void testToStandardWeeks() {
224         Hours test = Hours.hours(24 * 7 * 2);
225         Weeks expected = Weeks.weeks(2);
226         assertEquals(expected, test.toStandardWeeks());
227     }
228 
229     public void testToStandardDays() {
230         Hours test = Hours.hours(24 * 2);
231         Days expected = Days.days(2);
232         assertEquals(expected, test.toStandardDays());
233     }
234 
235     public void testToStandardMinutes() {
236         Hours test = Hours.hours(3);
237         Minutes expected = Minutes.minutes(3 * 60);
238         assertEquals(expected, test.toStandardMinutes());
239         
240         try {
241             Hours.MAX_VALUE.toStandardMinutes();
242             fail();
243         } catch (ArithmeticException ex) {
244             // expected
245         }
246     }
247 
248     public void testToStandardSeconds() {
249         Hours test = Hours.hours(3);
250         Seconds expected = Seconds.seconds(3 * 60 * 60);
251         assertEquals(expected, test.toStandardSeconds());
252         
253         try {
254             Hours.MAX_VALUE.toStandardSeconds();
255             fail();
256         } catch (ArithmeticException ex) {
257             // expected
258         }
259     }
260 
261     public void testToStandardDuration() {
262         Hours test = Hours.hours(20);
263         Duration expected = new Duration(20L * DateTimeConstants.MILLIS_PER_HOUR);
264         assertEquals(expected, test.toStandardDuration());
265         
266         expected = new Duration(((long) Integer.MAX_VALUE) * DateTimeConstants.MILLIS_PER_HOUR);
267         assertEquals(expected, Hours.MAX_VALUE.toStandardDuration());
268     }
269 
270     //-----------------------------------------------------------------------
271     public void testPlus_int() {
272         Hours test2 = Hours.hours(2);
273         Hours result = test2.plus(3);
274         assertEquals(2, test2.getHours());
275         assertEquals(5, result.getHours());
276         
277         assertEquals(1, Hours.ONE.plus(0).getHours());
278         
279         try {
280             Hours.MAX_VALUE.plus(1);
281             fail();
282         } catch (ArithmeticException ex) {
283             // expected
284         }
285     }
286 
287     public void testPlus_Hours() {
288         Hours test2 = Hours.hours(2);
289         Hours test3 = Hours.hours(3);
290         Hours result = test2.plus(test3);
291         assertEquals(2, test2.getHours());
292         assertEquals(3, test3.getHours());
293         assertEquals(5, result.getHours());
294         
295         assertEquals(1, Hours.ONE.plus(Hours.ZERO).getHours());
296         assertEquals(1, Hours.ONE.plus((Hours) null).getHours());
297         
298         try {
299             Hours.MAX_VALUE.plus(Hours.ONE);
300             fail();
301         } catch (ArithmeticException ex) {
302             // expected
303         }
304     }
305 
306     public void testMinus_int() {
307         Hours test2 = Hours.hours(2);
308         Hours result = test2.minus(3);
309         assertEquals(2, test2.getHours());
310         assertEquals(-1, result.getHours());
311         
312         assertEquals(1, Hours.ONE.minus(0).getHours());
313         
314         try {
315             Hours.MIN_VALUE.minus(1);
316             fail();
317         } catch (ArithmeticException ex) {
318             // expected
319         }
320     }
321 
322     public void testMinus_Hours() {
323         Hours test2 = Hours.hours(2);
324         Hours test3 = Hours.hours(3);
325         Hours result = test2.minus(test3);
326         assertEquals(2, test2.getHours());
327         assertEquals(3, test3.getHours());
328         assertEquals(-1, result.getHours());
329         
330         assertEquals(1, Hours.ONE.minus(Hours.ZERO).getHours());
331         assertEquals(1, Hours.ONE.minus((Hours) null).getHours());
332         
333         try {
334             Hours.MIN_VALUE.minus(Hours.ONE);
335             fail();
336         } catch (ArithmeticException ex) {
337             // expected
338         }
339     }
340 
341     public void testMultipliedBy_int() {
342         Hours test = Hours.hours(2);
343         assertEquals(6, test.multipliedBy(3).getHours());
344         assertEquals(2, test.getHours());
345         assertEquals(-6, test.multipliedBy(-3).getHours());
346         assertSame(test, test.multipliedBy(1));
347         
348         Hours halfMax = Hours.hours(Integer.MAX_VALUE / 2 + 1);
349         try {
350             halfMax.multipliedBy(2);
351             fail();
352         } catch (ArithmeticException ex) {
353             // expected
354         }
355     }
356 
357     public void testDividedBy_int() {
358         Hours test = Hours.hours(12);
359         assertEquals(6, test.dividedBy(2).getHours());
360         assertEquals(12, test.getHours());
361         assertEquals(4, test.dividedBy(3).getHours());
362         assertEquals(3, test.dividedBy(4).getHours());
363         assertEquals(2, test.dividedBy(5).getHours());
364         assertEquals(2, test.dividedBy(6).getHours());
365         assertSame(test, test.dividedBy(1));
366         
367         try {
368             Hours.ONE.dividedBy(0);
369             fail();
370         } catch (ArithmeticException ex) {
371             // expected
372         }
373     }
374 
375     public void testNegated() {
376         Hours test = Hours.hours(12);
377         assertEquals(-12, test.negated().getHours());
378         assertEquals(12, test.getHours());
379         
380         try {
381             Hours.MIN_VALUE.negated();
382             fail();
383         } catch (ArithmeticException ex) {
384             // expected
385         }
386     }
387 
388     //-----------------------------------------------------------------------
389     public void testAddToLocalDate() {
390         Hours test = Hours.hours(26);
391         LocalDateTime date = new LocalDateTime(2006, 6, 1, 0, 0, 0, 0);
392         LocalDateTime expected = new LocalDateTime(2006, 6, 2, 2, 0, 0, 0);
393         assertEquals(expected, date.plus(test));
394     }
395 
396 }