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             sb.setLength(0);
 112             JavacIntrinsicsSupport.formatterFormat(new Formatter(sb), conversions.get(i), src.get(i));
 113             if (!sb.toString().equals(expectedWithDefaultLocale.get(i))) {
 114                 throw new RuntimeException(
 115                         "Wrong uppercasing while using Formatter.format(" +
 116                                 "\"" + conversions.get(i) + "\"" +
 117                                 ") with the default locale: '"
 118                                 + defaultLocale.get(i) +
 119                                 "'. Expected: " + expectedWithDefaultLocale.get(i) +
 120                                 " Returned: " + sb.toString());
 121             }
 122 
 123         });
 124 
 125         // checks whether upper casing uses the locale set during creation of
 126         // Formatter instance, instead of the default locale
 127         IntStream.range(0, src.size()).forEach(i -> {
 128             sb.setLength(0);
 129             Locale.setDefault(Locale.Category.FORMAT, defaultLocale.get(i));
 130             new Formatter(sb, formatLocale.get(i)).format(conversions.get(i),
 131                     src.get(i));
 132             if (!sb.toString().equals(expectedWithFormatLocale.get(i))) {
 133                 throw new RuntimeException(
 134                         "Wrong uppercasing while using Formatter.format(" +
 135                                 "\"" + conversions.get(i) + "\"" +
 136                                 ") with the locale specified during instance" +
 137                                 " creation: '" + formatLocale.get(i) +
 138                                 "'. Expected: " + expectedWithFormatLocale.get(i) +
 139                                 " Returned: " + sb.toString());
 140             }
 141 
 142             sb.setLength(0);
 143             JavacIntrinsicsSupport.formatterFormat(new Formatter(sb, formatLocale.get(i)), conversions.get(i), src.get(i));
 144             if (!sb.toString().equals(expectedWithFormatLocale.get(i))) {
 145                 throw new RuntimeException(
 146                         "Wrong uppercasing while using Formatter.format(" +
 147                                 "\"" + conversions.get(i) + "\"" +
 148                                 ") with the locale specified during instance" +
 149                                 " creation: '" + formatLocale.get(i) +
 150                                 "'. Expected: " + expectedWithFormatLocale.get(i) +
 151                                 " Returned: " + sb.toString());
 152             }
 153         });
 154 
 155     }
 156 
 157     static void nullLocaleTest() {
 158         String fmt = "%1$ta %1$tA %1$th %1$tB %1tZ";
 159         String expected = "Fri Friday Jan January PST";
 160         StringBuilder sb = new StringBuilder();
 161         Locale orig = Locale.getDefault();
 162 
 163         try {
 164             Locale.setDefault(Locale.JAPAN);
 165             Formatter f = new Formatter(sb, (Locale)null);
 166             ZoneId zid = ZoneId.of("America/Los_Angeles");
 167             Calendar c = new GregorianCalendar(TimeZone.getTimeZone(zid), Locale.US);
 168             c.set(2016, 0, 1, 0, 0, 0);
 169             f.format(fmt, c);
 170             if (!sb.toString().equals(expected)) {
 171                 throw new RuntimeException(
 172                     "Localized text returned with null locale.\n" +
 173                     "    expected: " + expected + "\n" +
 174                     "    returned: " + sb.toString());
 175             }
 176 
 177             sb.setLength(0);
 178             ZonedDateTime zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, zid);
 179             f.format(fmt, zdt);
 180 
 181             if (!sb.toString().equals(expected)) {
 182                 throw new RuntimeException(
 183                     "Localized text returned with null locale.\n" +
 184                     "    expected: " + expected + "\n" +
 185                     "    returned: " + sb.toString());
 186             }
 187         } finally {
 188             Locale.setDefault(orig);
 189         }
 190     }
 191 
 192     public static void main(String [] args) {
 193         formatLocaleTest();
 194         nullLocaleTest();
 195     }
 196 }