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 4185816 27 * @library /java/text/testlib 28 * @build Bug4185816Test IntlTest HexDumpReader 29 * @run main Bug4185816Test 30 * @summary test that MessageFormat invariants are preserved across serialization 31 */ 32 /* 33 * 34 * 35 * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved 36 * 37 * Portions copyright (c) 2007 Sun Microsystems, Inc. 38 * All Rights Reserved. 39 * 40 * The original version of this source code and documentation 41 * is copyrighted and owned by Taligent, Inc., a wholly-owned 42 * subsidiary of IBM. These materials are provided under terms 43 * of a License Agreement between Taligent and Sun. This technology 44 * is protected by multiple US and International patents. 45 * 46 * This notice and attribution to Taligent may not be removed. 47 * Taligent is a registered trademark of Taligent, Inc. 48 * 49 * Permission to use, copy, modify, and distribute this software 50 * and its documentation for NON-COMMERCIAL purposes and without 51 * fee is hereby granted provided that this copyright notice 52 * appears in all copies. Please refer to the file "copyright.html" 53 * for further important copyright and licensing information. 54 * 55 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 56 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 57 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 58 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR 59 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR 60 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. 61 */ 62 63 import java.util.*; 64 import java.io.*; 65 import java.text.ChoiceFormat; 66 import java.text.MessageFormat; 67 68 /** 69 * A Locale can never contains language codes of he, yi or id. 70 */ 71 public class Bug4185816Test extends IntlTest { 72 private static final String FILE_NAME = "Bug4185816.ser"; 73 private static final String CORRUPT_FILE_NAME = "Bug4185816Corrupt.ser"; 74 75 public static void main(String[] args) throws Exception { 76 if (args.length == 1 && args[0].equals("prepTest")) { 77 prepTest(); 78 } else { 79 new Bug4185816Test().run(args); 80 } 81 } 82 83 public void testIt() throws Exception { 84 Exception e = checkStreaming(FILE_NAME); 85 if (e != null) { 86 errln("MessageFormat did not stream in valid stream: "+e); 87 e.printStackTrace(); 88 } 89 e = checkStreaming(CORRUPT_FILE_NAME); 90 if (!(e instanceof InvalidObjectException)) { 91 errln("MessageFormat did NOT detect corrupt stream: "+e); 92 e.printStackTrace(); 93 } 94 } 95 96 public Exception checkStreaming(final String fileName) { 97 try { 98 final InputStream is = HexDumpReader.getStreamFromHexDump(fileName + ".txt"); 99 final ObjectInputStream in = new ObjectInputStream(is); 100 final MessageFormat form = (MessageFormat)in.readObject(); 101 final Object[] testArgs = {12373L, "MyDisk"}; 102 final String result = form.format(testArgs); 103 in.close(); 104 } catch (Exception e) { 105 return e; 106 } 107 return null; 108 } 109 110 /** 111 * Create a data file for this test. The data file must be corrupted by hand. 112 */ 113 private static void prepTest() { 114 writeFormatToFile(FILE_NAME); 115 writeFormatToFile(CORRUPT_FILE_NAME); 116 } 117 118 private static void writeFormatToFile(final String name) { 119 try { 120 ObjectOutputStream out = new ObjectOutputStream( 121 new FileOutputStream(name)); 122 123 MessageFormat fmt = new MessageFormat("The disk \"{1}\" contains {0}."); 124 double[] filelimits = {0,1,2}; 125 String[] filepart = {"no files","one file","{0,number} files"}; 126 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); 127 fmt.setFormat(1,fileform); // NOT zero, see below 128 129 out.writeObject(fmt); 130 out.close(); 131 } catch (Exception e) { 132 System.out.println(e); 133 } 134 } 135 }