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