Path: blob/master/test/jdk/java/util/Locale/Bug4184873Test.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@summary test that locale invariants are preserved across serialization25@library /java/text/testlib26@run main Bug4184873Test27@bug 418487328*/29/*30*31*32* (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved33*34* Portions copyright (c) 2007 Sun Microsystems, Inc.35* All Rights Reserved.36*37* The original version of this source code and documentation38* is copyrighted and owned by Taligent, Inc., a wholly-owned39* subsidiary of IBM. These materials are provided under terms40* of a License Agreement between Taligent and Sun. This technology41* is protected by multiple US and International patents.42*43* This notice and attribution to Taligent may not be removed.44* Taligent is a registered trademark of Taligent, Inc.45*46* Permission to use, copy, modify, and distribute this software47* and its documentation for NON-COMMERCIAL purposes and without48* fee is hereby granted provided that this copyright notice49* appears in all copies. Please refer to the file "copyright.html"50* for further important copyright and licensing information.51*52* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF53* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED54* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A55* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR56* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR57* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.58*/5960import java.util.*;61import java.io.*;6263/**64* A Locale can never contain the following language codes: he, yi or id.65*/66public class Bug4184873Test extends IntlTest {67public static void main(String[] args) throws Exception {68if (args.length == 1 && args[0].equals("prepTest")) {69prepTest();70} else {71new Bug4184873Test().run(args);72}73}7475public void testIt() throws Exception {76verify("he");77verify("yi");78verify("id");79}8081private void verify(String lang) {82try {83ObjectInputStream in = getStream(lang);84if (in != null) {85final Locale loc = (Locale)in.readObject();86final Locale expected = new Locale(lang, "XX");87if (!(expected.equals(loc))) {88errln("Locale didn't maintain invariants for: "+lang);89errln(" got: "+loc);90errln(" excpeted: "+expected);91} else {92logln("Locale "+lang+" worked");93}94in.close();95}96} catch (Exception e) {97errln(e.toString());98}99}100101private ObjectInputStream getStream(String lang) {102try {103final File f = new File(System.getProperty("test.src", "."), "Bug4184873_"+lang);104return new ObjectInputStream(new FileInputStream(f));105} catch (Exception e) {106errln(e.toString());107return null;108}109}110111/**112* Create serialized output files of the test locales. After they are created, these test113* files should be corrupted (by hand) to contain invalid locale name values.114*/115private static void prepTest() {116outputLocale("he");117outputLocale("yi");118outputLocale("id");119}120121private static void outputLocale(String lang) {122try {123ObjectOutputStream out = new ObjectOutputStream(124new FileOutputStream("Bug4184873_"+lang));125out.writeObject(new Locale(lang, "XX"));126out.close();127} catch (Exception e) {128System.out.println(e);129}130}131132}133134135