< prev index next >

test/jdk/java/text/Format/ChoiceFormat/Bug4185732Test.java

Print this page

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










 68 import java.text.ChoiceFormat;
 69 
 70 /**
 71  *  A Locale can never contains language codes of he, yi or id.
 72  */
 73 public class Bug4185732Test extends IntlTest {
 74     public static void main(String[] args) throws Exception {
 75         if (args.length == 1 && args[0].equals("prepTest")) {
 76             prepTest();
 77         } else {
 78             new Bug4185732Test().run(args);
 79         }
 80     }
 81 
 82     public void testIt() throws Exception {










 83         try {

 84             final ObjectInputStream in
 85                 = new ObjectInputStream(HexDumpReader.getStreamFromHexDump("Bug4185732.ser.txt"));
 86             final ChoiceFormat loc = (ChoiceFormat)in.readObject();
 87             if (loc.getFormats().length != loc.getLimits().length) {
 88                 errln("ChoiceFormat did not properly check stream");
 89             } else {
 90                 //for some reason, the data file was VALID.  This test
 91                 //requires a corrupt data file the format and limit
 92                 //arrays are of different length.
 93                 errln("Test data file was not properly created");
 94             }
 95         } catch (InvalidObjectException e) {
 96             //this is what we want to have happen
 97         } catch (Exception e) {
 98             errln(e.toString());
 99         }
100     }
101 
102     /**
103      * Create a data file for this test.  The data file must be corrupted by hand.
104      */
105     private static void prepTest() {
106         try {
107             ObjectOutputStream out = new ObjectOutputStream(
108                     new FileOutputStream("Bug4185732.ser"));
109             final double[] limits = {1,2,3,4,5,6,7};
110             final String[] formats = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
111             final ChoiceFormat fmt = new ChoiceFormat(limits, formats);
112             out.writeObject(fmt);
113             out.close();
114             System.out.println("You must invalidate the output file before running the test");
115             System.out.println("by modifying the length of one of the array");
116         } catch (Exception e) {
117             System.out.println(e);
118         }
119     }
120 }

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