1 /* 2 * Copyright (c) 2000, 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 4112090 8008577 27 * @summary verify that MessageFormat can handle large numbers of arguments 28 * @modules jdk.localedata 29 * @run main/othervm -Djava.locale.providers=COMPAT,SPI LargeMessageFormat 30 */ 31 32 import java.text.MessageFormat; 33 import java.text.ParseException; 34 import java.util.Date; 35 import java.util.Locale; 36 import java.util.TimeZone; 37 38 public class LargeMessageFormat { 39 40 public static void main(String[] args) throws ParseException { 41 Locale reservedLocale = Locale.getDefault(); 42 TimeZone reservedTimeZone = TimeZone.getDefault(); 43 try { 44 Locale.setDefault(Locale.GERMANY); 45 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin")); 46 testFormat(); 47 testParse(); 48 } finally { 49 // restore the reserved locale and time zone 50 Locale.setDefault(reservedLocale); 51 TimeZone.setDefault(reservedTimeZone); 52 } 53 } 54 55 private static final int REPEATS = 89; 56 57 private static void testFormat() { 58 // construct large argument array 59 Object[] sample = { 60 new Integer(0), // replace with running count below 61 "hello", 62 new Date(89, 10, 9), 63 new Integer(567890), 64 new Double(1234.50) 65 }; 66 int samples = sample.length; 67 Object[] arguments = new Object[REPEATS * (samples + 1)]; 68 for (int i = 0; i < REPEATS; i++) { 69 System.arraycopy(sample, 0, arguments, i * samples, samples); 70 arguments[i * samples] = new Integer(i); 71 } 72 73 // construct large template 74 StringBuffer template = new StringBuffer(); 75 for (int i = 0; i < REPEATS; i++) { 76 template.append("section {" + (i * samples) + ", number} - "); 77 template.append("string: {" + (i * samples + 1) + "}; "); 78 template.append("date: {" + (i * samples + 2) + ", date}; "); 79 template.append("integer: {" + (i * samples + 3) + ", number}; "); 80 template.append("currency: {" + (i * samples + 4) + ", number, currency};\n"); 81 } 82 83 // construct expected result string 84 StringBuffer expected = new StringBuffer(); 85 for (int i = 0; i < REPEATS; i++) { 86 expected.append("section " + i + " - "); 87 expected.append("string: hello; "); 88 expected.append("date: 09.11.1989; "); 89 expected.append("integer: 567.890; "); 90 expected.append("currency: 1.234,50 \u20AC;\n"); 91 } 92 93 // create message format 94 MessageFormat format = new MessageFormat(template.toString()); 95 String result = format.format(arguments); 96 if (!result.equals(expected.toString())) { 97 System.out.println("Template:"); 98 System.out.println(template); 99 System.out.println("Expected result: "); 100 System.out.println(expected); 101 System.out.println("Actual result: "); 102 System.out.println(result); 103 throw new RuntimeException(); 104 } 105 } 106 107 private static void testParse() throws ParseException { 108 StringBuffer parseTemplate = new StringBuffer(); 109 StringBuffer parseInput = new StringBuffer(); 110 for (int i = 0; i < REPEATS; i++) { 111 parseTemplate.append("{" + i + ", number} "); 112 parseInput.append(i + " "); 113 } 114 MessageFormat parseFormat = new MessageFormat(parseTemplate.toString()); 115 Object[] parseResult = parseFormat.parse(parseInput.toString()); 116 for (int i = 0; i < REPEATS; i++) { 117 if (((Number) parseResult[i]).intValue() != i) { 118 throw new RuntimeException("got wrong parse result"); 119 } 120 } 121 } 122 }