Skip to content

Commit

Permalink
Fix #23418: Improve unit test consistency when run with non-English l…
Browse files Browse the repository at this point in the history
…ocales

This also fixes some tests that fail when run individually.

git-svn-id: https://josm.openstreetmap.de/svn/trunk@18958 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
taylor.smock committed Jan 25, 2024
1 parent b624023 commit bc7a6bd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.openstreetmap.josm.data.osm.Tag;
import org.openstreetmap.josm.data.validation.Severity;
import org.openstreetmap.josm.data.validation.TestError;
import org.openstreetmap.josm.testutils.annotations.I18n;
import org.openstreetmap.josm.testutils.annotations.TaggingPresets;

/**
* JUnit Test of {@link TagChecker}.
*/
@I18n
@TaggingPresets
class TagCheckerTest {
List<TestError> test(OsmPrimitive primitive) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.spi.preferences.Config;
import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
import org.openstreetmap.josm.testutils.annotations.I18n;
import org.openstreetmap.josm.testutils.annotations.Main;
import org.openstreetmap.josm.testutils.annotations.Projection;

Expand All @@ -32,6 +33,7 @@
@BasicPreferences
@Main
@Projection
@I18n
class MarkerLayerTest {
/**
* Setup tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.openstreetmap.josm.data.osm.Way;
import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSelector.PresetClassification;
import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSelector.PresetClassifications;
import org.openstreetmap.josm.testutils.annotations.Territories;
import org.xml.sax.SAXException;

/**
* Unit tests of {@link PresetClassifications} class.
*/
@Territories
class PresetClassificationsTest {

static final PresetClassifications classifications = new PresetClassifications();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.openstreetmap.josm.TestUtils;
import org.openstreetmap.josm.gui.tagging.presets.items.Check;
import org.openstreetmap.josm.gui.tagging.presets.items.Key;
import org.openstreetmap.josm.testutils.annotations.Territories;
import org.xml.sax.SAXException;

/**
Expand Down Expand Up @@ -76,6 +77,7 @@ void testExternalEntityResolving() throws IOException {
* @throws SAXException if any XML error occurs
* @throws IOException if any I/O error occurs
*/
@Territories
@Test
void testReadDefaultPresets() throws SAXException, IOException {
String presetfile = "resource://data/defaultpresets.xml";
Expand Down
13 changes: 10 additions & 3 deletions test/unit/org/openstreetmap/josm/testutils/annotations/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,22 @@ public void beforeEach(ExtensionContext context) {
String language = AnnotationUtils.findFirstParentAnnotation(context, I18n.class).map(I18n::value).orElse("en");
if (!Locale.getDefault().equals(LanguageInfo.getLocale(language, false))) {
org.openstreetmap.josm.tools.I18n.set(language);
// We want to have a consistent "country", so we don't use a locale with a country code from the original locale.
// Unless someone specified it via the <lang>_<country> syntax.
if (!language.contains("_")) {
Locale.setDefault(LanguageInfo.getLocale(language, false));
}
}
}

@Override
public void afterEach(ExtensionContext context) {
if (!Locale.ENGLISH.equals(Locale.getDefault())) {
Locale original = org.openstreetmap.josm.tools.I18n.getOriginalLocale();
if (original == null) {
org.openstreetmap.josm.tools.I18n.set("en");
org.openstreetmap.josm.tools.I18n.set(org.openstreetmap.josm.tools.I18n.getOriginalLocale().getLanguage());
Locale.setDefault(Locale.ENGLISH);
} else if (!original.equals(Locale.getDefault())) {
org.openstreetmap.josm.tools.I18n.set(original.getLanguage());
Locale.setDefault(original);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.Locale;
import java.util.Objects;

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
Expand All @@ -31,6 +33,7 @@

class TaggingPresetsExtension implements BeforeEachCallback, BeforeAllCallback {
private static int expectedHashcode = 0;
private static Locale lastLocale;

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
Expand All @@ -47,9 +50,10 @@ public void beforeEach(ExtensionContext extensionContext) {
*/
public static synchronized void setup() {
final Collection<TaggingPreset> oldPresets = org.openstreetmap.josm.gui.tagging.presets.TaggingPresets.getTaggingPresets();
if (oldPresets.isEmpty() || expectedHashcode != oldPresets.hashCode()) {
if (oldPresets.isEmpty() || expectedHashcode != oldPresets.hashCode() || !Objects.equals(lastLocale, Locale.getDefault())) {
org.openstreetmap.josm.gui.tagging.presets.TaggingPresets.readFromPreferences();
expectedHashcode = org.openstreetmap.josm.gui.tagging.presets.TaggingPresets.getTaggingPresets().hashCode();
lastLocale = Locale.getDefault();
}
}
}
Expand Down

0 comments on commit bc7a6bd

Please sign in to comment.