1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
25
25
* @test
26
26
* @bug 4290801 4692419 4693631 5101540 5104960 6296410 6336600 6371531
27
27
* 6488442 7036905 8008577 8039317 8074350 8074351 8150324 8167143
28
- * 8264792 8334653
28
+ * 8264792 8334653 8353713
29
29
* @summary Basic tests for Currency class.
30
30
* @modules java.base/java.util:open
31
31
* jdk.localedata
@@ -84,23 +84,33 @@ private static Stream<String> validCurrencies() {
84
84
public void invalidCurrencyTest (String currencyCode ) {
85
85
IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , () ->
86
86
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 ());
89
89
}
90
90
91
91
private static Stream <String > non4217Currencies () {
92
92
return Stream .of ("AQD" , "US$" );
93
93
}
94
94
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
+
95
105
// Calling getInstance() with a currency code not 3 characters long should throw
96
106
// an IAE
97
107
@ ParameterizedTest
98
108
@ MethodSource ("invalidLengthCurrencies" )
99
109
public void invalidCurrencyLengthTest (String currencyCode ) {
100
110
IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , () ->
101
111
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 ());
104
114
}
105
115
106
116
private static Stream <String > invalidLengthCurrencies () {
@@ -163,8 +173,8 @@ public void localeMappingTest() {
163
173
"AC|CP|DG|EA|EU|FX|IC|SU|TA|UK" )) { // exceptional reservation codes
164
174
IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
165
175
() -> 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 ());
168
178
} else {
169
179
goodCountries ++;
170
180
Currency currency = Currency .getInstance (locale );
@@ -180,14 +190,16 @@ public void localeMappingTest() {
180
190
}
181
191
}
182
192
183
- // Check an invalid country code
193
+ // Check an invalid country code supplied via the region override
184
194
@ 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" );
186
198
Locale l = new Locale ("" , "EU" );
187
199
IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
188
- ()-> Currency .getInstance (l ), "Did not throw IAE" );
189
- assertEquals ("The country of the input locale is not a valid" +
190
- " ISO 3166 country code" , ex .getMessage ());
200
+ ()-> Currency .getInstance (loc ), "Did not throw IAE" );
201
+ assertEquals ("The country of the input locale: \" %s \" is not a valid ISO 3166 country code"
202
+ . formatted ( loc ) , ex .getMessage ());
191
203
}
192
204
193
205
// Ensure a selection of countries have the expected currency
0 commit comments