Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Backport unable to parse valid currencies #53

Closed
keilw opened this issue Dec 24, 2018 · 7 comments
Closed

Backport unable to parse valid currencies #53

keilw opened this issue Dec 24, 2018 · 7 comments

Comments

@keilw
Copy link
Member

keilw commented Dec 24, 2018

When testing #46 with other locales also using spaces to separate large numbers, it fails for certain countries/locales the same way, while others show a different problem:

FAILED: testParseDK
javax.money.MonetaryException: Error parsing CurrencyUnit.
	at org.javamoney.moneta.internal.format.CurrencyToken.parse(CurrencyToken.java:226)
	at org.javamoney.moneta.internal.format.DefaultMonetaryAmountFormat.parse(DefaultMonetaryAmountFormat.java:192)
	at org.javamoney.moneta.format.MonetaryFormatsTest.testParseDK(MonetaryFormatsTest.java:39)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
	at org.testng.TestRunner.privateRun(TestRunner.java:746)
	at org.testng.TestRunner.run(TestRunner.java:600)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
	at org.testng.SuiteRunner.run(SuiteRunner.java:268)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1189)
	at org.testng.TestNG.runSuites(TestNG.java:1104)
	at org.testng.TestNG.run(TestNG.java:1076)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: UnknownCurrencyException [currencyCode=]
	at org.javamoney.moneta.spi.base.BaseMonetaryCurrenciesSingletonSpi.getCurrency(BaseMonetaryCurrenciesSingletonSpi.java:53)
	at javax.money.Monetary.getCurrency(Monetary.java:128)
	at org.javamoney.moneta.internal.format.CurrencyToken.parse(CurrencyToken.java:194)

DKK is a perfectly valid currency code and recognized in the Java 8+ Moneta RI.

@keilw
Copy link
Member Author

keilw commented Dec 24, 2018

This is the test showing the bug:

	@Test
	public void testParseDK() {
		final Locale.Builder localeBuilder = new Locale.Builder();
		localeBuilder.setLanguage("da"); // Danish
	    MonetaryAmountFormat format = MonetaryFormats
	            .getAmountFormat(AmountFormatQueryBuilder.of(localeBuilder.build())
	                    .set(CurrencyStyle.CODE)
	                    .build());
	    MonetaryAmount amountOk = format.parse("123,01 DKK"); // Fails in BP
	    MonetaryAmount amountKo = format.parse("14 000,12 DKK");
	    assertEquals(amountOk.getNumber().doubleValueExact(), 123.01); // OK
	    assertEquals(amountKo.getNumber().doubleValueExact(), 14000.12); // KO
	}

@keilw keilw added this to the 1.4 milestone Dec 24, 2018
@keilw keilw pinned this issue Dec 24, 2018
@acelopezco
Copy link
Contributor

I'm looking into this and here is what I have found so far.

First MonetaryAmount tested: "123,01 DKK"

I debugged the code and found that the pattern for DKK according to the java.text.DecimalFormat class is: ¤ #,##0.00

So if we test a MonetaryAmount like "DKK 123,01", the test will pass. During the revision I found that there was another issue JavaMoney/jsr354-ri#151 regarding to the Bulgarian formatting pattern so I applied the same fix and it worked:
jsr354-ri-bp-53-fixa

however, I'm not sure if that's the correct way of fixing this kind of issues, what will happen when we find another issue like this? I'm not sure that adding more else if clauses is the right move. So before pushing my changes I wanted to discuss them with you.

Regarding to the second MonetaryAmount tested: "14 000,12 DKK"

Test is failing with this message:
java.lang.AssertionError: expected [14000.12] but found [0.12]

Clearly it is happening due to the whitespace in the number "14 000,12" as stated in #46

Side Note
I found some tests that are trying to test MonetaryAmounts like 14.000,12\u00A0DKK and are failing. I also check that and found out that the ParseContext class is using Character.isWhitespace to check for spaces when splitting the input String, however, JavaDocs explicitly says that \u00A0 is not considered a whitespace by that method... there is another method that does consider it as a space which is Character.isSpaceChar so maybe we should check for whitespaces using both methods?

@acelopezco
Copy link
Contributor

Hi @keilw any comment on this?

@keilw
Copy link
Member Author

keilw commented Feb 29, 2020

Not really, we decided to archive the BP, so it won't be part of the upcoming MR1, do you have a use case where you must continue using Java 7?

@acelopezco
Copy link
Contributor

Oh I see. I don't have a use case, I just wanted to contribute.
When/Where was that decided, If I may ask...?

@keilw
Copy link
Member Author

keilw commented Mar 1, 2020

We only discussed it with the PMO so far, but here's an announcement: https://groups.io/g/javamoney/message/19

@keilw
Copy link
Member Author

keilw commented Mar 22, 2020

Actually for some reason under Java 7 it's "DKK 123,01", not sure, if this is a problem in Moneta, but we won't fix it here any more.

@keilw keilw closed this as completed Mar 22, 2020
keilw added a commit that referenced this issue Mar 22, 2020
keilw added a commit that referenced this issue May 21, 2020
@keilw keilw unpinned this issue May 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants