< prev index next >

test/jdk/java/util/Locale/LegacyCodesClassInvariant.java

Print this page

  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     @bug 4184873
 26     @summary test that locale invariants are preserved across serialization
 27     @library /java/text/testlib
 28     @run main LegacyCodesClassInvariant
 29 */
 30 /*
 31  * This file is available under and governed by the GNU General Public
 32  * License version 2 only, as published by the Free Software Foundation.
 33  * However, the following notice accompanied the original version of this
 34  * file and, per its terms, should not be removed:
 35  *
 36  * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
 37  *
 38  * Portions copyright (c) 2007 Sun Microsystems, Inc.
 39  * All Rights Reserved.
 40  *
 41  * The original version of this source code and documentation
 42  * is copyrighted and owned by Taligent, Inc., a wholly-owned
 43  * subsidiary of IBM. These materials are provided under terms
 44  * of a License Agreement between Taligent and Sun. This technology
 45  * is protected by multiple US and International patents.
 46  *
 47  * This notice and attribution to Taligent may not be removed.
 48  * Taligent is a registered trademark of Taligent, Inc.
 49  *
 50  * Permission to use, copy, modify, and distribute this software
 51  * and its documentation for NON-COMMERCIAL purposes and without
 52  * fee is hereby granted provided that this copyright notice
 53  * appears in all copies. Please refer to the file "copyright.html"
 54  * for further important copyright and licensing information.
 55  *
 56  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 57  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 58  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 59  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 60  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 61  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 62  */
 63 
 64 import java.io.File;
 65 import java.io.FileInputStream;
 66 import java.io.FileOutputStream;
 67 import java.io.ObjectInputStream;
 68 import java.io.ObjectOutputStream;
 69 import java.util.Locale;
 70 





 71 /**
 72  *  A Locale can never contain the following language codes: he, yi or id.
 73  */
 74 public class LegacyCodesClassInvariant extends IntlTest {
 75     public static void main(String[] args) throws Exception {
 76         if (args.length == 1 && args[0].equals("prepTest")) {
 77             prepTest();
 78         } else {
 79             new LegacyCodesClassInvariant().run(args);
 80         }
 81     }
 82 
 83     public void testIt() throws Exception {
 84         verify("he");
 85         verify("yi");
 86         verify("id");
 87     }
 88 
 89     private void verify(String lang) {
 90         try {
 91             ObjectInputStream in = getStream(lang);
 92             if (in != null) {
 93                 final Locale loc = (Locale)in.readObject();
 94                 final Locale expected = Locale.of(lang, "XX");
 95                 if (!(expected.equals(loc))) {
 96                     errln("Locale didn't maintain invariants for: "+lang);
 97                     errln("         got: "+loc);
 98                     errln("    excpeted: "+expected);
 99                 } else {
100                     logln("Locale "+lang+" worked");
101                 }
102                 in.close();
103             }
104         } catch (Exception e) {
105             errln(e.toString());
106         }
107     }
108 
109     private ObjectInputStream getStream(String lang) {
110         try {
111             final File f = new File(System.getProperty("test.src", "."), "LegacyCodesClassInvariant_"+lang);
112             return new ObjectInputStream(new FileInputStream(f));
113         } catch (Exception e) {
114             errln(e.toString());
115             return null;
116         }
117     }
118 
119     /**
120      * Create serialized output files of the test locales.  After they are created, these test
121      * files should be corrupted (by hand) to contain invalid locale name values.
122      */
123     private static void prepTest() {
124         outputLocale("he");
125         outputLocale("yi");
126         outputLocale("id");
127     }
128 
129     private static void outputLocale(String lang) {
130         try {
131             ObjectOutputStream out = new ObjectOutputStream(
132                     new FileOutputStream("LegacyCodesClassInvariant_"+lang));
133             out.writeObject(Locale.of(lang, "XX"));
134             out.close();
135         } catch (Exception e) {
136             System.out.println(e);
137         }
138     }
139 
140 }

  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 }
< prev index next >