1 /* 2 * Copyright (c) 1997, 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 * @library /java/text/testlib 27 * @summary test MessageFormat 28 */ 29 /* 30 (C) Copyright Taligent, Inc. 1996 - All Rights Reserved 31 (C) Copyright IBM Corp. 1996 - All Rights Reserved 32 33 The original version of this source code and documentation is copyrighted and 34 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are 35 provided under terms of a License Agreement between Taligent and Sun. This 36 technology is protected by multiple US and International patents. This notice and 37 attribution to Taligent may not be removed. 38 Taligent is a registered trademark of Taligent, Inc. 39 */ 40 41 42 package test.java.text.Format.MessageFormat; 43 44 import java.util.*; 45 import java.io.*; 46 import java.text.*; 47 48 import test.java.text.testlib.IntlTest; 49 50 public class MessageTest extends IntlTest { 51 main(String[] args)52 public static void main(String[] args) throws Exception { 53 new MessageTest().run(args); 54 } 55 56 TestMSGPatternTest()57 public void TestMSGPatternTest() { 58 Object[] testArgs = { 59 1D, 3456D, 60 "Disk", new Date(10000000000L)}; 61 62 String[] testCases = { 63 "Quotes '', '{', 'a' {0} '{0}'", 64 "Quotes '', '{', 'a' {0,number} '{0}'", 65 "'{'1,number,'#',##} {1,number,'#',##}", 66 "There are {1} files on {2} at {3}", 67 "On {2}, there are {1} files, with {0,number,currency}.", 68 "'{1,number,percent}', {1,number,percent}, ", 69 "'{1,date,full}', {1,date,full}, ", 70 "'{3,date,full}', {3,date,full}, ", 71 "'{1,number,#,##}' {1,number,#,##}", 72 }; 73 74 for (int i = 0; i < testCases.length; ++i) { 75 Locale save = Locale.getDefault(); 76 try { 77 Locale.setDefault(Locale.US); 78 logln(""); 79 logln( i + " Pat in: " + testCases[i]); 80 MessageFormat form = new MessageFormat(testCases[i]); 81 logln( i + " Pat out: " + form.toPattern()); 82 String result = form.format(testArgs); 83 logln( i + " Result: " + result); 84 Object[] values = form.parse(result); 85 for (int j = 0; j < testArgs.length; ++j) { 86 Object testArg = testArgs[j]; 87 Object value = null; 88 if (j < values.length) { 89 value = values[j]; 90 } 91 if ((testArg == null && value != null) 92 || (testArg != null && !testArg.equals(value))) { 93 logln( i + " " + j + " old: " + testArg); 94 logln( i + " " + j + " new: " + value); 95 } 96 } 97 } 98 catch(java.text.ParseException pe ) { 99 throw new RuntimeException("Error: MessageFormat.parse throws ParseException"); 100 } 101 finally{ 102 Locale.setDefault(save); 103 } 104 } 105 } 106 } 107