1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 8146156 8159548 27 * @modules jdk.localedata 28 * @summary test whether uppercasing follows Locale.Category.FORMAT locale. 29 * @run main/othervm FormatLocale 30 */ 31 32 import java.time.LocalDate; 33 import java.time.ZonedDateTime; 34 import java.time.ZoneId; 35 import java.time.Month; 36 import java.util.Calendar; 37 import java.util.Formatter; 38 import java.util.GregorianCalendar; 39 import java.util.List; 40 import java.util.Locale; 41 import java.util.Locale.Category; 42 import java.util.TimeZone; 43 import java.util.stream.IntStream; 44 45 public class FormatLocale { 46 47 static final Locale TURKISH = new Locale("tr"); 48 49 static final List<String> conversions = List.of( 50 "%S", 51 "%S", 52 "%TB", 53 "%G"); 54 static final List<Object> src = List.of( 55 "Turkish", 56 "Turkish", 57 LocalDate.of(2016, Month.APRIL, 1), 58 Float.valueOf(100_000_000)); 59 static final List<Locale> formatLocale = List.of( 60 Locale.ENGLISH, 61 TURKISH, 62 TURKISH, 63 Locale.FRANCE); 64 static final List<String> expected = List.of( 65 "TURKISH", 66 "TURK\u0130SH", 67 "N\u0130SAN", 68 "1,00000E+08"); 69 70 static void formatLocaleTest() { 71 StringBuilder sb = new StringBuilder(); 72 73 IntStream.range(0, src.size()).forEach(i -> { 74 sb.setLength(0); 75 Locale.setDefault(Locale.Category.FORMAT, formatLocale.get(i)); 76 new Formatter(sb).format(conversions.get(i), src.get(i)); 77 if (!sb.toString().equals(expected.get(i))) { 78 throw new RuntimeException( 79 "Wrong uppercasing with Formatter.format(" + 80 "\"" + conversions.get(i) + "\"" + 81 ") in locale " 82 + formatLocale.get(i) + 83 ". Expected: " + expected.get(i) + 84 " Returned: " + sb.toString()); 85 } 86 }); 87 } 88 89 static void nullLocaleTest() { 90 String fmt = "%1$ta %1$tA %1$th %1$tB %1tZ"; 91 String expected = "Fri Friday Jan January PST"; 92 StringBuilder sb = new StringBuilder(); 93 Locale orig = Locale.getDefault(); 94 95 try { 96 Locale.setDefault(Locale.JAPAN); 97 Formatter f = new Formatter(sb, (Locale)null); 98 ZoneId zid = ZoneId.of("America/Los_Angeles"); 99 Calendar c = new GregorianCalendar(TimeZone.getTimeZone(zid), Locale.US); 100 c.set(2016, 0, 1, 0, 0, 0); 101 f.format(fmt, c); 102 if (!sb.toString().equals(expected)) { 103 throw new RuntimeException( 104 "Localized text returned with null locale.\n" + 105 " expected: " + expected + "\n" + 106 " returned: " + sb.toString()); | 1 /* 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 8146156 8159548 8060094 27 * @modules jdk.localedata 28 * @summary test whether uppercasing follows Locale.Category.FORMAT locale. 29 * Also test whether the uppercasing uses the locale specified to the 30 * Formatter API. 31 * 32 * @run main/othervm FormatLocale 33 */ 34 35 import java.time.LocalDate; 36 import java.time.ZonedDateTime; 37 import java.time.ZoneId; 38 import java.time.Month; 39 import java.util.Calendar; 40 import java.util.Formatter; 41 import java.util.GregorianCalendar; 42 import java.util.List; 43 import java.util.Locale; 44 import java.util.TimeZone; 45 import java.util.stream.IntStream; 46 47 public class FormatLocale { 48 49 static final Locale TURKISH = new Locale("tr"); 50 51 static final List<String> conversions = List.of( 52 "%S", 53 "%S", 54 "%TB", 55 "%G", 56 "%C"); 57 58 static final List<Object> src = List.of( 59 "Turkish", 60 "Turkish", 61 LocalDate.of(2016, Month.APRIL, 1), 62 Float.valueOf(100_000_000), 63 'i'); 64 65 static final List<Locale> defaultLocale = List.of( 66 Locale.ENGLISH, 67 TURKISH, 68 TURKISH, 69 Locale.FRANCE, 70 TURKISH); 71 72 static final List<Locale> formatLocale = List.of( 73 TURKISH, 74 Locale.ENGLISH, 75 Locale.FRANCE, 76 Locale.ENGLISH, 77 Locale.ENGLISH); 78 79 static final List<String> expectedWithDefaultLocale = List.of( 80 "TURKISH", 81 "TURK\u0130SH", 82 "N\u0130SAN", 83 "1,00000E+08", 84 "\u0130"); 85 86 static final List<String> expectedWithFormatLocale = List.of( 87 "TURK\u0130SH", 88 "TURKISH", 89 "AVRIL", 90 "1.00000E+08", 91 "I"); 92 93 static void formatLocaleTest() { 94 StringBuilder sb = new StringBuilder(); 95 96 // checks whether upper casing follows Locale.Category.FORMAT locale 97 IntStream.range(0, src.size()).forEach(i -> { 98 sb.setLength(0); 99 Locale.setDefault(Locale.Category.FORMAT, defaultLocale.get(i)); 100 new Formatter(sb).format(conversions.get(i), src.get(i)); 101 if (!sb.toString().equals(expectedWithDefaultLocale.get(i))) { 102 throw new RuntimeException( 103 "Wrong uppercasing while using Formatter.format(" + 104 "\"" + conversions.get(i) + "\"" + 105 ") with the default locale: '" 106 + defaultLocale.get(i) + 107 "'. Expected: " + expectedWithDefaultLocale.get(i) + 108 " Returned: " + sb.toString()); 109 } 110 }); 111 112 // checks whether upper casing uses the locale set during creation of 113 // Formatter instance, instead of the default locale 114 IntStream.range(0, src.size()).forEach(i -> { 115 sb.setLength(0); 116 Locale.setDefault(Locale.Category.FORMAT, defaultLocale.get(i)); 117 new Formatter(sb, formatLocale.get(i)).format(conversions.get(i), 118 src.get(i)); 119 if (!sb.toString().equals(expectedWithFormatLocale.get(i))) { 120 throw new RuntimeException( 121 "Wrong uppercasing while using Formatter.format(" + 122 "\"" + conversions.get(i) + "\"" + 123 ") with the locale specified during instance" + 124 " creation: '" + formatLocale.get(i) + 125 "'. Expected: " + expectedWithFormatLocale.get(i) + 126 " Returned: " + sb.toString()); 127 } 128 }); 129 130 } 131 132 static void nullLocaleTest() { 133 String fmt = "%1$ta %1$tA %1$th %1$tB %1tZ"; 134 String expected = "Fri Friday Jan January PST"; 135 StringBuilder sb = new StringBuilder(); 136 Locale orig = Locale.getDefault(); 137 138 try { 139 Locale.setDefault(Locale.JAPAN); 140 Formatter f = new Formatter(sb, (Locale)null); 141 ZoneId zid = ZoneId.of("America/Los_Angeles"); 142 Calendar c = new GregorianCalendar(TimeZone.getTimeZone(zid), Locale.US); 143 c.set(2016, 0, 1, 0, 0, 0); 144 f.format(fmt, c); 145 if (!sb.toString().equals(expected)) { 146 throw new RuntimeException( 147 "Localized text returned with null locale.\n" + 148 " expected: " + expected + "\n" + 149 " returned: " + sb.toString()); |