Java Code Examples for java.text.NumberFormat#setCurrency()

The following examples show how to use java.text.NumberFormat#setCurrency() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From coins-android with MIT License 6 votes vote down vote up
protected void updateInterface() {
    double btc = Preferences.getBtc(this);
    double rate = Preferences.getRate(this);
    double value = btc * rate;

    // Value
    String code = Preferences.getCurrencyCode(this);
    NumberFormat format = NumberFormat.getCurrencyInstance();
    format.setCurrency(Currency.getInstance(code));
    mValueLabel.setText(format.format(value));

    // BTC
    format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(10);
    mBtcLabel.setText(format.format(btc) + " BTC");

    // Updated at
    updateUpdatedAtLabel();
}
 
Example 2
Source File: NumberFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @tests java.text.NumberFormat#setCurrency(java.util.Currency)
 */
public void test_setCurrencyLjava_util_Currency() {
    // Test for method void setCurrency(java.util.Currency)
    // a subclass that supports currency formatting
    Currency currA = Currency.getInstance("ARS");
    NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU"));
    format.setCurrency(currA);
    assertSame("Returned incorrect currency", currA, format.getCurrency());

    // a subclass that doesn't support currency formatting
    ChoiceFormat cformat = new ChoiceFormat(
            "0#Less than one|1#one|1<Between one and two|2<Greater than two");
    try {
        ((NumberFormat) cformat).setCurrency(currA);
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    }
}
 
Example 3
Source File: RelatedProductsImplTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
private void assertProducts() {
    List<ProductListItem> items = relatedProducts.getProducts();
    Assert.assertFalse(items.isEmpty());
    NumberFormat priceFormatter = NumberFormat.getCurrencyInstance(Locale.US);

    for (int i = 0; i < items.size(); i++) {
        ProductListItem item = items.get(i);
        ProductInterface product = products.get(i);

        Assert.assertEquals(product.getName(), item.getTitle());
        Assert.assertEquals(product.getSku(), item.getSKU());
        Assert.assertEquals(product.getUrlKey(), item.getSlug());

        Page productPage = context.pageManager().getPage(PRODUCT_PAGE);
        SiteNavigation siteNavigation = new SiteNavigation(context.request());
        Assert.assertEquals(siteNavigation.toPageUrl(productPage, product.getUrlKey()), item.getURL());

        Money amount = product.getPriceRange().getMinimumPrice().getFinalPrice();
        Assert.assertEquals(amount.getValue(), item.getPrice(), 0);
        Assert.assertEquals(amount.getCurrency().toString(), item.getCurrency());
        priceFormatter.setCurrency(Currency.getInstance(amount.getCurrency().toString()));
        Assert.assertEquals(priceFormatter.format(amount.getValue()), item.getFormattedPrice());

        Assert.assertEquals(product.getThumbnail().getUrl(), item.getImageURL());
    }
}
 
Example 4
Source File: DecimalFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testBug71369() {
    final String nonBreakingSpace = "\u00A0";

    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.GERMAN);
    numberFormat.setCurrency(Currency.getInstance("USD"));

    assertEquals("2,01" + nonBreakingSpace + "$", numberFormat.format(2.01));

    numberFormat.setMinimumFractionDigits(0);
    numberFormat.setMaximumFractionDigits(0);

    String expected = "2" + nonBreakingSpace + "$";
    assertEquals(expected, numberFormat.format(2.01));

    // Changing the currency must not reset the digits.
    numberFormat.setCurrency(Currency.getInstance("EUR"));
    numberFormat.setCurrency(Currency.getInstance("USD"));

    assertEquals(expected, numberFormat.format(2.01));
}
 
