001    /*
002     *  Copyright 2001-2006 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.time;
017    
018    import java.io.ByteArrayInputStream;
019    import java.io.ByteArrayOutputStream;
020    import java.io.ObjectInputStream;
021    import java.io.ObjectOutputStream;
022    
023    import junit.framework.TestCase;
024    import junit.framework.TestSuite;
025    
026    /**
027     * This class is a Junit unit test for Years.
028     *
029     * @author Stephen Colebourne
030     */
031    public class TestYears extends TestCase {
032        // Test in 2002/03 as time zones are more well known
033        // (before the late 90's they were all over the place)
034        private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
035    
036        public static void main(String[] args) {
037            junit.textui.TestRunner.run(suite());
038        }
039    
040        public static TestSuite suite() {
041            return new TestSuite(TestYears.class);
042        }
043    
044        public TestYears(String name) {
045            super(name);
046        }
047    
048        protected void setUp() throws Exception {
049        }
050    
051        protected void tearDown() throws Exception {
052        }
053    
054        //-----------------------------------------------------------------------
055        public void testConstants() {
056            assertEquals(0, Years.ZERO.getYears());
057            assertEquals(1, Years.ONE.getYears());
058            assertEquals(2, Years.TWO.getYears());
059            assertEquals(3, Years.THREE.getYears());
060            assertEquals(Integer.MAX_VALUE, Years.MAX_VALUE.getYears());
061            assertEquals(Integer.MIN_VALUE, Years.MIN_VALUE.getYears());
062        }
063    
064        //-----------------------------------------------------------------------
065        public void testFactory_years_int() {
066            assertSame(Years.ZERO, Years.years(0));
067            assertSame(Years.ONE, Years.years(1));
068            assertSame(Years.TWO, Years.years(2));
069            assertSame(Years.THREE, Years.years(3));
070            assertSame(Years.MAX_VALUE, Years.years(Integer.MAX_VALUE));
071            assertSame(Years.MIN_VALUE, Years.years(Integer.MIN_VALUE));
072            assertEquals(-1, Years.years(-1).getYears());
073            assertEquals(4, Years.years(4).getYears());
074        }
075    
076        //-----------------------------------------------------------------------
077        public void testFactory_yearsBetween_RInstant() {
078            DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
079            DateTime end1 = new DateTime(2009, 6, 9, 12, 0, 0, 0, PARIS);
080            DateTime end2 = new DateTime(2012, 6, 9, 12, 0, 0, 0, PARIS);
081            
082            assertEquals(3, Years.yearsBetween(start, end1).getYears());
083            assertEquals(0, Years.yearsBetween(start, start).getYears());
084            assertEquals(0, Years.yearsBetween(end1, end1).getYears());
085            assertEquals(-3, Years.yearsBetween(end1, start).getYears());
086            assertEquals(6, Years.yearsBetween(start, end2).getYears());
087        }
088    
089        public void testFactory_yearsBetween_RPartial() {
090            LocalDate start = new LocalDate(2006, 6, 9);
091            LocalDate end1 = new LocalDate(2009, 6, 9);
092            YearMonthDay end2 = new YearMonthDay(2012, 6, 9);
093            
094            assertEquals(3, Years.yearsBetween(start, end1).getYears());
095            assertEquals(0, Years.yearsBetween(start, start).getYears());
096            assertEquals(0, Years.yearsBetween(end1, end1).getYears());
097            assertEquals(-3, Years.yearsBetween(end1, start).getYears());
098            assertEquals(6, Years.yearsBetween(start, end2).getYears());
099        }
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    }