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  * This file is available under and governed by the GNU General Public
 26  * License version 2 only, as published by the Free Software Foundation.
 27  * However, the following notice accompanied the original version of this
 28  * file and, per its terms, should not be removed:
 29  *
 30  * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
 31  *
 32  * Portions copyright (c) 2007 Sun Microsystems, Inc.
 33  * All Rights Reserved.
 34  *
 35  * The original version of this source code and documentation
 36  * is copyrighted and owned by Taligent, Inc., a wholly-owned
 37  * subsidiary of IBM. These materials are provided under terms
 38  * of a License Agreement between Taligent and Sun. This technology
 39  * is protected by multiple US and International patents.
 40  *
 41  * This notice and attribution to Taligent may not be removed.
 42  * Taligent is a registered trademark of Taligent, Inc.
 43  *
 44  * Permission to use, copy, modify, and distribute this software
 45  * and its documentation for NON-COMMERCIAL purposes and without
 46  * fee is hereby granted provided that this copyright notice
 47  * appears in all copies. Please refer to the file "copyright.html"
 48  * for further important copyright and licensing information.
 49  *
 50  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 51  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 52  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 53  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 54  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 55  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 56  */
 57 
 58 /*
 59  * @test
 60  * @bug 4185732
 61  * @library /java/text/testlib
 62  * @build HexDumpReader
 63  * @summary Test that ChoiceFormat invariants are preserved across serialization.
 64  *          This test depends on Bug4185732.ser.txt and will fail otherwise.
 65  * @run junit Bug4185732Test
 66  */
 67 
 68 import java.io.InvalidObjectException;
 69 import java.io.ObjectInputStream;
 70 import java.text.ChoiceFormat;
 71 
 72 import org.junit.jupiter.api.Test;
 73 
 74 import static org.junit.jupiter.api.Assertions.fail;
 75 
 76 public class Bug4185732Test {
 77 
 78     /*
 79      * The ChoiceFormat class requires that its choiceFormats and choiceLimits
 80      * arrays have the same length. This test ensures that the invariant is enforced
 81      * during the readObject() call.
 82      */
 83     @Test
 84     public void choiceFormatSerializationInvariantsTest() {
 85         try {
 86             // A serialized ChoiceFormat with unequal formats and limits
 87             final ObjectInputStream in
 88                 = new ObjectInputStream(HexDumpReader.getStreamFromHexDump("Bug4185732.ser.txt"));
 89             final ChoiceFormat loc = (ChoiceFormat)in.readObject();
 90             if (loc.getFormats().length != loc.getLimits().length) {
 91                 fail("ChoiceFormat did not properly check stream");
 92             } else {
 93                 // for some reason, the data file was VALID.  This test
 94                 // requires a corrupt data file the format and limit
 95                 // arrays are of different length.
 96                 fail("Test data file was not properly created");
 97             }
 98         } catch (InvalidObjectException expectedException) {
 99             // Expecting an IOE
100         } catch (Exception wrongException) {
101             fail("Expected an InvalidObjectException, instead got: " + wrongException);
102         }
103     }
104 }