Path: blob/master/test/jdk/java/text/Format/MessageFormat/Bug7003643.java
41152 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 700364326* @summary Make sure MessageFormat.toPattern produces correct quoting. (SPI part is tested in PluggableLocale tests.)27* @key randomness28*/2930import java.text.*;31import java.util.*;3233public class Bug7003643 {34private static final int N = 5;3536private static final String[] elements = {37"'{'", "'{", "{", "''", "}", "a", "'",38};3940public static void main(String[] args) {41Random rand = new Random();42int count = 0;43int max = (int) (Math.pow((double)elements.length, (double)N)/0.52);44while (count < max) {45// Create a random pattern. If the produced pattern is46// valid, then proceed with the round-trip testing.47StringBuilder sb = new StringBuilder();48for (int i = 0; i < N; i++) {49sb.append(elements[rand.nextInt(elements.length)]);50}51String pattern = sb.toString();52MessageFormat mf = null;53try {54mf = new MessageFormat(pattern);55} catch (IllegalArgumentException e) {56// bad pattern data57}58if (mf == null) {59continue;60}61count++;62String res1 = MessageFormat.format(pattern, 123);63String toPattern = mf.toPattern();64String res2 = MessageFormat.format(toPattern, 123);65if (!res1.equals(res2)) {66String s = String.format("Failed%n pattern=\"%s\" => result=\"%s\"%n"67+ " toPattern()=\"%s\" => result=\"%s\"%n",68pattern, res1, toPattern, res2);69throw new RuntimeException(s);70}71}72}73}747576