]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/calendar/TestCase.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / calendar / TestCase.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2005, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.calendar;\r
8 \r
9 import com.ibm.icu.dev.test.*;\r
10 import com.ibm.icu.util.Calendar;\r
11 import com.ibm.icu.util.GregorianCalendar;\r
12 import com.ibm.icu.util.SimpleTimeZone;\r
13 import java.util.Date;\r
14 import java.util.Locale;\r
15 \r
16 /**\r
17  * A pseudo <code>Calendar</code> that is useful for testing\r
18  * new calendars.  A <code>TestCase</code> object is used to hold the\r
19  * field and millisecond values that the calendar should have at one\r
20  * particular instant in time.  The applyFields and applyTime\r
21  * methods are used to apply these settings to the calendar object being\r
22  * tested, and the equals and fieldsEqual methods are used to ensure\r
23  * that the calendar has ended up in the right state.\r
24  */\r
25 public class TestCase {\r
26 \r
27     //------------------------------------------------------------------\r
28     // Pseudo-Calendar fields and methods\r
29     //------------------------------------------------------------------\r
30 \r
31     protected int[] fields = new int[32];\r
32     protected boolean[] isSet = new boolean[32];\r
33     protected long time;\r
34 \r
35     protected void set(int field, int value) {\r
36         fields[field] = value;\r
37         isSet[field] = true;\r
38     }\r
39 \r
40     protected int get(int field) {\r
41         return fields[field];\r
42     }\r
43 \r
44     protected boolean isSet(int field) {\r
45         return isSet[field];\r
46     }\r
47 \r
48     protected void setTime(Date d) {\r
49         time = d.getTime();\r
50     }\r
51 \r
52     public Date getTime() {\r
53         return new Date(time);\r
54     }\r
55 \r
56     /**\r
57      * Return a String representation of this test case's time.\r
58      */\r
59     public String toString() {\r
60         return dowToString(get(Calendar.DAY_OF_WEEK)) + " " +\r
61             get(Calendar.YEAR) + "/" + (get(Calendar.MONTH)+1) + "/" +\r
62             get(Calendar.DATE);\r
63     }\r
64 \r
65     private static final String[] DOW_NAMES = {\r
66         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"\r
67     };\r
68 \r
69     public static String dowToString(int dow) {\r
70         --dow;\r
71         return (dow < 0 || dow > 6) ?\r
72             ("<DOW " + dow + ">") : DOW_NAMES[dow];\r
73     }\r
74 \r
75     /**\r
76      * Initialize a TestCase object using a julian day number and\r
77      * the corresponding fields for the calendar being tested.\r
78      *\r
79      * @param era       The ERA field of tested calendar on the given julian day\r
80      * @param year      The YEAR field of tested calendar on the given julian day\r
81      * @param month     The MONTH (1-based) field of tested calendar on the given julian day\r
82      * @param day       The DAY_OF_MONTH field of tested calendar on the given julian day\r
83      * @param dayOfWeek The DAY_OF_WEEK field of tested calendar on the given julian day\r
84      * @param hour      The HOUR field of tested calendar on the given julian day\r
85      * @param min       The MINUTE field of tested calendar on the given julian day\r
86      * @param sec       The SECOND field of tested calendar on the given julian day\r
87      */\r
88     public TestCase(double julian,\r
89                     int era, int year, int month, int day,\r
90                     int dayOfWeek,\r
91                     int hour, int min, int sec)\r
92     {\r
93         setTime(new Date(JULIAN_EPOCH + (long)(ONE_DAY * julian)));\r
94         \r
95         set(Calendar.ERA, era);\r
96         set(Calendar.YEAR, year);\r
97         set(Calendar.MONTH, month - 1);\r
98         set(Calendar.DATE, day);\r
99         set(Calendar.DAY_OF_WEEK, dayOfWeek);\r
100         set(Calendar.HOUR, hour);\r
101         set(Calendar.MINUTE, min);\r
102         set(Calendar.SECOND, sec);\r
103     }\r
104 \r
105     /**\r
106      * Initialize a TestCase object using a Gregorian year/month/day and\r
107      * the corresponding fields for the calendar being tested.\r
108      *\r
109      * @param gregYear  The Gregorian year of the date to be tested\r
110      * @param gregMonth The Gregorian month of the date to be tested\r
111      * @param gregDay   The Gregorian day of the month of the date to be tested\r
112      *\r
113      * @param era       The ERA field of tested calendar on the given gregorian date\r
114      * @param year      The YEAR field of tested calendar on the given gregorian date\r
115      * @param month     The MONTH (0-based) field of tested calendar on the given gregorian date\r
116      * @param day       The DAY_OF_MONTH field of tested calendar on the given gregorian date\r
117      * @param dayOfWeek The DAY_OF_WEEK field of tested calendar on the given gregorian date\r
118      * @param hour      The HOUR field of tested calendar on the given gregorian date\r
119      * @param min       The MINUTE field of tested calendar on the given gregorian date\r
120      * @param sec       The SECOND field of tested calendar on the given gregorian date\r
121      */\r
122     public TestCase(int gregYear, int gregMonth, int gregDay,\r
123                     int era, int year, int month, int day,\r
124                     int dayOfWeek,\r
125                     int hour, int min, int sec)\r
126     {\r
127         GregorianCalendar greg = new GregorianCalendar(UTC, Locale.getDefault());\r
128         greg.clear();\r
129         greg.set(gregYear, gregMonth-1, gregDay);\r
130         setTime(greg.getTime());\r
131         \r
132         set(Calendar.ERA, era);\r
133         set(Calendar.YEAR, year);\r
134         set(Calendar.MONTH, month - 1);\r
135         set(Calendar.DATE, day);\r
136         set(Calendar.DAY_OF_WEEK, dayOfWeek);\r
137         set(Calendar.HOUR, hour);\r
138         set(Calendar.MINUTE, min);\r
139         set(Calendar.SECOND, sec);\r
140     }\r
141     \r
142     /**\r
143      * For subclasses.\r
144      */\r
145     protected TestCase() {}\r
146 \r
147     /**\r
148      * Apply this test case's field values to another calendar\r
149      * by calling its set method for each field.  This is useful in combination\r
150      * with the equal method.\r
151      *\r
152      * @see com.ibm.icu.util.Calendar#equals\r
153      */\r
154     public void applyFields(Calendar c) {\r
155         for (int i=0; i < c.getFieldCount(); i++) {\r
156             if (isSet(i)) {\r
157                 c.set(i, get(i));\r
158             }\r
159         }\r
160     }\r
161     \r
162     /**\r
163      * Apply this test case's time in milliseconds to another calendar\r
164      * by calling its setTime method.  This is useful in combination\r
165      * with fieldsEqual\r
166      *\r
167      * @see #fieldsEqual\r
168      */\r
169     public void applyTime(Calendar c) {\r
170         c.setTime(new Date(time));\r
171     }\r
172 \r
173     /**\r
174      * Determine whether the fields of this calendar\r
175      * are the same as that of the other calendar.  This method is useful\r
176      * for determining whether the other calendar's computeFields method\r
177      * works properly.  For example:\r
178      * <pre>\r
179      *    Calendar testCalendar = ...\r
180      *    TestCase case = ...\r
181      *    case.applyTime(testCalendar);\r
182      *    if (!case.fieldsEqual(testCalendar)) {\r
183      *        // Error!\r
184      *    }\r
185      * </pre>\r
186      * \r
187      * @see #applyTime\r
188      */\r
189     public boolean fieldsEqual(Calendar c, TestLog log) {\r
190         for (int i=0; i < c.getFieldCount(); i++) {\r
191             if (isSet(i) && get(i) != c.get(i)) {\r
192                 StringBuffer buf = new StringBuffer();\r
193                 buf.append("Fail: " + CalendarTest.fieldName(i) + " = " + c.get(i) +\r
194                           ", expected " + get(i));\r
195                 for (int j=0; j<c.getFieldCount(); ++j) {\r
196                     if (isSet(j)) {\r
197                         if (get(j) == c.get(j)) {\r
198                             buf.append("\n  ok: " + CalendarTest.fieldName(j) + " = " +\r
199                                       c.get(j));\r
200                         } else {\r
201                             buf.append("\n  fail: " + CalendarTest.fieldName(j) + " = " +\r
202                                       c.get(j) + ", expected " + get(j));\r
203                         }\r
204                     }\r
205                 }\r
206                 log.errln(buf.toString());\r
207                 return false;\r
208             }\r
209         }\r
210         \r
211         return true;\r
212     }\r
213     \r
214     /**\r
215      * Determine whether time in milliseconds of this calendar\r
216      * is the same as that of the other calendar.  This method is useful\r
217      * for determining whether the other calendar's computeTime method\r
218      * works properly.  For example:\r
219      * <pre>\r
220      *    Calendar testCalendar = ...\r
221      *    TestCase case = ...\r
222      *    case.applyFields(testCalendar);\r
223      *    if (!case.equals(testCalendar)) {\r
224      *        // Error!\r
225      *    }\r
226      * </pre>\r
227      * \r
228      * @see #applyFields\r
229      */\r
230     public boolean equals(Object obj) {\r
231         return time == ((Calendar)obj).getTime().getTime();\r
232     }\r
233     \r
234     protected static final int  ONE_SECOND = 1000;\r
235     protected static final int  ONE_MINUTE = 60*ONE_SECOND;\r
236     protected static final int  ONE_HOUR   = 60*ONE_MINUTE;\r
237     protected static final long ONE_DAY    = 24*ONE_HOUR;\r
238     protected static final long JULIAN_EPOCH = -210866760000000L;   // 1/1/4713 BC 12:00\r
239 \r
240     public final static SimpleTimeZone UTC = new SimpleTimeZone(0, "GMT");\r
241 }\r