1 /* 2 * Copyright (c) 1999, 2016, 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 * @summary test that ChoiceFormat invariants are preserved across serialization 29 */ 30 /* 31 * 32 * 33 * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved 34 * 35 * Portions copyright (c) 2007 Sun Microsystems, Inc. 36 * All Rights Reserved. 37 * 38 * The original version of this source code and documentation 39 * is copyrighted and owned by Taligent, Inc., a wholly-owned 40 * subsidiary of IBM. These materials are provided under terms 41 * of a License Agreement between Taligent and Sun. This technology 42 * is protected by multiple US and International patents. 43 * 44 * This notice and attribution to Taligent may not be removed. 45 * Taligent is a registered trademark of Taligent, Inc. 46 * 47 * Permission to use, copy, modify, and distribute this software 48 * and its documentation for NON-COMMERCIAL purposes and without 49 * fee is hereby granted provided that this copyright notice 50 * appears in all copies. Please refer to the file "copyright.html" 51 * for further important copyright and licensing information. 52 * 53 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 54 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 55 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 56 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR 57 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR 58 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. 59 */ 60 61 import java.util.*; 62 import java.io.*; 63 import java.text.ChoiceFormat; 64 65 /** 66 * A Locale can never contains language codes of he, yi or id. 67 */ 68 public class Bug4185732Test extends IntlTest { 69 public static void main(String[] args) throws Exception { 70 if (args.length == 1 && args[0].equals("prepTest")) { 71 prepTest(); 72 } else { 73 new Bug4185732Test().run(args); 74 } 75 } 76 77 public void testIt() throws Exception { 78 try { 79 final File f = new File(System.getProperty("test.src", "."), "Bug4185732.data"); 80 final ObjectInputStream in = new ObjectInputStream( 81 new FileInputStream(f)); 82 final ChoiceFormat loc = (ChoiceFormat)in.readObject(); 83 if (loc.getFormats().length != loc.getLimits().length) { 84 errln("ChoiceFormat did not properly check stream"); 85 } else { 86 //for some reason, the data file was VALID. This test 87 //requires a corrupt data file the format and limit 88 //arrays are of different length. 89 errln("Test data file was not properly created"); 90 } 91 } catch (InvalidObjectException e) { 92 //this is what we want to have happen 93 } catch (Exception e) { 94 errln(e.toString()); 95 } 96 } 97 98 /** 99 * Create a data file for this test. The data file must be corrupted by hand. 100 */ 101 private static void prepTest() { 102 try { 103 ObjectOutputStream out = new ObjectOutputStream( 104 new FileOutputStream("Bug4185732.data")); 105 final double[] limits = {1,2,3,4,5,6,7}; 106 final String[] formats = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; 107 final ChoiceFormat fmt = new ChoiceFormat(limits, formats); 108 out.writeObject(fmt); 109 out.close(); 110 System.out.println("You must invalidate the output file before running the test"); 111 System.out.println("by modifying the length of one of the array"); 112 } catch (Exception e) { 113 System.out.println(e); 114 } 115 } 116 }