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 Months.
028: *
029: * @author Stephen Colebourne
030: */
031: public class TestMonths 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(TestMonths.class);
043: }
044:
045: public TestMonths(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, Months.ZERO.getMonths());
058: assertEquals(1, Months.ONE.getMonths());
059: assertEquals(2, Months.TWO.getMonths());
060: assertEquals(3, Months.THREE.getMonths());
061: assertEquals(4, Months.FOUR.getMonths());
062: assertEquals(5, Months.FIVE.getMonths());
063: assertEquals(6, Months.SIX.getMonths());
064: assertEquals(7, Months.SEVEN.getMonths());
065: assertEquals(8, Months.EIGHT.getMonths());
066: assertEquals(9, Months.NINE.getMonths());
067: assertEquals(10, Months.TEN.getMonths());
068: assertEquals(11, Months.ELEVEN.getMonths());
069: assertEquals(12, Months.TWELVE.getMonths());
070: assertEquals(Integer.MAX_VALUE, Months.MAX_VALUE.getMonths());
071: assertEquals(Integer.MIN_VALUE, Months.MIN_VALUE.getMonths());
072: }
073:
074: //-----------------------------------------------------------------------
075: public void testFactory_months_int() {
076: assertSame(Months.ZERO, Months.months(0));
077: assertSame(Months.ONE, Months.months(1));
078: assertSame(Months.TWO, Months.months(2));
079: assertSame(Months.THREE, Months.months(3));
080: assertSame(Months.FOUR, Months.months(4));
081: assertSame(Months.FIVE, Months.months(5));
082: assertSame(Months.SIX, Months.months(6));
083: assertSame(Months.SEVEN, Months.months(7));
084: assertSame(Months.EIGHT, Months.months(8));
085: assertSame(Months.NINE, Months.months(9));
086: assertSame(Months.TEN, Months.months(10));
087: assertSame(Months.ELEVEN, Months.months(11));
088: assertSame(Months.TWELVE, Months.months(12));
089: assertSame(Months.MAX_VALUE, Months.months(Integer.MAX_VALUE));
090: assertSame(Months.MIN_VALUE, Months.months(Integer.MIN_VALUE));
091: assertEquals(-1, Months.months(-1).getMonths());
092: assertEquals(13, Months.months(13).getMonths());
093: }
094:
095: //-----------------------------------------------------------------------
096: public void testFactory_monthsBetween_RInstant() {
097: DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
098: DateTime end1 = new DateTime(2006, 9, 9, 12, 0, 0, 0, PARIS);
099: DateTime end2 = new DateTime(2006, 12, 9, 12, 0, 0, 0, PARIS);
100:
101: assertEquals(3, Months.monthsBetween(start, end1).getMonths());
102: assertEquals(0, Months.monthsBetween(start, start).getMonths());
103: assertEquals(0, Months.monthsBetween(end1, end1).getMonths());
104: assertEquals(-3, Months.monthsBetween(end1, start).getMonths());
105: assertEquals(6, Months.monthsBetween(start, end2).getMonths());
106: }
107:
108: public void testFactory_monthsBetween_RPartial() {
109: LocalDate start = new LocalDate(2006, 6, 9);
110: LocalDate end1 = new LocalDate(2006, 9, 9);
111: YearMonthDay end2 = new YearMonthDay(2006, 12, 9);
112:
113: assertEquals(3, Months.monthsBetween(start, end1).getMonths());
114: assertEquals(0, Months.monthsBetween(start, start).getMonths());
115: assertEquals(0, Months.monthsBetween(end1, end1).getMonths());
116: assertEquals(-3, Months.monthsBetween(end1, start).getMonths());
117: assertEquals(6, Months.monthsBetween(start, end2).getMonths());
118: }
119:
120: public void testFactory_monthsIn_RInterval() {
121: DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
122: DateTime end1 = new DateTime(2006, 9, 9, 12, 0, 0, 0, PARIS);
123: DateTime end2 = new DateTime(2006, 12, 9, 12, 0, 0, 0, PARIS);
124:
125: assertEquals(0, Months.monthsIn((ReadableInterval) null)
126: .getMonths());
127: assertEquals(3, Months.monthsIn(new Interval(start, end1))
128: .getMonths());
129: assertEquals(0, Months.monthsIn(new Interval(start, start))
130: .getMonths());
131: assertEquals(0, Months.monthsIn(new Interval(end1, end1))
132: .getMonths());
133: assertEquals(6, Months.monthsIn(new Interval(start, end2))
134: .getMonths());
135: }
136:
137: public void testFactory_parseMonths_String() {
138: assertEquals(0, Months.parseMonths((String) null).getMonths());
139: assertEquals(0, Months.parseMonths("P0M").getMonths());
140: assertEquals(1, Months.parseMonths("P1M").getMonths());
141: assertEquals(-3, Months.parseMonths("P-3M").getMonths());
142: assertEquals(2, Months.parseMonths("P0Y2M").getMonths());
143: assertEquals(2, Months.parseMonths("P2MT0H0M").getMonths());
144: try {
145: Months.parseMonths("P1Y1D");
146: fail();
147: } catch (IllegalArgumentException ex) {
148: // expeceted
149: }
150: try {
151: Months.parseMonths("P1MT1H");
152: fail();
153: } catch (IllegalArgumentException ex) {
154: // expeceted
155: }
156: }
157:
158: //-----------------------------------------------------------------------
159: public void testGetMethods() {
160: Months test = Months.months(20);
161: assertEquals(20, test.getMonths());
162: }
163:
164: public void testGetFieldType() {
165: Months test = Months.months(20);
166: assertEquals(DurationFieldType.months(), test.getFieldType());
167: }
168:
169: public void testGetPeriodType() {
170: Months test = Months.months(20);
171: assertEquals(PeriodType.months(), test.getPeriodType());
172: }
173:
174: //-----------------------------------------------------------------------
175: public void testIsGreaterThan() {
176: assertEquals(true, Months.THREE.isGreaterThan(Months.TWO));
177: assertEquals(false, Months.THREE.isGreaterThan(Months.THREE));
178: assertEquals(false, Months.TWO.isGreaterThan(Months.THREE));
179: assertEquals(true, Months.ONE.isGreaterThan(null));
180: assertEquals(false, Months.months(-1).isGreaterThan(null));
181: }
182:
183: public void testIsLessThan() {
184: assertEquals(false, Months.THREE.isLessThan(Months.TWO));
185: assertEquals(false, Months.THREE.isLessThan(Months.THREE));
186: assertEquals(true, Months.TWO.isLessThan(Months.THREE));
187: assertEquals(false, Months.ONE.isLessThan(null));
188: assertEquals(true, Months.months(-1).isLessThan(null));
189: }
190:
191: //-----------------------------------------------------------------------
192: public void testToString() {
193: Months test = Months.months(20);
194: assertEquals("P20M", test.toString());
195:
196: test = Months.months(-20);
197: assertEquals("P-20M", test.toString());
198: }
199:
200: //-----------------------------------------------------------------------
201: public void testSerialization() throws Exception {
202: Months test = Months.THREE;
203:
204: ByteArrayOutputStream baos = new ByteArrayOutputStream();
205: ObjectOutputStream oos = new ObjectOutputStream(baos);
206: oos.writeObject(test);
207: byte[] bytes = baos.toByteArray();
208: oos.close();
209:
210: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
211: ObjectInputStream ois = new ObjectInputStream(bais);
212: Months result = (Months) ois.readObject();
213: ois.close();
214:
215: assertSame(test, result);
216: }
217:
218: //-----------------------------------------------------------------------
219: public void testPlus_int() {
220: Months test2 = Months.months(2);
221: Months result = test2.plus(3);
222: assertEquals(2, test2.getMonths());
223: assertEquals(5, result.getMonths());
224:
225: assertEquals(1, Months.ONE.plus(0).getMonths());
226:
227: try {
228: Months.MAX_VALUE.plus(1);
229: fail();
230: } catch (ArithmeticException ex) {
231: // expected
232: }
233: }
234:
235: public void testPlus_Months() {
236: Months test2 = Months.months(2);
237: Months test3 = Months.months(3);
238: Months result = test2.plus(test3);
239: assertEquals(2, test2.getMonths());
240: assertEquals(3, test3.getMonths());
241: assertEquals(5, result.getMonths());
242:
243: assertEquals(1, Months.ONE.plus(Months.ZERO).getMonths());
244: assertEquals(1, Months.ONE.plus((Months) null).getMonths());
245:
246: try {
247: Months.MAX_VALUE.plus(Months.ONE);
248: fail();
249: } catch (ArithmeticException ex) {
250: // expected
251: }
252: }
253:
254: public void testMinus_int() {
255: Months test2 = Months.months(2);
256: Months result = test2.minus(3);
257: assertEquals(2, test2.getMonths());
258: assertEquals(-1, result.getMonths());
259:
260: assertEquals(1, Months.ONE.minus(0).getMonths());
261:
262: try {
263: Months.MIN_VALUE.minus(1);
264: fail();
265: } catch (ArithmeticException ex) {
266: // expected
267: }
268: }
269:
270: public void testMinus_Months() {
271: Months test2 = Months.months(2);
272: Months test3 = Months.months(3);
273: Months result = test2.minus(test3);
274: assertEquals(2, test2.getMonths());
275: assertEquals(3, test3.getMonths());
276: assertEquals(-1, result.getMonths());
277:
278: assertEquals(1, Months.ONE.minus(Months.ZERO).getMonths());
279: assertEquals(1, Months.ONE.minus((Months) null).getMonths());
280:
281: try {
282: Months.MIN_VALUE.minus(Months.ONE);
283: fail();
284: } catch (ArithmeticException ex) {
285: // expected
286: }
287: }
288:
289: public void testMultipliedBy_int() {
290: Months test = Months.months(2);
291: assertEquals(6, test.multipliedBy(3).getMonths());
292: assertEquals(2, test.getMonths());
293: assertEquals(-6, test.multipliedBy(-3).getMonths());
294: assertSame(test, test.multipliedBy(1));
295:
296: Months halfMax = Months.months(Integer.MAX_VALUE / 2 + 1);
297: try {
298: halfMax.multipliedBy(2);
299: fail();
300: } catch (ArithmeticException ex) {
301: // expected
302: }
303: }
304:
305: public void testDividedBy_int() {
306: Months test = Months.months(12);
307: assertEquals(6, test.dividedBy(2).getMonths());
308: assertEquals(12, test.getMonths());
309: assertEquals(4, test.dividedBy(3).getMonths());
310: assertEquals(3, test.dividedBy(4).getMonths());
311: assertEquals(2, test.dividedBy(5).getMonths());
312: assertEquals(2, test.dividedBy(6).getMonths());
313: assertSame(test, test.dividedBy(1));
314:
315: try {
316: Months.ONE.dividedBy(0);
317: fail();
318: } catch (ArithmeticException ex) {
319: // expected
320: }
321: }
322:
323: public void testNegated() {
324: Months test = Months.months(12);
325: assertEquals(-12, test.negated().getMonths());
326: assertEquals(12, test.getMonths());
327:
328: try {
329: Months.MIN_VALUE.negated();
330: fail();
331: } catch (ArithmeticException ex) {
332: // expected
333: }
334: }
335:
336: //-----------------------------------------------------------------------
337: public void testAddToLocalDate() {
338: Months test = Months.months(3);
339: LocalDate date = new LocalDate(2006, 6, 1);
340: LocalDate expected = new LocalDate(2006, 9, 1);
341: assertEquals(expected, date.plus(test));
342: }
343:
344: }
|