Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Locale/InternationalBAT.java
38813 views
/*1* Copyright (c) 2007, 2013, 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*/22/*23* @test24* @bug 444963725* @summary Basic acceptance test for international J2RE. Verifies that the26* most important locale data and character converters exist and are27* minimally functional.28*/2930import java.io.UnsupportedEncodingException;31import java.text.DateFormat;32import java.util.Calendar;33import java.util.Date;34import java.util.Locale;35import java.util.TimeZone;3637public class InternationalBAT {3839public static void main(String[] args) {40boolean pass = true;4142TimeZone tz = TimeZone.getDefault();43try {44pass &= testRequiredLocales();45pass &= testRequiredEncodings();46} finally {47TimeZone.setDefault(tz);48}4950if (!pass) {51System.out.println("\nSome tests failed.\n"52+ "If you installed the US-only J2RE for Windows, "53+ "failures are expected and OK.\n"54+ "If you installed the international J2RE, or any J2SDK, "55+ "or if this occurs on any platform other than Windows, "56+ "please file a bug report.\n"57+ "Unfortunately, this test cannot determine whether you "58+ "installed a US-only J2RE, an international J2RE, or "59+ "a J2SDK.\n");60throw new RuntimeException();61}62}6364// We require the "fully supported locales" for java.util and java.text:65// http://webwork.eng/j2se/1.4/docs/guide/intl/locale.doc.html#util-text6667private static Locale[] requiredLocales = {68new Locale("ar", "SA"),69new Locale("zh", "CN"),70new Locale("zh", "TW"),71new Locale("nl", "NL"),72new Locale("en", "AU"),73new Locale("en", "CA"),74new Locale("en", "GB"),75new Locale("en", "US"),76new Locale("fr", "CA"),77new Locale("fr", "FR"),78new Locale("de", "DE"),79new Locale("iw", "IL"),80new Locale("hi", "IN"),81new Locale("it", "IT"),82new Locale("ja", "JP"),83new Locale("ko", "KR"),84new Locale("pt", "BR"),85new Locale("es", "ES"),86new Locale("sv", "SE"),87new Locale("th", "TH"),88};8990// Date strings for May 10, 2001, for the required locales91private static String[] requiredLocaleDates = {92"10 \u0645\u0627\u064A\u0648, 2001",93"2001\u5E745\u670810\u65E5 \u661F\u671F\u56DB",94"2001\u5E745\u670810\u65E5 \u661F\u671F\u56DB",95"donderdag 10 mei 2001",96"Thursday, 10 May 2001",97"Thursday, May 10, 2001",98"Thursday, 10 May 2001",99"Thursday, May 10, 2001",100"jeudi 10 mai 2001",101"jeudi 10 mai 2001",102"Donnerstag, 10. Mai 2001",103"\u05D9\u05D5\u05DD \u05D7\u05DE\u05D9\u05E9\u05D9 10 \u05DE\u05D0\u05D9 2001",104"\u0917\u0941\u0930\u0941\u0935\u093E\u0930, \u0967\u0966 \u092E\u0908, \u0968\u0966\u0966\u0967",105"gioved\u00EC 10 maggio 2001",106"2001\u5E745\u670810\u65E5", // ja_JP107"2001\uB144 5\uC6D4 10\uC77C \uBAA9\uC694\uC77C",108"Quinta-feira, 10 de Maio de 2001",109"jueves 10 de mayo de 2001",110"den 10 maj 2001",111"\u0E27\u0E31\u0E19\u0E1E\u0E24\u0E2B\u0E31\u0E2A\u0E1A\u0E14\u0E35\u0E17\u0E35\u0E48 10 \u0E1E\u0E24\u0E29\u0E20\u0E32\u0E04\u0E21 \u0E1E.\u0E28. 2544",112};113114private static boolean testRequiredLocales() {115boolean pass = true;116117TimeZone.setDefault(TimeZone.getTimeZone("GMT"));118Calendar calendar = Calendar.getInstance(Locale.US);119calendar.clear();120calendar.set(2001, 4, 10, 12, 0, 0);121Date date = calendar.getTime();122123Locale[] available = Locale.getAvailableLocales();124for (int i = 0; i < requiredLocales.length; i++) {125Locale locale = requiredLocales[i];126boolean found = false;127for (int j = 0; j < available.length; j++) {128if (available[j].equals(locale)) {129found = true;130break;131}132}133if (!found) {134System.out.println("Locale not available: " + locale);135pass = false;136} else {137DateFormat format =138DateFormat.getDateInstance(DateFormat.FULL, locale);139String dateString = format.format(date);140if (!dateString.equals(requiredLocaleDates[i])) {141System.out.println("Incorrect date string for locale "142+ locale + ". Expected: " + requiredLocaleDates[i]143+ ", got: " + dateString);144pass = false;145}146}147}148return pass;149}150151// We require the encodings of the fully supported writing systems:152// http://webwork.eng/j2se/1.4/docs/guide/intl/locale.doc.html#jfc153154private static String[] requiredEncodings = {155"Cp1256",156"MS936",157"MS950",158"Cp1255",159"MS932",160"MS949",161"Cp1252",162"MS874",163"ISO8859_6",164"EUC_CN",165"UTF8",166"GBK",167"EUC_TW",168"ISO8859_8",169"EUC_JP",170"PCK",171"EUC_KR",172"ISO8859_1",173"ISO8859_15",174"TIS620",175};176177// one sample locale each for the required encodings178179private static Locale[] sampleLocales = {180new Locale("ar", "SA"),181new Locale("zh", "CN"),182new Locale("zh", "TW"),183new Locale("iw", "IL"),184new Locale("ja", "JP"),185new Locale("ko", "KR"),186new Locale("it", "IT"),187new Locale("th", "TH"),188new Locale("ar", "SA"),189new Locale("zh", "CN"),190new Locale("zh", "CN"),191new Locale("zh", "CN"),192new Locale("zh", "TW"),193new Locale("iw", "IL"),194new Locale("ja", "JP"),195new Locale("ja", "JP"),196new Locale("ko", "KR"),197new Locale("it", "IT"),198new Locale("it", "IT"),199new Locale("th", "TH"),200};201202// expected conversion results for the date strings of the sample locales203204private static byte[][] expectedBytes = {205{ 0x31, 0x30, 0x20, (byte) 0xE3, (byte) 0xC7, (byte) 0xED, (byte) 0xE6, 0x2C, 0x20, 0x32, 0x30, 0x30, 0x31, },206{ 0x32, 0x30, 0x30, 0x31, (byte) 0xC4, (byte) 0xEA, 0x35, (byte) 0xD4, (byte) 0xC2, 0x31, 0x30, (byte) 0xC8, (byte) 0xD5, 0x20, (byte) 0xD0, (byte) 0xC7, (byte) 0xC6, (byte) 0xDA, (byte) 0xCB, (byte) 0xC4},207{ 0x32, 0x30, 0x30, 0x31, (byte) 0xA6, 0x7E, 0x35, (byte) 0xA4, (byte) 0xEB, 0x31, 0x30, (byte) 0xA4, (byte) 0xE9, 0x20, (byte) 0xAC, (byte)0x50, (byte) 0xB4, (byte) 0xC1, (byte) 0xA5, (byte) 0x7C},208{ (byte) 0xE9, (byte) 0xE5, (byte) 0xED, 0x20, (byte) 0xE7, (byte) 0xEE, (byte) 0xE9, (byte) 0xF9, (byte) 0xE9, 0x20, 0x31, 0x30, 0x20, (byte) 0xEE, (byte) 0xE0, (byte) 0xE9, 0x20, 0x32, 0x30, 0x30, 0x31, },209{ 0x32, 0x30, 0x30, 0x31, (byte) 0x94, 0x4E, 0x35, (byte) 0x8C, (byte) 0x8E, 0x31, 0x30, (byte) 0x93, (byte) 0xFA, },210{ 0x32, 0x30, 0x30, 0x31, (byte) 0xB3, (byte) 0xE2, 0x20, 0x35, (byte) 0xBF, (byte) 0xF9, 0x20, 0x31, 0x30, (byte) 0xC0, (byte) 0xCF, 0x20, (byte) 0xB8, (byte) 0xF1, (byte) 0xBF, (byte) 0xE4, (byte) 0xC0, (byte) 0xCF, },211{ 0x67, 0x69, 0x6F, 0x76, 0x65, 0x64, (byte) 0xEC, 0x20, 0x31, 0x30, 0x20, 0x6D, 0x61, 0x67, 0x67, 0x69, 0x6F, 0x20, 0x32, 0x30, 0x30, 0x31, },212{ (byte) 0xC7, (byte) 0xD1, (byte) 0xB9, (byte) 0xBE, (byte) 0xC4, (byte) 0xCB, (byte) 0xD1, (byte) 0xCA, (byte) 0xBA, (byte) 0xB4, (byte) 0xD5, (byte) 0xB7, (byte) 0xD5, (byte) 0xE8, 0x20, 0x31, 0x30, 0x20, (byte) 0xBE, (byte) 0xC4, (byte) 0xC9, (byte) 0xC0, (byte) 0xD2, (byte) 0xA4, (byte) 0xC1, 0x20, (byte) 0xBE, 0x2E, (byte) 0xC8, 0x2E, 0x20, 0x32, 0x35, 0x34, 0x34, },213{ 0x31, 0x30, 0x20, (byte) 0xE5, (byte) 0xC7, (byte) 0xEA, (byte) 0xE8, 0x2C, 0x20, 0x32, 0x30, 0x30, 0x31, },214{ 0x32, 0x30, 0x30, 0x31, (byte) 0xC4, (byte) 0xEA, 0x35, (byte) 0xD4, (byte) 0xC2, 0x31, 0x30, (byte) 0xC8, (byte) 0xD5, 0x20, (byte) 0xD0, (byte) 0xC7, (byte) 0xC6, (byte) 0xDA, (byte) 0xCB, (byte) 0xC4},215{ 0x32, 0x30, 0x30, 0x31, (byte) 0xE5, (byte) 0xB9, (byte) 0xB4, 0x35, (byte) 0xE6, (byte) 0x9C, (byte) 0x88, 0x31, 0x30, (byte) 0xE6, (byte) 0x97, (byte) 0xA5, 0x20, (byte) 0xE6, (byte)0x98, (byte) 0x9F, (byte) 0xE6, (byte) 0x9C, (byte) 0x9F, (byte) 0xE5, (byte) 0x9B, (byte) 0x9B},216{ 0x32, 0x30, 0x30, 0x31, (byte) 0xC4, (byte) 0xEA, 0x35, (byte) 0xD4, (byte) 0xC2, 0x31, 0x30, (byte) 0xC8, (byte) 0xD5, 0x20, (byte) 0xD0, (byte) 0xC7, (byte) 0xC6, (byte) 0xDA, (byte) 0xCB, (byte) 0xC4},217{ 0x32, 0x30, 0x30, 0x31, (byte) 0xC8, (byte) 0xA1, 0x35, (byte) 0xC5, (byte) 0xCC, 0x31, 0x30, (byte) 0xC5, (byte) 0xCA, 0x20, (byte) 0xD1, (byte) 0xD3, (byte) 0xDF, (byte) 0xE6, (byte) 0xC6, (byte) 0xBE},218{ (byte) 0xE9, (byte) 0xE5, (byte) 0xED, 0x20, (byte) 0xE7, (byte) 0xEE, (byte) 0xE9, (byte) 0xF9, (byte) 0xE9, 0x20, 0x31, 0x30, 0x20, (byte) 0xEE, (byte) 0xE0, (byte) 0xE9, 0x20, 0x32, 0x30, 0x30, 0x31, },219{ 0x32, 0x30, 0x30, 0x31, (byte) 0xC7, (byte) 0xAF, 0x35, (byte) 0xB7, (byte) 0xEE, 0x31, 0x30, (byte) 0xC6, (byte) 0xFC, },220{ 0x32, 0x30, 0x30, 0x31, (byte) 0x94, 0x4E, 0x35, (byte) 0x8C, (byte) 0x8E, 0x31, 0x30, (byte) 0x93, (byte) 0xFA, },221{ 0x32, 0x30, 0x30, 0x31, (byte) 0xB3, (byte) 0xE2, 0x20, 0x35, (byte) 0xBF, (byte) 0xF9, 0x20, 0x31, 0x30, (byte) 0xC0, (byte) 0xCF, 0x20, (byte) 0xB8, (byte) 0xF1, (byte) 0xBF, (byte) 0xE4, (byte) 0xC0, (byte) 0xCF, },222{ 0x67, 0x69, 0x6F, 0x76, 0x65, 0x64, (byte) 0xEC, 0x20, 0x31, 0x30, 0x20, 0x6D, 0x61, 0x67, 0x67, 0x69, 0x6F, 0x20, 0x32, 0x30, 0x30, 0x31, },223{ 0x67, 0x69, 0x6F, 0x76, 0x65, 0x64, (byte) 0xEC, 0x20, 0x31, 0x30, 0x20, 0x6D, 0x61, 0x67, 0x67, 0x69, 0x6F, 0x20, 0x32, 0x30, 0x30, 0x31, },224{ (byte) 0xC7, (byte) 0xD1, (byte) 0xB9, (byte) 0xBE, (byte) 0xC4, (byte) 0xCB, (byte) 0xD1, (byte) 0xCA, (byte) 0xBA, (byte) 0xB4, (byte) 0xD5, (byte) 0xB7, (byte) 0xD5, (byte) 0xE8, 0x20, 0x31, 0x30, 0x20, (byte) 0xBE, (byte) 0xC4, (byte) 0xC9, (byte) 0xC0, (byte) 0xD2, (byte) 0xA4, (byte) 0xC1, 0x20, (byte) 0xBE, 0x2E, (byte) 0xC8, 0x2E, 0x20, 0x32, 0x35, 0x34, 0x34, },225};226227228private static boolean testRequiredEncodings() {229boolean pass = true;230231for (int i = 0; i < requiredEncodings.length; i++) {232String encoding = requiredEncodings[i];233Locale sampleLocale = sampleLocales[i];234try {235int index = 0;236while (!sampleLocale.equals(requiredLocales[index])) {237index++;238}239byte[] out = requiredLocaleDates[index].getBytes(encoding);240byte[] expected = expectedBytes[i];241if (out.length != expected.length) {242reportConversionError(encoding, expected, out);243pass = false;244} else {245for (int j = 0; j < out.length; j++) {246if (out[j] != expected[j]) {247reportConversionError(encoding, expected, out);248pass = false;249break;250}251}252}253} catch (UnsupportedEncodingException e) {254System.out.println("Encoding not available: " + encoding);255pass = false;256}257}258return pass;259}260261private static void reportConversionError(String encoding,262byte[] expected, byte[] actual) {263264System.out.println("Incorrect conversion for encoding: " + encoding);265System.out.println("Expected output:");266dumpBytes(expected);267System.out.println("Actual output:");268dumpBytes(actual);269}270271private static void dumpBytes(byte[] bytes) {272System.out.print(" { ");273for (int i = 0; i < bytes.length; i++) {274byte b = bytes[i];275if (b < 0) {276System.out.print("(byte) ");277}278System.out.print("0x" + toHex((b & 0x00F0) >> 4)279+ toHex((b & 0x000F)) + ", ");280}281System.out.println("},");282}283284private static char toHex(int i) {285if (i <= 9) {286return (char) ('0' + i);287} else {288return (char) ('A' + i - 10);289}290}291}292293294