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
32import java.text.MessageFormat;
33import java.text.ParseException;
34import java.util.Date;
35import java.util.Locale;
36import java.util.TimeZone;
37
38public 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        @SuppressWarnings("deprecation")
60        Object[] sample = {
61                 0, // replace with running count below
62                "hello",
63                new Date(89, 10, 9),
64                567890,
65                1234.50
66        };
67        int samples = sample.length;
68        Object[] arguments = new Object[REPEATS * (samples + 1)];
69        for (int i = 0; i < REPEATS; i++) {
70            System.arraycopy(sample, 0, arguments, i * samples, samples);
71            arguments[i * samples] = i;
72        }
73
74        // construct large template
75        StringBuffer template = new StringBuffer();
76        for (int i = 0; i < REPEATS; i++) {
77            template.append("section {" + (i * samples) + ", number} - ");
78            template.append("string: {" + (i * samples + 1) + "}; ");
79            template.append("date: {" + (i * samples + 2) + ", date}; ");
80            template.append("integer: {" + (i * samples + 3) + ", number}; ");
81            template.append("currency: {" + (i * samples + 4) + ", number, currency};\n");
82        }
83
84        // construct expected result string
85        StringBuffer expected = new StringBuffer();
86        for (int i = 0; i < REPEATS; i++) {
87            expected.append("section " + i + " - ");
88            expected.append("string: hello; ");
89            expected.append("date: 09.11.1989; ");
90            expected.append("integer: 567.890; ");
91            expected.append("currency: 1.234,50 \u20AC;\n");
92        }
93
94        // create message format
95        MessageFormat format = new MessageFormat(template.toString());
96        String result = format.format(arguments);
97        if (!result.equals(expected.toString())) {
98           System.out.println("Template:");
99           System.out.println(template);
100           System.out.println("Expected result: ");
101           System.out.println(expected);
102           System.out.println("Actual result: ");
103           System.out.println(result);
104           throw new RuntimeException();
105       }
106    }
107
108    private static void testParse() throws ParseException {
109        StringBuffer parseTemplate = new StringBuffer();
110        StringBuffer parseInput = new StringBuffer();
111        for (int i = 0; i < REPEATS; i++) {
112            parseTemplate.append("{" + i + ", number} ");
113            parseInput.append(i + " ");
114        }
115        MessageFormat parseFormat = new MessageFormat(parseTemplate.toString());
116        Object[] parseResult = parseFormat.parse(parseInput.toString());
117        for (int i = 0; i < REPEATS; i++) {
118            if (((Number) parseResult[i]).intValue() != i) {
119                throw new RuntimeException("got wrong parse result");
120            }
121        }
122    }
123}
124