11/*
2- * Copyright (c) 2007, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2007, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525 * @test
2626 * @bug 4290801 4692419 4693631 5101540 5104960 6296410 6336600 6371531
2727 * 6488442 7036905 8008577 8039317 8074350 8074351 8150324 8167143
28- * 8264792 8334653
28+ * 8264792 8334653 8353713
2929 * @summary Basic tests for Currency class.
3030 * @modules java.base/java.util:open
3131 * jdk.localedata
@@ -84,23 +84,33 @@ private static Stream<String> validCurrencies() {
8484 public void invalidCurrencyTest (String currencyCode ) {
8585 IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , () ->
8686 Currency .getInstance (currencyCode ), "getInstance() did not throw IAE" );
87- assertEquals ("The input currency code is not a" +
88- " valid ISO 4217 code" , ex .getMessage ());
87+ assertEquals ("The input currency code: \" %s \" is not a valid ISO 4217 code"
88+ . formatted ( currencyCode ) , ex .getMessage ());
8989 }
9090
9191 private static Stream <String > non4217Currencies () {
9292 return Stream .of ("AQD" , "US$" );
9393 }
9494
95+ // Provide 3 length code, but first 2 chars should not be able to index
96+ // the main table, thus resulting as incorrect country code
97+ @ Test
98+ void invalidCountryInCodeTest () {
99+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , () ->
100+ Currency .getInstance ("..A" ), "getInstance() did not throw IAE" );
101+ assertEquals ("The country code: \" %s\" is not a valid ISO 3166 code"
102+ .formatted (".." ), ex .getMessage ());
103+ }
104+
95105 // Calling getInstance() with a currency code not 3 characters long should throw
96106 // an IAE
97107 @ ParameterizedTest
98108 @ MethodSource ("invalidLengthCurrencies" )
99109 public void invalidCurrencyLengthTest (String currencyCode ) {
100110 IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , () ->
101111 Currency .getInstance (currencyCode ), "getInstance() did not throw IAE" );
102- assertEquals ("The input currency code must have a length of 3" +
103- " characters" , ex .getMessage ());
112+ assertEquals ("The input currency code: \" %s \" must have a length of 3 characters"
113+ . formatted ( currencyCode ) , ex .getMessage ());
104114 }
105115
106116 private static Stream <String > invalidLengthCurrencies () {
@@ -163,8 +173,8 @@ public void localeMappingTest() {
163173 "AC|CP|DG|EA|EU|FX|IC|SU|TA|UK" )) { // exceptional reservation codes
164174 IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
165175 () -> Currency .getInstance (locale ), "Did not throw IAE" );
166- assertEquals ("The country of the input locale is not a" +
167- " valid ISO 3166 country code" , ex .getMessage ());
176+ assertEquals ("The country of the input locale: \" %s \" is not a valid ISO 3166 country code"
177+ . formatted ( locale ) , ex .getMessage ());
168178 } else {
169179 goodCountries ++;
170180 Currency currency = Currency .getInstance (locale );
@@ -180,13 +190,15 @@ public void localeMappingTest() {
180190 }
181191 }
182192
183- // Check an invalid country code
193+ // Check an invalid country code supplied via the region override
184194 @ Test
185- public void invalidCountryTest () {
195+ public void invalidCountryRegionOverrideTest () {
196+ // Override US with nonsensical country
197+ var loc = Locale .forLanguageTag ("en-US-u-rg-XXzzzz" );
186198 IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
187- ()-> Currency .getInstance (Locale . of ( "" , "EU" ) ), "Did not throw IAE" );
188- assertEquals ("The country of the input locale is not a valid" +
189- " ISO 3166 country code" , ex .getMessage ());
199+ ()-> Currency .getInstance (loc ), "Did not throw IAE" );
200+ assertEquals ("The country of the input locale: \" %s \" is not a valid ISO 3166 country code"
201+ . formatted ( loc ) , ex .getMessage ());
190202 }
191203
192204 // Ensure a selection of countries have the expected currency
0 commit comments