|
| 1 | +/* |
| 2 | + * Copyright (c) 2010, 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 6807534 |
| 27 | + * @summary check whether the default implementation of |
| 28 | + * CurrencyNameProvider.getDisplayName(String, Locale) throws appropriate |
| 29 | + * exceptions when necessary. |
| 30 | + * @run junit CNPGetDisplayName |
| 31 | + */ |
| 32 | + |
| 33 | +import java.util.Locale; |
| 34 | +import java.util.spi.CurrencyNameProvider; |
| 35 | +import java.util.stream.Stream; |
| 36 | + |
| 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.assertThrows; |
| 42 | + |
| 43 | +public class CNPGetDisplayName { |
| 44 | + |
| 45 | + static final CurrencyNameProvider cnp = new CurrencyNameProviderImpl(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Tests that the currency name provider throws a NullPointerException |
| 49 | + * under the expected circumstances. |
| 50 | + */ |
| 51 | + @ParameterizedTest |
| 52 | + @MethodSource("nullArgProvider") |
| 53 | + public void NPETest(String currencyCode, Locale locale, String err) { |
| 54 | + assertThrows(NullPointerException.class, |
| 55 | + () -> cnp.getDisplayName(currencyCode, locale), err); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Tests that the currency name provider throws a IllegalArgumentException |
| 60 | + * under the expected circumstances. |
| 61 | + */ |
| 62 | + @ParameterizedTest |
| 63 | + @MethodSource("illegalArgProvider") |
| 64 | + public void IAETest(String currencyCode, Locale locale, String err) { |
| 65 | + assertThrows(IllegalArgumentException.class, |
| 66 | + () -> cnp.getDisplayName(currencyCode, locale), err); |
| 67 | + } |
| 68 | + |
| 69 | + private static Stream<Arguments> nullArgProvider() { |
| 70 | + return Stream.of( |
| 71 | + Arguments.of(null, Locale.US, |
| 72 | + "NPE was not thrown with null currencyCode"), |
| 73 | + Arguments.of("USD", null, |
| 74 | + "NPE was not thrown with null locale") |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + private static Stream<Arguments> illegalArgProvider() { |
| 79 | + return Stream.of( |
| 80 | + Arguments.of("INVALID", Locale.US, |
| 81 | + "IAE was not thrown with invalid currency code"), |
| 82 | + Arguments.of("inv", Locale.US, |
| 83 | + "IAE was not thrown with invalid currency code"), |
| 84 | + Arguments.of("USD", Locale.JAPAN, |
| 85 | + "IllegalArgumentException was not thrown with non-supported locale") |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + static class CurrencyNameProviderImpl extends CurrencyNameProvider { |
| 90 | + // dummy implementation |
| 91 | + public String getSymbol(String currencyCode, Locale locale) { |
| 92 | + return ""; |
| 93 | + } |
| 94 | + |
| 95 | + public Locale[] getAvailableLocales() { |
| 96 | + Locale[] avail = new Locale[1]; |
| 97 | + avail[0] = Locale.US; |
| 98 | + return avail; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments