jdk/test/java/util/Locale/Bug4184873Test.java
author mfang
Wed, 17 Aug 2011 14:18:26 -0700
changeset 10294 8fcdae2a7ec7
parent 5506 202f599c92aa
child 42159 9ab10842acf7
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
    @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
    @summary test that locale invariants are preserved across serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
    @run main Bug4184873Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
    @bug 4184873
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Portions copyright (c) 2007 Sun Microsystems, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Permission to use, copy, modify, and distribute this software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * and its documentation for NON-COMMERCIAL purposes and without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * fee is hereby granted provided that this copyright notice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * appears in all copies. Please refer to the file "copyright.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * for further important copyright and licensing information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *  A Locale can never contain the following language codes: he, yi or id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class Bug4184873Test extends LocaleTestFmwk {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (args.length == 1 && args[0].equals("prepTest")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            prepTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            new Bug4184873Test().run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public void testIt() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        verify("he");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        verify("yi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        verify("id");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private void verify(String lang) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            ObjectInputStream in = getStream(lang);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                final Locale loc = (Locale)in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                final Locale expected = new Locale(lang, "XX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                if (!(expected.equals(loc))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    errln("Locale didn't maintain invariants for: "+lang);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    errln("         got: "+loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    errln("    excpeted: "+expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    logln("Locale "+lang+" worked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            errln(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private ObjectInputStream getStream(String lang) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            final File f = new File(System.getProperty("test.src", "."), "Bug4184873_"+lang);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return new ObjectInputStream(new FileInputStream(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            errln(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Create serialized output files of the test locales.  After they are created, these test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * files should be corrupted (by hand) to contain invalid locale name values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static void prepTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        outputLocale("he");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        outputLocale("yi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        outputLocale("id");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static void outputLocale(String lang) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            ObjectOutputStream out = new ObjectOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    new FileOutputStream("Bug4184873_"+lang));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            out.writeObject(new Locale(lang, "XX"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}