Debian Codegraph @

src/test/java/ezvcard/issue/Issue113.java

raw symbols SCIP
1 package ezvcard.issue;
2
3 import ezvcard.Ezvcard;
4 import ezvcard.VCard;
5 import ezvcard.VCardVersion;
6 import ezvcard.property.Geo;
7 import ezvcard.util.DefaultLocaleRule;
8
9 import static org.junit.Assert.assertEquals;
10
11 import org.junit.Rule;
12 import org.junit.Test;
13
14 import java.text.DateFormat;
15 import java.text.DecimalFormat;
16 import java.text.DecimalFormatSymbols;
17 import java.text.NumberFormat;
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
20 import java.util.Locale;
21
22 /**
23 * @author Michael Angstadt
24 * @see "https://github.com/mangstadt/ez-vcard/issues/113"
25 */
26 public class Issue113 {
27 private final Locale nonWesternLocale = new Locale("fa", "ir", "u-un-arabext");
28
29 @Rule
30 public final DefaultLocaleRule defaultLocaleRule = new DefaultLocaleRule(nonWesternLocale);
31
32 @Test
33 public void submitter_unit_test() {
34 VCard vcard = new VCard(VCardVersion.V4_0);
35 vcard.setGeo(new Geo(1.0, 2.0));
36
37 String actual = Ezvcard.write(vcard).prodId(false).go();
38 String expected = //@formatter:off
39 "BEGIN:VCARD\r\n" +
40 "VERSION:4.0\r\n" +
41 "GEO:geo:1.0,2.0\r\n" +
42 "END:VCARD\r\n"; //@formatter:on
43 assertEquals(expected, actual);
44 }
45
46 /**
47 * Will Java parse Western numerals if the default locale is non-Western?
48 */
49 @Test
50 public void parseDouble_other_locales() {
51 for (Locale locale : Locale.getAvailableLocales()) {
52 Locale.setDefault(locale);
53 double actual = Double.parseDouble("1.0"); //ez-vcard uses this method when parsing numbers
54 double expected = 1.0;
55 assertEquals(expected, actual, 0);
56 }
57 }
58
59 @Test
60 public void root_locale_numbers() {
61 double number = 1.23;
62 assertEquals("۱٫۲۳۰۰۰۰", String.format("%f", number));
63 assertEquals("1.230000", String.format(Locale.ROOT, "%f", number));
64 }
65
66 @Test
67 public void root_locale_dates() throws Exception {
68 String pattern = "yyyy-MM-dd HH:mm";
69 Date date = new SimpleDateFormat(pattern).parse("2020-10-28 12:00");
70
71 DateFormat df = new SimpleDateFormat(pattern);
72 assertEquals("۲۰۲۰-۱۰-۲۸ ۱۲:۰۰", df.format(date));
73
74 df = new SimpleDateFormat(pattern, Locale.ROOT);
75 assertEquals("2020-10-28 12:00", df.format(date));
76 }
77
78 @Test
79 public void decimal_format() {
80 NumberFormat nf = new DecimalFormat("00");
81 assertEquals("۰۸", nf.format(8));
82
83 nf = new DecimalFormat("00", DecimalFormatSymbols.getInstance(Locale.ROOT));
84 assertEquals("08", nf.format(8));
85 }
86 }