|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 6801704 |
| 27 | + * @summary Test the expected behavior for a wide range of patterns (both |
| 28 | + * correct and incorrect). This test documents the behavior of incorrect |
| 29 | + * ChoiceFormat patterns either throwing an exception, or discarding |
| 30 | + * the incorrect portion of a pattern. |
| 31 | + * @run junit PatternsTest |
| 32 | + */ |
| 33 | + |
| 34 | +import java.text.ChoiceFormat; |
| 35 | + |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.Arguments; |
| 39 | +import org.junit.jupiter.params.provider.MethodSource; |
| 40 | + |
| 41 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 44 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
| 45 | + |
| 46 | +public class PatternsTest { |
| 47 | + |
| 48 | + private static final String ERR1 = |
| 49 | + "Each interval must contain a number before a format"; |
| 50 | + private static final String ERR2 = |
| 51 | + "Incorrect order of intervals, must be in ascending order"; |
| 52 | + |
| 53 | + // Check that some valid patterns do not throw an exception. Check |
| 54 | + // them against the expected values they should be formatted as. |
| 55 | + @ParameterizedTest |
| 56 | + @MethodSource |
| 57 | + public void validPatternsTest(String pattern, String[] expectedValues) { |
| 58 | + var fmt = new ChoiceFormat(pattern); |
| 59 | + for (int i=1; i<=expectedValues.length; i++) { |
| 60 | + assertEquals(expectedValues[i-1], fmt.format(i), |
| 61 | + String.format("ChoiceFormat formatted %s incorrectly:", i)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Valid patterns ranging from normal appearing to odd. These should not |
| 66 | + // throw an exception or discard any portions of the pattern. |
| 67 | + private static Arguments[] validPatternsTest() { |
| 68 | + return new Arguments[] { |
| 69 | + // Multi pattern with trailing empty string Format |
| 70 | + arguments("1#foo|2#bar|3#", new String[]{"foo", "bar", ""}), |
| 71 | + // Multi patten with trailing '|' |
| 72 | + arguments("1#foo|2#bar|", new String[]{"foo", "bar"}), |
| 73 | + // Using a '>' (not a Relation) within a Format |
| 74 | + arguments("1#foo|2#bar>", new String[]{"foo", "bar>"}), |
| 75 | + // Standard Multi Pattern |
| 76 | + arguments("1#foo|2#bar", new String[]{"foo", "bar"}), |
| 77 | + // Same numerical value Limits, different Relations |
| 78 | + arguments("1#foo|1<baz", new String[]{"foo", "baz"}), |
| 79 | + // Standard Single Pattern |
| 80 | + arguments("1#foo", new String[]{"foo"}), |
| 81 | + // Single pattern with empty string Format |
| 82 | + arguments("1#", new String[]{""}) |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + // Check that the incorrect pattern throws an IAE with the desired error msg |
| 87 | + // This also tests applyPattern, as the ChoiceFormat constructor calls applyPattern |
| 88 | + @ParameterizedTest |
| 89 | + @MethodSource |
| 90 | + public void invalidPatternsThrowsTest(String pattern, String errMsg) { |
| 91 | + var ex = assertThrows(IllegalArgumentException.class, |
| 92 | + () -> new ChoiceFormat(pattern)); |
| 93 | + assertEquals(errMsg, ex.getMessage()); |
| 94 | + } |
| 95 | + |
| 96 | + // Variety of patterns that break the ChoiceFormat pattern syntax and throw |
| 97 | + // an exception. |
| 98 | + private static Arguments[] invalidPatternsThrowsTest() { |
| 99 | + return new Arguments[] { |
| 100 | + arguments("#foo", ERR1), // No Limit |
| 101 | + arguments("0#foo|#|1#bar", ERR1), // Missing Relation in SubPattern |
| 102 | + arguments("#|", ERR1), // Missing Limit |
| 103 | + arguments("##|", ERR1), // Double Relations |
| 104 | + arguments("0#foo1#", ERR1), // SubPattern not separated by '|' |
| 105 | + arguments("0#foo#", ERR1), // Using a Relation in a format |
| 106 | + arguments("0#test|#", ERR1), // SubPattern missing Limit |
| 107 | + arguments("0#foo|3#bar|1#baz", ERR2), // Non-ascending Limits |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + // Check that the incorrect pattern discards the trailing incorrect portion. |
| 112 | + // These incorrect patterns should ideally throw an exception, but for |
| 113 | + // behavioral compatibility reasons do not. |
| 114 | + @ParameterizedTest |
| 115 | + @MethodSource |
| 116 | + public void invalidPatternsDiscardedTest(String brokenPattern, String actualPattern) { |
| 117 | + var cf1 = new ChoiceFormat(brokenPattern); |
| 118 | + var cf2 = new ChoiceFormat(actualPattern); |
| 119 | + assertEquals(cf2, cf1, |
| 120 | + String.format("Expected %s, but got %s", cf2.toPattern(), cf1.toPattern())); |
| 121 | + } |
| 122 | + |
| 123 | + // Variety of incorrect patterns with the actual expected pattern |
| 124 | + // after discarding occurs. |
| 125 | + private static Arguments[] invalidPatternsDiscardedTest() { |
| 126 | + return new Arguments[] { |
| 127 | + // Incomplete SubPattern at the end of the Pattern |
| 128 | + arguments("0#foo|1#bar|baz", "0#foo|1#bar"), |
| 129 | + |
| 130 | + // --- These throw an ArrayIndexOutOfBoundsException |
| 131 | + // when attempting to format with them --- |
| 132 | + // SubPattern with only a Limit (which is interpreted as a Format) |
| 133 | + arguments("0", ""), |
| 134 | + // SubPattern with only a Format |
| 135 | + arguments("foo", ""), |
| 136 | + // empty string |
| 137 | + arguments("", "") |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + // Calling format() with empty limits and formats |
| 142 | + // throws an ArrayIndexOutOfBoundsException |
| 143 | + @Test |
| 144 | + public void emptyLimitsAndFormatsTest() { |
| 145 | + var cf1 = new ChoiceFormat(""); |
| 146 | + assertThrows(ArrayIndexOutOfBoundsException.class, |
| 147 | + () -> cf1.format(1)); |
| 148 | + |
| 149 | + var cf2 = new ChoiceFormat(new double[]{}, new String[]{}); |
| 150 | + assertThrows(ArrayIndexOutOfBoundsException.class, |
| 151 | + () -> cf2.format(2)); |
| 152 | + } |
| 153 | +} |
0 commit comments