KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TestMonths


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 JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26 /**
27  * This class is a Junit unit test for Months.
28  *
29  * @author Stephen Colebourne
30  */

31 public class TestMonths 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 JavaDoc[] args) {
37         junit.textui.TestRunner.run(suite());
38     }
39
40     public static TestSuite suite() {
41         return new TestSuite(TestMonths.class);
42     }
43
44     public TestMonths(String JavaDoc name) {
45         super(name);
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49     }
50
51     protected void tearDown() throws Exception JavaDoc {
52     }
53
54     //-----------------------------------------------------------------------
55
public void testConstants() {
56         assertEquals(0, Months.ZERO.getMonths());
57         assertEquals(1, Months.ONE.getMonths());
58         assertEquals(2, Months.TWO.getMonths());
59         assertEquals(3, Months.THREE.getMonths());
60         assertEquals(4, Months.FOUR.getMonths());
61         assertEquals(5, Months.FIVE.getMonths());
62         assertEquals(6, Months.SIX.getMonths());
63         assertEquals(7, Months.SEVEN.getMonths());
64         assertEquals(8, Months.EIGHT.getMonths());
65         assertEquals(9, Months.NINE.getMonths());
66         assertEquals(10, Months.TEN.getMonths());
67         assertEquals(11, Months.ELEVEN.getMonths());
68         assertEquals(12, Months.TWELVE.getMonths());
69         assertEquals(Integer.MAX_VALUE, Months.MAX_VALUE.getMonths());
70         assertEquals(Integer.MIN_VALUE, Months.MIN_VALUE.getMonths());
71     }
72
73     //-----------------------------------------------------------------------
74
public void testFactory_months_int() {
75         assertSame(Months.ZERO, Months.months(0));
76         assertSame(Months.ONE, Months.months(1));
77         assertSame(Months.TWO, Months.months(2));
78         assertSame(Months.THREE, Months.months(3));
79         assertSame(Months.FOUR, Months.months(4));
80         assertSame(Months.FIVE, Months.months(5));
81         assertSame(Months.SIX, Months.months(6));
82         assertSame(Months.SEVEN, Months.months(7));
83         assertSame(Months.EIGHT, Months.months(8));
84         assertSame(Months.NINE, Months.months(9));
85         assertSame(Months.TEN, Months.months(10));
86         assertSame(Months.ELEVEN, Months.months(11));
87         assertSame(Months.TWELVE, Months.months(12));
88         assertSame(Months.MAX_VALUE, Months.months(Integer.MAX_VALUE));
89         assertSame(Months.MIN_VALUE, Months.months(Integer.MIN_VALUE));
90         assertEquals(-1, Months.months(-1).getMonths());
91         assertEquals(13, Months.months(13).getMonths());
92     }
93
94     //-----------------------------------------------------------------------
95
public void testFactory_monthsBetween_RInstant() {
96         DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
97         DateTime end1 = new DateTime(2006, 9, 9, 12, 0, 0, 0, PARIS);
98         DateTime end2 = new DateTime(2006, 12, 9, 12, 0, 0, 0, PARIS);
99         
100         assertEquals(3, Months.monthsBetween(start, end1).getMonths());
101         assertEquals(0, Months.monthsBetween(start, start).getMonths());
102         assertEquals(0, Months.monthsBetween(end1, end1).getMonths());
103         assertEquals(-3, Months.monthsBetween(end1, start).getMonths());
104         assertEquals(6, Months.monthsBetween(start, end2).getMonths());
105     }
106
107     public void testFactory_monthsBetween_RPartial() {
108         LocalDate start = new LocalDate(2006, 6, 9);
109         LocalDate end1 = new LocalDate(2006, 9, 9);
110         YearMonthDay end2 = new YearMonthDay(2006, 12, 9);
111         
112         assertEquals(3, Months.monthsBetween(start, end1).getMonths());
113         assertEquals(0, Months.monthsBetween(start, start).getMonths());
114         assertEquals(0, Months.monthsBetween(end1, end1).getMonths());
115         assertEquals(-3, Months.monthsBetween(end1, start).getMonths());
116         assertEquals(6, Months.monthsBetween(start, end2).getMonths());
117     }
118
119     public void testFactory_monthsIn_RInterval() {
120         DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
121         DateTime end1 = new DateTime(2006, 9, 9, 12, 0, 0, 0, PARIS);
122         DateTime end2 = new DateTime(2006, 12, 9, 12, 0, 0, 0, PARIS);
123         
124         assertEquals(0, Months.monthsIn((ReadableInterval) null).getMonths());
125         assertEquals(3, Months.monthsIn(new Interval(start, end1)).getMonths());
126         assertEquals(0, Months.monthsIn(new Interval(start, start)).getMonths());
127         assertEquals(0, Months.monthsIn(new Interval(end1, end1)).getMonths());
128         assertEquals(6, Months.monthsIn(new Interval(start, end2)).getMonths());
129     }
130
131     public void testFactory_parseMonths_String() {
132         assertEquals(0, Months.parseMonths((String JavaDoc) null).getMonths());
133         assertEquals(0, Months.parseMonths("P0M").getMonths());
134         assertEquals(1, Months.parseMonths("P1M").getMonths());
135         assertEquals(-3, Months.parseMonths("P-3M").getMonths());
136         assertEquals(2, Months.parseMonths("P0Y2M").getMonths());
137         assertEquals(2, Months.parseMonths("P2MT0H0M").getMonths());
138         try {
139             Months.parseMonths("P1Y1D");
140             fail();
141         } catch (IllegalArgumentException JavaDoc ex) {
142             // expeceted
143
}
144         try {
145             Months.parseMonths("P1MT1H");
146             fail();
147         } catch (IllegalArgumentException JavaDoc ex) {
148             // expeceted
149
}
150     }
151
152     //-----------------------------------------------------------------------
153
public void testGetMethods() {
154         Months test = Months.months(20);
155         assertEquals(20, test.getMonths());
156     }
157
158     public void testGetFieldType() {
159         Months test = Months.months(20);
160         assertEquals(DurationFieldType.months(), test.getFieldType());
161     }
162
163     public void testGetPeriodType() {
164         Months test = Months.months(20);
165         assertEquals(PeriodType.months(), test.getPeriodType());
166     }
167
168     //-----------------------------------------------------------------------
169
public void testIsGreaterThan() {
170         assertEquals(true, Months.THREE.isGreaterThan(Months.TWO));
171         assertEquals(false, Months.THREE.isGreaterThan(Months.THREE));
172         assertEquals(false, Months.TWO.isGreaterThan(Months.THREE));
173         assertEquals(true, Months.ONE.isGreaterThan(null));
174         assertEquals(false, Months.months(-1).isGreaterThan(null));
175     }
176
177     public void testIsLessThan() {
178         assertEquals(false, Months.THREE.isLessThan(Months.TWO));
179         assertEquals(false, Months.THREE.isLessThan(Months.THREE));
180         assertEquals(true, Months.TWO.isLessThan(Months.THREE));
181         assertEquals(false, Months.ONE.isLessThan(null));
182         assertEquals(true, Months.months(-1).isLessThan(null));
183     }
184
185     //-----------------------------------------------------------------------
186
public void testToString() {
187         Months test = Months.months(20);
188         assertEquals("P20M", test.toString());
189         
190         test = Months.months(-20);
191         assertEquals("P-20M", test.toString());
192     }
193
194     //-----------------------------------------------------------------------
195
public void testSerialization() throws Exception JavaDoc {
196         Months test = Months.THREE;
197         
198         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
199         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
200         oos.writeObject(test);
201         byte[] bytes = baos.toByteArray();
202         oos.close();
203         
204         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
205         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
206         Months result = (Months) ois.readObject();
207         ois.close();
208         
209         assertSame(test, result);
210     }
211
212     //-----------------------------------------------------------------------
213
public void testPlus_int() {
214         Months test2 = Months.months(2);
215         Months result = test2.plus(3);
216         assertEquals(2, test2.getMonths());
217         assertEquals(5, result.getMonths());
218         
219         assertEquals(1, Months.ONE.plus(0).getMonths());
220         
221         try {
222             Months.MAX_VALUE.plus(1);
223             fail();
224         } catch (ArithmeticException JavaDoc ex) {
225             // expected
226
}
227     }
228
229     public void testPlus_Months() {
230         Months test2 = Months.months(2);
231         Months test3 = Months.months(3);
232         Months result = test2.plus(test3);
233         assertEquals(2, test2.getMonths());
234         assertEquals(3, test3.getMonths());
235         assertEquals(5, result.getMonths());
236         
237         assertEquals(1, Months.ONE.plus(Months.ZERO).getMonths());
238         assertEquals(1, Months.ONE.plus((Months) null).getMonths());
239         
240         try {
241             Months.MAX_VALUE.plus(Months.ONE);
242             fail();
243         } catch (ArithmeticException JavaDoc ex) {
244             // expected
245
}
246     }
247
248     public void testMinus_int() {
249         Months test2 = Months.months(2);
250         Months result = test2.minus(3);
251         assertEquals(2, test2.getMonths());
252         assertEquals(-1, result.getMonths());
253         
254         assertEquals(1, Months.ONE.minus(0).getMonths());
255         
256         try {
257             Months.MIN_VALUE.minus(1);
258             fail();
259         } catch (ArithmeticException JavaDoc ex) {
260             // expected
261
}
262     }
263
264     public void testMinus_Months() {
265         Months test2 = Months.months(2);
266         Months test3 = Months.months(3);
267         Months result = test2.minus(test3);
268         assertEquals(2, test2.getMonths());
269         assertEquals(3, test3.getMonths());
270         assertEquals(-1, result.getMonths());
271         
272         assertEquals(1, Months.ONE.minus(Months.ZERO).getMonths());
273         assertEquals(1, Months.ONE.minus((Months) null).getMonths());
274         
275         try {
276             Months.MIN_VALUE.minus(Months.ONE);
277             fail();
278         } catch (ArithmeticException JavaDoc ex) {
279             // expected
280
}
281     }
282
283     public void testMultipliedBy_int() {
284         Months test = Months.months(2);
285         assertEquals(6, test.multipliedBy(3).getMonths());
286         assertEquals(2, test.getMonths());
287         assertEquals(-6, test.multipliedBy(-3).getMonths());
288         assertSame(test, test.multipliedBy(1));
289         
290         Months halfMax = Months.months(Integer.MAX_VALUE / 2 + 1);
291         try {
292             halfMax.multipliedBy(2);
293             fail();
294         } catch (ArithmeticException JavaDoc ex) {
295             // expected
296
}
297     }
298
299     public void testDividedBy_int() {
300         Months test = Months.months(12);
301         assertEquals(6, test.dividedBy(2).getMonths());
302         assertEquals(12, test.getMonths());
303         assertEquals(4, test.dividedBy(3).getMonths());
304         assertEquals(3, test.dividedBy(4).getMonths());
305         assertEquals(2, test.dividedBy(5).getMonths());
306         assertEquals(2, test.dividedBy(6).getMonths());
307         assertSame(test, test.dividedBy(1));
308         
309         try {
310             Months.ONE.dividedBy(0);
311             fail();
312         } catch (ArithmeticException JavaDoc ex) {
313             // expected
314
}
315     }
316
317     public void testNegated() {
318         Months test = Months.months(12);
319         assertEquals(-12, test.negated().getMonths());
320         assertEquals(12, test.getMonths());
321         
322         try {
323             Months.MIN_VALUE.negated();
324             fail();
325         } catch (ArithmeticException JavaDoc ex) {
326             // expected
327
}
328     }
329
330     //-----------------------------------------------------------------------
331
public void testAddToLocalDate() {
332         Months test = Months.months(3);
333         LocalDate date = new LocalDate(2006, 6, 1);
334         LocalDate expected = new LocalDate(2006, 9, 1);
335         assertEquals(expected, date.plus(test));
336     }
337
338 }
339
Popular Tags