< prev index next >

test/jdk/java/text/Format/MessageFormat/Bug4185816Test.java

Print this page

  1 /*
  2  * Copyright (c) 1999, 2022, 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  * This file is available under and governed by the GNU General Public
 34  * License version 2 only, as published by the Free Software Foundation.
 35  * However, the following notice accompanied the original version of this
 36  * file and, per its terms, should not be removed:
 37  *
 38  * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
 39  *
 40  * Portions copyright (c) 2007 Sun Microsystems, Inc.
 41  * All Rights Reserved.
 42  *
 43  * The original version of this source code and documentation
 44  * is copyrighted and owned by Taligent, Inc., a wholly-owned
 45  * subsidiary of IBM. These materials are provided under terms
 46  * of a License Agreement between Taligent and Sun. This technology
 47  * is protected by multiple US and International patents.
 48  *
 49  * This notice and attribution to Taligent may not be removed.
 50  * Taligent is a registered trademark of Taligent, Inc.
 51  *
 52  * Permission to use, copy, modify, and distribute this software
 53  * and its documentation for NON-COMMERCIAL purposes and without
 54  * fee is hereby granted provided that this copyright notice
 55  * appears in all copies. Please refer to the file "copyright.html"
 56  * for further important copyright and licensing information.
 57  *
 58  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 59  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 60  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 61  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 62  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 63  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 64  */
 65 
 66 import java.util.*;
 67 import java.io.*;
 68 import java.text.ChoiceFormat;
 69 import java.text.MessageFormat;
 70 




 71 /**
 72  *  A Locale can never contains language codes of he, yi or id.
 73  */
 74 public class Bug4185816Test extends IntlTest {
 75     private static final String FILE_NAME = "Bug4185816.ser";
 76     private static final String CORRUPT_FILE_NAME = "Bug4185816Corrupt.ser";
 77 
 78     public static void main(String[] args) throws Exception {
 79         if (args.length == 1 && args[0].equals("prepTest")) {
 80             prepTest();
 81         } else {
 82             new Bug4185816Test().run(args);
 83         }
 84     }
 85 
 86     public void testIt() throws Exception {
 87         Exception e = checkStreaming(FILE_NAME);
 88         if (e != null) {
 89             errln("MessageFormat did not stream in valid stream: "+e);
 90             e.printStackTrace();
 91         }
 92         e = checkStreaming(CORRUPT_FILE_NAME);
 93         if (!(e instanceof InvalidObjectException)) {
 94             errln("MessageFormat did NOT detect corrupt stream: "+e);
 95             e.printStackTrace();
 96         }
 97     }
 98 
 99     public Exception checkStreaming(final String fileName) {
100         try {
101             final InputStream is = HexDumpReader.getStreamFromHexDump(fileName + ".txt");
102             final ObjectInputStream in = new ObjectInputStream(is);
103             final MessageFormat form = (MessageFormat)in.readObject();
104             final Object[] testArgs = {12373L, "MyDisk"};
105             final String result = form.format(testArgs);
106             in.close();
107         } catch (Exception e) {
108             return e;
109         }
110         return null;
111     }
112 
113     /**
114      * Create a data file for this test.  The data file must be corrupted by hand.
115      */
116     private static void prepTest() {
117         writeFormatToFile(FILE_NAME);
118         writeFormatToFile(CORRUPT_FILE_NAME);
119     }
120 
121     private static void writeFormatToFile(final String name) {
122         try {
123             ObjectOutputStream out = new ObjectOutputStream(
124                     new FileOutputStream(name));
125 
126             MessageFormat fmt = new MessageFormat("The disk \"{1}\" contains {0}.");
127             double[] filelimits = {0,1,2};
128             String[] filepart = {"no files","one file","{0,number} files"};
129             ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
130             fmt.setFormat(1,fileform); // NOT zero, see below
131 
132             out.writeObject(fmt);
133             out.close();
134         } catch (Exception e) {
135             System.out.println(e);
136         }
137     }
138 }

  1 /*
  2  * Copyright (c) 1999, 2023, 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 HexDumpReader
 29  * @run junit Bug4185816Test
 30  * @summary test that MessageFormat invariants are preserved across serialization.
 31  */
 32 
 33 /*
 34  * This file is available under and governed by the GNU General Public
 35  * License version 2 only, as published by the Free Software Foundation.
 36  * However, the following notice accompanied the original version of this
 37  * file and, per its terms, should not be removed:
 38  *
 39  * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
 40  *
 41  * Portions copyright (c) 2007 Sun Microsystems, Inc.
 42  * All Rights Reserved.
 43  *
 44  * The original version of this source code and documentation
 45  * is copyrighted and owned by Taligent, Inc., a wholly-owned
 46  * subsidiary of IBM. These materials are provided under terms
 47  * of a License Agreement between Taligent and Sun. This technology
 48  * is protected by multiple US and International patents.
 49  *
 50  * This notice and attribution to Taligent may not be removed.
 51  * Taligent is a registered trademark of Taligent, Inc.
 52  *
 53  * Permission to use, copy, modify, and distribute this software
 54  * and its documentation for NON-COMMERCIAL purposes and without
 55  * fee is hereby granted provided that this copyright notice
 56  * appears in all copies. Please refer to the file "copyright.html"
 57  * for further important copyright and licensing information.
 58  *
 59  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 60  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 61  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 62  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 63  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 64  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 65  */
 66 
 67 import java.util.*;
 68 import java.io.*;
 69 import java.text.ChoiceFormat;
 70 import java.text.MessageFormat;
 71 
 72 import org.junit.jupiter.api.Test;
 73 
 74 import static org.junit.jupiter.api.Assertions.fail;
 75 
 76 /**
 77  *  A Locale can never contain language codes of he, yi or id.
 78  */
 79 public class Bug4185816Test {
 80     private static final String FILE_NAME = "Bug4185816.ser";
 81     private static final String CORRUPT_FILE_NAME = "Bug4185816Corrupt.ser";
 82 
 83     @Test







 84     public void testIt() throws Exception {
 85         Exception e = checkStreaming(FILE_NAME);
 86         if (e != null) {
 87             fail("MessageFormat did not stream in valid stream: "+e);
 88             e.printStackTrace();
 89         }
 90         e = checkStreaming(CORRUPT_FILE_NAME);
 91         if (!(e instanceof InvalidObjectException)) {
 92             fail("MessageFormat did NOT detect corrupt stream: "+e);
 93             e.printStackTrace();
 94         }
 95     }
 96 
 97     public Exception checkStreaming(final String fileName) {
 98         try {
 99             final InputStream is = HexDumpReader.getStreamFromHexDump(fileName + ".txt");
100             final ObjectInputStream in = new ObjectInputStream(is);
101             final MessageFormat form = (MessageFormat)in.readObject();
102             final Object[] testArgs = {12373L, "MyDisk"};
103             final String result = form.format(testArgs);
104             in.close();
105         } catch (Exception e) {
106             return e;
107         }
108         return null;
109     }


























110 }
< prev index next >