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 Years.
28   *
29   * @author Stephen Colebourne
30   */
31  public class TestYears 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(TestYears.class);
42      }
43  
44      public TestYears(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, Years.ZERO.getYears());
57          assertEquals(1, Years.ONE.getYears());
58          assertEquals(2, Years.TWO.getYears());
59          assertEquals(3, Years.THREE.getYears());
60          assertEquals(Integer.MAX_VALUE, Years.MAX_VALUE.getYears());
61          assertEquals(Integer.MIN_VALUE, Years.MIN_VALUE.getYears());
62      }
63  
64      //-----------------------------------------------------------------------
65      public void testFactory_years_int() {
66          assertSame(Years.ZERO, Years.years(0));
67          assertSame(Years.ONE, Years.years(1));
68          assertSame(Years.TWO, Years.years(2));
69          assertSame(Years.THREE, Years.years(3));
70          assertSame(Years.MAX_VALUE, Years.years(Integer.MAX_VALUE));
71          assertSame(Years.MIN_VALUE, Years.years(Integer.MIN_VALUE));
72          assertEquals(-1, Years.years(-1).getYears());
73          assertEquals(4, Years.years(4).getYears());
74      }
75  
76      //-----------------------------------------------------------------------
77      public void testFactory_yearsBetween_RInstant() {
78          DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
79          DateTime end1 = new DateTime(2009, 6, 9, 12, 0, 0, 0, PARIS);
80          DateTime end2 = new DateTime(2012, 6, 9, 12, 0, 0, 0, PARIS);
81          
82          assertEquals(3, Years.yearsBetween(start, end1).getYears());
83          assertEquals(0, Years.yearsBetween(start, start).getYears());
84          assertEquals(0, Years.yearsBetween(end1, end1).getYears());
85          assertEquals(-3, Years.yearsBetween(end1, start).getYears());
86          assertEquals(6, Years.yearsBetween(start, end2).getYears());
87      }
88  
89      public void testFactory_yearsBetween_RPartial() {
90          LocalDate start = new LocalDate(2006, 6, 9);
91          LocalDate end1 = new LocalDate(2009, 6, 9);
92          YearMonthDay end2 = new YearMonthDay(2012, 6, 9);
93          
94          assertEquals(3, Years.yearsBetween(start, end1).getYears());
95          assertEquals(0, Years.yearsBetween(start, start).getYears());
96          assertEquals(0, Years.yearsBetween(end1, end1).getYears());
97          assertEquals(-3, Years.yearsBetween(end1, start).getYears());
98          assertEquals(6, Years.yearsBetween(start, end2).getYears());
99      }
100 
101     public void testFactory_yearsIn_RInterval() {
102         DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
103         DateTime end1 = new DateTime(2009, 6, 9, 12, 0, 0, 0, PARIS);
104         DateTime end2 = new DateTime(2012, 6, 9, 12, 0, 0, 0, PARIS);
105         
106         assertEquals(0, Years.yearsIn((ReadableInterval) null).getYears());
107         assertEquals(3, Years.yearsIn(new Interval(start, end1)).getYears());
108         assertEquals(0, Years.yearsIn(new Interval(start, start)).getYears());
109         assertEquals(0, Years.yearsIn(new Interval(end1, end1)).getYears());
110         assertEquals(6, Years.yearsIn(new Interval(start, end2)).getYears());
111     }
112 
113     public void testFactory_parseYears_String() {
114         assertEquals(0, Years.parseYears((String) null).getYears());
115         assertEquals(0, Years.parseYears("P0Y").getYears());
116         assertEquals(1, Years.parseYears("P1Y").getYears());
117         assertEquals(-3, Years.parseYears("P-3Y").getYears());
118         assertEquals(2, Years.parseYears("P2Y0M").getYears());
119         assertEquals(2, Years.parseYears("P2YT0H0M").getYears());
120         try {
121             Years.parseYears("P1M1D");
122             fail();
123         } catch (IllegalArgumentException ex) {
124             // expeceted
125         }
126         try {
127             Years.parseYears("P1YT1H");
128             fail();
129         } catch (IllegalArgumentException ex) {
130             // expeceted
131         }
132     }
133 
134     //-----------------------------------------------------------------------
135     public void testGetMethods() {
136         Years test = Years.years(20);
137         assertEquals(20, test.getYears());
138     }
139 
140     public void testGetFieldType() {
141         Years test = Years.years(20);
142         assertEquals(DurationFieldType.years(), test.getFieldType());
143     }
144 
145     public void testGetPeriodType() {
146         Years test = Years.years(20);
147         assertEquals(PeriodType.years(), test.getPeriodType());
148     }
149 
150     //-----------------------------------------------------------------------
151     public void testIsGreaterThan() {
152         assertEquals(true, Years.THREE.isGreaterThan(Years.TWO));
153         assertEquals(false, Years.THREE.isGreaterThan(Years.THREE));
154         assertEquals(false, Years.TWO.isGreaterThan(Years.THREE));
155         assertEquals(true, Years.ONE.isGreaterThan(null));
156         assertEquals(false, Years.years(-1).isGreaterThan(null));
157     }
158 
159     public void testIsLessThan() {
160         assertEquals(false, Years.THREE.isLessThan(Years.TWO));
161         assertEquals(false, Years.THREE.isLessThan(Years.THREE));
162         assertEquals(true, Years.TWO.isLessThan(Years.THREE));
163         assertEquals(false, Years.ONE.isLessThan(null));
164         assertEquals(true, Years.years(-1).isLessThan(null));
165     }
166 
167     //-----------------------------------------------------------------------
168     public void testToString() {
169         Years test = Years.years(20);
170         assertEquals("P20Y", test.toString());
171         
172         test = Years.years(-20);
173         assertEquals("P-20Y", test.toString());
174     }
175 
176     //-----------------------------------------------------------------------
177     public void testSerialization() throws Exception {
178         Years test = Years.THREE;
179         
180         ByteArrayOutputStream baos = new ByteArrayOutputStream();
181         ObjectOutputStream oos = new ObjectOutputStream(baos);
182         oos.writeObject(test);
183         byte[] bytes = baos.toByteArray();
184         oos.close();
185         
186         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
187         ObjectInputStream ois = new ObjectInputStream(bais);
188         Years result = (Years) ois.readObject();
189         ois.close();
190         
191         assertSame(test, result);
192     }
193 
194     //-----------------------------------------------------------------------
195     public void testPlus_int() {
196         Years test2 = Years.years(2);
197         Years result = test2.plus(3);
198         assertEquals(2, test2.getYears());
199         assertEquals(5, result.getYears());
200         
201         assertEquals(1, Years.ONE.plus(0).getYears());
202         
203         try {
204             Years.MAX_VALUE.plus(1);
205             fail();
206         } catch (ArithmeticException ex) {
207             // expected
208         }
209     }
210 
211     public void testPlus_Years() {
212         Years test2 = Years.years(2);
213         Years test3 = Years.years(3);
214         Years result = test2.plus(test3);
215         assertEquals(2, test2.getYears());
216         assertEquals(3, test3.getYears());
217         assertEquals(5, result.getYears());
218         
219         assertEquals(1, Years.ONE.plus(Years.ZERO).getYears());
220         assertEquals(1, Years.ONE.plus((Years) null).getYears());
221         
222         try {
223             Years.MAX_VALUE.plus(Years.ONE);
224             fail();
225         } catch (ArithmeticException ex) {
226             // expected
227         }
228     }
229 
230     public void testMinus_int() {
231         Years test2 = Years.years(2);
232         Years result = test2.minus(3);
233         assertEquals(2, test2.getYears());
234         assertEquals(-1, result.getYears());
235         
236         assertEquals(1, Years.ONE.minus(0).getYears());
237         
238         try {
239             Years.MIN_VALUE.minus(1);
240             fail();
241         } catch (ArithmeticException ex) {
242             // expected
243         }
244     }
245 
246     public void testMinus_Years() {
247         Years test2 = Years.years(2);
248         Years test3 = Years.years(3);
249         Years result = test2.minus(test3);
250         assertEquals(2, test2.getYears());
251         assertEquals(3, test3.getYears());
252         assertEquals(-1, result.getYears());
253         
254         assertEquals(1, Years.ONE.minus(Years.ZERO).getYears());
255         assertEquals(1, Years.ONE.minus((Years) null).getYears());
256         
257         try {
258             Years.MIN_VALUE.minus(Years.ONE);
259             fail();
260         } catch (ArithmeticException ex) {
261             // expected
262         }
263     }
264 
265     public void testMultipliedBy_int() {
266         Years test = Years.years(2);
267         assertEquals(6, test.multipliedBy(3).getYears());
268         assertEquals(2, test.getYears());
269         assertEquals(-6, test.multipliedBy(-3).getYears());
270         assertSame(test, test.multipliedBy(1));
271         
272         Years halfMax = Years.years(Integer.MAX_VALUE / 2 + 1);
273         try {
274             halfMax.multipliedBy(2);
275             fail();
276         } catch (ArithmeticException ex) {
277             // expected
278         }
279     }
280 
281     public void testDividedBy_int() {
282         Years test = Years.years(12);
283         assertEquals(6, test.dividedBy(2).getYears());
284         assertEquals(12, test.getYears());
285         assertEquals(4, test.dividedBy(3).getYears());
286         assertEquals(3, test.dividedBy(4).getYears());
287         assertEquals(2, test.dividedBy(5).getYears());
288         assertEquals(2, test.dividedBy(6).getYears());
289         assertSame(test, test.dividedBy(1));
290         
291         try {
292             Years.ONE.dividedBy(0);
293             fail();
294         } catch (ArithmeticException ex) {
295             // expected
296         }
297     }
298 
299     public void testNegated() {
300         Years test = Years.years(12);
301         assertEquals(-12, test.negated().getYears());
302         assertEquals(12, test.getYears());
303         
304         try {
305             Years.MIN_VALUE.negated();
306             fail();
307         } catch (ArithmeticException ex) {
308             // expected
309         }
310     }
311 
312     //-----------------------------------------------------------------------
313     public void testAddToLocalDate() {
314         Years test = Years.years(3);
315         LocalDate date = new LocalDate(2006, 6, 1);
316         LocalDate expected = new LocalDate(2009, 6, 1);
317         assertEquals(expected, date.plus(test));
318     }
319 
320 }