1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
7 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.text.DecimalFormat;
|
---|
13 | import java.util.Locale;
|
---|
14 | import java.util.stream.Stream;
|
---|
15 |
|
---|
16 | import org.junit.jupiter.api.BeforeAll;
|
---|
17 | import org.junit.jupiter.api.Test;
|
---|
18 | import org.junit.jupiter.params.ParameterizedTest;
|
---|
19 | import org.junit.jupiter.params.provider.MethodSource;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Unit tests of {@link JosmDecimalFormatSymbolsProvider}.
|
---|
23 | */
|
---|
24 | @org.openstreetmap.josm.testutils.annotations.I18n
|
---|
25 | class 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 | }
|
---|