21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /** |
24 /** |
25 * @test |
25 * @test |
26 * @bug 8146156 |
26 * @bug 8146156 8159548 |
27 * @summary test whether uppercasing follows Locale.Category.FORMAT locale. |
27 * @summary test whether uppercasing follows Locale.Category.FORMAT locale. |
28 * @run main/othervm FormatLocale |
28 * @run main/othervm FormatLocale |
29 */ |
29 */ |
30 |
30 |
31 import java.time.LocalDate; |
31 import java.time.LocalDate; |
|
32 import java.time.ZonedDateTime; |
|
33 import java.time.ZoneId; |
32 import java.time.Month; |
34 import java.time.Month; |
|
35 import java.util.Calendar; |
33 import java.util.Formatter; |
36 import java.util.Formatter; |
|
37 import java.util.GregorianCalendar; |
34 import java.util.List; |
38 import java.util.List; |
35 import java.util.Locale; |
39 import java.util.Locale; |
36 import java.util.Locale.Category; |
40 import java.util.Locale.Category; |
|
41 import java.util.TimeZone; |
37 import java.util.stream.IntStream; |
42 import java.util.stream.IntStream; |
38 |
43 |
39 public class FormatLocale { |
44 public class FormatLocale { |
40 |
45 |
41 static final Locale TURKISH = new Locale("tr"); |
46 static final Locale TURKISH = new Locale("tr"); |
59 "TURKISH", |
64 "TURKISH", |
60 "TURK\u0130SH", |
65 "TURK\u0130SH", |
61 "N\u0130SAN", |
66 "N\u0130SAN", |
62 "1,00000E+08"); |
67 "1,00000E+08"); |
63 |
68 |
64 public static void main(String [] args) { |
69 static void formatLocaleTest() { |
65 StringBuilder sb = new StringBuilder(); |
70 StringBuilder sb = new StringBuilder(); |
66 |
71 |
67 IntStream.range(0, src.size()).forEach(i -> { |
72 IntStream.range(0, src.size()).forEach(i -> { |
68 sb.setLength(0); |
73 sb.setLength(0); |
69 Locale.setDefault(Locale.Category.FORMAT, formatLocale.get(i)); |
74 Locale.setDefault(Locale.Category.FORMAT, formatLocale.get(i)); |
77 ". Expected: " + expected.get(i) + |
82 ". Expected: " + expected.get(i) + |
78 " Returned: " + sb.toString()); |
83 " Returned: " + sb.toString()); |
79 } |
84 } |
80 }); |
85 }); |
81 } |
86 } |
|
87 |
|
88 static void nullLocaleTest() { |
|
89 String fmt = "%1$ta %1$tA %1$th %1$tB %1tZ"; |
|
90 String expected = "Fri Friday Jan January PST"; |
|
91 StringBuilder sb = new StringBuilder(); |
|
92 Locale orig = Locale.getDefault(); |
|
93 |
|
94 try { |
|
95 Locale.setDefault(Locale.JAPAN); |
|
96 Formatter f = new Formatter(sb, (Locale)null); |
|
97 ZoneId zid = ZoneId.of("America/Los_Angeles"); |
|
98 Calendar c = new GregorianCalendar(TimeZone.getTimeZone(zid), Locale.US); |
|
99 c.set(2016, 0, 1, 0, 0, 0); |
|
100 f.format(fmt, c); |
|
101 if (!sb.toString().equals(expected)) { |
|
102 throw new RuntimeException( |
|
103 "Localized text returned with null locale.\n" + |
|
104 " expected: " + expected + "\n" + |
|
105 " returned: " + sb.toString()); |
|
106 } |
|
107 |
|
108 sb.setLength(0); |
|
109 ZonedDateTime zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, zid); |
|
110 f.format(fmt, zdt); |
|
111 |
|
112 if (!sb.toString().equals(expected)) { |
|
113 throw new RuntimeException( |
|
114 "Localized text returned with null locale.\n" + |
|
115 " expected: " + expected + "\n" + |
|
116 " returned: " + sb.toString()); |
|
117 } |
|
118 } finally { |
|
119 Locale.setDefault(orig); |
|
120 } |
|
121 } |
|
122 |
|
123 public static void main(String [] args) { |
|
124 formatLocaleTest(); |
|
125 nullLocaleTest(); |
|
126 } |
82 } |
127 } |