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