01: /* DateAssert.java
02: *
03: * DDSteps - Data Driven JUnit Test Steps
04: * Copyright (C) 2005 Jayway AB
05: * www.ddsteps.org
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License version 2.1 as published by the Free Software Foundation.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, visit
18: * http://www.opensource.org/licenses/lgpl-license.php
19: */
20:
21: package org.ddsteps.util;
22:
23: import java.text.SimpleDateFormat;
24: import java.util.Calendar;
25: import java.util.Date;
26: import java.util.TimeZone;
27:
28: import junit.framework.ComparisonFailure;
29:
30: /**
31: * Assertions for Dates.
32: *
33: * @author adamskogman
34: * @version $Id: DateAssert.java,v 1.1 2005/12/03 12:51:41 adamskogman Exp $
35: */
36: public class DateAssert {
37:
38: /**
39: * Compare two dates using the System timezone. Does NOT compare the time
40: * portion, only the Dates.
41: *
42: * @param message
43: * Message if dates are now equal.
44: * @param expected
45: * The expected date.
46: * @param actual
47: * The actual date.
48: */
49: public static void assertDatesEqual(String message, Date expected,
50: Date actual) {
51:
52: assertDatesEqual(message, expected, actual, TimeZone
53: .getDefault());
54:
55: }
56:
57: /**
58: * Compare two dates using the System timezone. Does NOT compare the time
59: * portion, only the Dates.
60: *
61: * @param message
62: * Message if dates are now equal.
63: * @param expected
64: * The expected date.
65: * @param actual
66: * The actual date.
67: * @param timeZone
68: * The timezone to compare in.
69: */
70: public static void assertDatesEqual(String message, Date expected,
71: Date actual, TimeZone timeZone) {
72:
73: Calendar expectedCalendar = Calendar.getInstance(timeZone);
74: Calendar actualCalendar = Calendar.getInstance(timeZone);
75:
76: expectedCalendar.setTime(expected);
77: actualCalendar.setTime(actual);
78:
79: // year
80: boolean equal = expectedCalendar.get(Calendar.YEAR) == actualCalendar
81: .get(Calendar.YEAR);
82: // month
83: equal &= expectedCalendar.get(Calendar.MONTH) == actualCalendar
84: .get(Calendar.MONTH);
85: // day
86: equal &= expectedCalendar.get(Calendar.DAY_OF_MONTH) == actualCalendar
87: .get(Calendar.DAY_OF_MONTH);
88:
89: if (!equal) {
90: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
91: throw new ComparisonFailure(message, format
92: .format(expected), format.format(actual));
93: }
94: }
95: }
|