|
1 | 1 | /** |
2 | | - * Copyright (c) 2012, 2018, Werner Keil and others by the @author tag. |
| 2 | + * Copyright (c) 2012, 2019, Werner Keil and others by the @author tag. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | 5 | * use this file except in compliance with the License. You may obtain a copy of |
|
15 | 15 | */ |
16 | 16 | package org.javamoney.moneta.format; |
17 | 17 |
|
| 18 | +import static java.util.Locale.CHINA; |
| 19 | +import static java.util.Locale.FRANCE; |
| 20 | +import static java.util.Locale.GERMANY; |
| 21 | +import static org.javamoney.moneta.format.CurrencyStyle.CODE; |
18 | 22 | import static org.testng.Assert.assertEquals; |
19 | 23 |
|
20 | | -import java.math.BigDecimal; |
21 | 24 | import java.util.Locale; |
22 | 25 |
|
23 | 26 | import javax.money.MonetaryAmount; |
| 27 | +import javax.money.format.AmountFormatQuery; |
24 | 28 | import javax.money.format.AmountFormatQueryBuilder; |
25 | 29 | import javax.money.format.MonetaryAmountFormat; |
26 | 30 | import javax.money.format.MonetaryFormats; |
|
29 | 33 | import org.testng.annotations.Test; |
30 | 34 |
|
31 | 35 | public class MonetaryFormatsTest { |
32 | | - |
33 | | - @Test(enabled=false) |
34 | | - public void testParseDK() { |
35 | | - final Locale.Builder localeBuilder = new Locale.Builder(); |
36 | | - localeBuilder.setLanguage("da"); // Danish |
37 | | - MonetaryAmountFormat format = MonetaryFormats |
38 | | - .getAmountFormat(AmountFormatQueryBuilder.of(localeBuilder.build()) |
39 | | - .set(CurrencyStyle.CODE) |
40 | | - .build()); |
41 | | - MonetaryAmount amountOk = format.parse("123,01 DKK"); |
42 | | - MonetaryAmount amountKo = format.parse("14 000,12 DKK"); |
43 | | - assertEquals(amountOk.getNumber().doubleValueExact(), 123.01); // OK |
44 | | - assertEquals(amountKo.getNumber().doubleValueExact(), 14000.12); // KO |
45 | | - } |
46 | | - |
47 | | - @Test(enabled=false) |
48 | | - public void testParseFR() { |
49 | | - MonetaryAmountFormat format = MonetaryFormats |
50 | | - .getAmountFormat(AmountFormatQueryBuilder.of(Locale.FRANCE) |
51 | | - .set(CurrencyStyle.CODE) |
52 | | - .build()); |
53 | | - MonetaryAmount amountOk = format.parse("123,01 EUR"); |
54 | | - MonetaryAmount amountKo = format.parse("14 000,12 EUR"); |
55 | | - assertEquals(amountOk.getNumber().doubleValueExact(), 123.01); // OK |
56 | | - assertEquals(amountKo.getNumber().doubleValueExact(), 14000.12); // KO |
57 | | - } |
58 | | - |
59 | | - /** |
60 | | - * Test related to parsing and formatting for India. |
61 | | - * https://github.com/JavaMoney/jsr354-ri-bp/issues/55 |
62 | | - */ |
63 | | - @Test(enabled=false) |
64 | | - public void testRupeeFormatting() { |
65 | | - BigDecimal amount = new BigDecimal("67890000000000"); |
66 | | - Locale india = new Locale("en, IN"); |
67 | | - |
68 | | - MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(india); |
69 | | - Money money = Money.of(amount, "INR"); |
70 | | - String expectedFormattedString = "INR 6,78,90,00,00,00,000.00"; |
71 | | - assertEquals(format.format(money), expectedFormattedString); |
72 | | - assertEquals(money, Money.parse(expectedFormattedString, format)); |
| 36 | + private static final Locale DANISH = new Locale("da"); |
| 37 | + private static final Locale BULGARIA = new Locale("bg", "BG"); |
| 38 | + public static final Locale INDIA = new Locale("en, IN"); |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testParse_DKK_da() { |
| 42 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(DANISH).set(CODE).build(); |
| 43 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 44 | + assertMoneyParse(format, "123 DKK", 123.0, "DKK"); |
| 45 | + assertMoneyParse(format, "123,01 DKK", 123.01, "DKK"); |
| 46 | + assertMoneyParse(format, "14.000,12 DKK", 14000.12, "DKK"); |
| 47 | + assertMoneyParse(format, "14.000,12\u00A0DKK", 14000.12, "DKK"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testFormat_DKK_da() { |
| 52 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(DANISH).set(CODE).build(); |
| 53 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 54 | + assertMoneyFormat(format, Money.of(123.01, "DKK"), "123,01 DKK"); |
| 55 | + assertMoneyFormat(format, Money.of(14000.12, "DKK"), "14.000,12 DKK"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testParse_EUR_fr_FR() { |
| 60 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(FRANCE).set(CODE).build(); |
| 61 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 62 | + assertMoneyParse(format, "123 EUR", 123.0, "EUR"); |
| 63 | + assertMoneyParse(format, "123,01 EUR", 123.01, "EUR"); |
| 64 | + assertMoneyParse(format, "14 000,12 EUR", 14000.12, "EUR"); |
| 65 | + assertMoneyParse(format, "14\u00A0000,12\u00A0EUR", 14000.12, "EUR"); |
| 66 | + assertMoneyParse(format, "14\u202F000,12\u00A0EUR", 14000.12, "EUR"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testFormat_EUR_fr_FR() { |
| 71 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(FRANCE).set(CODE).build(); |
| 72 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 73 | + assertMoneyFormat(format, Money.of(123.01, "EUR"), "123,01 EUR"); |
| 74 | + assertMoneyFormat(format, Money.of(14000.12, "EUR"), "14 000,12 EUR"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testParse_BGN_bg_BG() { |
| 79 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(BULGARIA).set(CODE).build(); |
| 80 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 81 | + assertMoneyParse(format, "123 BGN", 123.0, "BGN"); |
| 82 | + assertMoneyParse(format, "123,01 BGN", 123.01, "BGN"); |
| 83 | + assertMoneyParse(format, "14 000,12 BGN", 14000.12, "BGN"); |
| 84 | + assertMoneyParse(format, "14\u00A0000,12\u00A0BGN", 14000.12, "BGN"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testFormat_BGN_bg_BG() { |
| 89 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(BULGARIA).set(CODE).build(); |
| 90 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 91 | + assertMoneyFormat(format, Money.of(123.01, "BGN"), "123,01 BGN"); |
| 92 | + assertMoneyFormat(format, Money.of(14000.12, "BGN"), "14 000,12 BGN"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testParse_EUR_de_DE() { |
| 97 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(GERMANY).set(CODE).build(); |
| 98 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 99 | + assertMoneyParse(format, "123 EUR", 123.0, "EUR"); |
| 100 | + assertMoneyParse(format, "123,01 EUR", 123.01, "EUR"); |
| 101 | + assertMoneyParse(format, "14.000,12 EUR", 14000.12, "EUR"); |
| 102 | + assertMoneyParse(format, "14.000,12\u00A0EUR", 14000.12, "EUR"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testFormat_EUR_de_DE() { |
| 107 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(GERMANY).set(CODE).build(); |
| 108 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 109 | + assertMoneyFormat(format, Money.of(123.01, "EUR"), "123,01 EUR"); |
| 110 | + assertMoneyFormat(format, Money.of(14000.12, "EUR"), "14.000,12 EUR"); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testParse_INR_en_IN() { |
| 115 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(INDIA); |
| 116 | + assertMoneyParse(format, "INR 6,78,90,00,00,00,000.00", 67890000000000L, "INR"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testFormat_INR_en_IN() { |
| 121 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(INDIA); |
| 122 | + assertMoneyFormat(format, Money.of(67890000000000L, "INR"), "INR 67,890,000,000,000.00"); |
| 123 | +//TODO assertMoneyFormat(format, Money.of(67890000000000L, "INR"), "INR 6,78,90,00,00,00,000.00"); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testParse_CNY_zh_CN() { |
| 128 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(CHINA).set(CODE).build(); |
| 129 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 130 | + assertMoneyParse(format, "CNY123", 123.0, "CNY"); |
| 131 | + assertMoneyParse(format, "CNY123.01", 123.01, "CNY"); |
| 132 | + assertMoneyParse(format, "CNY14,000.12", 14000.12, "CNY"); |
| 133 | + assertMoneyParse(format, "CNY 14,000.12", 14000.12, "CNY"); |
| 134 | + assertMoneyParse(format, "CNY\u00A014,000.12", 14000.12, "CNY"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testFormat_CNY_zh_CN() { |
| 139 | + AmountFormatQuery formatQuery = AmountFormatQueryBuilder.of(CHINA).set(CODE).build(); |
| 140 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(formatQuery); |
| 141 | + assertMoneyFormat(format, Money.of(123.01, "CNY"), "CNY123.01"); |
| 142 | + assertMoneyFormat(format, Money.of(14000.12, "CNY"), "CNY14,000.12"); |
| 143 | + } |
| 144 | + |
| 145 | + private void assertMoneyParse(MonetaryAmountFormat format, String text, double expected, String currencyCode) { |
| 146 | + MonetaryAmount amountInt = format.parse(text); |
| 147 | + assertEquals(amountInt.getNumber().doubleValueExact(), expected); |
| 148 | + assertEquals(amountInt.getCurrency().getCurrencyCode(), currencyCode); |
| 149 | + } |
| 150 | + |
| 151 | + private void assertMoneyFormat(MonetaryAmountFormat format, MonetaryAmount amount, String expected) { |
| 152 | + String formatted = format.format(amount); |
| 153 | + assertEquals(formatted, expected); |
73 | 154 | } |
74 | 155 | } |
0 commit comments