/** @tests java.text.DecimalFormatSymbols#setInternationalCurrencySymbol(java.lang.String) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "setInternationalCurrencySymbol",
      args = {java.lang.String.class})
  @KnownFailure("getCurrency() doesn't return null for bogus currency code.")
  public void test_setInternationalCurrencySymbolLjava_lang_String() {
    Locale locale = Locale.CANADA;
    DecimalFormatSymbols dfs =
        ((DecimalFormat) NumberFormat.getCurrencyInstance(locale)).getDecimalFormatSymbols();
    Currency currency = Currency.getInstance("JPY");
    dfs.setInternationalCurrencySymbol(currency.getCurrencyCode());

    assertTrue("Test1: Returned incorrect currency", currency == dfs.getCurrency());
    assertEquals(
        "Test1: Returned incorrect currency symbol",
        currency.getSymbol(locale),
        dfs.getCurrencySymbol());
    assertTrue(
        "Test1: Returned incorrect international currency symbol",
        currency.getCurrencyCode().equals(dfs.getInternationalCurrencySymbol()));

    String symbol = dfs.getCurrencySymbol();
    dfs.setInternationalCurrencySymbol("bogus");
    assertNull("Test2: Returned incorrect currency", dfs.getCurrency());
    assertTrue("Test2: Returned incorrect currency symbol", dfs.getCurrencySymbol().equals(symbol));
    assertEquals(
        "Test2: Returned incorrect international currency symbol",
        "bogus",
        dfs.getInternationalCurrencySymbol());
  }
 static void assertDecimalFormatSymbolsRIFrance(DecimalFormatSymbols dfs) {
   // Values based on Java 1.5 RI DecimalFormatSymbols for Locale.FRANCE
   /*
    * currency = [EUR]
    * currencySymbol = [€][U+20ac]
    * decimalSeparator = [,][U+002c]
    * digit = [#][U+0023]
    * groupingSeparator = [ ][U+00a0]
    * infinity = [∞][U+221e]
    * internationalCurrencySymbol = [EUR]
    * minusSign = [-][U+002d]
    * monetaryDecimalSeparator = [,][U+002c]
    * naN = [�][U+fffd]
    * patternSeparator = [;][U+003b]
    * perMill = [‰][U+2030]
    * percent = [%][U+0025]
    * zeroDigit = [0][U+0030]
    */
   assertEquals("EUR", dfs.getCurrency().getCurrencyCode());
   assertEquals("\u20AC", dfs.getCurrencySymbol());
   assertEquals(',', dfs.getDecimalSeparator());
   assertEquals('#', dfs.getDigit());
   assertEquals('\u00a0', dfs.getGroupingSeparator());
   assertEquals("\u221e", dfs.getInfinity());
   assertEquals("EUR", dfs.getInternationalCurrencySymbol());
   assertEquals('-', dfs.getMinusSign());
   assertEquals(',', dfs.getMonetaryDecimalSeparator());
   // RI's default NaN is U+FFFD, Harmony's is based on ICU
   assertEquals("\uFFFD", dfs.getNaN());
   assertEquals('\u003b', dfs.getPatternSeparator());
   assertEquals('\u2030', dfs.getPerMill());
   assertEquals('%', dfs.getPercent());
   assertEquals('0', dfs.getZeroDigit());
 }
  /** @tests java.text.DecimalFormatSymbols#setCurrency(java.util.Currency) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "setCurrency",
      args = {java.util.Currency.class})
  public void test_setCurrencyLjava_util_Currency() {
    Locale locale = Locale.CANADA;
    DecimalFormatSymbols dfs =
        ((DecimalFormat) NumberFormat.getCurrencyInstance(locale)).getDecimalFormatSymbols();

    try {
      dfs.setCurrency(null);
      fail("Expected NullPointerException");
    } catch (NullPointerException e) {
    }

    Currency currency = Currency.getInstance("JPY");
    dfs.setCurrency(currency);

    assertTrue("Returned incorrect currency", currency == dfs.getCurrency());
    assertEquals(
        "Returned incorrect currency symbol", currency.getSymbol(locale), dfs.getCurrencySymbol());
    assertTrue(
        "Returned incorrect international currency symbol",
        currency.getCurrencyCode().equals(dfs.getInternationalCurrencySymbol()));
  }
Exemplo n.º 4
0
  private static void describeDecimalFormatSymbols(StringBuilder result, DecimalFormatSymbols dfs) {
    result.append("Currency Symbol: " + unicodeString(dfs.getCurrencySymbol()) + "\n");
    result.append(
        "International Currency Symbol: "
            + unicodeString(dfs.getInternationalCurrencySymbol())
            + "\n");
    result.append("<p>");

    result.append("Digit: " + unicodeString(dfs.getDigit()) + "\n");
    result.append("Pattern Separator: " + unicodeString(dfs.getPatternSeparator()) + "\n");
    result.append("<p>");

    result.append("Decimal Separator: " + unicodeString(dfs.getDecimalSeparator()) + "\n");
    result.append(
        "Monetary Decimal Separator: " + unicodeString(dfs.getMonetaryDecimalSeparator()) + "\n");
    // 1.6: result.append("Exponent Separator: " + dfs.getExponentSeparator() + "\n");
    result.append("Grouping Separator: " + unicodeString(dfs.getGroupingSeparator()) + "\n");

    result.append("Infinity: " + unicodeString(dfs.getInfinity()) + "\n");
    result.append("Minus Sign: " + unicodeString(dfs.getMinusSign()) + "\n");
    result.append("NaN: " + unicodeString(dfs.getNaN()) + "\n");
    result.append("Percent: " + unicodeString(dfs.getPercent()) + "\n");
    result.append("Per Mille: " + unicodeString(dfs.getPerMill()) + "\n");
    result.append("Zero Digit: " + unicodeString(dfs.getZeroDigit()) + "\n");
    StringBuilder digits = new StringBuilder();
    for (int i = 0; i <= 9; ++i) {
      digits.append((char) (dfs.getZeroDigit() + i));
    }
    result.append("Digits: " + digits.toString() + "\n");
    result.append("<p>");
    result.append("<p>");
  }
 /** @tests java.text.DecimalFormatSymbols#getInternationalCurrencySymbol() */
 @TestTargetNew(
     level = TestLevel.COMPLETE,
     notes = "",
     method = "getInternationalCurrencySymbol",
     args = {})
 public void test_getInternationalCurrencySymbol() {
   assertEquals(
       "Returned incorrect InternationalCurrencySymbol",
       "USD",
       dfsUS.getInternationalCurrencySymbol());
 }
 /** Adjusts {@link DecimalFormat}'s symbol according to given {@link CurrencyModel}. */
 protected DecimalFormat adjustSymbol(
     final DecimalFormat format, final CurrencyModel currencyModel) {
   final String symbol = currencyModel.getSymbol();
   if (symbol != null) {
     final DecimalFormatSymbols symbols = format.getDecimalFormatSymbols(); // does cloning
     final String iso = currencyModel.getIsocode();
     boolean changed = false;
     if (!iso.equalsIgnoreCase(symbols.getInternationalCurrencySymbol())) {
       symbols.setInternationalCurrencySymbol(iso);
       changed = true;
     }
     if (!symbol.equals(symbols.getCurrencySymbol())) {
       symbols.setCurrencySymbol(symbol);
       changed = true;
     }
     if (changed) {
       format.setDecimalFormatSymbols(symbols);
     }
   }
   return format;
 }
  /** @tests java.text.DecimalFormatSymbols#getCurrency() */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "getCurrency",
      args = {})
  public void test_getCurrency() {
    Locale csCzLocale = new Locale("cs", "CZ");
    Locale czLocale = new Locale("", "CZ");
    Locale csLocale = new Locale("cs", "");
    Locale deLocale = new Locale("de", "AT");
    Locale[] requiredLocales = {Locale.US, csCzLocale, czLocale, csLocale, deLocale};
    if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
      // locale dependent test, bug 1943269
      return;
    }
    Currency currency = Currency.getInstance("USD");
    assertEquals("Returned incorrect currency", dfsUS.getCurrency(), currency);

    // use cs_CZ instead
    // Currency currK = Currency.getInstance("KRW");
    Currency currC = Currency.getInstance("CZK");
    Currency currX = Currency.getInstance("XXX");
    Currency currE = Currency.getInstance("EUR");
    // Currency currF = Currency.getInstance("FRF");

    DecimalFormatSymbols dfs1 = new DecimalFormatSymbols(csCzLocale);
    assertTrue("Test1: Returned incorrect currency", dfs1.getCurrency() == currC);
    assertEquals("Test1: Returned incorrect currencySymbol", "K\u010d", dfs1.getCurrencySymbol());
    assertEquals(
        "Test1: Returned incorrect intlCurrencySymbol",
        "CZK",
        dfs1.getInternationalCurrencySymbol());

    dfs1 = new DecimalFormatSymbols(czLocale);
    assertTrue("Test2: Returned incorrect currency", dfs1.getCurrency() == currC);
    assertEquals("Test2: Returned incorrect currencySymbol", "CZK", dfs1.getCurrencySymbol());
    assertEquals(
        "Test2: Returned incorrect intlCurrencySymbol",
        "CZK",
        dfs1.getInternationalCurrencySymbol());

    dfs1 = new DecimalFormatSymbols(csLocale);
    assertEquals("Test3: Returned incorrect currency", currX, dfs1.getCurrency());
    assertEquals("Test3: Returned incorrect currencySymbol", "\u00a4", dfs1.getCurrencySymbol());
    assertEquals(
        "Test3: Returned incorrect intlCurrencySymbol",
        "XXX",
        dfs1.getInternationalCurrencySymbol());

    dfs1 = new DecimalFormatSymbols(deLocale);
    assertTrue("Test4: Returned incorrect currency", dfs1.getCurrency() == currE);
    assertEquals("Test4: Returned incorrect currencySymbol", "\u20ac", dfs1.getCurrencySymbol());
    assertEquals(
        "Test4: Returned incorrect intlCurrencySymbol",
        "EUR",
        dfs1.getInternationalCurrencySymbol());

    // RI fails these tests since it doesn't have the PREEURO variant
    // dfs1 = new DecimalFormatSymbols(new Locale("fr", "FR","PREEURO"));
    // assertTrue("Test5: Returned incorrect currency", dfs1.getCurrency()
    // == currF);
    // assertTrue("Test5: Returned incorrect currencySymbol",
    // dfs1.getCurrencySymbol().equals("F"));
    // assertTrue("Test5: Returned incorrect intlCurrencySymbol",
    // dfs1.getInternationalCurrencySymbol().equals("FRF"));
  }