]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/calendar/TestCase.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / calendar / TestCase.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, 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 java.util.Date;\r
10 import java.util.Locale;\r
11 \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
16 \r
17 /**\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
25  */\r
26 public class TestCase {\r
27 \r
28     //------------------------------------------------------------------\r
29     // Pseudo-Calendar fields and methods\r
30     //------------------------------------------------------------------\r
31 \r
32     protected int[] fields = new int[32];\r
33     protected boolean[] isSet = new boolean[32];\r
34     protected long time;\r
35 \r
36     protected void set(int field, int value) {\r
37         fields[field] = value;\r
38         isSet[field] = true;\r
39     }\r
40 \r
41     protected int get(int field) {\r
42         return fields[field];\r
43     }\r
44 \r
45     protected boolean isSet(int field) {\r
46         return isSet[field];\r
47     }\r
48 \r
49     protected void setTime(Date d) {\r
50         time = d.getTime();\r
51     }\r
52 \r
53     public Date getTime() {\r
54         return new Date(time);\r
55     }\r
56 \r
57     /**\r
58      * Return a String representation of this test case's time.\r
59      */\r
60     public String toString() {\r
61         return dowToString(get(Calendar.DAY_OF_WEEK)) + " " +\r
62             get(Calendar.YEAR) + "/" + (get(Calendar.MONTH)+1) + "/" +\r
63             get(Calendar.DATE);\r
64     }\r
65 \r
66     private static final String[] DOW_NAMES = {\r
67         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"\r
68     };\r
69 \r
70     public static String dowToString(int dow) {\r
71         --dow;\r
72         return (dow < 0 || dow > 6) ?\r
73             ("<DOW " + dow + ">") : DOW_NAMES[dow];\r
74     }\r
75 \r
76     /**\r
77      * Initialize a TestCase object using a julian day number and\r
78      * the corresponding fields for the calendar being tested.\r
79      *\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
88      */\r
89     public TestCase(double julian,\r
90                     int era, int year, int month, int day,\r
91                     int dayOfWeek,\r
92                     int hour, int min, int sec)\r
93     {\r
94         setTime(new Date(JULIAN_EPOCH + (long)(ONE_DAY * julian)));\r
95         \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
104     }\r
105 \r
106     /**\r
107      * Initialize a TestCase object using a Gregorian year/month/day and\r
108      * the corresponding fields for the calendar being tested.\r
109      *\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
113      *\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
122      */\r
123     public TestCase(int gregYear, int gregMonth, int gregDay,\r
124                     int era, int year, int month, int day,\r
125                     int dayOfWeek,\r
126                     int hour, int min, int sec)\r
127     {\r
128         GregorianCalendar greg = new GregorianCalendar(UTC, Locale.getDefault());\r
129         greg.clear();\r
130         greg.set(gregYear, gregMonth-1, gregDay);\r
131         setTime(greg.getTime());\r
132         \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
141     }\r
142     \r
143     /**\r
144      * For subclasses.\r
145      */\r
146     protected TestCase() {}\r
147 \r
148     /**\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
152      *\r
153      * @see com.ibm.icu.util.Calendar#equals\r
154      */\r
155     public void applyFields(Calendar c) {\r
156         for (int i=0; i < c.getFieldCount(); i++) {\r
157             if (isSet(i)) {\r
158                 c.set(i, get(i));\r
159             }\r
160         }\r
161     }\r
162     \r
163     /**\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
166      * with fieldsEqual\r
167      *\r
168      * @see #fieldsEqual\r
169      */\r
170     public void applyTime(Calendar c) {\r
171         c.setTime(new Date(time));\r
172     }\r
173 \r
174     /**\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
179      * <pre>\r
180      *    Calendar testCalendar = ...\r
181      *    TestCase case = ...\r
182      *    case.applyTime(testCalendar);\r
183      *    if (!case.fieldsEqual(testCalendar)) {\r
184      *        // Error!\r
185      *    }\r
186      * </pre>\r
187      * \r
188      * @see #applyTime\r
189      */\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
197                     if (isSet(j)) {\r
198                         if (get(j) == c.get(j)) {\r
199                             buf.append("\n  ok: " + CalendarTest.fieldName(j) + " = " +\r
200                                       c.get(j));\r
201                         } else {\r
202                             buf.append("\n  fail: " + CalendarTest.fieldName(j) + " = " +\r
203                                       c.get(j) + ", expected " + get(j));\r
204                         }\r
205                     }\r
206                 }\r
207                 log.errln(buf.toString());\r
208                 return false;\r
209             }\r
210         }\r
211         \r
212         return true;\r
213     }\r
214     \r
215     /**\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
220      * <pre>\r
221      *    Calendar testCalendar = ...\r
222      *    TestCase case = ...\r
223      *    case.applyFields(testCalendar);\r
224      *    if (!case.equals(testCalendar)) {\r
225      *        // Error!\r
226      *    }\r
227      * </pre>\r
228      * \r
229      * @see #applyFields\r
230      */\r
231     public boolean equals(Object obj) {\r
232         return time == ((Calendar)obj).getTime().getTime();\r
233     }\r
234     \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
240 \r
241     public final static SimpleTimeZone UTC = new SimpleTimeZone(0, "GMT");\r
242 }\r