Example 5
Source File: NumberFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_setCurrency() throws Exception {
    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

    // The Japanese Yen is a special case where the fractional digits are 0.
    Currency jpy = Currency.getInstance("JPY");
    assertEquals(0, jpy.getDefaultFractionDigits());

    nf.setCurrency(jpy);
    assertEquals(2, nf.getMinimumFractionDigits());  // Check DecimalFormat has not taken the
    assertEquals(2, nf.getMaximumFractionDigits());  // currency specific fractional digits.
    assertEquals("¥50.00", nf.format(50.00));

    // Try and explicitly request fractional digits for the specified currency.
    nf.setMaximumFractionDigits(jpy.getDefaultFractionDigits());
    assertEquals("¥50", nf.format(50.00));

    nf = NumberFormat.getCurrencyInstance(Locale.US);

    // Euro sign.
    nf.setCurrency(Currency.getInstance("EUR"));
    assertEquals("€50.00", nf.format(50.00));

    // Armenian Dram symbol.
    nf.setCurrency(Currency.getInstance("AMD"));
    assertEquals("AMD50.00", nf.format(50.00));

    // Swiss Franc ISO 4217 code.
    nf.setCurrency(Currency.getInstance("CHF"));
    assertEquals("CHF50.00", nf.format(50.00));
}
 
Example 6
Source File: ProductImplTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
@Test
public void testVariants() {
    productModel = context.request().adaptTo(ProductImpl.class);
    List<Variant> variants = productModel.getVariants();
    Assert.assertNotNull(variants);

    ConfigurableProduct cp = (ConfigurableProduct) product;
    Assert.assertEquals(cp.getVariants().size(), variants.size());

    // Old pricing should still work
    NumberFormat priceFormatter = NumberFormat.getCurrencyInstance(Locale.US);
    priceFormatter.setCurrency(Currency.getInstance(productModel.getCurrency()));

    for (int i = 0; i < variants.size(); i++) {
        Variant variant = variants.get(i);
        SimpleProduct sp = cp.getVariants().get(i).getProduct();

        Assert.assertEquals(sp.getSku(), variant.getSku());
        Assert.assertEquals(sp.getName(), variant.getName());
        Assert.assertEquals(sp.getDescription().getHtml(), variant.getDescription());

        // Old pricing API should still work
        Assert.assertEquals(variant.getPriceRange().getFinalPrice(), variant.getPrice(), 0.001);
        Assert.assertEquals(priceFormatter.format(variant.getPrice()), variant.getFormattedPrice());

        Assert.assertEquals(ProductStockStatus.IN_STOCK.equals(sp.getStockStatus()), variant.getInStock().booleanValue());
        Assert.assertEquals(sp.getColor(), variant.getColor());

        Assert.assertEquals(sp.getMediaGalleryEntries().size(), variant.getAssets().size());
        String baseMediaPath = storeConfig.getSecureBaseMediaUrl() + "catalog/product";
        for (int j = 0; j < sp.getMediaGalleryEntries().size(); j++) {
            MediaGalleryEntry mge = sp.getMediaGalleryEntries().get(j);
            Asset asset = variant.getAssets().get(j);
            Assert.assertEquals(mge.getLabel(), asset.getLabel());
            Assert.assertEquals(mge.getPosition(), asset.getPosition());
            Assert.assertEquals(mge.getMediaType(), asset.getType());
            Assert.assertEquals(baseMediaPath + mge.getFile(), asset.getPath());
        }
    }
}
 
Example 7
Source File: ProductTeaserImplTest.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyProductVariant() throws Exception {
    setUp(PRODUCTTEASER_VARIANT, false);

    // Find the selected variant
    ConfigurableProduct cp = (ConfigurableProduct) product;
    String selection = teaserResource.getValueMap().get("selection", String.class);
    String variantSku = SiteNavigation.toProductSkus(selection).getRight();
    SimpleProduct variant = cp.getVariants()
        .stream()
        .map(v -> v.getProduct())
        .filter(sp -> variantSku.equals(sp.getSku()))
        .findFirst()
        .orElse(null);

    Assert.assertEquals(variant.getName(), productTeaser.getName());
    Page productPage = context.pageManager().getPage(PRODUCT_PAGE);
    SiteNavigation siteNavigation = new SiteNavigation(context.request());
    Assert.assertEquals(siteNavigation.toProductUrl(productPage, product.getUrlKey(), variantSku), productTeaser.getUrl());

    NumberFormat priceFormatter = NumberFormat.getCurrencyInstance(Locale.US);
    Money amount = variant.getPriceRange().getMinimumPrice().getFinalPrice();
    priceFormatter.setCurrency(Currency.getInstance(amount.getCurrency().toString()));
    Assert.assertEquals(priceFormatter.format(amount.getValue()), productTeaser.getFormattedPrice());

    Assert.assertEquals(variant.getImage().getUrl(), productTeaser.getImage());
}
 
