|
| 1 | +/* |
| 2 | + * Copyright (c) 2012, 2019, Werner Keil and others by the @author tag. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.javamoney.moneta.format; |
| 17 | + |
| 18 | +import static org.testng.Assert.assertEquals; |
| 19 | + |
| 20 | +import java.math.BigDecimal; |
| 21 | +import java.util.Locale; |
| 22 | + |
| 23 | +import javax.money.MonetaryAmount; |
| 24 | +import javax.money.format.AmountFormatQueryBuilder; |
| 25 | +import javax.money.format.MonetaryAmountFormat; |
| 26 | +import javax.money.format.MonetaryFormats; |
| 27 | + |
| 28 | +import org.javamoney.moneta.Money; |
| 29 | +import org.testng.annotations.Ignore; |
| 30 | +import org.testng.annotations.Test; |
| 31 | + |
| 32 | +public class MonetaryFormatsParseTest { |
| 33 | + public static final Locale INDIA = new Locale("en, IN"); |
| 34 | + |
| 35 | + /** |
| 36 | + * Test related to parsing currency symbols. |
| 37 | + */ |
| 38 | + @Test |
| 39 | + @Ignore("see https://github.com/JavaMoney/jsr354-ri/issues/274") |
| 40 | + public void testParseCurrencySymbol1() { |
| 41 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat( |
| 42 | + AmountFormatQueryBuilder.of(Locale.GERMANY) |
| 43 | + .set(CurrencyStyle.SYMBOL) |
| 44 | + .build()); |
| 45 | + Money money = Money.of(new BigDecimal("1234567.89"), "EUR"); |
| 46 | + String expectedFormattedString = "1.234.567,89 €"; |
| 47 | + assertEquals(expectedFormattedString, format.format(money)); |
| 48 | + assertEquals(money, Money.parse(expectedFormattedString, format)); |
| 49 | + |
| 50 | + money = Money.of(new BigDecimal("1234567.89"), "INR"); |
| 51 | + expectedFormattedString = "1.234.567,89 ₹"; |
| 52 | + assertEquals(expectedFormattedString, format.format(money)); |
| 53 | + assertEquals(money, Money.parse(expectedFormattedString, format)); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test related to parsing currency symbols. |
| 58 | + */ |
| 59 | + @Test |
| 60 | + @Ignore("see https://github.com/JavaMoney/jsr354-ri/issues/274") |
| 61 | + public void testParseCurrencySymbol2() { |
| 62 | + MonetaryAmountFormat format = MonetaryFormats.getAmountFormat( |
| 63 | + AmountFormatQueryBuilder.of(INDIA) |
| 64 | + .set(CurrencyStyle.SYMBOL) |
| 65 | + .build()); |
| 66 | + Money money = Money.of(new BigDecimal("1234567.89"), "EUR"); |
| 67 | + String expectedFormattedString = "€ 1,234,567.89"; |
| 68 | + assertEquals(expectedFormattedString, format.format(money)); |
| 69 | + assertEquals(money, Money.parse(expectedFormattedString, format)); |
| 70 | + |
| 71 | + money = Money.of(new BigDecimal("1234567.89"), "INR"); |
| 72 | + expectedFormattedString = "₹ 1,234,567.89"; |
| 73 | + assertEquals(expectedFormattedString, format.format(money)); |
| 74 | + assertEquals(money, Money.parse(expectedFormattedString, format)); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + private void assertMoneyParse(MonetaryAmountFormat format, String text, double expected, String currencyCode) { |
| 79 | + MonetaryAmount amountInt = format.parse(text); |
| 80 | + assertEquals(amountInt.getNumber().doubleValueExact(), expected); |
| 81 | + assertEquals(amountInt.getCurrency().getCurrencyCode(), currencyCode); |
| 82 | + } |
| 83 | + |
| 84 | + private void assertMoneyFormat(MonetaryAmountFormat format, MonetaryAmount amount, String expected) { |
| 85 | + String formatted = format.format(amount); |
| 86 | + assertEquals(formatted, expected); |
| 87 | + } |
| 88 | +} |
0 commit comments