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());
 150             }
 151 
 152             sb.setLength(0);
 153             ZonedDateTime zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, zid);
 154             f.format(fmt, zdt);
 155 
 156             if (!sb.toString().equals(expected)) {
 157                 throw new RuntimeException(
 158                     "Localized text returned with null locale.\n" +
 159                     "    expected: " + expected + "\n" +
 160                     "    returned: " + sb.toString());
 161             }
 162         } finally {
 163             Locale.setDefault(orig);
 164         }
 165     }
 166 
 167     public static void main(String [] args) {
 168         formatLocaleTest();
 169         nullLocaleTest();
 170     }
 171 }