@@ -80,14 +80,31 @@ private static Stream<String> validCurrencies() {
80
80
81
81
// Calling getInstance() with an invalid currency code should throw an IAE
82
82
@ ParameterizedTest
83
- @ MethodSource ("invalidCurrencies " )
83
+ @ MethodSource ("non4217Currencies " )
84
84
public void invalidCurrencyTest (String currencyCode ) {
85
- assertThrows (IllegalArgumentException .class , () ->
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
89
}
88
90
89
- private static Stream <String > invalidCurrencies () {
90
- return Stream .of ("AQD" , "US$" , "\u20AC " );
91
+ private static Stream <String > non4217Currencies () {
92
+ return Stream .of ("AQD" , "US$" );
93
+ }
94
+
95
+ // Calling getInstance() with a currency code not 3 characters long should throw
96
+ // an IAE
97
+ @ ParameterizedTest
98
+ @ MethodSource ("invalidLengthCurrencies" )
99
+ public void invalidCurrencyLengthTest (String currencyCode ) {
100
+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , () ->
101
+ Currency .getInstance (currencyCode ), "getInstance() did not throw IAE" );
102
+ assertEquals ("The input currency code must have a length of 3" +
103
+ " characters" , ex .getMessage ());
104
+ }
105
+
106
+ private static Stream <String > invalidLengthCurrencies () {
107
+ return Stream .of ("\u20AC " , "" , "12345" );
91
108
}
92
109
}
93
110
@@ -144,7 +161,10 @@ public void localeMappingTest() {
144
161
ctryLength == 3 || // UN M.49 code
145
162
ctryCode .matches ("AA|Q[M-Z]|X[A-JL-Z]|ZZ" + // user defined codes, excluding "XK" (Kosovo)
146
163
"AC|CP|DG|EA|EU|FX|IC|SU|TA|UK" )) { // exceptional reservation codes
147
- assertThrows (IllegalArgumentException .class , () -> Currency .getInstance (locale ), "Did not throw IAE" );
164
+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
165
+ () -> 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 ());
148
168
} else {
149
169
goodCountries ++;
150
170
Currency currency = Currency .getInstance (locale );
@@ -163,8 +183,10 @@ public void localeMappingTest() {
163
183
// Check an invalid country code
164
184
@ Test
165
185
public void invalidCountryTest () {
166
- assertThrows (IllegalArgumentException .class , ()->
167
- Currency .getInstance (Locale .of ("" , "EU" )), "Did not throw IAE" );
186
+ 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 ());
168
190
}
169
191
170
192
// Ensure a selection of countries have the expected currency
0 commit comments