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 Weeks.
028: *
029: * @author Stephen Colebourne
030: */
031: public class TestWeeks 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(TestWeeks.class);
043: }
044:
045: public TestWeeks(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, Weeks.ZERO.getWeeks());
058: assertEquals(1, Weeks.ONE.getWeeks());
059: assertEquals(2, Weeks.TWO.getWeeks());
060: assertEquals(3, Weeks.THREE.getWeeks());
061: assertEquals(Integer.MAX_VALUE, Weeks.MAX_VALUE.getWeeks());
062: assertEquals(Integer.MIN_VALUE, Weeks.MIN_VALUE.getWeeks());
063: }
064:
065: //-----------------------------------------------------------------------
066: public void testFactory_weeks_int() {
067: assertSame(Weeks.ZERO, Weeks.weeks(0));
068: assertSame(Weeks.ONE, Weeks.weeks(1));
069: assertSame(Weeks.TWO, Weeks.weeks(2));
070: assertSame(Weeks.THREE, Weeks.weeks(3));
071: assertSame(Weeks.MAX_VALUE, Weeks.weeks(Integer.MAX_VALUE));
072: assertSame(Weeks.MIN_VALUE, Weeks.weeks(Integer.MIN_VALUE));
073: assertEquals(-1, Weeks.weeks(-1).getWeeks());
074: assertEquals(4, Weeks.weeks(4).getWeeks());
075: }
076:
077: //-----------------------------------------------------------------------
078: public void testFactory_weeksBetween_RInstant() {
079: DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
080: DateTime end1 = new DateTime(2006, 6, 30, 12, 0, 0, 0, PARIS);
081: DateTime end2 = new DateTime(2006, 7, 21, 12, 0, 0, 0, PARIS);
082:
083: assertEquals(3, Weeks.weeksBetween(start, end1).getWeeks());
084: assertEquals(0, Weeks.weeksBetween(start, start).getWeeks());
085: assertEquals(0, Weeks.weeksBetween(end1, end1).getWeeks());
086: assertEquals(-3, Weeks.weeksBetween(end1, start).getWeeks());
087: assertEquals(6, Weeks.weeksBetween(start, end2).getWeeks());
088: }
089:
090: public void testFactory_weeksBetween_RPartial() {
091: LocalDate start = new LocalDate(2006, 6, 9);
092: LocalDate end1 = new LocalDate(2006, 6, 30);
093: YearMonthDay end2 = new YearMonthDay(2006, 7, 21);
094:
095: assertEquals(3, Weeks.weeksBetween(start, end1).getWeeks());
096: assertEquals(0, Weeks.weeksBetween(start, start).getWeeks());
097: assertEquals(0, Weeks.weeksBetween(end1, end1).getWeeks());
098: assertEquals(-3, Weeks.weeksBetween(end1, start).getWeeks());
099: assertEquals(6, Weeks.weeksBetween(start, end2).getWeeks());
100: }
101:
102: public void testFactory_weeksIn_RInterval() {
103: DateTime start = new DateTime(2006, 6, 9, 12, 0, 0, 0, PARIS);
104: DateTime end1 = new DateTime(2006, 6, 30, 12, 0, 0, 0, PARIS);
105: DateTime end2 = new DateTime(2006, 7, 21, 12, 0, 0, 0, PARIS);
106:
107: assertEquals(0, Weeks.weeksIn((ReadableInterval) null)
108: .getWeeks());
109: assertEquals(3, Weeks.weeksIn(new Interval(start, end1))
110: .getWeeks());
111: assertEquals(0, Weeks.weeksIn(new Interval(start, start))
112: .getWeeks());
113: assertEquals(0, Weeks.weeksIn(new Interval(end1, end1))
114: .getWeeks());
115: assertEquals(6, Weeks.weeksIn(new Interval(start, end2))
116: .getWeeks());
117: }
118:
119: public void testFactory_standardWeeksIn_RPeriod() {
120: assertEquals(0, Weeks.standardWeeksIn((ReadablePeriod) null)
121: .getWeeks());
122: assertEquals(0, Weeks.standardWeeksIn(Period.ZERO).getWeeks());
123: assertEquals(1, Weeks.standardWeeksIn(
124: new Period(0, 0, 1, 0, 0, 0, 0, 0)).getWeeks());
125: assertEquals(123, Weeks.standardWeeksIn(Period.weeks(123))
126: .getWeeks());
127: assertEquals(-987, Weeks.standardWeeksIn(Period.weeks(-987))
128: .getWeeks());
129: assertEquals(1, Weeks.standardWeeksIn(Period.days(13))
130: .getWeeks());
131: assertEquals(2, Weeks.standardWeeksIn(Period.days(14))
132: .getWeeks());
133: assertEquals(2, Weeks.standardWeeksIn(Period.days(15))
134: .getWeeks());
135: try {
136: Weeks.standardWeeksIn(Period.months(1));
137: fail();
138: } catch (IllegalArgumentException ex) {
139: // expeceted
140: }
141: }
142:
143: public void testFactory_parseWeeks_String() {
144: assertEquals(0, Weeks.parseWeeks((String) null).getWeeks());
145: assertEquals(0, Weeks.parseWeeks("P0W").getWeeks());
146: assertEquals(1, Weeks.parseWeeks("P1W").getWeeks());
147: assertEquals(-3, Weeks.parseWeeks("P-3W").getWeeks());
148: assertEquals(2, Weeks.parseWeeks("P0Y0M2W").getWeeks());
149: assertEquals(2, Weeks.parseWeeks("P2WT0H0M").getWeeks());
150: try {
151: Weeks.parseWeeks("P1Y1D");
152: fail();
153: } catch (IllegalArgumentException ex) {
154: // expeceted
155: }
156: try {
157: Weeks.parseWeeks("P1WT1H");
158: fail();
159: } catch (IllegalArgumentException ex) {
160: // expeceted
161: }
162: }
163:
164: //-----------------------------------------------------------------------
165: public void testGetMethods() {
166: Weeks test = Weeks.weeks(20);
167: assertEquals(20, test.getWeeks());
168: }
169:
170: public void testGetFieldType() {
171: Weeks test = Weeks.weeks(20);
172: assertEquals(DurationFieldType.weeks(), test.getFieldType());
173: }
174:
175: public void testGetPeriodType() {
176: Weeks test = Weeks.weeks(20);
177: assertEquals(PeriodType.weeks(), test.getPeriodType());
178: }
179:
180: //-----------------------------------------------------------------------
181: public void testIsGreaterThan() {
182: assertEquals(true, Weeks.THREE.isGreaterThan(Weeks.TWO));
183: assertEquals(false, Weeks.THREE.isGreaterThan(Weeks.THREE));
184: assertEquals(false, Weeks.TWO.isGreaterThan(Weeks.THREE));
185: assertEquals(true, Weeks.ONE.isGreaterThan(null));
186: assertEquals(false, Weeks.weeks(-1).isGreaterThan(null));
187: }
188:
189: public void testIsLessThan() {
190: assertEquals(false, Weeks.THREE.isLessThan(Weeks.TWO));
191: assertEquals(false, Weeks.THREE.isLessThan(Weeks.THREE));
192: assertEquals(true, Weeks.TWO.isLessThan(Weeks.THREE));
193: assertEquals(false, Weeks.ONE.isLessThan(null));
194: assertEquals(true, Weeks.weeks(-1).isLessThan(null));
195: }
196:
197: //-----------------------------------------------------------------------
198: public void testToString() {
199: Weeks test = Weeks.weeks(20);
200: assertEquals("P20W", test.toString());
201:
202: test = Weeks.weeks(-20);
203: assertEquals("P-20W", test.toString());
204: }
205:
206: //-----------------------------------------------------------------------
207: public void testSerialization() throws Exception {
208: Weeks test = Weeks.THREE;
209:
210: ByteArrayOutputStream baos = new ByteArrayOutputStream();
211: ObjectOutputStream oos = new ObjectOutputStream(baos);
212: oos.writeObject(test);
213: byte[] bytes = baos.toByteArray();
214: oos.close();
215:
216: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
217: ObjectInputStream ois = new ObjectInputStream(bais);
218: Weeks result = (Weeks) ois.readObject();
219: ois.close();
220:
221: assertSame(test, result);
222: }
223:
224: //-----------------------------------------------------------------------
225: public void testToStandardDays() {
226: Weeks test = Weeks.weeks(2);
227: Days expected = Days.days(14);
228: assertEquals(expected, test.toStandardDays());
229:
230: try {
231: Weeks.MAX_VALUE.toStandardDays();
232: fail();
233: } catch (ArithmeticException ex) {
234: // expected
235: }
236: }
237:
238: public void testToStandardHours() {
239: Weeks test = Weeks.weeks(2);
240: Hours expected = Hours.hours(2 * 7 * 24);
241: assertEquals(expected, test.toStandardHours());
242:
243: try {
244: Weeks.MAX_VALUE.toStandardHours();
245: fail();
246: } catch (ArithmeticException ex) {
247: // expected
248: }
249: }
250:
251: public void testToStandardMinutes() {
252: Weeks test = Weeks.weeks(2);
253: Minutes expected = Minutes.minutes(2 * 7 * 24 * 60);
254: assertEquals(expected, test.toStandardMinutes());
255:
256: try {
257: Weeks.MAX_VALUE.toStandardMinutes();
258: fail();
259: } catch (ArithmeticException ex) {
260: // expected
261: }
262: }
263:
264: public void testToStandardSeconds() {
265: Weeks test = Weeks.weeks(2);
266: Seconds expected = Seconds.seconds(2 * 7 * 24 * 60 * 60);
267: assertEquals(expected, test.toStandardSeconds());
268:
269: try {
270: Weeks.MAX_VALUE.toStandardSeconds();
271: fail();
272: } catch (ArithmeticException ex) {
273: // expected
274: }
275: }
276:
277: public void testToStandardDuration() {
278: Weeks test = Weeks.weeks(20);
279: Duration expected = new Duration(
280: 20L * DateTimeConstants.MILLIS_PER_WEEK);
281: assertEquals(expected, test.toStandardDuration());
282:
283: expected = new Duration(((long) Integer.MAX_VALUE)
284: * DateTimeConstants.MILLIS_PER_WEEK);
285: assertEquals(expected, Weeks.MAX_VALUE.toStandardDuration());
286: }
287:
288: //-----------------------------------------------------------------------
289: public void testPlus_int() {
290: Weeks test2 = Weeks.weeks(2);
291: Weeks result = test2.plus(3);
292: assertEquals(2, test2.getWeeks());
293: assertEquals(5, result.getWeeks());
294:
295: assertEquals(1, Weeks.ONE.plus(0).getWeeks());
296:
297: try {
298: Weeks.MAX_VALUE.plus(1);
299: fail();
300: } catch (ArithmeticException ex) {
301: // expected
302: }
303: }
304:
305: public void testPlus_Weeks() {
306: Weeks test2 = Weeks.weeks(2);
307: Weeks test3 = Weeks.weeks(3);
308: Weeks result = test2.plus(test3);
309: assertEquals(2, test2.getWeeks());
310: assertEquals(3, test3.getWeeks());
311: assertEquals(5, result.getWeeks());
312:
313: assertEquals(1, Weeks.ONE.plus(Weeks.ZERO).getWeeks());
314: assertEquals(1, Weeks.ONE.plus((Weeks) null).getWeeks());
315:
316: try {
317: Weeks.MAX_VALUE.plus(Weeks.ONE);
318: fail();
319: } catch (ArithmeticException ex) {
320: // expected
321: }
322: }
323:
324: public void testMinus_int() {
325: Weeks test2 = Weeks.weeks(2);
326: Weeks result = test2.minus(3);
327: assertEquals(2, test2.getWeeks());
328: assertEquals(-1, result.getWeeks());
329:
330: assertEquals(1, Weeks.ONE.minus(0).getWeeks());
331:
332: try {
333: Weeks.MIN_VALUE.minus(1);
334: fail();
335: } catch (ArithmeticException ex) {
336: // expected
337: }
338: }
339:
340: public void testMinus_Weeks() {
341: Weeks test2 = Weeks.weeks(2);
342: Weeks test3 = Weeks.weeks(3);
343: Weeks result = test2.minus(test3);
344: assertEquals(2, test2.getWeeks());
345: assertEquals(3, test3.getWeeks());
346: assertEquals(-1, result.getWeeks());
347:
348: assertEquals(1, Weeks.ONE.minus(Weeks.ZERO).getWeeks());
349: assertEquals(1, Weeks.ONE.minus((Weeks) null).getWeeks());
350:
351: try {
352: Weeks.MIN_VALUE.minus(Weeks.ONE);
353: fail();
354: } catch (ArithmeticException ex) {
355: // expected
356: }
357: }
358:
359: public void testMultipliedBy_int() {
360: Weeks test = Weeks.weeks(2);
361: assertEquals(6, test.multipliedBy(3).getWeeks());
362: assertEquals(2, test.getWeeks());
363: assertEquals(-6, test.multipliedBy(-3).getWeeks());
364: assertSame(test, test.multipliedBy(1));
365:
366: Weeks halfMax = Weeks.weeks(Integer.MAX_VALUE / 2 + 1);
367: try {
368: halfMax.multipliedBy(2);
369: fail();
370: } catch (ArithmeticException ex) {
371: // expected
372: }
373: }
374:
375: public void testDividedBy_int() {
376: Weeks test = Weeks.weeks(12);
377: assertEquals(6, test.dividedBy(2).getWeeks());
378: assertEquals(12, test.getWeeks());
379: assertEquals(4, test.dividedBy(3).getWeeks());
380: assertEquals(3, test.dividedBy(4).getWeeks());
381: assertEquals(2, test.dividedBy(5).getWeeks());
382: assertEquals(2, test.dividedBy(6).getWeeks());
383: assertSame(test, test.dividedBy(1));
384:
385: try {
386: Weeks.ONE.dividedBy(0);
387: fail();
388: } catch (ArithmeticException ex) {
389: // expected
390: }
391: }
392:
393: public void testNegated() {
394: Weeks test = Weeks.weeks(12);
395: assertEquals(-12, test.negated().getWeeks());
396: assertEquals(12, test.getWeeks());
397:
398: try {
399: Weeks.MIN_VALUE.negated();
400: fail();
401: } catch (ArithmeticException ex) {
402: // expected
403: }
404: }
405:
406: //-----------------------------------------------------------------------
407: public void testAddToLocalDate() {
408: Weeks test = Weeks.weeks(3);
409: LocalDate date = new LocalDate(2006, 6, 1);
410: LocalDate expected = new LocalDate(2006, 6, 22);
411: assertEquals(expected, date.plus(test));
412: }
413:
414: }
|