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