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

Last change on this file since 18051 was 18051, checked in by Don-vip, 3 years ago

upgrade to checkstyle 8.43, fix violations

  • Property svn:eol-style set to native
File size: 3.0 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.assertTrue;
6import static org.junit.jupiter.api.Assumptions.assumeTrue;
7
8import java.io.IOException;
9import java.io.InputStream;
10import java.nio.charset.StandardCharsets;
11import java.text.DecimalFormat;
12import java.util.Locale;
13import java.util.stream.Stream;
14
15import org.junit.jupiter.api.BeforeAll;
16import org.junit.jupiter.api.Test;
17import org.junit.jupiter.api.extension.RegisterExtension;
18import org.openstreetmap.josm.testutils.JOSMTestRules;
19
20import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21
22/**
23 * Unit tests of {@link JosmDecimalFormatSymbolsProvider}.
24 */
25class 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}
Note: See TracBrowser for help on using the repository browser.