Path: blob/master/test/jdk/java/text/Format/ChoiceFormat/Bug4185732Test.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 418573226* @library /java/text/testlib27* @build Bug4185732Test IntlTest HexDumpReader28* @run main Bug4185732Test29* @summary test that ChoiceFormat 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;6566/**67* A Locale can never contains language codes of he, yi or id.68*/69public class Bug4185732Test extends IntlTest {70public static void main(String[] args) throws Exception {71if (args.length == 1 && args[0].equals("prepTest")) {72prepTest();73} else {74new Bug4185732Test().run(args);75}76}7778public void testIt() throws Exception {79try {80final ObjectInputStream in81= new ObjectInputStream(HexDumpReader.getStreamFromHexDump("Bug4185732.ser.txt"));82final ChoiceFormat loc = (ChoiceFormat)in.readObject();83if (loc.getFormats().length != loc.getLimits().length) {84errln("ChoiceFormat did not properly check stream");85} else {86//for some reason, the data file was VALID. This test87//requires a corrupt data file the format and limit88//arrays are of different length.89errln("Test data file was not properly created");90}91} catch (InvalidObjectException e) {92//this is what we want to have happen93} catch (Exception e) {94errln(e.toString());95}96}9798/**99* Create a data file for this test. The data file must be corrupted by hand.100*/101private static void prepTest() {102try {103ObjectOutputStream out = new ObjectOutputStream(104new FileOutputStream("Bug4185732.ser"));105final double[] limits = {1,2,3,4,5,6,7};106final String[] formats = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};107final ChoiceFormat fmt = new ChoiceFormat(limits, formats);108out.writeObject(fmt);109out.close();110System.out.println("You must invalidate the output file before running the test");111System.out.println("by modifying the length of one of the array");112} catch (Exception e) {113System.out.println(e);114}115}116}117118119