use of android.icu.util.BuddhistCalendar in project j2objc by google.
the class IBMCalendarTest method TestBuddhistLimits.
/**
* Test limits of the Buddhist calendar.
*/
@Test
public void TestBuddhistLimits() {
// Final parameter is either number of days, if > 0, or test
// duration in seconds, if < 0.
Calendar cal = Calendar.getInstance();
cal.set(2007, Calendar.JANUARY, 1);
BuddhistCalendar buddhist = new BuddhistCalendar();
doLimitsTest(buddhist, null, cal.getTime());
doTheoreticalLimitsTest(buddhist, false);
}
use of android.icu.util.BuddhistCalendar in project j2objc by google.
the class GlobalizationPreferencesTest method TestDateFormat.
@Test
public void TestDateFormat() {
GlobalizationPreferences gp = new GlobalizationPreferences();
String pattern;
DateFormat df;
// Set unsupported locale - ach
logln("Set locale - ach");
gp.setLocale(new ULocale("ach"));
// Date - short
df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
// root pattern must be used
if (!pattern.equals("y-MM-dd")) {
errln("FAIL: SHORT date pattern is " + pattern + " Expected: y-MM-dd");
}
// Set locale - fr, fr_CA, fr_FR
ArrayList lcls = new ArrayList(3);
lcls.add(new ULocale("fr"));
lcls.add(new ULocale("fr_CA"));
lcls.add(new ULocale("fr_FR"));
logln("Set locales - fr, fr_CA, fr_FR");
gp.setLocales(lcls);
// Date - short
df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
// fr_CA pattern must be used
if (!pattern.equals("yy-MM-dd")) {
errln("FAIL: SHORT date pattern is " + pattern + " Expected: yy-MM-dd");
}
// Set locale - en_GB
logln("Set locale - en_GB");
gp.setLocale(new ULocale("en_GB"));
// Date - full
df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("EEEE, d MMMM y")) {
errln("FAIL: FULL date pattern is " + pattern + " Expected: EEEE, d MMMM y");
}
// Date - long
df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("d MMMM y")) {
errln("FAIL: LONG date pattern is " + pattern + " Expected: d MMMM y");
}
// Date - medium
df = gp.getDateFormat(GlobalizationPreferences.DF_MEDIUM, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("d MMM y")) {
errln("FAIL: MEDIUM date pattern is " + pattern + " Expected: d MMM y");
}
// Date - short
df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("dd/MM/y")) {
errln("FAIL: SHORT date pattern is " + pattern + " Expected: dd/MM/y");
}
// Time - full
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_FULL);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("HH:mm:ss zzzz")) {
errln("FAIL: FULL time pattern is " + pattern + " Expected: HH:mm:ss zzzz");
}
// Time - long
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_LONG);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("HH:mm:ss z")) {
errln("FAIL: LONG time pattern is " + pattern + " Expected: HH:mm:ss z");
}
// Time - medium
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_MEDIUM);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("HH:mm:ss")) {
errln("FAIL: MEDIUM time pattern is " + pattern + " Expected: HH:mm:ss");
}
// Time - short
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_SHORT);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("HH:mm")) {
errln("FAIL: SHORT time pattern is " + pattern + " Expected: HH:mm");
}
// Date/Time - full
df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_FULL);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("EEEE, d MMMM y 'at' HH:mm:ss zzzz")) {
errln("FAIL: FULL date/time pattern is " + pattern + " Expected: EEEE, d MMMM y 'at' HH:mm:ss zzzz");
}
// Invalid style
boolean illegalArg = false;
try {
df = gp.getDateFormat(-1, GlobalizationPreferences.DF_NONE);
} catch (IllegalArgumentException iae) {
logln("Illegal date style -1");
illegalArg = true;
}
if (!illegalArg) {
errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle -1");
}
illegalArg = false;
try {
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_NONE);
} catch (IllegalArgumentException iae) {
logln("Illegal style - dateStyle:DF_NONE / timeStyle:DF_NONE");
illegalArg = true;
}
if (!illegalArg) {
errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle:DF_NONE/timeStyle:DF_NONE");
}
// Set explicit time zone
logln("Set timezone - America/Sao_Paulo");
TimeZone tz = TimeZone.getTimeZone("America/Sao_Paulo");
gp.setTimeZone(tz);
df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_MEDIUM);
String tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
// Set explicit calendar
logln("Set calendar - japanese");
Calendar jcal = new JapaneseCalendar();
jcal.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
gp.setCalendar(jcal);
df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_SHORT);
Calendar dfCal = df.getCalendar();
if (!(dfCal instanceof JapaneseCalendar)) {
errln("FAIL: The DateFormat instance must use Japanese calendar");
}
// TimeZone must be still America/Sao_Paulo
tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
// Set explicit DateFormat
logln("Set explicit date format - full date");
DateFormat customFD = DateFormat.getDateInstance(new IslamicCalendar(), DateFormat.FULL, new ULocale("ar_SA"));
customFD.setTimeZone(TimeZone.getTimeZone("Asia/Riyadh"));
gp.setDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE, customFD);
df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE);
dfCal = df.getCalendar();
if (!(dfCal instanceof IslamicCalendar)) {
errln("FAIL: The DateFormat instance must use Islamic calendar");
}
// TimeZone in the custom DateFormat is overridden by GP's timezone setting
tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
// Freeze
logln("Freeze this object");
gp.freeze();
DateFormat customLD = DateFormat.getDateInstance(new BuddhistCalendar(), DateFormat.LONG, new ULocale("th"));
customLD.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
boolean isFrozen = false;
try {
gp.setDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE, customLD);
} catch (UnsupportedOperationException uoe) {
logln("setDateFormat is blocked");
isFrozen = true;
}
if (!isFrozen) {
errln("FAIL: setDateFormat must be blocked after frozen");
}
// Modifiable clone
logln("cloneAsThawed");
GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
gp1.setDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE, customLD);
df = gp1.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_SHORT);
dfCal = df.getCalendar();
if (!(dfCal instanceof JapaneseCalendar)) {
errln("FAIL: The DateFormat instance must use Japanese calendar");
}
// TimeZone must be still America/Sao_Paulo
tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
df = gp1.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE);
dfCal = df.getCalendar();
if (!(dfCal instanceof BuddhistCalendar)) {
errln("FAIL: The DateFormat instance must use Buddhist calendar");
}
// TimeZone must be still America/Sao_Paulo
tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
}
use of android.icu.util.BuddhistCalendar in project j2objc by google.
the class IBMCalendarTest method TestCoverage.
/**
* Miscellaneous tests to increase coverage.
*/
@Test
public void TestCoverage() {
// BuddhistCalendar
BuddhistCalendar bcal = new BuddhistCalendar();
/*int i =*/
bcal.getMinimum(Calendar.ERA);
bcal.add(Calendar.YEAR, 1);
bcal.add(Calendar.MONTH, 1);
/*Date d = */
bcal.getTime();
// CalendarAstronomer
// (This class should probably be made package-private.)
CalendarAstronomer astro = new CalendarAstronomer();
/*String s = */
astro.local(0);
// ChineseCalendar
ChineseCalendar ccal = new ChineseCalendar(TimeZone.getDefault(), Locale.getDefault());
ccal.add(Calendar.MONTH, 1);
ccal.add(Calendar.YEAR, 1);
ccal.roll(Calendar.MONTH, 1);
ccal.roll(Calendar.YEAR, 1);
ccal.getTime();
// ICU 2.6
Calendar cal = Calendar.getInstance(Locale.US);
logln(cal.toString());
logln(cal.getDisplayName(Locale.US));
int weekendOnset = -1;
int weekendCease = -1;
for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; ++i) {
if (cal.getDayOfWeekType(i) == Calendar.WEEKEND_ONSET) {
weekendOnset = i;
}
if (cal.getDayOfWeekType(i) == Calendar.WEEKEND_CEASE) {
weekendCease = i;
}
}
// but make the call anyway for coverage reasons
try {
/*int x=*/
cal.getWeekendTransition(weekendOnset);
/*int x=*/
cal.getWeekendTransition(weekendCease);
} catch (IllegalArgumentException e) {
}
/*int x=*/
cal.isWeekend(new Date());
// new GregorianCalendar(ULocale)
GregorianCalendar gcal = new GregorianCalendar(ULocale.getDefault());
if (gcal == null) {
errln("could not create GregorianCalendar with ULocale");
} else {
logln("Calendar display name: " + gcal.getDisplayName(ULocale.getDefault()));
}
// cover getAvailableULocales
final ULocale[] locales = Calendar.getAvailableULocales();
long count = locales.length;
if (count == 0)
errln("getAvailableULocales return empty list");
logln("" + count + " available ulocales in Calendar.");
// Jitterbug 4451, for coverage
class StubCalendar extends Calendar {
/**
* For serialization
*/
private static final long serialVersionUID = -4558903444622684759L;
@Override
protected int handleGetLimit(int field, int limitType) {
if (limitType == Calendar.LEAST_MAXIMUM) {
return 1;
} else if (limitType == Calendar.GREATEST_MINIMUM) {
return 7;
}
return -1;
}
@Override
protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
if (useMonth) {
return eyear * 365 + month * 31;
} else {
return eyear * 365;
}
}
@Override
protected int handleGetExtendedYear() {
return 2017;
}
public void run() {
if (Calendar.gregorianPreviousMonthLength(2000, 2) != 29) {
errln("Year 2000 Feb should have 29 days.");
}
long millis = Calendar.julianDayToMillis(Calendar.MAX_JULIAN);
if (millis != Calendar.MAX_MILLIS) {
errln("Did not get the expected value from julianDayToMillis. Got:" + millis);
}
DateFormat df = handleGetDateFormat("", Locale.getDefault());
if (!df.equals(handleGetDateFormat("", ULocale.getDefault()))) {
errln("Calendar.handleGetDateFormat(String, Locale) should delegate to ( ,ULocale)");
}
if (!getType().equals("unknown")) {
errln("Calendar.getType() should be 'unknown'");
}
// Tests for complete coverage of Calendar functions.
int julianDay = Calendar.millisToJulianDay(millis - 1);
assertEquals("Julian max day -1", julianDay, Calendar.MAX_JULIAN - 1);
DateFormat df1 = handleGetDateFormat("GG yyyy-d:MM", "option=xyz", Locale.getDefault());
if (!df1.equals(handleGetDateFormat("GG yyyy-d:MM", "option=xyz", ULocale.getDefault()))) {
errln("Calendar.handleGetDateFormat(String, Locale) should delegate to ( ,ULocale)");
}
// Prove that the local overrides are used.
int leastMsInDay = handleGetLimit(Calendar.MILLISECONDS_IN_DAY, Calendar.LEAST_MAXIMUM);
assertEquals("getLimit test 1", leastMsInDay, 1);
int maxMsInDay = handleGetLimit(Calendar.WEEK_OF_MONTH, Calendar.GREATEST_MINIMUM);
assertEquals("getLimit test 2", 7, maxMsInDay);
int febLeapLength = handleGetMonthLength(2020, Calendar.FEBRUARY);
assertEquals("handleMonthLength", 31, febLeapLength);
int exYear = handleGetExtendedYear();
assertEquals("handleGetExtendeYear", exYear, 2017);
int monthStart = handleComputeMonthStart(2016, 4, false);
assertEquals("handleComputeMonthStart false", 735840, monthStart);
monthStart = handleComputeMonthStart(2016, 4, true);
assertEquals("handleComputeMonthStart true", 735964, monthStart);
Calendar cal = Calendar.getInstance();
cal.set(1980, 5, 2);
this.setTime(cal.getTime());
assertEquals("handleComputeFields: year set", 1980, get(YEAR));
assertEquals("handleComputeFields: month set", 5, get(MONTH));
assertEquals("handleComputeFields: day set", 2, get(DAY_OF_MONTH));
}
}
StubCalendar stub = new StubCalendar();
stub.run();
}
use of android.icu.util.BuddhistCalendar in project j2objc by google.
the class DateFormatTest method TestRoundtripWithCalendar.
/*
* Test for format/parse method with calendar which is different
* from what DateFormat instance internally use. See ticket#6420.
*/
@Test
public void TestRoundtripWithCalendar() {
TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
TimeZone gmt = TimeZone.getTimeZone("Etc/GMT");
final Calendar[] calendars = { new GregorianCalendar(tz), new BuddhistCalendar(tz), new HebrewCalendar(tz), new IslamicCalendar(tz), new JapaneseCalendar(tz) };
final String pattern = "GyMMMMdEEEEHHmmssVVVV";
// FIXME The formatters commented out below are currently failing because of
// the calendar calculation problem reported by #6691
// The order of test formatters mus match the order of calendars above.
final DateFormat[] formatters = { // calendar=gregorian
DateFormat.getPatternInstance(pattern, new ULocale("en_US")), // calendar=buddhist
DateFormat.getPatternInstance(pattern, new ULocale("th_TH")), DateFormat.getPatternInstance(pattern, new ULocale("he_IL@calendar=hebrew")) // DateFormat.getPatternInstance(pattern, new ULocale("ar_EG@calendar=islamic")),
// DateFormat.getPatternInstance(pattern, new ULocale("ja_JP@calendar=japanese")),
};
Date d = new Date();
StringBuffer buf = new StringBuffer();
FieldPosition fpos = new FieldPosition(0);
ParsePosition ppos = new ParsePosition(0);
for (int i = 0; i < formatters.length; i++) {
buf.setLength(0);
fpos.setBeginIndex(0);
fpos.setEndIndex(0);
calendars[i].setTime(d);
// Normal case output - the given calendar matches the calendar
// used by the formatter
formatters[i].format(calendars[i], buf, fpos);
String refStr = buf.toString();
for (int j = 0; j < calendars.length; j++) {
if (j == i) {
continue;
}
buf.setLength(0);
fpos.setBeginIndex(0);
fpos.setEndIndex(0);
calendars[j].setTime(d);
// Even the different calendar type is specified,
// we should get the same result.
formatters[i].format(calendars[j], buf, fpos);
if (!refStr.equals(buf.toString())) {
errln("FAIL: Different format result with a different calendar for the same time -" + "\n Reference calendar type=" + calendars[i].getType() + "\n Another calendar type=" + calendars[j].getType() + "\n Expected result=" + refStr + "\n Actual result=" + buf.toString());
}
}
calendars[i].setTimeZone(gmt);
calendars[i].clear();
ppos.setErrorIndex(-1);
ppos.setIndex(0);
// Normal case parse result - the given calendar matches the calendar
// used by the formatter
formatters[i].parse(refStr, calendars[i], ppos);
for (int j = 0; j < calendars.length; j++) {
if (j == i) {
continue;
}
calendars[j].setTimeZone(gmt);
calendars[j].clear();
ppos.setErrorIndex(-1);
ppos.setIndex(0);
// Even the different calendar type is specified,
// we should get the same time and time zone.
formatters[i].parse(refStr, calendars[j], ppos);
if (calendars[i].getTimeInMillis() != calendars[j].getTimeInMillis() || !calendars[i].getTimeZone().equals(calendars[j].getTimeZone())) {
errln("FAIL: Different parse result with a different calendar for the same string -" + "\n Reference calendar type=" + calendars[i].getType() + "\n Another calendar type=" + calendars[j].getType() + "\n Date string=" + refStr + "\n Expected time=" + calendars[i].getTimeInMillis() + "\n Expected time zone=" + calendars[i].getTimeZone().getID() + "\n Actual time=" + calendars[j].getTimeInMillis() + "\n Actual time zone=" + calendars[j].getTimeZone().getID());
}
}
}
}
Aggregations