1 /*
   2  * Copyright (c) 2015, 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 8075548
  27  * @summary Make sure that the format form of month names are produced when there are
  28  *          no stand-alone ones available.
  29  */
  30 
  31 import java.text.*;
  32 import java.util.*;
  33 import static java.util.Calendar.*;
  34 
  35 public class Bug8075548 {
  36     static int errors = 0;
  37 
  38     public static void main(String[] args) throws Throwable {
  39         Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.US).parse("2010-09-15");
  40         String[][] FORMAT_PAIRS = {
  41             { "LLLL", "MMMM" },
  42             { "LLL",  "MMM" }
  43         };
  44         Locale[] LOCALES = {
  45             Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN, Locale.JAPANESE
  46         };
  47 
  48         for (Locale locale : LOCALES) {
  49             for (String[] formats : FORMAT_PAIRS) {
  50                 String el = new SimpleDateFormat(formats[0], locale).format(date);
  51                 String em = new SimpleDateFormat(formats[1], locale).format(date);
  52                 if (!el.equals(em)) {
  53                     errors++;
  54                     System.err.println(locale + ": " +
  55                                        formats[0] + " -> " + el + ", " +
  56                                        formats[1] + " -> " + em);
  57                 }
  58             }
  59         }
  60 
  61         // Test Calendar.getDisplayName() and .getDisplayNames().
  62         for (Locale locale : LOCALES) {
  63             testDisplayNames(locale, LONG_FORMAT, LONG_STANDALONE);
  64             testDisplayNames(locale, SHORT_FORMAT, SHORT_STANDALONE);
  65             testDisplayNames(locale, NARROW_FORMAT, NARROW_STANDALONE);
  66         }
  67 
  68         if (errors > 0) {
  69             throw new RuntimeException("Failed");
  70         }
  71     }
  72 
  73     private static void testDisplayNames(Locale locale, int formatStyle, int standaloneStyle) {
  74         Map<String, Integer> map = new HashMap<>();
  75         for (int month = JANUARY; month <= DECEMBER; month++) {
  76             Calendar cal = new GregorianCalendar(2015, month, 1);
  77             String format = cal.getDisplayName(MONTH, formatStyle, locale);
  78             String standalone = cal.getDisplayName(MONTH, standaloneStyle, locale);
  79             if (!format.equals(standalone)) {
  80                 System.err.println("Calendar.getDisplayName: " + (month+1) +
  81                                    ", locale=" + locale +
  82                                    ", format=" + format + ", standalone=" + standalone);
  83                 errors++;
  84             }
  85             if (standalone != null) {
  86                 map.put(standalone, month);
  87             }
  88         }
  89         if (formatStyle == NARROW_FORMAT) {
  90             // Narrow styles don't support unique names.
  91             // (e.g., "J" for JANUARY, JUNE, and JULY)
  92             return;
  93         }
  94         Calendar cal = new GregorianCalendar(2015, JANUARY, 1);
  95         Map<String, Integer> mapStandalone = cal.getDisplayNames(MONTH, standaloneStyle, locale);
  96         if (!map.equals(mapStandalone)) {
  97             System.err.printf("Calendar.getDisplayNames: locale=%s%n    map=%s%n    mapStandalone=%s%n",
  98                               locale, map, mapStandalone);
  99             errors++;
 100         }
 101         Map<String, Integer> mapAll = cal.getDisplayNames(MONTH, ALL_STYLES, locale);
 102         if (!mapAll.entrySet().containsAll(map.entrySet())) {
 103             System.err.printf("Calendar.getDisplayNames: locale=%s%n    map=%s%n    mapAll=%s%n",
 104                               locale, map, mapAll);
 105             errors++;
 106         }
 107     }
 108 }