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 * @summary test that MessageFormat invariants are preserved across serialization 27 * @bug 4185816 28 * @library /java/text/testlib 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 import java.text.MessageFormat; 65 66 /** 67 * A Locale can never contains language codes of he, yi or id. 68 */ 69 public class Bug4185816Test extends IntlTest { 70 private static final String FILE_NAME = "Bug4185816.data"; 71 private static final String CORRUPT_FILE_NAME = "Bug4185816Corrupt.data"; 72 73 public static void main(String[] args) throws Exception { 74 if (args.length == 1 && args[0].equals("prepTest")) { 75 prepTest(); 76 } else { 77 new Bug4185816Test().run(args); 78 } 79 } 80 81 public void testIt() throws Exception { 82 Exception e = checkStreaming(FILE_NAME); 83 if (e != null) { 84 errln("MessageFormat did not stream in valid stream: "+e); 85 e.printStackTrace(); 86 } 87 e = checkStreaming(CORRUPT_FILE_NAME); 88 if (!(e instanceof InvalidObjectException)) { 89 errln("MessageFormat did NOT detect corrupt stream: "+e); 90 e.printStackTrace(); 91 } 92 } 93 94 public Exception checkStreaming(final String fileName) { 95 try { 96 final File f = new File(System.getProperty("test.src", "."), fileName); 97 final ObjectInputStream in = new ObjectInputStream(new FileInputStream(f)); 98 final MessageFormat form = (MessageFormat)in.readObject(); 99 final Object[] testArgs = {new Long(12373), "MyDisk"}; 100 final String result = form.format(testArgs); 101 in.close(); 102 } catch (Exception e) { 103 return e; 104 } 105 return null; 106 } 107 108 /** 109 * Create a data file for this test. The data file must be corrupted by hand. 110 */ 111 private static void prepTest() { 112 writeFormatToFile(FILE_NAME); 113 writeFormatToFile(CORRUPT_FILE_NAME); 114 } 115 116 private static void writeFormatToFile(final String name) { 117 try { 118 ObjectOutputStream out = new ObjectOutputStream( 119 new FileOutputStream(name)); 120 121 MessageFormat fmt = new MessageFormat("The disk \"{1}\" contains {0}."); 122 double[] filelimits = {0,1,2}; 123 String[] filepart = {"no files","one file","{0,number} files"}; 124 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); 125 fmt.setFormat(1,fileform); // NOT zero, see below 126 127 out.writeObject(fmt); 128 out.close(); 129 } catch (Exception e) { 130 System.out.println(e); 131 } 132 } 133 }