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