Ignore:
Timestamp:
2024-01-25T20:00:41+01:00 (2 years ago)
Author:
taylor.smock
Message:

Fix #23418: Improve unit test consistency when run with non-English locales

This also fixes some tests that fail when run individually.

Location:
trunk/test/unit/org/openstreetmap/josm/testutils/annotations
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java

    r18870 r18958  
    4646            if (!Locale.getDefault().equals(LanguageInfo.getLocale(language, false))) {
    4747                org.openstreetmap.josm.tools.I18n.set(language);
     48                // We want to have a consistent "country", so we don't use a locale with a country code from the original locale.
     49                // Unless someone specified it via the <lang>_<country> syntax.
     50                if (!language.contains("_")) {
     51                    Locale.setDefault(LanguageInfo.getLocale(language, false));
     52                }
    4853            }
    4954        }
     
    5156        @Override
    5257        public void afterEach(ExtensionContext context) {
    53             if (!Locale.ENGLISH.equals(Locale.getDefault())) {
     58            Locale original = org.openstreetmap.josm.tools.I18n.getOriginalLocale();
     59            if (original == null) {
    5460                org.openstreetmap.josm.tools.I18n.set("en");
    55                 org.openstreetmap.josm.tools.I18n.set(org.openstreetmap.josm.tools.I18n.getOriginalLocale().getLanguage());
    56                 Locale.setDefault(Locale.ENGLISH);
     61            } else if (!original.equals(Locale.getDefault())) {
     62                org.openstreetmap.josm.tools.I18n.set(original.getLanguage());
     63                Locale.setDefault(original);
    5764            }
    5865        }
  • trunk/test/unit/org/openstreetmap/josm/testutils/annotations/TaggingPresets.java

    r18918 r18958  
    88import java.lang.annotation.Target;
    99import java.util.Collection;
     10import java.util.Locale;
     11import java.util.Objects;
    1012
    1113import org.junit.jupiter.api.extension.BeforeAllCallback;
     
    3234    class TaggingPresetsExtension implements BeforeEachCallback, BeforeAllCallback {
    3335        private static int expectedHashcode = 0;
     36        private static Locale lastLocale;
    3437
    3538        @Override
     
    4851        public static synchronized void setup() {
    4952            final Collection<TaggingPreset> oldPresets = org.openstreetmap.josm.gui.tagging.presets.TaggingPresets.getTaggingPresets();
    50             if (oldPresets.isEmpty() || expectedHashcode != oldPresets.hashCode()) {
     53            if (oldPresets.isEmpty() || expectedHashcode != oldPresets.hashCode() || !Objects.equals(lastLocale, Locale.getDefault())) {
    5154                org.openstreetmap.josm.gui.tagging.presets.TaggingPresets.readFromPreferences();
    5255                expectedHashcode = org.openstreetmap.josm.gui.tagging.presets.TaggingPresets.getTaggingPresets().hashCode();
     56                lastLocale = Locale.getDefault();
    5357            }
    5458        }
Note: See TracChangeset for help on using the changeset viewer.