Skip to content

Commit

Permalink
Better test failure message
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Aug 10, 2023
1 parent 8a18534 commit 5440b18
Showing 1 changed file with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.text.DateFormatSymbols;
import java.text.ParseException;
Expand Down Expand Up @@ -56,7 +57,8 @@ public void testTimeZoneStrategyPattern(final Locale locale) {
private void testTimeZoneStrategyPattern(final Locale locale, final TimeZone tzDefault) {
Objects.requireNonNull(locale, "locale");
Objects.requireNonNull(tzDefault, "tzDefault");
assumeFalse(LocaleUtils.isLanguageUndetermined(locale));
assumeFalse(LocaleUtils.isLanguageUndetermined(locale), () -> toFailureMessage(locale, null));
assumeTrue(LocaleUtils.isAvailableLocale(locale), () -> toFailureMessage(locale, null));
final FastDateParser parser = new FastDateParser("z", tzDefault, locale);
final String[][] zones = DateFormatSymbols.getInstance(locale).getZoneStrings();
for (final String[] zone : zones) {
Expand All @@ -68,7 +70,7 @@ private void testTimeZoneStrategyPattern(final Locale locale, final TimeZone tzD
// An exception will be thrown and the test will fail if parsing isn't successful
try {
parser.parse(tzDisplay);
} catch (ParseException e) {
} catch (final ParseException e) {
fail(String.format(
"%s: with tzDefault = %s, locale = %s, zones[][] size = '%s', zone[] size = '%s', zIndex = %,d, tzDisplay = '%s', parser = '%s'", e,
tzDefault, locale, zones.length, zone.length, zIndex, tzDisplay, parser.toStringAll()), e);
Expand All @@ -77,23 +79,50 @@ private void testTimeZoneStrategyPattern(final Locale locale, final TimeZone tzD
}
}

private void testTimeZoneStrategyPattern(final String languageTag) throws ParseException {
final Locale locale = Locale.forLanguageTag(languageTag);
assumeFalse(LocaleUtils.isLanguageUndetermined(locale), () -> toFailureMessage(locale, languageTag));
assumeTrue(LocaleUtils.isAvailableLocale(locale), () -> toFailureMessage(locale, null));
final TimeZone tzDefault = TimeZone.getTimeZone("Etc/UTC");
final FastDateParser parser = new FastDateParser("z", tzDefault, locale);
parser.parse("Horário do Meridiano de Greenwich");
testTimeZoneStrategyPattern(locale, tzDefault);
}

private String toFailureMessage(final Locale locale, final String languageTag) {
return String.format("locale = %s, languageTag = '%s', isAvailableLocale = %s, isLanguageUndetermined = %s", languageTag, locale,
LocaleUtils.isAvailableLocale(locale), LocaleUtils.isLanguageUndetermined(locale));
}

/**
* Breaks randomly on GitHub for Locale "pt_PT", TimeZone "Etc/UTC" if we do not check if the Locale's language is "undetermined".
*
* <pre>{@code
* java.text.ParseException: Unparseable date: Horário do Meridiano de Greenwich: with tzDefault =
* java.text.ParseException: Unparseable date: Horário do Meridiano de Greenwich: with tzDefault =
* sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null], locale = pt_LU, zones[][] size = '601',
* zone[] size = '7', zIndex = 3, tzDisplay = 'Horário do Meridiano de Greenwich'
* zone[] size = '7', zIndex = 3, tzDisplay = 'Horário do Meridiano de Greenwich'
* }</pre>
*
* @throws ParseException Test failure
*/
@Test
public void testTimeZoneStrategyPatternPortugal() throws ParseException {
final Locale locale = Locale.forLanguageTag("pt_PT");
assumeFalse(LocaleUtils.isLanguageUndetermined(locale));
final TimeZone tzDefault = TimeZone.getTimeZone("Etc/UTC");
testTimeZoneStrategyPattern(locale, tzDefault);
final FastDateParser parser = new FastDateParser("z", tzDefault, locale);
parser.parse("Horário do Meridiano de Greenwich");
testTimeZoneStrategyPattern("pt_PT");
}

/**
* Breaks randomly on GitHub for Locale "sr_ME_#Cyrl", TimeZone "Etc/UTC" if we do not check if the Locale's language is "undetermined".
*
* <pre>{@code
* java.text.ParseException: Unparseable date: Srednje vreme po Griniču: with tzDefault = sun.util.calendar.ZoneInfo[id="Etc/UTC",
* offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null], locale = sr_ME_#Cyrl, zones[][] size = '601',
* zone[] size = '7', zIndex = 3, tzDisplay = 'Srednje vreme po Griniču'
* }</pre>
*
* @throws ParseException Test failure
*/
@Test
public void testTimeZoneStrategyPatternSuriname() throws ParseException {
testTimeZoneStrategyPattern("sr_ME_#Cyrl");
}
}

0 comments on commit 5440b18

Please sign in to comment.