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