Path: blob/master/test/jdk/java/text/Format/MessageFormat/Bug4185816Test.java
41152 views
/*1* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 418581626* @library /java/text/testlib27* @build Bug4185816Test IntlTest HexDumpReader28* @run main Bug4185816Test29* @summary test that MessageFormat invariants are preserved across serialization30*/31/*32*33*34* (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved35*36* Portions copyright (c) 2007 Sun Microsystems, Inc.37* All Rights Reserved.38*39* The original version of this source code and documentation40* is copyrighted and owned by Taligent, Inc., a wholly-owned41* subsidiary of IBM. These materials are provided under terms42* of a License Agreement between Taligent and Sun. This technology43* is protected by multiple US and International patents.44*45* This notice and attribution to Taligent may not be removed.46* Taligent is a registered trademark of Taligent, Inc.47*48* Permission to use, copy, modify, and distribute this software49* and its documentation for NON-COMMERCIAL purposes and without50* fee is hereby granted provided that this copyright notice51* appears in all copies. Please refer to the file "copyright.html"52* for further important copyright and licensing information.53*54* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF55* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED56* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A57* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR58* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR59* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.60*/6162import java.util.*;63import java.io.*;64import java.text.ChoiceFormat;65import java.text.MessageFormat;6667/**68* A Locale can never contains language codes of he, yi or id.69*/70public class Bug4185816Test extends IntlTest {71private static final String FILE_NAME = "Bug4185816.ser";72private static final String CORRUPT_FILE_NAME = "Bug4185816Corrupt.ser";7374public static void main(String[] args) throws Exception {75if (args.length == 1 && args[0].equals("prepTest")) {76prepTest();77} else {78new Bug4185816Test().run(args);79}80}8182public void testIt() throws Exception {83Exception e = checkStreaming(FILE_NAME);84if (e != null) {85errln("MessageFormat did not stream in valid stream: "+e);86e.printStackTrace();87}88e = checkStreaming(CORRUPT_FILE_NAME);89if (!(e instanceof InvalidObjectException)) {90errln("MessageFormat did NOT detect corrupt stream: "+e);91e.printStackTrace();92}93}9495public Exception checkStreaming(final String fileName) {96try {97final InputStream is = HexDumpReader.getStreamFromHexDump(fileName + ".txt");98final ObjectInputStream in = new ObjectInputStream(is);99final MessageFormat form = (MessageFormat)in.readObject();100final Object[] testArgs = {12373L, "MyDisk"};101final String result = form.format(testArgs);102in.close();103} catch (Exception e) {104return e;105}106return null;107}108109/**110* Create a data file for this test. The data file must be corrupted by hand.111*/112private static void prepTest() {113writeFormatToFile(FILE_NAME);114writeFormatToFile(CORRUPT_FILE_NAME);115}116117private static void writeFormatToFile(final String name) {118try {119ObjectOutputStream out = new ObjectOutputStream(120new FileOutputStream(name));121122MessageFormat fmt = new MessageFormat("The disk \"{1}\" contains {0}.");123double[] filelimits = {0,1,2};124String[] filepart = {"no files","one file","{0,number} files"};125ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);126fmt.setFormat(1,fileform); // NOT zero, see below127128out.writeObject(fmt);129out.close();130} catch (Exception e) {131System.out.println(e);132}133}134}135136137