Search in sources :

Example 6 with CurrencyFilter

use of android.icu.text.CurrencyMetaInfo.CurrencyFilter in project j2objc by google.

the class CurrencyTest method testCurrencyMetaInfoRanges.

// A real test of CurrencyMetaInfo.
@Test
public void testCurrencyMetaInfoRanges() {
    CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance(true);
    assertNotNull("have metainfo", metainfo);
    // must be capitalized
    CurrencyFilter filter = CurrencyFilter.onRegion("DE");
    List<CurrencyInfo> currenciesInGermany = metainfo.currencyInfo(filter);
    logln("currencies: " + currenciesInGermany.size());
    DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
    fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date demLastDate = new Date(Long.MAX_VALUE);
    Date eurFirstDate = new Date(Long.MIN_VALUE);
    for (CurrencyInfo info : currenciesInGermany) {
        logln(info.toString());
        logln("from: " + fmt.format(info.from) + Long.toHexString(info.from));
        logln("  to: " + fmt.format(info.to) + Long.toHexString(info.to));
        if (info.code.equals("DEM")) {
            demLastDate = new Date(info.to);
        } else if (info.code.equals("EUR")) {
            eurFirstDate = new Date(info.from);
        }
    }
    // the Euro and Deutschmark overlapped for several years
    assertEquals("DEM available at last date", 2, metainfo.currencyInfo(filter.withDate(demLastDate)).size());
    // demLastDate + 1 millisecond is not the start of the last day, we consider it the next day, so...
    Date demLastDatePlus1ms = new Date(demLastDate.getTime() + 1);
    assertEquals("DEM not available after very start of last date", 1, metainfo.currencyInfo(filter.withDate(demLastDatePlus1ms)).size());
    // both available for start of euro
    assertEquals("EUR available on start of first date", 2, metainfo.currencyInfo(filter.withDate(eurFirstDate)).size());
    // but not one millisecond before the start of the first day
    Date eurFirstDateMinus1ms = new Date(eurFirstDate.getTime() - 1);
    assertEquals("EUR not avilable before very start of first date", 1, metainfo.currencyInfo(filter.withDate(eurFirstDateMinus1ms)).size());
    // end time is last millisecond of day
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    cal.setTime(demLastDate);
    assertEquals("hour is 23", 23, cal.get(GregorianCalendar.HOUR_OF_DAY));
    assertEquals("minute is 59", 59, cal.get(GregorianCalendar.MINUTE));
    assertEquals("second is 59", 59, cal.get(GregorianCalendar.SECOND));
    assertEquals("millisecond is 999", 999, cal.get(GregorianCalendar.MILLISECOND));
    // start time is first millisecond of day
    cal.setTime(eurFirstDate);
    assertEquals("hour is 0", 0, cal.get(GregorianCalendar.HOUR_OF_DAY));
    assertEquals("minute is 0", 0, cal.get(GregorianCalendar.MINUTE));
    assertEquals("second is 0", 0, cal.get(GregorianCalendar.SECOND));
    assertEquals("millisecond is 0", 0, cal.get(GregorianCalendar.MILLISECOND));
}
Also used : CurrencyMetaInfo(android.icu.text.CurrencyMetaInfo) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) CurrencyInfo(android.icu.text.CurrencyMetaInfo.CurrencyInfo) Date(java.util.Date) CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter) Test(org.junit.Test)

Example 7 with CurrencyFilter

use of android.icu.text.CurrencyMetaInfo.CurrencyFilter in project j2objc by google.

the class CurrencyTest method TestCurrencyMetaInfo.

// Coverage-only test of the CurrencyMetaInfo class
@Test
public void TestCurrencyMetaInfo() {
    CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance();
    if (metainfo == null) {
        errln("Unable to get CurrencyMetaInfo instance.");
        return;
    }
    if (!CurrencyMetaInfo.hasData()) {
        errln("hasData() should note return false.");
        return;
    }
    CurrencyMetaInfo.CurrencyFilter filter;
    CurrencyMetaInfo.CurrencyInfo info;
    CurrencyMetaInfo.CurrencyDigits digits;
    {
        // CurrencyFilter
        filter = CurrencyMetaInfo.CurrencyFilter.onCurrency("currency");
        CurrencyMetaInfo.CurrencyFilter filter2 = CurrencyMetaInfo.CurrencyFilter.onCurrency("test");
        if (filter == null) {
            errln("Unable to create CurrencyFilter.");
            return;
        }
        if (filter.equals(new Object())) {
            errln("filter should not equal to Object");
            return;
        }
        if (filter.equals(filter2)) {
            errln("filter should not equal filter2");
            return;
        }
        if (filter.hashCode() == 0) {
            errln("Error getting filter hashcode");
            return;
        }
        if (filter.toString() == null) {
            errln("Error calling toString()");
            return;
        }
    }
    {
        // CurrencyInfo
        info = new CurrencyMetaInfo.CurrencyInfo("region", "code", 0, 1, 1, false);
        if (info == null) {
            errln("Error creating CurrencyInfo.");
            return;
        }
        if (info.toString() == null) {
            errln("Error calling toString()");
            return;
        }
    }
    {
        // CurrencyDigits
        digits = metainfo.currencyDigits("isoCode");
        if (digits == null) {
            errln("Unable to get CurrencyDigits.");
            return;
        }
        if (digits.toString() == null) {
            errln("Error calling toString()");
            return;
        }
    }
}
Also used : CurrencyMetaInfo(android.icu.text.CurrencyMetaInfo) CurrencyInfo(android.icu.text.CurrencyMetaInfo.CurrencyInfo) CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter) CurrencyInfo(android.icu.text.CurrencyMetaInfo.CurrencyInfo) CurrencyFilter(android.icu.text.CurrencyMetaInfo.CurrencyFilter) Test(org.junit.Test)

Aggregations

CurrencyFilter (android.icu.text.CurrencyMetaInfo.CurrencyFilter)7 Test (org.junit.Test)4 CurrencyMetaInfo (android.icu.text.CurrencyMetaInfo)3 CurrencyInfo (android.icu.text.CurrencyMetaInfo.CurrencyInfo)3 DateFormat (android.icu.text.DateFormat)1 SimpleDateFormat (android.icu.text.SimpleDateFormat)1 GregorianCalendar (android.icu.util.GregorianCalendar)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1