source: josm/trunk/test/unit/org/openstreetmap/josm/tools/JosmDecimalFormatSymbolsProviderTest.java

Last change on this file was 19054, checked in by taylor.smock, 30 hours ago

See r19043: Drop COMPAT locale provider

The CLDR provider does not currently return the CET for GMT+01:00. Using only SPI
will result in the "correct" CET timezone abbreviation. If using both, CLDR wins
and returns GMT+01:00. In addition, there are locales that do not use the Western
Arabic numeral system. Specifically, Arabic uses the Eastern Arabic numeral
system, Persian uses the Urdu numeral system, and Marathi uses the Devanagari
numeral system. Of note, while the first character for the Eastern Arabic and
Urdu numeral systems looks the same, it is a different character.

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertNotNull;
6import static org.junit.jupiter.api.Assertions.assertTrue;
7import static org.junit.jupiter.api.Assertions.fail;
8
9import java.io.IOException;
10import java.io.InputStream;
11import java.nio.charset.StandardCharsets;
12import java.text.DecimalFormat;
13import java.util.Locale;
14import java.util.stream.Stream;
15
16import org.junit.jupiter.api.BeforeAll;
17import org.junit.jupiter.api.Test;
18import org.junit.jupiter.params.ParameterizedTest;
19import org.junit.jupiter.params.provider.MethodSource;
20
21/**
22 * Unit tests of {@link JosmDecimalFormatSymbolsProvider}.
23 */
24@org.openstreetmap.josm.testutils.annotations.I18n
25class JosmDecimalFormatSymbolsProviderTest {
26 @BeforeAll
27 static void beforeAll() throws IOException {
28 assertEquals("SPI,CLDR", System.getProperty("java.locale.providers"),
29 "This test must be launched with -Djava.locale.providers=SPI,CLDR");
30 try (InputStream in = I18n.class.getResourceAsStream("/META-INF/services/java.text.spi.DecimalFormatSymbolsProvider")) {
31 assertNotNull(in);
32 assertEquals("org.openstreetmap.josm.tools.JosmDecimalFormatSymbolsProvider",
33 new String(in.readAllBytes(), StandardCharsets.UTF_8).trim());
34 }
35 }
36
37 static Stream<Locale> testGroupingSeparator() {
38 System.out.println(Locale.getDefault());
39
40 assertTrue(I18n.getAvailableTranslations().count() > 10);
41 return Stream.concat(
42 I18n.getAvailableTranslations(),
43 Stream.concat(Stream.of("", "AU", "IE", "US", "UK").map(country -> new Locale("en", country, "")),
44 Stream.of("", "AT", "CH", "DE").map(country -> new Locale("de", country, ""))
45 ));
46 }
47
48 @ParameterizedTest
49 @MethodSource
50 void testGroupingSeparator(Locale locale) {
51 final String formattedNumber = DecimalFormat.getInstance(locale).format(123_456);
52 // Note: If you have to add another numeral system, please indicate the name and the locale(s) it is for.
53 if (formattedNumber.startsWith("1")) {
54 // Western Arabic (for most locales)
55 assertEquals("123\u202F456", formattedNumber, locale.toString() + ": " + locale.getDisplayName());
56 } else if (formattedNumber.startsWith("١")) {
57 // Eastern Arabic (for Arabic locale)
58 assertEquals("١٢٣\u202F٤٥٦", formattedNumber, locale.toString() + ": " + locale.getDisplayName());
59 } else if (formattedNumber.startsWith("۱")) {
60 // Urdu (for Persian locale)
61 assertEquals("۱۲۳\u202F۴۵۶", formattedNumber, locale.toString() + ": " + locale.getDisplayName());
62 } else if (formattedNumber.startsWith("१")) {
63 // Devanagari (for Marathi locale)
64 assertEquals("१२३\u202F४५६", formattedNumber, locale.toString() + ": " + locale.getDisplayName());
65 } else {
66 fail(locale.toString() + " (" + locale.getDisplayName() + "): " + formattedNumber);
67 }
68 }
69
70 /**
71 * Test {@link JosmDecimalFormatSymbolsProvider#parseDouble}.
72 */
73 @Test
74 void testParseDouble() {
75 final Locale defLocale = Locale.getDefault();
76 try {
77 Locale.setDefault(Locale.ENGLISH);
78 assertEquals(0.3, JosmDecimalFormatSymbolsProvider.parseDouble("0.3"), 1e-7);
79 assertEquals(0.3, JosmDecimalFormatSymbolsProvider.parseDouble("0,3"), 1e-7);
80 Locale.setDefault(Locale.FRENCH);
81 assertEquals(0.3, JosmDecimalFormatSymbolsProvider.parseDouble("0.3"), 1e-7);
82 assertEquals(0.3, JosmDecimalFormatSymbolsProvider.parseDouble("0,3"), 1e-7);
83 } finally {
84 Locale.setDefault(defLocale);
85 }
86 }
87}
Note: See TracBrowser for help on using the repository browser.