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());
 107             }
 108 
 109             sb.setLength(0);
 110             ZonedDateTime zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, zid);
 111             f.format(fmt, zdt);
 112 
 113             if (!sb.toString().equals(expected)) {
 114                 throw new RuntimeException(
 115                     "Localized text returned with null locale.\n" +
 116                     "    expected: " + expected + "\n" +
 117                     "    returned: " + sb.toString());
 118             }
 119         } finally {
 120             Locale.setDefault(orig);
 121         }
 122     }
 123 
 124     public static void main(String [] args) {
 125         formatLocaleTest();
 126         nullLocaleTest();
 127     }
 128 }