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