Path: blob/master/test/jdk/java/util/Formatter/FormatLocale.java
41149 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 8146156 8159548 806009426* @modules jdk.localedata27* @summary test whether uppercasing follows Locale.Category.FORMAT locale.28* Also test whether the uppercasing uses the locale specified to the29* Formatter API.30*31* @run main/othervm FormatLocale32*/3334import java.time.LocalDate;35import java.time.ZonedDateTime;36import java.time.ZoneId;37import java.time.Month;38import java.util.Calendar;39import java.util.Formatter;40import java.util.GregorianCalendar;41import java.util.List;42import java.util.Locale;43import java.util.TimeZone;44import java.util.stream.IntStream;4546public class FormatLocale {4748static final Locale TURKISH = new Locale("tr");4950static final List<String> conversions = List.of(51"%S",52"%S",53"%TB",54"%G",55"%C");5657static final List<Object> src = List.of(58"Turkish",59"Turkish",60LocalDate.of(2016, Month.APRIL, 1),61Float.valueOf(100_000_000),62'i');6364static final List<Locale> defaultLocale = List.of(65Locale.ENGLISH,66TURKISH,67TURKISH,68Locale.FRANCE,69TURKISH);7071static final List<Locale> formatLocale = List.of(72TURKISH,73Locale.ENGLISH,74Locale.FRANCE,75Locale.ENGLISH,76Locale.ENGLISH);7778static final List<String> expectedWithDefaultLocale = List.of(79"TURKISH",80"TURK\u0130SH",81"N\u0130SAN",82"1,00000E+08",83"\u0130");8485static final List<String> expectedWithFormatLocale = List.of(86"TURK\u0130SH",87"TURKISH",88"AVRIL",89"1.00000E+08",90"I");9192static void formatLocaleTest() {93StringBuilder sb = new StringBuilder();9495// checks whether upper casing follows Locale.Category.FORMAT locale96IntStream.range(0, src.size()).forEach(i -> {97sb.setLength(0);98Locale.setDefault(Locale.Category.FORMAT, defaultLocale.get(i));99new Formatter(sb).format(conversions.get(i), src.get(i));100if (!sb.toString().equals(expectedWithDefaultLocale.get(i))) {101throw new RuntimeException(102"Wrong uppercasing while using Formatter.format(" +103"\"" + conversions.get(i) + "\"" +104") with the default locale: '"105+ defaultLocale.get(i) +106"'. Expected: " + expectedWithDefaultLocale.get(i) +107" Returned: " + sb.toString());108}109});110111// checks whether upper casing uses the locale set during creation of112// Formatter instance, instead of the default locale113IntStream.range(0, src.size()).forEach(i -> {114sb.setLength(0);115Locale.setDefault(Locale.Category.FORMAT, defaultLocale.get(i));116new Formatter(sb, formatLocale.get(i)).format(conversions.get(i),117src.get(i));118if (!sb.toString().equals(expectedWithFormatLocale.get(i))) {119throw new RuntimeException(120"Wrong uppercasing while using Formatter.format(" +121"\"" + conversions.get(i) + "\"" +122") with the locale specified during instance" +123" creation: '" + formatLocale.get(i) +124"'. Expected: " + expectedWithFormatLocale.get(i) +125" Returned: " + sb.toString());126}127});128129}130131static void nullLocaleTest() {132String fmt = "%1$ta %1$tA %1$th %1$tB %1tZ";133String expected = "Fri Friday Jan January PST";134StringBuilder sb = new StringBuilder();135Locale orig = Locale.getDefault();136137try {138Locale.setDefault(Locale.JAPAN);139Formatter f = new Formatter(sb, (Locale)null);140ZoneId zid = ZoneId.of("America/Los_Angeles");141Calendar c = new GregorianCalendar(TimeZone.getTimeZone(zid), Locale.US);142c.set(2016, 0, 1, 0, 0, 0);143f.format(fmt, c);144if (!sb.toString().equals(expected)) {145throw new RuntimeException(146"Localized text returned with null locale.\n" +147" expected: " + expected + "\n" +148" returned: " + sb.toString());149}150151sb.setLength(0);152ZonedDateTime zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, zid);153f.format(fmt, zdt);154155if (!sb.toString().equals(expected)) {156throw new RuntimeException(157"Localized text returned with null locale.\n" +158" expected: " + expected + "\n" +159" returned: " + sb.toString());160}161} finally {162Locale.setDefault(orig);163}164}165166public static void main(String [] args) {167formatLocaleTest();168nullLocaleTest();169}170}171172173