Search in sources :

Example 16 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestGetAppendItemName.

@Test
public void TestGetAppendItemName() {
    final AppendItemName[] appendItemNames = { new AppendItemName(DateTimePatternGenerator.YEAR, "vuosi"), new AppendItemName(DateTimePatternGenerator.MONTH, "kuukausi"), new AppendItemName(DateTimePatternGenerator.WEEKDAY, "viikonp\u00E4iv\u00E4"), new AppendItemName(DateTimePatternGenerator.DAY, "p\u00E4iv\u00E4"), new AppendItemName(DateTimePatternGenerator.HOUR, "tunti") };
    DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance();
    String[] cases = { "d", "u", "m", "m", "y" };
    for (int i = 0; i < cases.length; i++) {
        dtpg.setAppendItemName(i, cases[i]);
        if (!dtpg.getAppendItemName(i).equals(cases[i])) {
            errln("DateTimePatternGenerator.getAppendItemFormat(int field) " + "did not return as expected. Value set at " + i + " was " + cases[i] + " but got back " + dtpg.getAppendItemName(i));
        }
    }
    DateTimePatternGenerator dtpgfi = DateTimePatternGenerator.getInstance(ULocale.forLanguageTag("fi"));
    for (AppendItemName appendItemName : appendItemNames) {
        String name = dtpgfi.getAppendItemName(appendItemName.field);
        if (!name.equals(appendItemName.name)) {
            errln("DateTimePatternGenerator.getAppendItemName returns invalid name for field " + appendItemName.field + ": got " + name + " but expected " + appendItemName.name);
        }
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 17 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestGetBaseSkeletons.

/* Tests the method
     *        public Set<String> getBaseSkeletons(Set<String> result)
     */
@Test
public void TestGetBaseSkeletons() {
    DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance();
    // Tests when "if (result == null)" is true
    try {
        dtpg.getBaseSkeletons(null);
    } catch (Exception e) {
        errln("DateTimePatternGenerator.getBaseSkeletons(Map) was suppose to " + "return a new LinkedHashMap for a null parameter.");
    }
    // Tests when "if (result == null)" is false
    Set<String> mm = new HashSet<String>();
    try {
        dtpg.getBaseSkeletons(mm);
    } catch (Exception e) {
        errln("DateTimePatternGenerator.getBaseSkeletons(Map) was suppose to " + "return a new LinkedHashMap for a HashSet parameter.");
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 18 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method DayMonthTest.

@Test
public void DayMonthTest() {
    final ULocale locale = ULocale.FRANCE;
    // set up the generator
    DateTimePatternGenerator dtpgen = DateTimePatternGenerator.getInstance(locale);
    // get a pattern for an abbreviated month and day
    final String pattern = dtpgen.getBestPattern("MMMd");
    SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
    // use it to format (or parse)
    String formatted = formatter.format(new Date());
    logln("formatted=" + formatted);
// for French, the result is "13 sept."
}
Also used : ULocale(android.icu.util.ULocale) DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 19 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestGetSkeleton.

/* Tests the method
     *        public String getSkeleton(String pattern)
     */
@Test
public void TestGetSkeleton() {
    DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance();
    String[] cases = { "MMDD", "MMMDD", "MMM-DD", "DD/MMM", "ddM", "MMMMd" };
    String[] results = { "MMDD", "MMMDD", "MMMDD", "MMMDD", "Mdd", "MMMMd" };
    for (int i = 0; i < cases.length; i++) {
        if (!dtpg.getSkeleton(cases[i]).equals(results[i])) {
            errln("DateTimePatternGenerator.getSkeleton(String) did " + "return the expected result when passing " + cases[i] + " and expected " + results[i] + " but got " + dtpg.getSkeleton(cases[i]));
        }
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 20 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestCoverage.

/*
     * Coverage for methods otherwise not covered by other tests.
     */
@Test
public void TestCoverage() {
    DateTimePatternGenerator dtpg;
    // DateTimePatternGenerator#getDefaultHourFormatChar
    // DateTimePatternGenerator#setDefaultHourFormatChar
    {
        dtpg = DateTimePatternGenerator.getEmptyInstance();
        assertEquals("Default hour char on empty instance", 'H', dtpg.getDefaultHourFormatChar());
        dtpg.setDefaultHourFormatChar('e');
        assertEquals("Default hour char after explicit set", 'e', dtpg.getDefaultHourFormatChar());
        dtpg = DateTimePatternGenerator.getInstance(ULocale.ENGLISH);
        assertEquals("Default hour char on populated English instance", 'h', dtpg.getDefaultHourFormatChar());
    }
    // DateTimePatternGenerator#getSkeletonAllowingDuplicates
    // DateTimePatternGenerator#getCanonicalSkeletonAllowingDuplicates
    // DateTimePatternGenerator#getCanonicalChar
    {
        dtpg = DateTimePatternGenerator.getInstance(ULocale.ENGLISH);
        assertEquals("Example skeleton with no duplicate fields", "MMMdd", dtpg.getSkeleton("dd/MMM"));
        assertEquals("Should return same result as getSkeleton with no duplicate fields", dtpg.getSkeleton("dd/MMM"), dtpg.getSkeletonAllowingDuplicates("dd/MMM"));
        try {
            dtpg.getSkeleton("dd/MMM Zz");
            fail("getSkeleton should throw upon duplicate fields");
        } catch (IllegalArgumentException e) {
            assertEquals("getSkeleton should throw upon duplicate fields", "Conflicting fields:\tZ, z\t in dd/MMM Zz", e.getMessage());
        }
        assertEquals("Should not throw upon duplicate fields", "MMMddZ", dtpg.getSkeletonAllowingDuplicates("dd/MMM Zz"));
        assertEquals("Should not throw upon duplicate fields and should return Canonical fields", "MMMddv", dtpg.getCanonicalSkeletonAllowingDuplicates("dd/MMM Zz"));
    }
    // DateTimePatternGenerator#showMask
    try {
        String actual = invokeToString("android.icu.text.DateTimePatternGenerator$DistanceInfo");
        assertEquals("DistanceInfo toString", "missingFieldMask: , extraFieldMask: ", actual);
    } catch (Exception e) {
        errln("Couldn't call DistanceInfo.toString(): " + e.toString());
    }
    // DateTimePatternGenerator#skeletonsAreSimilar
    // DateTimePatternGenerator#getSet
    {
        dtpg = DateTimePatternGenerator.getInstance(ULocale.ENGLISH);
        assertTrue("Trivial skeletonsAreSimilar", dtpg.skeletonsAreSimilar("MMMdd", "MMMdd"));
        assertTrue("Different number of chars in skeletonsAreSimilar", dtpg.skeletonsAreSimilar("Mddd", "MMMdd"));
        assertFalse("Failure case for skeletonsAreSimilar", dtpg.skeletonsAreSimilar("mmDD", "MMMdd"));
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Aggregations

DateTimePatternGenerator (android.icu.text.DateTimePatternGenerator)28 Test (org.junit.Test)27 SimpleDateFormat (android.icu.text.SimpleDateFormat)6 ULocale (android.icu.util.ULocale)6 SimpleTimeZone (android.icu.util.SimpleTimeZone)3 TimeZone (android.icu.util.TimeZone)3 Date (java.util.Date)3 LinkedHashSet (java.util.LinkedHashSet)2 Calendar (android.icu.util.Calendar)1 GregorianCalendar (android.icu.util.GregorianCalendar)1 ParsePosition (java.text.ParsePosition)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1