1 /*
  2  * Copyright (c) 2007, 2023, 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 4184873
 27  * @summary test that locale invariants are preserved across serialization.
 28  * @run junit LegacyCodesClassInvariant
 29  */
 30 
 31 /*
 32  * This file is available under and governed by the GNU General Public
 33  * License version 2 only, as published by the Free Software Foundation.
 34  * However, the following notice accompanied the original version of this
 35  * file and, per its terms, should not be removed:
 36  *
 37  * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
 38  *
 39  * Portions copyright (c) 2007 Sun Microsystems, Inc.
 40  * All Rights Reserved.
 41  *
 42  * The original version of this source code and documentation
 43  * is copyrighted and owned by Taligent, Inc., a wholly-owned
 44  * subsidiary of IBM. These materials are provided under terms
 45  * of a License Agreement between Taligent and Sun. This technology
 46  * is protected by multiple US and International patents.
 47  *
 48  * This notice and attribution to Taligent may not be removed.
 49  * Taligent is a registered trademark of Taligent, Inc.
 50  *
 51  * Permission to use, copy, modify, and distribute this software
 52  * and its documentation for NON-COMMERCIAL purposes and without
 53  * fee is hereby granted provided that this copyright notice
 54  * appears in all copies. Please refer to the file "copyright.html"
 55  * for further important copyright and licensing information.
 56  *
 57  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 58  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 59  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 60  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 61  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 62  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 63  */
 64 
 65 import java.io.File;
 66 import java.io.FileInputStream;
 67 import java.io.ObjectInputStream;
 68 import java.util.Locale;
 69 
 70 import org.junit.jupiter.api.Test;
 71 
 72 import static org.junit.jupiter.api.Assertions.assertEquals;
 73 import static org.junit.jupiter.api.Assertions.fail;
 74 
 75 /**
 76  *  A Locale can never contain the following language codes: he, yi or id.
 77  */
 78 public class LegacyCodesClassInvariant {
 79     @Test
 80     public void testIt() throws Exception {
 81         verify("he");
 82         verify("yi");
 83         verify("id");
 84     }
 85 
 86     private void verify(String lang) {
 87         try {
 88             ObjectInputStream in = getStream(lang);
 89             if (in != null) {
 90                 final Locale loc = (Locale)in.readObject();
 91                 final Locale expected = Locale.of(lang, "XX");
 92                 assertEquals(expected, loc,
 93                         "Locale didn't maintain invariants for: "+lang);
 94                 in.close();
 95             }
 96         } catch (Exception e) {
 97             fail(e.toString());
 98         }
 99     }
100 
101     private ObjectInputStream getStream(String lang) {
102         try {
103             final File f = new File(System.getProperty("test.src", "."), "LegacyCodesClassInvariant_"+lang);
104             return new ObjectInputStream(new FileInputStream(f));
105         } catch (Exception e) {
106             fail(e.toString());
107             return null;
108         }
109     }
110 }