jdk/test/java/text/Format/MessageFormat/Bug7003643.java
author amurillo
Fri, 20 Apr 2012 16:23:48 -0700
changeset 12384 55ac5f20c7bf
parent 7787 d2420a14d0a2
child 30046 cf2c86e1819e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7787
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     1
/*
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     4
 *
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     8
 *
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    13
 * accompanied this code).
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    14
 *
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    18
 *
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    21
 * questions.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    22
 */
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    23
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    24
/*
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    25
 * @test
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    26
 * @bug 7003643
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    27
 * @summary Make sure MessageFormat.toPattern produces correct quoting. (SPI part is tested in PluggableLocale tests.)
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    28
 */
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    29
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    30
import java.text.*;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    31
import java.util.*;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    32
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    33
public class Bug7003643 {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    34
    private static final int N = 5;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    35
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    36
    private static final String[] elements = {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    37
        "'{'", "'{", "{", "''", "}", "a", "'",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    38
    };
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    39
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    40
    public static void main(String[] args) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    41
        Random rand = new Random();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    42
        int count = 0;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    43
        int max = (int) (Math.pow((double)elements.length, (double)N)/0.52);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    44
        while (count < max) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    45
            // Create a random pattern. If the produced pattern is
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    46
            // valid, then proceed with the round-trip testing.
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    47
            StringBuilder sb = new StringBuilder();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    48
            for (int i = 0; i < N; i++) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    49
                sb.append(elements[rand.nextInt(elements.length)]);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    50
            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    51
            String pattern = sb.toString();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    52
            MessageFormat mf = null;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    53
            try {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    54
                mf = new MessageFormat(pattern);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    55
            } catch (IllegalArgumentException e) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    56
                // bad pattern data
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    57
            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    58
            if (mf == null) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    59
                continue;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    60
            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    61
            count++;
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    62
            String res1 = MessageFormat.format(pattern, 123);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    63
            String toPattern = mf.toPattern();
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    64
            String res2 = MessageFormat.format(toPattern, 123);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    65
            if (!res1.equals(res2)) {
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    66
                String s = String.format("Failed%n      pattern=\"%s\"  =>  result=\"%s\"%n"
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    67
                                         + "  toPattern()=\"%s\"  =>  result=\"%s\"%n",
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    68
                                         pattern, res1, toPattern, res2);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    69
                throw new RuntimeException(s);
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    70
            }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    71
        }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    72
    }
d2420a14d0a2 7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
diff changeset
    73
}