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 Minutes.
28   *
29   * @author Stephen Colebourne
30   */
31  public class TestMinutes 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(TestMinutes.class);
42      }
43  
44      public TestMinutes(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, Minutes.ZERO.getMinutes());
57          assertEquals(1, Minutes.ONE.getMinutes());
58          assertEquals(2, Minutes.TWO.getMinutes());
59          assertEquals(3, Minutes.THREE.getMinutes());
60          assertEquals(Integer.MAX_VALUE, Minutes.MAX_VALUE.getMinutes());
61          assertEquals(Integer.MIN_VALUE, Minutes.MIN_VALUE.getMinutes());
62      }
63  
64      //-----------------------------------------------------------------------
65      public void testFactory_minutes_int() {
66          assertSame(Minutes.ZERO, Minutes.minutes(0));
67          assertSame(Minutes.ONE, Minutes.minutes(1));
68          assertSame(Minutes.TWO, Minutes.minutes(2));
69          assertSame(Minutes.THREE, Minutes.minutes(3));
70          assertSame(Minutes.MAX_VALUE, Minutes.minutes(Integer.MAX_VALUE));
71          assertSame(Minutes.MIN_VALUE, Minutes.minutes(Integer.MIN_VALUE));
72          assertEquals(-1, Minutes.minutes(-1).getMinutes());
73          assertEquals(4, Minutes.minutes(4).getMinutes());
74      }
75  
76      //-----------------------------------------------------------------------
77      public void testFactory_minutesBetween_RInstant() {
78          DateTime start = new DateTime(2006, 6, 9, 12, 3, 0, 0, PARIS);
79          DateTime end1 = new DateTime(2006, 6, 9, 12, 6, 0, 0, PARIS);
80          DateTime end2 = new DateTime(2006, 6, 9, 12, 9, 0, 0, PARIS);
81          
82          assertEquals(3, Minutes.minutesBetween(start, end1).getMinutes());
83          assertEquals(0, Minutes.minutesBetween(start, start).getMinutes());
84          assertEquals(0, Minutes.minutesBetween(end1, end1).getMinutes());
85          assertEquals(-3, Minutes.minutesBetween(end1, start).getMinutes());
86          assertEquals(6, Minutes.minutesBetween(start, end2).getMinutes());
87      }
88  
89      public void testFactory_minutesBetween_RPartial() {
90          LocalTime start = new LocalTime(12, 3);
91          LocalTime end1 = new LocalTime(12, 6);
92          TimeOfDay end2 = new TimeOfDay(12, 9);
93          
94          assertEquals(3, Minutes.minutesBetween(start, end1).getMinutes());
95          assertEquals(0, Minutes.minutesBetween(start, start).getMinutes());
96          assertEquals(0, Minutes.minutesBetween(end1, end1).getMinutes());
97          assertEquals(-3, Minutes.minutesBetween(end1, start).getMinutes());
98          assertEquals(6, Minutes.minutesBetween(start, end2).getMinutes());
99      }
100 
101     public void testFactory_minutesIn_RInterval() {
102         DateTime start = new DateTime(2006, 6, 9, 12, 3, 0, 0, PARIS);
103         DateTime end1 = new DateTime(2006, 6, 9, 12, 6, 0, 0, PARIS);
104         DateTime end2 = new DateTime(2006, 6, 9, 12, 9, 0, 0, PARIS);
105         
106         assertEquals(0, Minutes.minutesIn((ReadableInterval) null).getMinutes());
107         assertEquals(3, Minutes.minutesIn(new Interval(start, end1)).getMinutes());
108         assertEquals(0, Minutes.minutesIn(new Interval(start, start)).getMinutes());
109         assertEquals(0, Minutes.minutesIn(new Interval(end1, end1)).getMinutes());
110         assertEquals(6, Minutes.minutesIn(new Interval(start, end2)).getMinutes());
111     }
112 
113     public void testFactory_standardMinutesIn_RPeriod() {
114         assertEquals(0, Minutes.standardMinutesIn((ReadablePeriod) null).getMinutes());
115         assertEquals(0, Minutes.standardMinutesIn(Period.ZERO).getMinutes());
116         assertEquals(1, Minutes.standardMinutesIn(new Period(0, 0, 0, 0, 0, 1, 0, 0)).getMinutes());
117         assertEquals(123, Minutes.standardMinutesIn(Period.minutes(123)).getMinutes());
118         assertEquals(-987, Minutes.standardMinutesIn(Period.minutes(-987)).getMinutes());
119         assertEquals(1, Minutes.standardMinutesIn(Period.seconds(119)).getMinutes());
120         assertEquals(2, Minutes.standardMinutesIn(Period.seconds(120)).getMinutes());
121         assertEquals(2, Minutes.standardMinutesIn(Period.seconds(121)).getMinutes());
122         assertEquals(120, Minutes.standardMinutesIn(Period.hours(2)).getMinutes());
123         try {
124             Minutes.standardMinutesIn(Period.months(1));
125             fail();
126         } catch (IllegalArgumentException ex) {
127             // expeceted
128         }
129     }
130 
131     public void testFactory_parseMinutes_String() {
132         assertEquals(0, Minutes.parseMinutes((String) null).getMinutes());
133         assertEquals(0, Minutes.parseMinutes("PT0M").getMinutes());
134         assertEquals(1, Minutes.parseMinutes("PT1M").getMinutes());
135         assertEquals(-3, Minutes.parseMinutes("PT-3M").getMinutes());
136         assertEquals(2, Minutes.parseMinutes("P0Y0M0DT2M").getMinutes());
137         assertEquals(2, Minutes.parseMinutes("PT0H2M").getMinutes());
138         try {
139             Minutes.parseMinutes("P1Y1D");
140             fail();
141         } catch (IllegalArgumentException ex) {
142             // expeceted
143         }
144         try {
145             Minutes.parseMinutes("P1DT1M");
146             fail();
147         } catch (IllegalArgumentException ex) {
148             // expeceted
149         }
150     }
151 
152     //-----------------------------------------------------------------------
153     public void testGetMethods() {
154         Minutes test = Minutes.minutes(20);
155         assertEquals(20, test.getMinutes());
156     }
157 
158     public void testGetFieldType() {
159         Minutes test = Minutes.minutes(20);
160         assertEquals(DurationFieldType.minutes(), test.getFieldType());
161     }
162 
163     public void testGetPeriodType() {
164         Minutes test = Minutes.minutes(20);
165         assertEquals(PeriodType.minutes(), test.getPeriodType());
166     }
167 
168     //-----------------------------------------------------------------------
169     public void testIsGreaterThan() {
170         assertEquals(true, Minutes.THREE.isGreaterThan(Minutes.TWO));
171         assertEquals(false, Minutes.THREE.isGreaterThan(Minutes.THREE));
172         assertEquals(false, Minutes.TWO.isGreaterThan(Minutes.THREE));
173         assertEquals(true, Minutes.ONE.isGreaterThan(null));
174         assertEquals(false, Minutes.minutes(-1).isGreaterThan(null));
175     }
176 
177     public void testIsLessThan() {
178         assertEquals(false, Minutes.THREE.isLessThan(Minutes.TWO));
179         assertEquals(false, Minutes.THREE.isLessThan(Minutes.THREE));
180         assertEquals(true, Minutes.TWO.isLessThan(Minutes.THREE));
181         assertEquals(false, Minutes.ONE.isLessThan(null));
182         assertEquals(true, Minutes.minutes(-1).isLessThan(null));
183     }
184 
185     //-----------------------------------------------------------------------
186     public void testToString() {
187         Minutes test = Minutes.minutes(20);
188         assertEquals("PT20M", test.toString());
189         
190         test = Minutes.minutes(-20);
191         assertEquals("PT-20M", test.toString());
192     }
193 
194     //-----------------------------------------------------------------------
195     public void testSerialization() throws Exception {
196         Minutes test = Minutes.THREE;
197         
198         ByteArrayOutputStream baos = new ByteArrayOutputStream();
199         ObjectOutputStream oos = new ObjectOutputStream(baos);
200         oos.writeObject(test);
201         byte[] bytes = baos.toByteArray();
202         oos.close();
203         
204         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
205         ObjectInputStream ois = new ObjectInputStream(bais);
206         Minutes result = (Minutes) ois.readObject();
207         ois.close();
208         
209         assertSame(test, result);
210     }
211 
212     //-----------------------------------------------------------------------
213     public void testToStandardWeeks() {
214         Minutes test = Minutes.minutes(60 * 24 * 7 * 2);
215         Weeks expected = Weeks.weeks(2);
216         assertEquals(expected, test.toStandardWeeks());
217     }
218 
219     public void testToStandardDays() {
220         Minutes test = Minutes.minutes(60 * 24 * 2);
221         Days expected = Days.days(2);
222         assertEquals(expected, test.toStandardDays());
223     }
224 
225     public void testToStandardHours() {
226         Minutes test = Minutes.minutes(3 * 60);
227         Hours expected = Hours.hours(3);
228         assertEquals(expected, test.toStandardHours());
229     }
230 
231     public void testToStandardSeconds() {
232         Minutes test = Minutes.minutes(3);
233         Seconds expected = Seconds.seconds(3 * 60);
234         assertEquals(expected, test.toStandardSeconds());
235         
236         try {
237             Minutes.MAX_VALUE.toStandardSeconds();
238             fail();
239         } catch (ArithmeticException ex) {
240             // expected
241         }
242     }
243 
244     public void testToStandardDuration() {
245         Minutes test = Minutes.minutes(20);
246         Duration expected = new Duration(20L * DateTimeConstants.MILLIS_PER_MINUTE);
247         assertEquals(expected, test.toStandardDuration());
248         
249         expected = new Duration(((long) Integer.MAX_VALUE) * DateTimeConstants.MILLIS_PER_MINUTE);
250         assertEquals(expected, Minutes.MAX_VALUE.toStandardDuration());
251     }
252 
253     //-----------------------------------------------------------------------
254     public void testPlus_int() {
255         Minutes test2 = Minutes.minutes(2);
256         Minutes result = test2.plus(3);
257         assertEquals(2, test2.getMinutes());
258         assertEquals(5, result.getMinutes());
259         
260         assertEquals(1, Minutes.ONE.plus(0).getMinutes());
261         
262         try {
263             Minutes.MAX_VALUE.plus(1);
264             fail();
265         } catch (ArithmeticException ex) {
266             // expected
267         }
268     }
269 
270     public void testPlus_Minutes() {
271         Minutes test2 = Minutes.minutes(2);
272         Minutes test3 = Minutes.minutes(3);
273         Minutes result = test2.plus(test3);
274         assertEquals(2, test2.getMinutes());
275         assertEquals(3, test3.getMinutes());
276         assertEquals(5, result.getMinutes());
277         
278         assertEquals(1, Minutes.ONE.plus(Minutes.ZERO).getMinutes());
279         assertEquals(1, Minutes.ONE.plus((Minutes) null).getMinutes());
280         
281         try {
282             Minutes.MAX_VALUE.plus(Minutes.ONE);
283             fail();
284         } catch (ArithmeticException ex) {
285             // expected
286         }
287     }
288 
289     public void testMinus_int() {
290         Minutes test2 = Minutes.minutes(2);
291         Minutes result = test2.minus(3);
292         assertEquals(2, test2.getMinutes());
293         assertEquals(-1, result.getMinutes());
294         
295         assertEquals(1, Minutes.ONE.minus(0).getMinutes());
296         
297         try {
298             Minutes.MIN_VALUE.minus(1);
299             fail();
300         } catch (ArithmeticException ex) {
301             // expected
302         }
303     }
304 
305     public void testMinus_Minutes() {
306         Minutes test2 = Minutes.minutes(2);
307         Minutes test3 = Minutes.minutes(3);
308         Minutes result = test2.minus(test3);
309         assertEquals(2, test2.getMinutes());
310         assertEquals(3, test3.getMinutes());
311         assertEquals(-1, result.getMinutes());
312         
313         assertEquals(1, Minutes.ONE.minus(Minutes.ZERO).getMinutes());
314         assertEquals(1, Minutes.ONE.minus((Minutes) null).getMinutes());
315         
316         try {
317             Minutes.MIN_VALUE.minus(Minutes.ONE);
318             fail();
319         } catch (ArithmeticException ex) {
320             // expected
321         }
322     }
323 
324     public void testMultipliedBy_int() {
325         Minutes test = Minutes.minutes(2);
326         assertEquals(6, test.multipliedBy(3).getMinutes());
327         assertEquals(2, test.getMinutes());
328         assertEquals(-6, test.multipliedBy(-3).getMinutes());
329         assertSame(test, test.multipliedBy(1));
330         
331         Minutes halfMax = Minutes.minutes(Integer.MAX_VALUE / 2 + 1);
332         try {
333             halfMax.multipliedBy(2);
334             fail();
335         } catch (ArithmeticException ex) {
336             // expected
337         }
338     }
339 
340     public void testDividedBy_int() {
341         Minutes test = Minutes.minutes(12);
342         assertEquals(6, test.dividedBy(2).getMinutes());
343         assertEquals(12, test.getMinutes());
344         assertEquals(4, test.dividedBy(3).getMinutes());
345         assertEquals(3, test.dividedBy(4).getMinutes());
346         assertEquals(2, test.dividedBy(5).getMinutes());
347         assertEquals(2, test.dividedBy(6).getMinutes());
348         assertSame(test, test.dividedBy(1));
349         
350         try {
351             Minutes.ONE.dividedBy(0);
352             fail();
353         } catch (ArithmeticException ex) {
354             // expected
355         }
356     }
357 
358     public void testNegated() {
359         Minutes test = Minutes.minutes(12);
360         assertEquals(-12, test.negated().getMinutes());
361         assertEquals(12, test.getMinutes());
362         
363         try {
364             Minutes.MIN_VALUE.negated();
365             fail();
366         } catch (ArithmeticException ex) {
367             // expected
368         }
369     }
370 
371     //-----------------------------------------------------------------------
372     public void testAddToLocalDate() {
373         Minutes test = Minutes.minutes(26);
374         LocalDateTime date = new LocalDateTime(2006, 6, 1, 0, 0, 0, 0);
375         LocalDateTime expected = new LocalDateTime(2006, 6, 1, 0, 26, 0, 0);
376         assertEquals(expected, date.plus(test));
377     }
378 
379 }