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