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.assertTrue;
|
---|
6 | import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
---|
7 |
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.nio.charset.StandardCharsets;
|
---|
11 | import java.text.DecimalFormat;
|
---|
12 | import java.util.Locale;
|
---|
13 | import java.util.stream.Stream;
|
---|
14 |
|
---|
15 | import org.junit.jupiter.api.BeforeAll;
|
---|
16 | import org.junit.jupiter.api.Test;
|
---|
17 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
18 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
19 |
|
---|
20 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Unit tests of {@link JosmDecimalFormatSymbolsProvider}.
|
---|
24 | */
|
---|
25 | class JosmDecimalFormatSymbolsProviderTest {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Setup rule.
|
---|
29 | */
|
---|
30 | @RegisterExtension
|
---|
31 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
32 | public JOSMTestRules test = new JOSMTestRules();
|
---|
33 |
|
---|
34 | @BeforeAll
|
---|
35 | static void beforeAll() throws IOException {
|
---|
36 | if (Utils.getJavaVersion() >= 9) {
|
---|
37 | assertEquals("SPI,JRE,CLDR", System.getProperty("java.locale.providers"),
|
---|
38 | "This test must be launched with -Djava.locale.providers=SPI,JRE,CLDR");
|
---|
39 | try (InputStream in = I18n.class.getResourceAsStream("/META-INF/services/java.text.spi.DecimalFormatSymbolsProvider")) {
|
---|
40 | assertEquals("org.openstreetmap.josm.tools.JosmDecimalFormatSymbolsProvider",
|
---|
41 | new String(Utils.readBytesFromStream(in), StandardCharsets.UTF_8).trim());
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Test
|
---|
47 | void testGroupingSeparator() {
|
---|
48 | System.out.println(Locale.getDefault());
|
---|
49 | assumeTrue(Utils.getJavaVersion() >= 9);
|
---|
50 |
|
---|
51 | assertTrue(I18n.getAvailableTranslations().count() > 10);
|
---|
52 | I18n.getAvailableTranslations().forEach(this::checkGroupingSymbol);
|
---|
53 | Stream.of("", "AU", "IE", "US", "UK").map(country -> new Locale("en", country, "")).forEach(this::checkGroupingSymbol);
|
---|
54 | Stream.of("", "AT", "CH", "DE").map(country -> new Locale("de", country, "")).forEach(this::checkGroupingSymbol);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void checkGroupingSymbol(Locale locale) {
|
---|
58 | assertEquals("123\u202F456", DecimalFormat.getInstance(locale).format(123_456), locale.toString());
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Test {@link JosmDecimalFormatSymbolsProvider#parseDouble}.
|
---|
63 | */
|
---|
64 | @Test
|
---|
65 | void testParseDouble() {
|
---|
66 | final Locale defLocale = Locale.getDefault();
|
---|
67 | try {
|
---|
68 | Locale.setDefault(Locale.ENGLISH);
|
---|
69 | assertEquals(0.3, JosmDecimalFormatSymbolsProvider.parseDouble("0.3"), 1e-7);
|
---|
70 | assertEquals(0.3, JosmDecimalFormatSymbolsProvider.parseDouble("0,3"), 1e-7);
|
---|
71 | Locale.setDefault(Locale.FRENCH);
|
---|
72 | assertEquals(0.3, JosmDecimalFormatSymbolsProvider.parseDouble("0.3"), 1e-7);
|
---|
73 | assertEquals(0.3, JosmDecimalFormatSymbolsProvider.parseDouble("0,3"), 1e-7);
|
---|
74 | } finally {
|
---|
75 | Locale.setDefault(defLocale);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|