Path: blob/master/test/jdk/java/util/Locale/InternationalBAT.java
41149 views
/*1* Copyright (c) 2007, 2016, 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 4449637 800857725* @summary Basic acceptance test for international J2RE. Verifies that the26* most important locale data and character converters exist and are27* minimally functional.28* @modules jdk.localedata29* jdk.charsets30* @run main/othervm -Djava.locale.providers=JRE,SPI InternationalBAT31*/3233import java.io.UnsupportedEncodingException;34import java.text.DateFormat;35import java.util.Calendar;36import java.util.Date;37import java.util.Locale;38import java.util.TimeZone;3940public class InternationalBAT {4142public static void main(String[] args) {43boolean pass = true;4445TimeZone tz = TimeZone.getDefault();46try {47pass &= testRequiredLocales();48pass &= testRequiredEncodings();49} finally {50TimeZone.setDefault(tz);51}5253if (!pass) {54System.out.println("\nSome tests failed.\n"55+ "If you installed the US-only J2RE for Windows, "56+ "failures are expected and OK.\n"57+ "If you installed the international J2RE, or any J2SDK, "58+ "or if this occurs on any platform other than Windows, "59+ "please file a bug report.\n"60+ "Unfortunately, this test cannot determine whether you "61+ "installed a US-only J2RE, an international J2RE, or "62+ "a J2SDK.\n");63throw new RuntimeException();64}65}6667// We require the "fully supported locales" for java.util and java.text:68// http://webwork.eng/j2se/1.4/docs/guide/intl/locale.doc.html#util-text6970private static Locale[] requiredLocales = {71new Locale("ar", "SA"),72new Locale("zh", "CN"),73new Locale("zh", "TW"),74new Locale("nl", "NL"),75new Locale("en", "AU"),76new Locale("en", "CA"),77new Locale("en", "GB"),78new Locale("en", "US"),79new Locale("fr", "CA"),80new Locale("fr", "FR"),81new Locale("de", "DE"),82new Locale("iw", "IL"),83new Locale("hi", "IN"),84new Locale("it", "IT"),85new Locale("ja", "JP"),86new Locale("ko", "KR"),87new Locale("pt", "BR"),88new Locale("es", "ES"),89new Locale("sv", "SE"),90new Locale("th", "TH"),91};9293// Date strings for May 10, 2001, for the required locales94private static String[] requiredLocaleDates = {95"10 \u0645\u0627\u064A\u0648, 2001",96"2001\u5E745\u670810\u65E5 \u661F\u671F\u56DB",97"2001\u5E745\u670810\u65E5 \u661F\u671F\u56DB",98"donderdag 10 mei 2001",99"Thursday, 10 May 2001",100"Thursday, May 10, 2001",101"Thursday, 10 May 2001",102"Thursday, May 10, 2001",103"jeudi 10 mai 2001",104"jeudi 10 mai 2001",105"Donnerstag, 10. Mai 2001",106"\u05D9\u05D5\u05DD \u05D7\u05DE\u05D9\u05E9\u05D9 10 \u05DE\u05D0\u05D9 2001",107"\u0917\u0941\u0930\u0941\u0935\u093E\u0930, \u0967\u0966 \u092E\u0908, \u0968\u0966\u0966\u0967",108"gioved\u00EC 10 maggio 2001",109"2001\u5E745\u670810\u65E5", // ja_JP110"2001\uB144 5\uC6D4 10\uC77C \uBAA9\uC694\uC77C",111"Quinta-feira, 10 de Maio de 2001",112"jueves 10 de mayo de 2001",113"den 10 maj 2001",114"\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",115};116117private static boolean testRequiredLocales() {118boolean pass = true;119120TimeZone.setDefault(TimeZone.getTimeZone("GMT"));121Calendar calendar = Calendar.getInstance(Locale.US);122calendar.clear();123calendar.set(2001, 4, 10, 12, 0, 0);124Date date = calendar.getTime();125126Locale[] available = Locale.getAvailableLocales();127for (int i = 0; i < requiredLocales.length; i++) {128Locale locale = requiredLocales[i];129boolean found = false;130for (int j = 0; j < available.length; j++) {131if (available[j].equals(locale)) {132found = true;133break;134}135}136if (!found) {137System.out.println("Locale not available: " + locale);138pass = false;139} else {140DateFormat format =141DateFormat.getDateInstance(DateFormat.FULL, locale);142String dateString = format.format(date);143if (!dateString.equals(requiredLocaleDates[i])) {144System.out.println("Incorrect date string for locale "145+ locale + ". Expected: " + requiredLocaleDates[i]146+ ", got: " + dateString);147pass = false;148}149}150}151return pass;152}153154// We require the encodings of the fully supported writing systems:155// http://webwork.eng/j2se/1.4/docs/guide/intl/locale.doc.html#jfc156157private static String[] requiredEncodings = {158"Cp1256",159"MS936",160"MS950",161"Cp1255",162"MS932",163"MS949",164"Cp1252",165"MS874",166"ISO8859_6",167"EUC_CN",168"UTF8",169"GBK",170"EUC_TW",171"ISO8859_8",172"EUC_JP",173"PCK",174"EUC_KR",175"ISO8859_1",176"ISO8859_15",177"TIS620",178};179180// one sample locale each for the required encodings181182private static Locale[] sampleLocales = {183new Locale("ar", "SA"),184new Locale("zh", "CN"),185new Locale("zh", "TW"),186new Locale("iw", "IL"),187new Locale("ja", "JP"),188new Locale("ko", "KR"),189new Locale("it", "IT"),190new Locale("th", "TH"),191new Locale("ar", "SA"),192new Locale("zh", "CN"),193new Locale("zh", "CN"),194new Locale("zh", "CN"),195new Locale("zh", "TW"),196new Locale("iw", "IL"),197new Locale("ja", "JP"),198new Locale("ja", "JP"),199new Locale("ko", "KR"),200new Locale("it", "IT"),201new Locale("it", "IT"),202new Locale("th", "TH"),203};204205// expected conversion results for the date strings of the sample locales206207private static byte[][] expectedBytes = {208{ 0x31, 0x30, 0x20, (byte) 0xE3, (byte) 0xC7, (byte) 0xED, (byte) 0xE6, 0x2C, 0x20, 0x32, 0x30, 0x30, 0x31, },209{ 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},210{ 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},211{ (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, },212{ 0x32, 0x30, 0x30, 0x31, (byte) 0x94, 0x4E, 0x35, (byte) 0x8C, (byte) 0x8E, 0x31, 0x30, (byte) 0x93, (byte) 0xFA, },213{ 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, },214{ 0x67, 0x69, 0x6F, 0x76, 0x65, 0x64, (byte) 0xEC, 0x20, 0x31, 0x30, 0x20, 0x6D, 0x61, 0x67, 0x67, 0x69, 0x6F, 0x20, 0x32, 0x30, 0x30, 0x31, },215{ (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, },216{ 0x31, 0x30, 0x20, (byte) 0xE5, (byte) 0xC7, (byte) 0xEA, (byte) 0xE8, 0x2C, 0x20, 0x32, 0x30, 0x30, 0x31, },217{ 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},218{ 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},219{ 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},220{ 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},221{ (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, },222{ 0x32, 0x30, 0x30, 0x31, (byte) 0xC7, (byte) 0xAF, 0x35, (byte) 0xB7, (byte) 0xEE, 0x31, 0x30, (byte) 0xC6, (byte) 0xFC, },223{ 0x32, 0x30, 0x30, 0x31, (byte) 0x94, 0x4E, 0x35, (byte) 0x8C, (byte) 0x8E, 0x31, 0x30, (byte) 0x93, (byte) 0xFA, },224{ 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, },225{ 0x67, 0x69, 0x6F, 0x76, 0x65, 0x64, (byte) 0xEC, 0x20, 0x31, 0x30, 0x20, 0x6D, 0x61, 0x67, 0x67, 0x69, 0x6F, 0x20, 0x32, 0x30, 0x30, 0x31, },226{ 0x67, 0x69, 0x6F, 0x76, 0x65, 0x64, (byte) 0xEC, 0x20, 0x31, 0x30, 0x20, 0x6D, 0x61, 0x67, 0x67, 0x69, 0x6F, 0x20, 0x32, 0x30, 0x30, 0x31, },227{ (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, },228};229230231private static boolean testRequiredEncodings() {232boolean pass = true;233234for (int i = 0; i < requiredEncodings.length; i++) {235String encoding = requiredEncodings[i];236Locale sampleLocale = sampleLocales[i];237try {238int index = 0;239while (!sampleLocale.equals(requiredLocales[index])) {240index++;241}242byte[] out = requiredLocaleDates[index].getBytes(encoding);243byte[] expected = expectedBytes[i];244if (out.length != expected.length) {245reportConversionError(encoding, expected, out);246pass = false;247} else {248for (int j = 0; j < out.length; j++) {249if (out[j] != expected[j]) {250reportConversionError(encoding, expected, out);251pass = false;252break;253}254}255}256} catch (UnsupportedEncodingException e) {257System.out.println("Encoding not available: " + encoding);258pass = false;259}260}261return pass;262}263264private static void reportConversionError(String encoding,265byte[] expected, byte[] actual) {266267System.out.println("Incorrect conversion for encoding: " + encoding);268System.out.println("Expected output:");269dumpBytes(expected);270System.out.println("Actual output:");271dumpBytes(actual);272}273274private static void dumpBytes(byte[] bytes) {275System.out.print(" { ");276for (int i = 0; i < bytes.length; i++) {277byte b = bytes[i];278if (b < 0) {279System.out.print("(byte) ");280}281System.out.print("0x" + toHex((b & 0x00F0) >> 4)282+ toHex((b & 0x000F)) + ", ");283}284System.out.println("},");285}286287private static char toHex(int i) {288if (i <= 9) {289return (char) ('0' + i);290} else {291return (char) ('A' + i - 10);292}293}294}295296297