2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.calendar;
\r
9 import java.util.Date;
\r
10 import java.util.Locale;
\r
12 import com.ibm.icu.dev.test.TestLog;
\r
13 import com.ibm.icu.util.Calendar;
\r
14 import com.ibm.icu.util.GregorianCalendar;
\r
15 import com.ibm.icu.util.SimpleTimeZone;
\r
18 * A pseudo <code>Calendar</code> that is useful for testing
\r
19 * new calendars. A <code>TestCase</code> object is used to hold the
\r
20 * field and millisecond values that the calendar should have at one
\r
21 * particular instant in time. The applyFields and applyTime
\r
22 * methods are used to apply these settings to the calendar object being
\r
23 * tested, and the equals and fieldsEqual methods are used to ensure
\r
24 * that the calendar has ended up in the right state.
\r
26 public class TestCase {
\r
28 //------------------------------------------------------------------
\r
29 // Pseudo-Calendar fields and methods
\r
30 //------------------------------------------------------------------
\r
32 protected int[] fields = new int[32];
\r
33 protected boolean[] isSet = new boolean[32];
\r
34 protected long time;
\r
36 protected void set(int field, int value) {
\r
37 fields[field] = value;
\r
38 isSet[field] = true;
\r
41 protected int get(int field) {
\r
42 return fields[field];
\r
45 protected boolean isSet(int field) {
\r
46 return isSet[field];
\r
49 protected void setTime(Date d) {
\r
53 public Date getTime() {
\r
54 return new Date(time);
\r
58 * Return a String representation of this test case's time.
\r
60 public String toString() {
\r
61 return dowToString(get(Calendar.DAY_OF_WEEK)) + " " +
\r
62 get(Calendar.YEAR) + "/" + (get(Calendar.MONTH)+1) + "/" +
\r
66 private static final String[] DOW_NAMES = {
\r
67 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
\r
70 public static String dowToString(int dow) {
\r
72 return (dow < 0 || dow > 6) ?
\r
73 ("<DOW " + dow + ">") : DOW_NAMES[dow];
\r
77 * Initialize a TestCase object using a julian day number and
\r
78 * the corresponding fields for the calendar being tested.
\r
80 * @param era The ERA field of tested calendar on the given julian day
\r
81 * @param year The YEAR field of tested calendar on the given julian day
\r
82 * @param month The MONTH (1-based) field of tested calendar on the given julian day
\r
83 * @param day The DAY_OF_MONTH field of tested calendar on the given julian day
\r
84 * @param dayOfWeek The DAY_OF_WEEK field of tested calendar on the given julian day
\r
85 * @param hour The HOUR field of tested calendar on the given julian day
\r
86 * @param min The MINUTE field of tested calendar on the given julian day
\r
87 * @param sec The SECOND field of tested calendar on the given julian day
\r
89 public TestCase(double julian,
\r
90 int era, int year, int month, int day,
\r
92 int hour, int min, int sec)
\r
94 setTime(new Date(JULIAN_EPOCH + (long)(ONE_DAY * julian)));
\r
96 set(Calendar.ERA, era);
\r
97 set(Calendar.YEAR, year);
\r
98 set(Calendar.MONTH, month - 1);
\r
99 set(Calendar.DATE, day);
\r
100 set(Calendar.DAY_OF_WEEK, dayOfWeek);
\r
101 set(Calendar.HOUR, hour);
\r
102 set(Calendar.MINUTE, min);
\r
103 set(Calendar.SECOND, sec);
\r
107 * Initialize a TestCase object using a Gregorian year/month/day and
\r
108 * the corresponding fields for the calendar being tested.
\r
110 * @param gregYear The Gregorian year of the date to be tested
\r
111 * @param gregMonth The Gregorian month of the date to be tested
\r
112 * @param gregDay The Gregorian day of the month of the date to be tested
\r
114 * @param era The ERA field of tested calendar on the given gregorian date
\r
115 * @param year The YEAR field of tested calendar on the given gregorian date
\r
116 * @param month The MONTH (0-based) field of tested calendar on the given gregorian date
\r
117 * @param day The DAY_OF_MONTH field of tested calendar on the given gregorian date
\r
118 * @param dayOfWeek The DAY_OF_WEEK field of tested calendar on the given gregorian date
\r
119 * @param hour The HOUR field of tested calendar on the given gregorian date
\r
120 * @param min The MINUTE field of tested calendar on the given gregorian date
\r
121 * @param sec The SECOND field of tested calendar on the given gregorian date
\r
123 public TestCase(int gregYear, int gregMonth, int gregDay,
\r
124 int era, int year, int month, int day,
\r
126 int hour, int min, int sec)
\r
128 GregorianCalendar greg = new GregorianCalendar(UTC, Locale.getDefault());
\r
130 greg.set(gregYear, gregMonth-1, gregDay);
\r
131 setTime(greg.getTime());
\r
133 set(Calendar.ERA, era);
\r
134 set(Calendar.YEAR, year);
\r
135 set(Calendar.MONTH, month - 1);
\r
136 set(Calendar.DATE, day);
\r
137 set(Calendar.DAY_OF_WEEK, dayOfWeek);
\r
138 set(Calendar.HOUR, hour);
\r
139 set(Calendar.MINUTE, min);
\r
140 set(Calendar.SECOND, sec);
\r
146 protected TestCase() {}
\r
149 * Apply this test case's field values to another calendar
\r
150 * by calling its set method for each field. This is useful in combination
\r
151 * with the equal method.
\r
153 * @see com.ibm.icu.util.Calendar#equals
\r
155 public void applyFields(Calendar c) {
\r
156 for (int i=0; i < c.getFieldCount(); i++) {
\r
164 * Apply this test case's time in milliseconds to another calendar
\r
165 * by calling its setTime method. This is useful in combination
\r
168 * @see #fieldsEqual
\r
170 public void applyTime(Calendar c) {
\r
171 c.setTime(new Date(time));
\r
175 * Determine whether the fields of this calendar
\r
176 * are the same as that of the other calendar. This method is useful
\r
177 * for determining whether the other calendar's computeFields method
\r
178 * works properly. For example:
\r
180 * Calendar testCalendar = ...
\r
181 * TestCase case = ...
\r
182 * case.applyTime(testCalendar);
\r
183 * if (!case.fieldsEqual(testCalendar)) {
\r
190 public boolean fieldsEqual(Calendar c, TestLog log) {
\r
191 for (int i=0; i < c.getFieldCount(); i++) {
\r
192 if (isSet(i) && get(i) != c.get(i)) {
\r
193 StringBuffer buf = new StringBuffer();
\r
194 buf.append("Fail: " + CalendarTest.fieldName(i) + " = " + c.get(i) +
\r
195 ", expected " + get(i));
\r
196 for (int j=0; j<c.getFieldCount(); ++j) {
\r
198 if (get(j) == c.get(j)) {
\r
199 buf.append("\n ok: " + CalendarTest.fieldName(j) + " = " +
\r
202 buf.append("\n fail: " + CalendarTest.fieldName(j) + " = " +
\r
203 c.get(j) + ", expected " + get(j));
\r
207 log.errln(buf.toString());
\r
216 * Determine whether time in milliseconds of this calendar
\r
217 * is the same as that of the other calendar. This method is useful
\r
218 * for determining whether the other calendar's computeTime method
\r
219 * works properly. For example:
\r
221 * Calendar testCalendar = ...
\r
222 * TestCase case = ...
\r
223 * case.applyFields(testCalendar);
\r
224 * if (!case.equals(testCalendar)) {
\r
229 * @see #applyFields
\r
231 public boolean equals(Object obj) {
\r
232 return time == ((Calendar)obj).getTime().getTime();
\r
235 protected static final int ONE_SECOND = 1000;
\r
236 protected static final int ONE_MINUTE = 60*ONE_SECOND;
\r
237 protected static final int ONE_HOUR = 60*ONE_MINUTE;
\r
238 protected static final long ONE_DAY = 24*ONE_HOUR;
\r
239 protected static final long JULIAN_EPOCH = -210866760000000L; // 1/1/4713 BC 12:00
\r
241 public final static SimpleTimeZone UTC = new SimpleTimeZone(0, "GMT");
\r