Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize CalendarValidatorTest.testFormat() #301

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void testCalendarValidatorMethods() {
// Don't rely on specific German format - it varies between JVMs
final DateFormat df = new SimpleDateFormat(germanPattern, locale);
final Calendar cal = Calendar.getInstance(Locale.US);
cal.set(2005, 11, 31); // month is 0-based
cal.set(2005, Calendar.DECEMBER, 31);
final String germanVal = df.format(cal.getTime());
final String localeVal = "31.12.2005";
final String defaultVal = "12/31/05";
Expand Down Expand Up @@ -241,12 +241,12 @@ public void testDateTimeStyle() {
// Formats vary between JVMs, so create the test strings using the current JVM
final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
final Calendar cal = Calendar.getInstance();
cal.set(2005, 11, 31, 14, 23); // month is 0-based
cal.set(2005, Calendar.DECEMBER, 31, 14, 23);
final String val = df.format(cal.getTime());

final DateFormat usdf = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US);
final Calendar uscal = Calendar.getInstance(Locale.US);
uscal.set(2005, 11, 31, 14, 23); // month is 0-based
uscal.set(2005, Calendar.DECEMBER, 31, 14, 23);
final String usval = usdf.format(uscal.getTime());

final AbstractCalendarValidator dateTimeValidator = new AbstractCalendarValidator(true, DateFormat.SHORT, DateFormat.SHORT) {
Expand All @@ -271,49 +271,51 @@ protected Object processParsedValue(final Object value, final Format formatter)
public void testFormat() {
final Calendar cal20051231 = createCalendar(TimeZones.GMT, 20051231, 11500);
// validator defaults to SHORT, but the format varies between JVMs
assertNull(calValidator.format(null), "null");

final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
final String val = df.format(cal20051231.getTime());
final DateFormat dfus = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
final String usval = dfus.format(cal20051231.getTime());
final String germanPattern = "dd MMM yyyy";
final DateFormat dedf = new SimpleDateFormat(germanPattern, Locale.GERMAN);
final String deval = dedf.format(cal20051231.getTime());
assertNull(calValidator.format(null), "null");
assertEquals(val, calValidator.format(cal20051231), "default");
assertEquals(usval, calValidator.format(cal20051231, Locale.US), "locale");
assertEquals("2005-12-31 01:15", calValidator.format(cal20051231, "yyyy-MM-dd HH:mm"), "patternA");
assertEquals("2005-12-31 GMT", calValidator.format(cal20051231, "yyyy-MM-dd z"), "patternB");
assertEquals(deval, calValidator.format(cal20051231, germanPattern, Locale.GERMAN), "both");

// EST Time Zone
final DateFormat dfest = DateFormat.getDateInstance(DateFormat.SHORT);
dfest.setTimeZone(TestTimeZones.EST);
final String valest = dfest.format(cal20051231.getTime());

final DateFormat dfusest = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
dfusest.setTimeZone(TestTimeZones.EST);
final String valusest = dfusest.format(cal20051231.getTime());

final DateFormat dedfest = new SimpleDateFormat(germanPattern, Locale.GERMAN);
dedfest.setTimeZone(TestTimeZones.EST);
final String devalest = dedfest.format(cal20051231.getTime());

assertEquals(valest, calValidator.format(cal20051231, TestTimeZones.EST), "EST default");
assertEquals(valusest, calValidator.format(cal20051231, Locale.US, TestTimeZones.EST), "EST locale");
final DateFormat dfUs = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
final String valUs = dfUs.format(cal20051231.getTime());
assertEquals(valUs, calValidator.format(cal20051231, Locale.US), "locale");

final String patternA = "yyyy-MM-dd HH:mm";
final DateFormat dfA = new SimpleDateFormat(patternA);
dfA.setTimeZone(TestTimeZones.EST);
final String valA = dfA.format(cal20051231.getTime());
assertEquals(valA, calValidator.format(cal20051231, patternA, TestTimeZones.EST), "EST patternA");
assertEquals(valA, calValidator.format(cal20051231, patternA), "patternA");

final String patternB = "yyyy-MM-dd z";
final DateFormat dfB = new SimpleDateFormat(patternB);
dfB.setTimeZone(TestTimeZones.EST);
final String valB = dfB.format(cal20051231.getTime());
assertEquals(valB, calValidator.format(cal20051231, patternB), "patternB");

final String patternGerman = "dd MMM yyyy";
final DateFormat dfGerman = new SimpleDateFormat(patternGerman, Locale.GERMAN);
final String valGerman = dfGerman.format(cal20051231.getTime());
assertEquals(valGerman, calValidator.format(cal20051231, patternGerman, Locale.GERMAN), "patternGerman");

// EST Time Zone
df.setTimeZone(TestTimeZones.EST);
final String valEst = df.format(cal20051231.getTime());
assertEquals(valEst, calValidator.format(cal20051231, TestTimeZones.EST), "EST default");

dfUs.setTimeZone(TestTimeZones.EST);
final String valUsEst = dfUs.format(cal20051231.getTime());
assertEquals(valUsEst, calValidator.format(cal20051231, Locale.US, TestTimeZones.EST), "EST locale");

dfA.setTimeZone(TestTimeZones.EST);
final String valAEst = dfA.format(cal20051231.getTime());
assertEquals(valAEst, calValidator.format(cal20051231, patternA, TestTimeZones.EST), "EST patternA");

dfB.setTimeZone(TestTimeZones.EST);
final String valBEst = dfB.format(cal20051231.getTime());
assertEquals(valBEst, calValidator.format(cal20051231, patternB, TestTimeZones.EST), "EST patternB");

assertEquals(valB, calValidator.format(cal20051231, patternB, TestTimeZones.EST), "EST patternB");
assertEquals(devalest, calValidator.format(cal20051231, germanPattern, Locale.GERMAN, TestTimeZones.EST), "EST both");
dfGerman.setTimeZone(TestTimeZones.EST);
final String valGermanEst = dfGerman.format(cal20051231.getTime());
assertEquals(valGermanEst, calValidator.format(cal20051231, patternGerman, Locale.GERMAN, TestTimeZones.EST), "EST patternGerman");
}

}