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