Search in sources :

Example 1 with DurationFormat

use of android.icu.text.DurationFormat in project j2objc by google.

the class ICUDurationTest method TestSimpleXMLDuration.

@Test
public void TestSimpleXMLDuration() {
    Duration d;
    DurationFormat df;
    String out;
    String expected;
    String expected2;
    // test 1
    // "PT2H46M40S"
    d = newDuration(1, 2, 46, 40);
    df = DurationFormat.getInstance(new ULocale("en"));
    expected = "2 hours, 46 minutes, and 40 seconds";
    out = df.format(d);
    if (out.equals(expected)) {
        logln("out=expected: " + expected + " from " + d);
    } else {
        errln("FAIL: got " + out + " wanted " + expected + " from " + d);
    }
    // test 2
    d = newDuration(10000);
    df = DurationFormat.getInstance(new ULocale("en"));
    expected = "10 seconds";
    out = df.format(d);
    if (out.equals(expected)) {
        logln("out=expected: " + expected + " from " + d);
    } else {
        errln("FAIL: got " + out + " wanted " + expected + " from " + d);
    }
    // test 3
    // "P0DT0H0M10.0S"
    d = newDuration(1, 0, 0, 0, 10);
    df = DurationFormat.getInstance(new ULocale("en"));
    expected = "10 seconds";
    out = df.format(d);
    if (out.equals(expected)) {
        logln("out=expected: " + expected + " from " + d);
    } else {
        errln("FAIL: got " + out + " wanted " + expected + " from " + d);
    }
    // test 4
    d = newDuration(86400000);
    df = DurationFormat.getInstance(new ULocale("en"));
    expected = "1 day, 0 hours, 0 minutes, and 0 seconds";
    // This is the expected result for Windows with IBM JRE6
    expected2 = "1 day and 0 seconds";
    out = df.format(d);
    if (out.equals(expected)) {
        logln("out=expected: " + expected + " from " + d);
    } else {
        if (out.equals(expected2)) {
            logln("WARNING: got " + out + " wanted " + expected + " from " + d);
        } else {
            errln("FAIL: got " + out + " wanted " + expected + " from " + d);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DurationFormat(android.icu.text.DurationFormat) Duration(javax.xml.datatype.Duration) Test(org.junit.Test)

Example 2 with DurationFormat

use of android.icu.text.DurationFormat in project j2objc by google.

the class ICUDurationTest method TestXMLDuration.

@Test
public void TestXMLDuration() {
    final class TestCase {

        final String localeString;

        final ULocale locale;

        final String durationString;

        final Duration duration;

        final String expected;

        TestCase(String loc, String ds, Duration d, String exp) {
            localeString = loc;
            locale = new ULocale(loc);
            durationString = ds;
            duration = d;
            expected = exp;
        }
    }
    TestCase[] cases = { new TestCase("en", "PT10.00099S", newDuration(1, 10.00099F), "10 seconds"), new TestCase("en", "#10000", newDuration(10000), "10 seconds"), new TestCase("en", "-PT10.00099S", newDuration(-1, 10.00099F), "10 seconds"), new TestCase("en", "#-10000", newDuration(-10000), "10 seconds"), // from BD req's
    new TestCase("en", "PT2H46M40S", newDuration(1, 2, 46, 40), "2 hours, 46 minutes, and 40 seconds"), new TestCase("it", "PT2H46M40S", newDuration(1, 2, 46, 40), "due ore, 46 minuti e 40 secondi"), // more cases
    new TestCase("en", "PT10S", newDuration(1, 10), "10 seconds"), new TestCase("en", "PT88M70S", newDuration(1, -1, 88, 70), "88 minutes and 70 seconds"), new TestCase("en", "PT10.100S", newDuration(1, 10.100F), "10 seconds and 100 milliseconds"), new TestCase("en", "-PT10S", newDuration(-1, 10), "10 seconds"), new TestCase("en", "PT0H5M0S", newDuration(1, 0, 5, 0), "5 minutes and 0 seconds") };
    for (TestCase tc : cases) {
        String loc = tc.localeString;
        String from = tc.durationString;
        String to = tc.expected;
        ULocale locale = tc.locale;
        Duration d = tc.duration;
        DurationFormat df = DurationFormat.getInstance(locale);
        String output = df.format(d);
        if (output.equals(to)) {
            logln("SUCCESS: locale: " + loc + ", from " + from + " [" + d.toString() + "] " + " to " + to + "= " + output);
        } else {
            logln("FAIL: locale: " + loc + ", from " + from + " [" + d.toString() + "] " + ": expected " + to + " got " + output);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DurationFormat(android.icu.text.DurationFormat) Duration(javax.xml.datatype.Duration) Test(org.junit.Test)

Example 3 with DurationFormat

use of android.icu.text.DurationFormat in project j2objc by google.

the class ICUDurationTest method TestBasics.

/**
 * Basic test
 */
@Test
public void TestBasics() {
    DurationFormat df;
    String expect;
    String formatted;
    df = DurationFormat.getInstance(new ULocale("it"));
    formatted = df.formatDurationFromNow(4096);
    expect = "fra quattro secondi";
    if (!expect.equals(formatted)) {
        errln("Expected " + expect + " but got " + formatted);
    } else {
        logln("format duration -> " + formatted);
    }
    formatted = df.formatDurationFromNowTo(new Date(0));
    Calendar cal = Calendar.getInstance();
    // year of Date(0)
    int years = cal.get(Calendar.YEAR) - 1970;
    expect = years + " anni fa";
    if (!expect.equals(formatted)) {
        errln("Expected " + expect + " but got " + formatted);
    } else {
        logln("format date  -> " + formatted);
    }
    formatted = df.formatDurationFrom(1000 * 3600 * 24, new Date(0).getTime());
    expect = "fra un giorno";
    if (!expect.equals(formatted)) {
        errln("Expected " + expect + " but got " + formatted);
    } else {
        logln("format date from -> " + formatted);
    }
    formatted = df.format(new Long(1000 * 3600 * 24 * 2));
    expect = "fra due giorni";
    if (!expect.equals(formatted)) {
        errln("Expected " + expect + " but got " + formatted);
    } else {
        logln("format long obj -> " + formatted);
    }
}
Also used : ULocale(android.icu.util.ULocale) DurationFormat(android.icu.text.DurationFormat) Calendar(android.icu.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Example 4 with DurationFormat

use of android.icu.text.DurationFormat in project j2objc by google.

the class ICUDurationTest method TestBadLocaleError.

@Test
public void TestBadLocaleError() {
    try {
        DurationFormat df = DurationFormat.getInstance(new ULocale("und"));
        df.format(new Date());
        logln("Should have thrown err.");
        errln("failed, should have thrown err.");
    } catch (MissingResourceException mre) {
        logln("PASS: caught missing resource exception on locale 'und'");
        logln(mre.toString());
    }
}
Also used : ULocale(android.icu.util.ULocale) DurationFormat(android.icu.text.DurationFormat) MissingResourceException(java.util.MissingResourceException) Date(java.util.Date) Test(org.junit.Test)

Example 5 with DurationFormat

use of android.icu.text.DurationFormat in project j2objc by google.

the class ICUDurationTest method TestBadObjectError.

@Test
public void TestBadObjectError() {
    Runtime r = Runtime.getRuntime();
    DurationFormat df = DurationFormat.getInstance(new ULocale("en"));
    String output = null;
    try {
        output = df.format(r);
        errln("FAIL: did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");
    } catch (IllegalArgumentException iae) {
        logln("PASS: expected: Caught iae: " + iae.toString());
    }
    // try a second time, because it is a different code path for java < 1.5
    try {
        output = df.format(r);
        errln("FAIL: [#2] did NOT get IllegalArgumentException! Should have. Formatted Runtime as " + output + " ???");
    } catch (IllegalArgumentException iae) {
        logln("PASS: [#2] expected: Caught iae: " + iae.toString());
    }
}
Also used : ULocale(android.icu.util.ULocale) DurationFormat(android.icu.text.DurationFormat) Test(org.junit.Test)

Aggregations

DurationFormat (android.icu.text.DurationFormat)6 ULocale (android.icu.util.ULocale)6 Test (org.junit.Test)6 Date (java.util.Date)3 Duration (javax.xml.datatype.Duration)2 Calendar (android.icu.util.Calendar)1 MissingResourceException (java.util.MissingResourceException)1