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 * @test
25 * @bug 4290801 4692419 4693631 5101540 5104960 6296410 6336600 6371531
26 * 6488442 7036905 8039317 8074350 8074351
27 * @summary Basic tests for Currency class.
28 */
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.ObjectInputStream;
33 import java.io.ObjectOutputStream;
34 import java.util.Calendar;
35 import java.util.Date;
36 import java.util.Currency;
37 import java.util.GregorianCalendar;
38 import java.util.Locale;
39 import java.util.TimeZone;
40
41
42 public class CurrencyTest {
43
44 public static void main(String[] args) throws Exception {
45 CheckDataVersion.check();
46 testCurrencyCodeValidation();
81 static void testInvalidCurrency(String currencyCode) {
82 boolean gotException = false;
83 try {
84 Currency currency = Currency.getInstance(currencyCode);
85 } catch (IllegalArgumentException e) {
86 gotException = true;
87 }
88 if (!gotException) {
89 throw new RuntimeException("didn't get specified exception");
90 }
91 }
92
93 static void testLocaleMapping() {
94 // very basic test: most countries have their own currency, and then
95 // their currency code is an extension of their country code.
96 Locale[] locales = Locale.getAvailableLocales();
97 int goodCountries = 0;
98 int ownCurrencies = 0;
99 for (int i = 0; i < locales.length; i++) {
100 Locale locale = locales[i];
101 if (locale.getCountry().length() == 0) {
102 boolean gotException = false;
103 try {
104 Currency.getInstance(locale);
105 } catch (IllegalArgumentException e) {
106 gotException = true;
107 }
108 if (!gotException) {
109 throw new RuntimeException("didn't get specified exception");
110 }
111 } else {
112 goodCountries++;
113 Currency currency = Currency.getInstance(locale);
114 if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {
115 ownCurrencies++;
116 }
117 }
118 }
119 System.out.println("Countries tested: " + goodCountries +
120 ", own currencies: " + ownCurrencies);
121 if (ownCurrencies < (goodCountries / 2 + 1)) {
232 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
233 ObjectInputStream iStream = new ObjectInputStream(bais);
234 Currency currency2 = (Currency) iStream.readObject();
235
236 if (currency1 != currency2) {
237 throw new RuntimeException("serialization breaks class invariant");
238 }
239 }
240
241 static void testDisplayNames() {
242 // null argument test
243 try {
244 testDisplayName("USD", null, "");
245 throw new RuntimeException("getDisplayName(NULL) did not throw an NPE.");
246 } catch (NullPointerException npe) {}
247
248 testDisplayName("USD", Locale.ENGLISH, "US Dollar");
249 testDisplayName("FRF", Locale.FRENCH, "franc fran\u00e7ais");
250 testDisplayName("DEM", Locale.GERMAN, "Deutsche Mark");
251 testDisplayName("ESP", new Locale("es"), "peseta espa\u00f1ola");
252 testDisplayName("ITL", new Locale("it"), "Lira Italiana");
253 testDisplayName("JPY", Locale.JAPANESE, "\u65e5\u672c\u5186");
254 testDisplayName("KRW", Locale.KOREAN, "\ub300\ud55c\ubbfc\uad6d \uc6d0");
255 testDisplayName("SEK", new Locale("sv"), "svensk krona");
256 testDisplayName("CNY", Locale.SIMPLIFIED_CHINESE, "\u4eba\u6c11\u5e01");
257 testDisplayName("TWD", Locale.TRADITIONAL_CHINESE, "\u65b0\u81fa\u5e63");
258 }
259
260 static void testDisplayName(String currencyCode, Locale locale, String expectedName) {
261 String name = Currency.getInstance(currencyCode).getDisplayName(locale);
262 if (!name.equals(expectedName)) {
263 throw new RuntimeException("Wrong display name for currency " +
264 currencyCode +": expected '" + expectedName +
265 "', got '" + name + "'");
266 }
267 }
268
269 static void testFundsCodes() {
270 testValidCurrency("BOV");
271 testValidCurrency("CHE");
272 testValidCurrency("CHW");
273 testValidCurrency("CLF");
274 testValidCurrency("COU");
275 testValidCurrency("MXV");
276 testValidCurrency("USN");
277 testValidCurrency("UYI");
278
279 testFractionDigits("BOV", 2);
280 testFractionDigits("CHE", 2);
281 testFractionDigits("CHW", 2);
282 testFractionDigits("CLF", 4);
283 testFractionDigits("COU", 2);
284 testFractionDigits("MXV", 2);
285 testFractionDigits("USN", 2);
286 testFractionDigits("UYI", 0);
287
288 testNumericCode("BOV", 984);
|
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 * @test
25 * @bug 4290801 4692419 4693631 5101540 5104960 6296410 6336600 6371531
26 * 6488442 7036905 8008577 8039317 8074350 8074351
27 * @summary Basic tests for Currency class.
28 */
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.ObjectInputStream;
33 import java.io.ObjectOutputStream;
34 import java.util.Calendar;
35 import java.util.Date;
36 import java.util.Currency;
37 import java.util.GregorianCalendar;
38 import java.util.Locale;
39 import java.util.TimeZone;
40
41
42 public class CurrencyTest {
43
44 public static void main(String[] args) throws Exception {
45 CheckDataVersion.check();
46 testCurrencyCodeValidation();
81 static void testInvalidCurrency(String currencyCode) {
82 boolean gotException = false;
83 try {
84 Currency currency = Currency.getInstance(currencyCode);
85 } catch (IllegalArgumentException e) {
86 gotException = true;
87 }
88 if (!gotException) {
89 throw new RuntimeException("didn't get specified exception");
90 }
91 }
92
93 static void testLocaleMapping() {
94 // very basic test: most countries have their own currency, and then
95 // their currency code is an extension of their country code.
96 Locale[] locales = Locale.getAvailableLocales();
97 int goodCountries = 0;
98 int ownCurrencies = 0;
99 for (int i = 0; i < locales.length; i++) {
100 Locale locale = locales[i];
101 String ctryCode = locale.getCountry();
102 int ctryLength = ctryCode.length();
103 if (ctryLength == 0 ||
104 ctryLength == 3 || // UN M.49 code
105 ctryCode.matches("AA|Q[M-Z]|X[A-Z]|ZZ" + // user defined codes
106 "AC|CP|DG|EA|EU|FX|IC|SU|TA|UK")) { // exceptional reservation codes
107 boolean gotException = false;
108 try {
109 Currency.getInstance(locale);
110 } catch (IllegalArgumentException e) {
111 gotException = true;
112 }
113 if (!gotException) {
114 throw new RuntimeException("didn't get specified exception");
115 }
116 } else {
117 goodCountries++;
118 Currency currency = Currency.getInstance(locale);
119 if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {
120 ownCurrencies++;
121 }
122 }
123 }
124 System.out.println("Countries tested: " + goodCountries +
125 ", own currencies: " + ownCurrencies);
126 if (ownCurrencies < (goodCountries / 2 + 1)) {
237 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
238 ObjectInputStream iStream = new ObjectInputStream(bais);
239 Currency currency2 = (Currency) iStream.readObject();
240
241 if (currency1 != currency2) {
242 throw new RuntimeException("serialization breaks class invariant");
243 }
244 }
245
246 static void testDisplayNames() {
247 // null argument test
248 try {
249 testDisplayName("USD", null, "");
250 throw new RuntimeException("getDisplayName(NULL) did not throw an NPE.");
251 } catch (NullPointerException npe) {}
252
253 testDisplayName("USD", Locale.ENGLISH, "US Dollar");
254 testDisplayName("FRF", Locale.FRENCH, "franc fran\u00e7ais");
255 testDisplayName("DEM", Locale.GERMAN, "Deutsche Mark");
256 testDisplayName("ESP", new Locale("es"), "peseta espa\u00f1ola");
257 testDisplayName("ITL", new Locale("it"), "lira italiana");
258 testDisplayName("JPY", Locale.JAPANESE, "\u65e5\u672c\u5186");
259 testDisplayName("KRW", Locale.KOREAN, "\ub300\ud55c\ubbfc\uad6d \uc6d0");
260 testDisplayName("SEK", new Locale("sv"), "svensk krona");
261 testDisplayName("CNY", Locale.SIMPLIFIED_CHINESE, "\u4eba\u6c11\u5e01");
262 testDisplayName("TWD", Locale.TRADITIONAL_CHINESE, "\u65b0\u81fa\u5e63");
263 }
264
265 static void testDisplayName(String currencyCode, Locale locale, String expectedName) {
266 String name = Currency.getInstance(currencyCode).getDisplayName(locale);
267 if (!name.equals(expectedName)) {
268 throw new RuntimeException("Wrong display name for currency " +
269 currencyCode +": expected '" + expectedName +
270 "', got '" + name + "'");
271 }
272 }
273 static void testFundsCodes() {
274 testValidCurrency("BOV");
275 testValidCurrency("CHE");
276 testValidCurrency("CHW");
277 testValidCurrency("CLF");
278 testValidCurrency("COU");
279 testValidCurrency("MXV");
280 testValidCurrency("USN");
281 testValidCurrency("UYI");
282
283 testFractionDigits("BOV", 2);
284 testFractionDigits("CHE", 2);
285 testFractionDigits("CHW", 2);
286 testFractionDigits("CLF", 4);
287 testFractionDigits("COU", 2);
288 testFractionDigits("MXV", 2);
289 testFractionDigits("USN", 2);
290 testFractionDigits("UYI", 0);
291
292 testNumericCode("BOV", 984);
|