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
035: .forID("Europe/Paris");
036:
037: public static void main(String[] args) {
038: junit.textui.TestRunner.run(suite());
039: }
040:
041: public static TestSuite suite() {
042: return new TestSuite(TestYears.class);
043: }
044:
045: public TestYears(String name) {
046: super (name);
047: }
048:
049: protected void setUp() throws Exception {
050: }
051:
052: protected void tearDown() throws Exception {
053: }
054:
055: //-----------------------------------------------------------------------
056: public void testConstants() {
057: assertEquals(0, Years.ZERO.getYears());
058: assertEquals(1, Years.ONE.getYears());
059: assertEquals(2, Years.TWO.getYears());
060: assertEquals(3, Years.THREE.getYears());
061: assertEquals(Integer.MAX_VALUE, Years.MAX_VALUE.getYears());
062: assertEquals(Integer.MIN_VALUE, Years.MIN_VALUE.getYears());
063: }
064:
065: //-----------------------------------------------------------------------
066: public void testFactory_years_int() {
067: assertSame(Years.ZERO, Years.years(0));
068: assertSame(Years.ONE, Years.years(1));
069: assertSame(Years.TWO, Years.years(2));
070: assertSame(Years.THREE, Years.years(3));
071: assertSame(Years.MAX_VALUE, Years.years(Integer.MAX_VALUE));
072: assertSame(Years.MIN_VALUE, Years.years(Integer.MIN_VALUE));
073: assertEquals(-1, Years.years(-1).getYears());
074: assertEquals(4, Years.years(4).getYears());
075: }
076:
077: //-----------------------------------------------------------------------
078: public void testFactory_yearsBetween_RInstant() {
079: DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
080: DateTime end1 = new DateTime(2009, 6, 9, 12, 0, 0, 0, PARIS);
081: DateTime end2 = new DateTime(2012, 6, 9, 12, 0, 0, 0, PARIS);
082:
083: assertEquals(3, Years.yearsBetween(start, end1).getYears());
084: assertEquals(0, Years.yearsBetween(start, start).getYears());
085: assertEquals(0, Years.yearsBetween(end1, end1).getYears());
086: assertEquals(-3, Years.yearsBetween(end1, start).getYears());
087: assertEquals(6, Years.yearsBetween(start, end2).getYears());
088: }
089:
090: public void testFactory_yearsBetween_RPartial() {
091: LocalDate start = new LocalDate(2006, 6, 9);
092: LocalDate end1 = new LocalDate(2009, 6, 9);
093: YearMonthDay end2 = new YearMonthDay(2012, 6, 9);
094:
095: assertEquals(3, Years.yearsBetween(start, end1).getYears());
096: assertEquals(0, Years.yearsBetween(start, start).getYears());
097: assertEquals(0, Years.yearsBetween(end1, end1).getYears());
098: assertEquals(-3, Years.yearsBetween(end1, start).getYears());
099: assertEquals(6, Years.yearsBetween(start, end2).getYears());
100: }
101:
102: public void testFactory_yearsIn_RInterval() {
103: DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
104: DateTime end1 = new DateTime(2009, 6, 9, 12, 0, 0, 0, PARIS);
105: DateTime end2 = new DateTime(2012, 6, 9, 12, 0, 0, 0, PARIS);
106:
107: assertEquals(0, Years.yearsIn((ReadableInterval) null)
108: .getYears());
109: assertEquals(3, Years.yearsIn(new Interval(start, end1))
110: .getYears());
111: assertEquals(0, Years.yearsIn(new Interval(start, start))
112: .getYears());
113: assertEquals(0, Years.yearsIn(new Interval(end1, end1))
114: .getYears());
115: assertEquals(6, Years.yearsIn(new Interval(start, end2))
116: .getYears());
117: }
118:
119: public void testFactory_parseYears_String() {
120: assertEquals(0, Years.parseYears((String) null).getYears());
121: assertEquals(0, Years.parseYears("P0Y").getYears());
122: assertEquals(1, Years.parseYears("P1Y").getYears());
123: assertEquals(-3, Years.parseYears("P-3Y").getYears());
124: assertEquals(2, Years.parseYears("P2Y0M").getYears());
125: assertEquals(2, Years.parseYears("P2YT0H0M").getYears());
126: try {
127: Years.parseYears("P1M1D");
128: fail();
129: } catch (IllegalArgumentException ex) {
130: // expeceted
131: }
132: try {
133: Years.parseYears("P1YT1H");
134: fail();
135: } catch (IllegalArgumentException ex) {
136: // expeceted
137: }
138: }
139:
140: //-----------------------------------------------------------------------
141: public void testGetMethods() {
142: Years test = Years.years(20);
143: assertEquals(20, test.getYears());
144: }
145:
146: public void testGetFieldType() {
147: Years test = Years.years(20);
148: assertEquals(DurationFieldType.years(), test.getFieldType());
149: }
150:
151: public void testGetPeriodType() {
152: Years test = Years.years(20);
153: assertEquals(PeriodType.years(), test.getPeriodType());
154: }
155:
156: //-----------------------------------------------------------------------
157: public void testIsGreaterThan() {
158: assertEquals(true, Years.THREE.isGreaterThan(Years.TWO));
159: assertEquals(false, Years.THREE.isGreaterThan(Years.THREE));
160: assertEquals(false, Years.TWO.isGreaterThan(Years.THREE));
161: assertEquals(true, Years.ONE.isGreaterThan(null));
162: assertEquals(false, Years.years(-1).isGreaterThan(null));
163: }
164:
165: public void testIsLessThan() {
166: assertEquals(false, Years.THREE.isLessThan(Years.TWO));
167: assertEquals(false, Years.THREE.isLessThan(Years.THREE));
168: assertEquals(true, Years.TWO.isLessThan(Years.THREE));
169: assertEquals(false, Years.ONE.isLessThan(null));
170: assertEquals(true, Years.years(-1).isLessThan(null));
171: }
172:
173: //-----------------------------------------------------------------------
174: public void testToString() {
175: Years test = Years.years(20);
176: assertEquals("P20Y", test.toString());
177:
178: test = Years.years(-20);
179: assertEquals("P-20Y", test.toString());
180: }
181:
182: //-----------------------------------------------------------------------
183: public void testSerialization() throws Exception {
184: Years test = Years.THREE;
185:
186: ByteArrayOutputStream baos = new ByteArrayOutputStream();
187: ObjectOutputStream oos = new ObjectOutputStream(baos);
188: oos.writeObject(test);
189: byte[] bytes = baos.toByteArray();
190: oos.close();
191:
192: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
193: ObjectInputStream ois = new ObjectInputStream(bais);
194: Years result = (Years) ois.readObject();
195: ois.close();
196:
197: assertSame(test, result);
198: }
199:
200: //-----------------------------------------------------------------------
201: public void testPlus_int() {
202: Years test2 = Years.years(2);
203: Years result = test2.plus(3);
204: assertEquals(2, test2.getYears());
205: assertEquals(5, result.getYears());
206:
207: assertEquals(1, Years.ONE.plus(0).getYears());
208:
209: try {
210: Years.MAX_VALUE.plus(1);
211: fail();
212: } catch (ArithmeticException ex) {
213: // expected
214: }
215: }
216:
217: public void testPlus_Years() {
218: Years test2 = Years.years(2);
219: Years test3 = Years.years(3);
220: Years result = test2.plus(test3);
221: assertEquals(2, test2.getYears());
222: assertEquals(3, test3.getYears());
223: assertEquals(5, result.getYears());
224:
225: assertEquals(1, Years.ONE.plus(Years.ZERO).getYears());
226: assertEquals(1, Years.ONE.plus((Years) null).getYears());
227:
228: try {
229: Years.MAX_VALUE.plus(Years.ONE);
230: fail();
231: } catch (ArithmeticException ex) {
232: // expected
233: }
234: }
235:
236: public void testMinus_int() {
237: Years test2 = Years.years(2);
238: Years result = test2.minus(3);
239: assertEquals(2, test2.getYears());
240: assertEquals(-1, result.getYears());
241:
242: assertEquals(1, Years.ONE.minus(0).getYears());
243:
244: try {
245: Years.MIN_VALUE.minus(1);
246: fail();
247: } catch (ArithmeticException ex) {
248: // expected
249: }
250: }
251:
252: public void testMinus_Years() {
253: Years test2 = Years.years(2);
254: Years test3 = Years.years(3);
255: Years result = test2.minus(test3);
256: assertEquals(2, test2.getYears());
257: assertEquals(3, test3.getYears());
258: assertEquals(-1, result.getYears());
259:
260: assertEquals(1, Years.ONE.minus(Years.ZERO).getYears());
261: assertEquals(1, Years.ONE.minus((Years) null).getYears());
262:
263: try {
264: Years.MIN_VALUE.minus(Years.ONE);
265: fail();
266: } catch (ArithmeticException ex) {
267: // expected
268: }
269: }
270:
271: public void testMultipliedBy_int() {
272: Years test = Years.years(2);
273: assertEquals(6, test.multipliedBy(3).getYears());
274: assertEquals(2, test.getYears());
275: assertEquals(-6, test.multipliedBy(-3).getYears());
276: assertSame(test, test.multipliedBy(1));
277:
278: Years halfMax = Years.years(Integer.MAX_VALUE / 2 + 1);
279: try {
280: halfMax.multipliedBy(2);
281: fail();
282: } catch (ArithmeticException ex) {
283: // expected
284: }
285: }
286:
287: public void testDividedBy_int() {
288: Years test = Years.years(12);
289: assertEquals(6, test.dividedBy(2).getYears());
290: assertEquals(12, test.getYears());
291: assertEquals(4, test.dividedBy(3).getYears());
292: assertEquals(3, test.dividedBy(4).getYears());
293: assertEquals(2, test.dividedBy(5).getYears());
294: assertEquals(2, test.dividedBy(6).getYears());
295: assertSame(test, test.dividedBy(1));
296:
297: try {
298: Years.ONE.dividedBy(0);
299: fail();
300: } catch (ArithmeticException ex) {
301: // expected
302: }
303: }
304:
305: public void testNegated() {
306: Years test = Years.years(12);
307: assertEquals(-12, test.negated().getYears());
308: assertEquals(12, test.getYears());
309:
310: try {
311: Years.MIN_VALUE.negated();
312: fail();
313: } catch (ArithmeticException ex) {
314: // expected
315: }
316: }
317:
318: //-----------------------------------------------------------------------
319: public void testAddToLocalDate() {
320: Years test = Years.years(3);
321: LocalDate date = new LocalDate(2006, 6, 1);
322: LocalDate expected = new LocalDate(2009, 6, 1);
323: assertEquals(expected, date.plus(test));
324: }
325:
326: }
|