1 /* 2 * Copyright (c) 2017, 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 * @test 25 * @bug 8187551 26 * @summary test MessageFormat.setFormat() method to throw AIOOBE on invalid 27 * index 28 */ 29 30 import java.text.ChoiceFormat; 31 import java.text.MessageFormat; 32 import java.util.Arrays; 33 34 public class Bug8187551 { 35 36 public static void main(String[] args) { 37 38 //invalid cases ("pattern", "invalid format element index") 39 String[][] invalidCases = {{"The disk \"{1}\" contains {0}.", "2"}, 40 {"The disk \"{1}\" contains {0}.", "9"}, 41 {"On {1}, there are {0} and {2} folders", "3"}}; 42 43 //invalid cases (must throw exception) 44 Arrays.stream(invalidCases).forEach(entry -> testSetFormat(entry[0], 45 Integer.valueOf(entry[1]))); 46 47 } 48 49 // test the setFormat method for the given pattern and 50 // format element index 51 private static void testSetFormat(String pattern, int elemIndex) { 52 MessageFormat form = new MessageFormat(pattern); 53 54 double[] fileLimits = {0, 1, 2}; 55 String[] filePart = {"no files", "one file", "{0,number} files"}; 56 ChoiceFormat fileForm = new ChoiceFormat(fileLimits, filePart); 57 58 boolean AIOOBEThrown = false; 59 try { 60 form.setFormat(elemIndex, fileForm); 61 } catch (ArrayIndexOutOfBoundsException ex) { 62 AIOOBEThrown = true; 63 } 64 65 if (!AIOOBEThrown) { 66 throw new RuntimeException("[FAILED: Must throw" + 67 " ArrayIndexOutOfBoundsException for" + 68 " invalid index " + elemIndex + " used in" + 69 " MessageFormat.setFormat(index, format)]"); 70 } 71 } 72 73 }