Example 8
Source File: CurrencyFormat.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
static void testFormatting() {
    boolean failed = false;
    Locale[] locales = {
        Locale.US,
        Locale.JAPAN,
        Locale.GERMANY,
        Locale.ITALY,
        new Locale("it", "IT", "EURO") };
    Currency[] currencies = {
        null,
        Currency.getInstance("USD"),
        Currency.getInstance("JPY"),
        Currency.getInstance("DEM"),
        Currency.getInstance("EUR"),
    };
    String[][] expecteds = {
        {"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"},
        {"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"},
        {"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
    };

    for (int i = 0; i < locales.length; i++) {
        Locale locale = locales[i];
        NumberFormat format = NumberFormat.getCurrencyInstance(locale);
        for (int j = 0; j < currencies.length; j++) {
            Currency currency = currencies[j];
            String expected = expecteds[i][j];
            if (currency != null) {
                format.setCurrency(currency);
                int digits = currency.getDefaultFractionDigits();
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            }
            String result = format.format(1234.56);
            if (!result.equals(expected)) {
                failed = true;
                System.out.println("FAIL: Locale " + locale
                    + (currency == null ? ", default currency" : (", currency: " + currency))
                    + ", expected: " + expected
                    + ", actual: " + result);
            }
        }
    }

    if (failed) {
        throw new RuntimeException();
    }
}
 
Example 9
Source File: LocaleController.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public String formatCurrencyString(long amount, String type) {
    type = type.toUpperCase();
    String customFormat;
    double doubleAmount;
    boolean discount = amount < 0;
    amount = Math.abs(amount);
    Currency currency = Currency.getInstance(type);
    switch (type) {
        case "CLF":
            customFormat = " %.4f";
            doubleAmount = amount / 10000.0;
            break;

        case "IRR":
            doubleAmount = amount / 100.0f;
            if (amount % 100 == 0) {
                customFormat = " %.0f";
            } else {
                customFormat = " %.2f";
            }
            break;

        case "BHD":
        case "IQD":
        case "JOD":
        case "KWD":
        case "LYD":
        case "OMR":
        case "TND":
            customFormat = " %.3f";
            doubleAmount = amount / 1000.0;
            break;

        case "BIF":
        case "BYR":
        case "CLP":
        case "CVE":
        case "DJF":
        case "GNF":
        case "ISK":
        case "JPY":
        case "KMF":
        case "KRW":
        case "MGA":
        case "PYG":
        case "RWF":
        case "UGX":
        case "UYI":
        case "VND":
        case "VUV":
        case "XAF":
        case "XOF":
        case "XPF":
            customFormat = " %.0f";
            doubleAmount = amount;
            break;

        case "MRO":
            customFormat = " %.1f";
            doubleAmount = amount / 10.0;
            break;

        default:
            customFormat = " %.2f";
            doubleAmount = amount / 100.0;
            break;
    }
    String result;
    if (currency != null) {
        NumberFormat format = NumberFormat.getCurrencyInstance(currentLocale != null ? currentLocale : systemDefaultLocale);
        format.setCurrency(currency);
        if (type.equals("IRR")) {
            format.setMaximumFractionDigits(0);
        }
        return (discount ? "-" : "") + format.format(doubleAmount);
    }
    return (discount ? "-" : "") + String.format(Locale.US, type + customFormat, doubleAmount);
}
 
Example 10
Source File: DecimalFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private String formatArbitraryCurrencyAmountInLocale(Currency currency, Locale locale) {
    NumberFormat localeCurrencyFormat = NumberFormat.getCurrencyInstance(locale);
    localeCurrencyFormat.setCurrency(currency);
    return localeCurrencyFormat.format(1000);
}
 
Example 11
Source File: CurrencyFormat.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void testFormatting() {
    boolean failed = false;
    Locale[] locales = {
        Locale.US,
        Locale.JAPAN,
        Locale.GERMANY,
        Locale.ITALY,
        new Locale("it", "IT", "EURO") };
    Currency[] currencies = {
        null,
        Currency.getInstance("USD"),
        Currency.getInstance("JPY"),
        Currency.getInstance("DEM"),
        Currency.getInstance("EUR"),
    };
    String[][] expecteds = {
        {"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"},
        {"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"},
        {"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
    };

    for (int i = 0; i < locales.length; i++) {
        Locale locale = locales[i];
        NumberFormat format = NumberFormat.getCurrencyInstance(locale);
        for (int j = 0; j < currencies.length; j++) {
            Currency currency = currencies[j];
            String expected = expecteds[i][j];
            if (currency != null) {
                format.setCurrency(currency);
                int digits = currency.getDefaultFractionDigits();
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            }
            String result = format.format(1234.56);
            if (!result.equals(expected)) {
                failed = true;
                System.out.println("FAIL: Locale " + locale
                    + (currency == null ? ", default currency" : (", currency: " + currency))
                    + ", expected: " + expected
                    + ", actual: " + result);
            }
        }
    }

    if (failed) {
        throw new RuntimeException();
    }
}
 
Example 12
Source File: CurrencyFormat.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static void testFormatting() {
    boolean failed = false;
    Locale[] locales = {
        Locale.US,
        Locale.JAPAN,
        Locale.GERMANY,
        Locale.ITALY,
        new Locale("it", "IT", "EURO") };
    Currency[] currencies = {
        null,
        Currency.getInstance("USD"),
        Currency.getInstance("JPY"),
        Currency.getInstance("DEM"),
        Currency.getInstance("EUR"),
    };
    String[][] expecteds = {
        {"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"},
        {"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"},
        {"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
    };

    for (int i = 0; i < locales.length; i++) {
        Locale locale = locales[i];
        NumberFormat format = NumberFormat.getCurrencyInstance(locale);
        for (int j = 0; j < currencies.length; j++) {
            Currency currency = currencies[j];
            String expected = expecteds[i][j];
            if (currency != null) {
                format.setCurrency(currency);
                int digits = currency.getDefaultFractionDigits();
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            }
            String result = format.format(1234.56);
            if (!result.equals(expected)) {
                failed = true;
                System.out.println("FAIL: Locale " + locale
                    + (currency == null ? ", default currency" : (", currency: " + currency))
                    + ", expected: " + expected
                    + ", actual: " + result);
            }
        }
    }

    if (failed) {
        throw new RuntimeException();
    }
}
 
Example 13
Source File: LocaleController.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public String formatCurrencyString(long amount, String type) {
    type = type.toUpperCase();
    String customFormat;
    double doubleAmount;
    boolean discount = amount < 0;
    amount = Math.abs(amount);
    Currency currency = Currency.getInstance(type);
    switch (type) {
        case "CLF":
            customFormat = " %.4f";
            doubleAmount = amount / 10000.0;
            break;

        case "IRR":
            doubleAmount = amount / 100.0f;
            if (amount % 100 == 0) {
                customFormat = " %.0f";
            } else {
                customFormat = " %.2f";
            }
            break;

        case "BHD":
        case "IQD":
        case "JOD":
        case "KWD":
        case "LYD":
        case "OMR":
        case "TND":
            customFormat = " %.3f";
            doubleAmount = amount / 1000.0;
            break;

        case "BIF":
        case "BYR":
        case "CLP":
        case "CVE":
        case "DJF":
        case "GNF":
        case "ISK":
        case "JPY":
        case "KMF":
        case "KRW":
        case "MGA":
        case "PYG":
        case "RWF":
        case "UGX":
        case "UYI":
        case "VND":
        case "VUV":
        case "XAF":
        case "XOF":
        case "XPF":
            customFormat = " %.0f";
            doubleAmount = amount;
            break;

        case "MRO":
            customFormat = " %.1f";
            doubleAmount = amount / 10.0;
            break;

        default:
            customFormat = " %.2f";
            doubleAmount = amount / 100.0;
            break;
    }
    String result;
    if (currency != null) {
        NumberFormat format = NumberFormat.getCurrencyInstance(currentLocale != null ? currentLocale : systemDefaultLocale);
        format.setCurrency(currency);
        if (type.equals("IRR")) {
            format.setMaximumFractionDigits(0);
        }
        return (discount ? "-" : "") + format.format(doubleAmount);
    }
    return (discount ? "-" : "") + String.format(Locale.US, type + customFormat, doubleAmount);
}
 
Example 14
Source File: LocaleController.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public String formatCurrencyString(long amount, String type)
{
    type = type.toUpperCase();
    String customFormat;
    double doubleAmount;
    boolean discount = amount < 0;
    amount = Math.abs(amount);
    Currency currency = Currency.getInstance(type);
    switch (type)
    {
        case "CLF":
            customFormat = " %.4f";
            doubleAmount = amount / 10000.0;
            break;

        case "IRR":
            doubleAmount = amount / 100.0f;
            if (amount % 100 == 0)
            {
                customFormat = " %.0f";
            }
            else
            {
                customFormat = " %.2f";
            }
            break;

        case "BHD":
        case "IQD":
        case "JOD":
        case "KWD":
        case "LYD":
        case "OMR":
        case "TND":
            customFormat = " %.3f";
            doubleAmount = amount / 1000.0;
            break;

        case "BIF":
        case "BYR":
        case "CLP":
        case "CVE":
        case "DJF":
        case "GNF":
        case "ISK":
        case "JPY":
        case "KMF":
        case "KRW":
        case "MGA":
        case "PYG":
        case "RWF":
        case "UGX":
        case "UYI":
        case "VND":
        case "VUV":
        case "XAF":
        case "XOF":
        case "XPF":
            customFormat = " %.0f";
            doubleAmount = amount;
            break;

        case "MRO":
            customFormat = " %.1f";
            doubleAmount = amount / 10.0;
            break;

        default:
            customFormat = " %.2f";
            doubleAmount = amount / 100.0;
            break;
    }
    String result;
    if (currency != null)
    {
        NumberFormat format = NumberFormat.getCurrencyInstance(currentLocale != null ? currentLocale : systemDefaultLocale);
        format.setCurrency(currency);
        if (type.equals("IRR"))
        {
            format.setMaximumFractionDigits(0);
        }
        return (discount ? "-" : "") + format.format(doubleAmount);
    }
    return (discount ? "-" : "") + String.format(Locale.US, type + customFormat, doubleAmount);
}
 
Example 15
Source File: CurrencyFormat.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void testFormatting() {
    boolean failed = false;
    Locale[] locales = {
        Locale.US,
        Locale.JAPAN,
        Locale.GERMANY,
        Locale.ITALY,
        new Locale("it", "IT", "EURO") };
    Currency[] currencies = {
        null,
        Currency.getInstance("USD"),
        Currency.getInstance("JPY"),
        Currency.getInstance("DEM"),
        Currency.getInstance("EUR"),
    };
    String[][] expecteds = {
        {"$1,234.56", "$1,234.56", "JPY1,235", "DEM1,234.56", "EUR1,234.56"},
        {"\uFFE51,235", "USD1,234.56", "\uFFE51,235", "DEM1,234.56", "EUR1,234.56"},
        {"1.234,56 \u20AC", "1.234,56 USD", "1.235 JPY", "1.234,56 DM", "1.234,56 \u20AC"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
        {"\u20AC 1.234,56", "USD 1.234,56", "JPY 1.235", "DEM 1.234,56", "\u20AC 1.234,56"},
    };

    for (int i = 0; i < locales.length; i++) {
        Locale locale = locales[i];
        NumberFormat format = NumberFormat.getCurrencyInstance(locale);
        for (int j = 0; j < currencies.length; j++) {
            Currency currency = currencies[j];
            String expected = expecteds[i][j];
            if (currency != null) {
                format.setCurrency(currency);
                int digits = currency.getDefaultFractionDigits();
                format.setMinimumFractionDigits(digits);
                format.setMaximumFractionDigits(digits);
            }
            String result = format.format(1234.56);
            if (!result.equals(expected)) {
                failed = true;
                System.out.println("FAIL: Locale " + locale
                    + (currency == null ? ", default currency" : (", currency: " + currency))
                    + ", expected: " + expected
                    + ", actual: " + result);
            }
        }
    }

    if (failed) {
        throw new RuntimeException();
    }
}
 
Example 16
Source File: ProductCarouselImplTest.java    From aem-core-cif-components with Apache License 2.0 4 votes vote down vote up
@Test
public void getProducts() {

    List<ProductListItem> items = productCarousel.getProducts();
    Assert.assertEquals(4, items.size()); // one product is not found and the JSON response contains a "faulty" product

    List<String> productSkuList = Arrays.asList(productSkuArray)
        .stream()
        .map(s -> s.startsWith("/") ? StringUtils.substringAfterLast(s, "/") : s)
        .collect(Collectors.toList());

    NumberFormat priceFormatter = NumberFormat.getCurrencyInstance(Locale.US);

    int idx = 0;
    for (String combinedSku : productSkuList) {
        Pair<String, String> skus = SiteNavigation.toProductSkus(combinedSku);
        ProductInterface product = products.stream().filter(p -> p.getSku().equals(skus.getLeft())).findFirst().orElse(null);
        if (product == null) {
            continue; // Can happen that a product is not found in the Magento JSON response
        }

        if (!items.stream().filter(i -> i.getSKU().equals(skus.getLeft())).findFirst().isPresent()) {
            continue; // A "faulty" product does not appear in the parsed product instances
        }

        ProductInterface productOrVariant = toProductOrVariant(product, skus);
        ProductListItem item = items.get(idx);

        Assert.assertEquals(productOrVariant.getName(), item.getTitle());
        Assert.assertEquals(product.getSku(), item.getSKU());
        Assert.assertEquals(product.getUrlKey(), item.getSlug());

        Page productPage = context.pageManager().getPage(PRODUCT_PAGE);
        SiteNavigation siteNavigation = new SiteNavigation(context.request());
        Assert.assertEquals(siteNavigation.toProductUrl(productPage, product.getUrlKey(), skus.getRight()), item.getURL());

        Money amount = productOrVariant.getPriceRange().getMinimumPrice().getFinalPrice();
        Assert.assertEquals(amount.getValue(), item.getPrice(), 0);
        Assert.assertEquals(amount.getCurrency().toString(), item.getCurrency());
        priceFormatter.setCurrency(Currency.getInstance(amount.getCurrency().toString()));
        Assert.assertEquals(priceFormatter.format(amount.getValue()), item.getFormattedPrice());

        ProductImage thumbnail = productOrVariant.getThumbnail();
        if (thumbnail == null) {
            // if thumbnail is missing for a product in GraphQL response then thumbnail is null for the related item
            Assert.assertNull(item.getImageURL());
        } else {
            Assert.assertEquals(thumbnail.getUrl(), item.getImageURL());
        }
        idx++;
    }
}
 
Example 17
Source File: NumberFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void test_setCurrency_leavesFractionDigitsUntouched() {
    NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
    format.setMinimumFractionDigits(0);
    format.setCurrency(Currency.getInstance("USD"));
    assertEquals("$10", format.format(10d));
}
 
Example 18
Source File: LocaleUtils.java    From sms-ticket with Apache License 2.0 2 votes vote down vote up
/**
 * Format value in desired currency.
 *
 * @param value price
 * @return formatted price
 */
public static String formatCurrency(double value, Currency currency) {
    NumberFormat f = getCurrencyFormatter();
    f.setCurrency(currency);
    return postFormatCurrency(f.format(value));
}
 
Example 19
Source File: LocaleUtils.java    From sms-ticket with Apache License 2.0 2 votes vote down vote up
/**
 * Format value in desired currency.
 *
 * @param value price
 * @return formatted price
 */
public static String formatCurrency(double value, Currency currency) {
    NumberFormat f = getCurrencyFormatter();
    f.setCurrency(currency);
    return postFormatCurrency(f.format(value));
}
 
Example 20
Source File: LocaleUtils.java    From sms-ticket with Apache License 2.0 2 votes vote down vote up
/**
 * Format value in desired currency.
 *
 * @param value price
 * @return formatted price
 */
public static String formatCurrency(BigDecimal value, Currency currency) {
    NumberFormat f = getCurrencyFormatter();
    f.setCurrency(currency);
    return postFormatCurrency(f.format(value));
}