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 4105380 27 * @summary basic tests for new methods getFormatsByArgumentIndex, setFormatByArgumentIndex, setFormatsByArgumentIndex 28 */ 29 30 import java.text.ChoiceFormat; 31 import java.text.Format; 32 import java.text.MessageFormat; 33 import java.text.NumberFormat; 34 35 public class MessageFormatsByArgumentIndex { 36 37 private static String choicePattern = "0.0#are no files|1.0#is one file|1.0<are {0,number,integer} files"; 38 39 public static void main(String[] args) { 40 Format[] subformats; 41 42 MessageFormat format = new MessageFormat("{3, choice," + choicePattern + "}, {2}, {0}"); 43 44 subformats = format.getFormatsByArgumentIndex(); 45 checkSubformatLength(subformats, 4); 46 checkSubformat(subformats, 0, null); 47 checkSubformat(subformats, 1, null); 48 checkSubformat(subformats, 2, null); 49 checkSubformat(subformats, 3, new ChoiceFormat(choicePattern)); 50 51 subformats = format.getFormats(); 52 checkSubformatLength(subformats, 3); 53 checkSubformat(subformats, 0, new ChoiceFormat(choicePattern)); 54 checkSubformat(subformats, 1, null); 55 checkSubformat(subformats, 2, null); 56 57 format.setFormatByArgumentIndex(0, NumberFormat.getInstance()); 58 59 checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2}, {0,number}"); 60 61 subformats = format.getFormatsByArgumentIndex(); 62 checkSubformatLength(subformats, 4); 63 checkSubformat(subformats, 0, NumberFormat.getInstance()); 64 checkSubformat(subformats, 1, null); 65 checkSubformat(subformats, 2, null); 66 checkSubformat(subformats, 3, new ChoiceFormat(choicePattern)); 67 68 subformats = format.getFormats(); 69 checkSubformatLength(subformats, 3); 70 checkSubformat(subformats, 0, new ChoiceFormat(choicePattern)); 71 checkSubformat(subformats, 1, null); 72 checkSubformat(subformats, 2, NumberFormat.getInstance()); 73 74 format.setFormatsByArgumentIndex(subformats); 75 76 checkPattern(format.toPattern(), "{3,choice," + choicePattern + "}, {2,number}, {0,choice," + choicePattern + "}"); 77 78 subformats = format.getFormatsByArgumentIndex(); 79 checkSubformatLength(subformats, 4); 80 checkSubformat(subformats, 0, new ChoiceFormat(choicePattern)); 81 checkSubformat(subformats, 1, null); 82 checkSubformat(subformats, 2, NumberFormat.getInstance()); 83 checkSubformat(subformats, 3, new ChoiceFormat(choicePattern)); 84 85 subformats = format.getFormats(); 86 checkSubformatLength(subformats, 3); 87 checkSubformat(subformats, 0, new ChoiceFormat(choicePattern)); 88 checkSubformat(subformats, 1, NumberFormat.getInstance()); 89 checkSubformat(subformats, 2, new ChoiceFormat(choicePattern)); 90 91 } 92 93 private static void checkPattern(String actual, String expected) { 94 if (!expected.equals(actual)) { 95 throw new RuntimeException("unexpected pattern:\n expected: " + expected + "\n actual: " + actual); 96 } 97 } 98 99 private static void checkSubformatLength(Format[] subformats, int expected) { 100 if (subformats.length != expected) { 101 throw new RuntimeException("unexpected subformat length:\n expected: " + expected + "\n actual: " + subformats.length); 102 } 103 } 104 105 private static void checkSubformat(Format[] subformats, int index, Format expected) { 106 Format subformat = subformats[index]; 107 if (subformat == expected) { 108 return; 109 } 110 if ((subformat != null) && subformat.equals(expected)) { 111 return; 112 } 113 throw new RuntimeException("found unexpected subformat for argument " + index + ":\n expected: " + expected + "\n actual: " + subformat); 114 } 115 }