Skip to content

Strange currency code strings like X+|\u0019F allow to create a CurrencyUnit #428

@gerardbosch

Description

@gerardbosch

I faced something strange when instantiating CurrencyUnit from the currencyCode string.

Some strings like "X*", "X+|" and others were allowed to create a currency.

Here is a small test to exemplify the situation and some comparison with java.util.Currency:

  @Test
  void monetaCurrencyUnit() {
    // SOME STRANGE STRINGS ARE ALLOWED! The test PASSED
    Monetary.getCurrency("X*");
    Monetary.getCurrency("X*|helloWorld");
    Monetary.getCurrency("X+|\u0019F\"CW:'NEvS�\u0017N\u000BL\u001B ~K\u0001");
  }

  @Test
  void monetaCurrencyUnitLowerHistorical() {
    // This fails as I was expecting, and that led me think that the Moneta impl of currency is more sound.
    // Because on the other hand, when using java.util.Currency, it works.
    Monetary.getCurrency("EUr");
  }

  @Test
  void jdkCurrency() {
    // Both throw exception as I would expect (not the case in Moneta `CurrencyUnit`: `Monetary.getCurrency()`)
    java.util.Currency.getInstance("X*");
    java.util.Currency.getInstance("X+|\u0019F\"CW:'NEvS�\u0017N\u000BL\u001B ~K\u0001");
  }

  @Test
  void jdkCurrencyLowerHistorical() {
    // I was not expecting this, but only some currencies work when the last char is lowercase like:
    java.util.Currency.getInstance("ESp"); // YUm ok, BEf ok, DEm ok, ADp ok (they're all historical currencies), EUr also works
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions