Search in sources :

Example 1 with Field

use of android.icu.text.ChineseDateFormat.Field in project j2objc by google.

the class DateFormatTest method TestFormatToCharacterIteratorCoverage.

/*
     * API coverage test case for formatToCharacterIterator
     */
@Test
public void TestFormatToCharacterIteratorCoverage() {
    // Calling formatToCharacterIterator, using various argument types
    DateFormat df = DateFormat.getDateTimeInstance();
    AttributedCharacterIterator acit = null;
    Calendar cal = Calendar.getInstance();
    try {
        acit = df.formatToCharacterIterator(cal);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Calendar)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Calendar must be accepted by formatToCharacterIterator");
    }
    Date d = cal.getTime();
    try {
        acit = df.formatToCharacterIterator(d);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Date)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Date must be accepted by formatToCharacterIterator");
    }
    Number num = new Long(d.getTime());
    try {
        acit = df.formatToCharacterIterator(num);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Number)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Number must be accepted by formatToCharacterIterator");
    }
    boolean isException = false;
    String str = df.format(d);
    try {
        acit = df.formatToCharacterIterator(str);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(String)");
        }
    } catch (IllegalArgumentException iae) {
        logln("IllegalArgumentException is thrown by formatToCharacterIterator");
        isException = true;
    }
    if (!isException) {
        errln("FAIL: String must not be accepted by formatToCharacterIterator");
    }
    // DateFormat.Field#ofCalendarField and getCalendarField
    for (int i = 0; i < DATEFORMAT_FIELDS.length; i++) {
        int calField = DATEFORMAT_FIELDS[i].getCalendarField();
        if (calField != -1) {
            DateFormat.Field field = DateFormat.Field.ofCalendarField(calField);
            if (field != DATEFORMAT_FIELDS[i]) {
                errln("FAIL: " + field + " is returned for a Calendar field " + calField + " - Expected: " + DATEFORMAT_FIELDS[i]);
            }
        }
    }
    // IllegalArgument for ofCalendarField
    isException = false;
    try {
        DateFormat.Field.ofCalendarField(-1);
    } catch (IllegalArgumentException iae) {
        logln("IllegalArgumentException is thrown by ofCalendarField");
        isException = true;
    }
    if (!isException) {
        errln("FAIL: IllegalArgumentException must be thrown by ofCalendarField for calendar field value -1");
    }
    // ChineseDateFormat.Field#ofCalendarField and getCalendarField
    int ccalField = ChineseDateFormat.Field.IS_LEAP_MONTH.getCalendarField();
    if (ccalField != Calendar.IS_LEAP_MONTH) {
        errln("FAIL: ChineseCalendar field " + ccalField + " is returned for ChineseDateFormat.Field.IS_LEAP_MONTH.getCalendarField()");
    } else {
        DateFormat.Field cfield = ChineseDateFormat.Field.ofCalendarField(ccalField);
        if (cfield != ChineseDateFormat.Field.IS_LEAP_MONTH) {
            errln("FAIL: " + cfield + " is returned for a ChineseCalendar field " + ccalField + " - Expected: " + ChineseDateFormat.Field.IS_LEAP_MONTH);
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) BuddhistCalendar(android.icu.util.BuddhistCalendar) HebrewCalendar(android.icu.util.HebrewCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Field(android.icu.text.ChineseDateFormat.Field) Date(java.util.Date) AttributedCharacterIterator(java.text.AttributedCharacterIterator) Test(org.junit.Test)

Example 2 with Field

use of android.icu.text.ChineseDateFormat.Field in project j2objc by google.

the class DateFormatTest method TestFormatToCharacterIterator.

/*
    @Test
    public void TestJB4757(){
        DateFormat dfmt = DateFormat.getDateInstance(DateFormat.FULL, ULocale.ROOT);
    }
    */
/*
     * Test case for formatToCharacterIterator
     */
@Test
public void TestFormatToCharacterIterator() {
    // Generate pattern string including all pattern letters with various length
    AttributedCharacterIterator acit;
    final char SEPCHAR = '~';
    String[] patterns = new String[5];
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < patterns.length; i++) {
        sb.setLength(0);
        for (int j = 0; j < PATTERN_CHARS.length(); j++) {
            if (j != 0) {
                for (int k = 0; k <= i; k++) {
                    sb.append(SEPCHAR);
                }
            }
            char letter = PATTERN_CHARS.charAt(j);
            for (int k = 0; k <= i; k++) {
                sb.append(letter);
            }
        }
        patterns[i] = sb.toString();
    }
    if (isVerbose()) {
        for (int i = 0; i < patterns.length; i++) {
            logln("patterns[" + i + "] = " + patterns[i]);
        }
    }
    Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JULY, 16, 8, 20, 25);
    cal.set(Calendar.MILLISECOND, 567);
    final Date d = cal.getTime();
    // Test AttributedCharacterIterator returned by SimpleDateFormat
    for (int i = 0; i < patterns.length; i++) {
        SimpleDateFormat sdf = new SimpleDateFormat(patterns[i]);
        acit = sdf.formatToCharacterIterator(d);
        int patidx = 0;
        while (true) {
            Map map = acit.getAttributes();
            int limit = acit.getRunLimit();
            if (map.isEmpty()) {
                // Must be pattern literal - '~'
                while (acit.getIndex() < limit) {
                    if (acit.current() != SEPCHAR) {
                        errln("FAIL: Invalid pattern literal at " + acit.current() + " in patterns[" + i + "]");
                    }
                    acit.next();
                }
            } else {
                Set keySet = map.keySet();
                if (keySet.size() == 1) {
                    // Check the attribute
                    Iterator keyIterator = keySet.iterator();
                    DateFormat.Field attr = (DateFormat.Field) keyIterator.next();
                    if (!DATEFORMAT_FIELDS[patidx].equals(attr)) {
                        errln("FAIL: The attribute at " + acit.getIndex() + " in patterns[" + i + "" + "] is " + attr + " - Expected: " + DATEFORMAT_FIELDS[patidx]);
                    }
                } else {
                    // SimpleDateFormat#formatToCharacterIterator never set multiple
                    // attributes to a single text run.
                    errln("FAIL: Multiple attributes were set");
                }
                patidx++;
                // Move to the run limit
                acit.setIndex(limit);
            }
            if (acit.current() == CharacterIterator.DONE) {
                break;
            }
        }
    }
    // ChineseDateFormat has pattern letter 'l' for leap month marker in addition to regular DateFormat
    cal.clear();
    // 26x78-5-30
    cal.set(2009, Calendar.JUNE, 22);
    // non-leap month
    Date nonLeapMonthDate = cal.getTime();
    // 26x78-5*-1
    cal.set(2009, Calendar.JUNE, 23);
    // leap month
    Date leapMonthDate = cal.getTime();
    ChineseDateFormat cdf = new ChineseDateFormat("y'x'G-Ml-d", ULocale.US);
    acit = cdf.formatToCharacterIterator(nonLeapMonthDate);
    Set keys = acit.getAllAttributeKeys();
    if (keys.contains(ChineseDateFormat.Field.IS_LEAP_MONTH)) {
        errln("FAIL: separate IS_LEAP_MONTH field should not be present for a Chinese calendar non-leap date" + cdf.format(nonLeapMonthDate));
    }
    acit = cdf.formatToCharacterIterator(leapMonthDate);
    keys = acit.getAllAttributeKeys();
    if (keys.contains(ChineseDateFormat.Field.IS_LEAP_MONTH)) {
        errln("FAIL: separate IS_LEAP_MONTH field should no longer be present for a Chinese calendar leap date" + cdf.format(leapMonthDate));
    }
}
Also used : HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) Set(java.util.Set) BuddhistCalendar(android.icu.util.BuddhistCalendar) HebrewCalendar(android.icu.util.HebrewCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Field(android.icu.text.ChineseDateFormat.Field) Date(java.util.Date) AttributedCharacterIterator(java.text.AttributedCharacterIterator) Field(android.icu.text.ChineseDateFormat.Field) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Iterator(java.util.Iterator) CharacterIterator(java.text.CharacterIterator) AttributedCharacterIterator(java.text.AttributedCharacterIterator) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Field

use of android.icu.text.ChineseDateFormat.Field in project j2objc by google.

the class DateFormatTest method TestFieldPosition.

/**
 * Verify that returned field position indices are correct.
 */
@Test
public void TestFieldPosition() {
    int i, j, exp;
    StringBuffer buf = new StringBuffer();
    // Verify data
    if (VersionInfo.ICU_VERSION.compareTo(VersionInfo.getInstance(3, 7)) >= 0) {
        DateFormatSymbols rootSyms = new DateFormatSymbols(new Locale("", "", ""));
        assertEquals("patternChars", PATTERN_CHARS, rootSyms.getLocalPatternChars());
    }
    assertTrue("DATEFORMAT_FIELD_NAMES", DATEFORMAT_FIELD_NAMES.length == DateFormat.FIELD_COUNT);
    if (DateFormat.FIELD_COUNT != PATTERN_CHARS.length() + 1) {
        // +1 for missing TIME_SEPARATOR pattern char
        errln("Did not get the correct value for DateFormat.FIELD_COUNT. Expected:  " + PATTERN_CHARS.length() + 1);
    }
    // Create test formatters
    final int COUNT = 4;
    DateFormat[] dateFormats = new DateFormat[COUNT];
    dateFormats[0] = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
    dateFormats[1] = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.FRANCE);
    // Make the pattern "G y M d..."
    buf.append(PATTERN_CHARS);
    for (j = buf.length() - 1; j >= 0; --j) buf.insert(j, ' ');
    dateFormats[2] = new SimpleDateFormat(buf.toString(), Locale.US);
    // Make the pattern "GGGG yyyy MMMM dddd..."
    for (j = buf.length() - 1; j >= 0; j -= 2) {
        for (i = 0; i < 3; ++i) {
            buf.insert(j, buf.charAt(j));
        }
    }
    dateFormats[3] = new SimpleDateFormat(buf.toString(), Locale.US);
    Date aug13 = new Date((long) 871508052513.0);
    // Expected output field values for above DateFormats on aug13
    // Fields are given in order of DateFormat field number
    final String[] EXPECTED = { "", "1997", "August", "13", "", "", "34", "12", "", "Wednesday", "", "", "", "", "PM", "2", "", "Pacific Daylight Time", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "1997", "ao\u00FBt", "13", "", "14", "34", "12", "", "mercredi", "", "", "", "", "", "", "", "heure d\u2019\u00E9t\u00E9 du Pacifique", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "AD", "1997", "8", "13", "14", "14", "34", "12", "5", "Wed", "225", "2", "33", "3", "PM", "2", "2", "PDT", "1997", "4", "1997", "2450674", "52452513", "-0700", "PT", "4", "8", "3", "3", "uslax", "1997", "GMT-7", "-07", "-07", "1997", "PM", "in the afternoon", "", "Anno Domini", "1997", "August", "0013", "0014", "0014", "0034", "0012", "5130", "Wednesday", "0225", "0002", "0033", "0003", "PM", "0002", "0002", "Pacific Daylight Time", "1997", "Wednesday", "1997", "2450674", "52452513", "GMT-07:00", "Pacific Time", "Wednesday", "August", "3rd quarter", "3rd quarter", "Los Angeles Time", "1997", "GMT-07:00", "-0700", "-0700", "1997", "PM", "in the afternoon", "" };
    assertTrue("data size", EXPECTED.length == COUNT * DateFormat.FIELD_COUNT);
    final DateFormat.Field[] DTFMT_FIELDS = { DateFormat.Field.AM_PM, DateFormat.Field.DAY_OF_MONTH, DateFormat.Field.DAY_OF_WEEK, DateFormat.Field.DAY_OF_WEEK_IN_MONTH, DateFormat.Field.DAY_OF_YEAR, DateFormat.Field.DOW_LOCAL, DateFormat.Field.ERA, DateFormat.Field.EXTENDED_YEAR, DateFormat.Field.HOUR_OF_DAY0, DateFormat.Field.HOUR_OF_DAY1, DateFormat.Field.HOUR0, DateFormat.Field.HOUR1, DateFormat.Field.JULIAN_DAY, DateFormat.Field.MILLISECOND, DateFormat.Field.MILLISECONDS_IN_DAY, DateFormat.Field.MINUTE, DateFormat.Field.MONTH, DateFormat.Field.QUARTER, DateFormat.Field.SECOND, DateFormat.Field.TIME_ZONE, DateFormat.Field.WEEK_OF_MONTH, DateFormat.Field.WEEK_OF_YEAR, DateFormat.Field.YEAR, DateFormat.Field.YEAR_WOY };
    final String[][] EXPECTED_BY_FIELD = { { "PM", "13", "Wednesday", "", "", "", "", "", "", "", "", "2", "", "", "", "34", "August", "", "12", "Pacific Daylight Time", "", "", "1997", "" }, { "", "13", "mercredi", "", "", "", "", "", "14", "", "", "", "", "", "", "34", "ao\u00FBt", "", "12", "heure d\u2019\u00E9t\u00E9 du Pacifique", "", "", "1997", "" }, { "PM", "13", "Wed", "2", "225", "4", "AD", "1997", "14", "14", "2", "2", "2450674", "5", "52452513", "34", "8", "3", "12", "PDT", "3", "33", "1997", "1997" }, { "PM", "0013", "Wednesday", "0002", "0225", "Wednesday", "Anno Domini", "1997", "0014", "0014", "0002", "0002", "2450674", "5130", "52452513", "0034", "August", "3rd quarter", "0012", "Pacific Daylight Time", "0003", "0033", "1997", "1997" } };
    TimeZone PT = TimeZone.getTimeZone("America/Los_Angeles");
    for (j = 0, exp = 0; j < COUNT; ++j) {
        // String str;
        DateFormat df = dateFormats[j];
        df.setTimeZone(PT);
        logln(" Pattern = " + ((SimpleDateFormat) df).toPattern());
        try {
            logln("  Result = " + df.format(aug13));
        } catch (Exception e) {
            errln("FAIL: " + e);
            e.printStackTrace();
            continue;
        }
        FieldPosition pos;
        String field;
        for (i = 0; i < DateFormat.FIELD_COUNT; ++i, ++exp) {
            pos = new FieldPosition(i);
            buf.setLength(0);
            df.format(aug13, buf, pos);
            field = buf.substring(pos.getBeginIndex(), pos.getEndIndex());
            assertEquals("pattern#" + j + " field #" + i + " " + DATEFORMAT_FIELD_NAMES[i], EXPECTED[exp], field);
        }
        // FieldPostion initialized by DateFormat.Field trac#6089
        for (i = 0; i < DTFMT_FIELDS.length; i++) {
            // The format method only set position for the first occurrence of
            // the specified field.
            pos = new FieldPosition(DTFMT_FIELDS[i]);
            buf.setLength(0);
            df.format(aug13, buf, pos);
            field = buf.substring(pos.getBeginIndex(), pos.getEndIndex());
            assertEquals("pattern#" + j + " " + DTFMT_FIELDS[i].toString(), EXPECTED_BY_FIELD[j][i], field);
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) FieldPosition(java.text.FieldPosition) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) Field(android.icu.text.ChineseDateFormat.Field) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormatSymbols(android.icu.text.DateFormatSymbols) ChineseDateFormatSymbols(android.icu.text.ChineseDateFormatSymbols) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

ChineseDateFormat (android.icu.text.ChineseDateFormat)3 Field (android.icu.text.ChineseDateFormat.Field)3 DateFormat (android.icu.text.DateFormat)3 SimpleDateFormat (android.icu.text.SimpleDateFormat)3 Date (java.util.Date)3 Test (org.junit.Test)3 BuddhistCalendar (android.icu.util.BuddhistCalendar)2 Calendar (android.icu.util.Calendar)2 ChineseCalendar (android.icu.util.ChineseCalendar)2 GregorianCalendar (android.icu.util.GregorianCalendar)2 HebrewCalendar (android.icu.util.HebrewCalendar)2 IslamicCalendar (android.icu.util.IslamicCalendar)2 JapaneseCalendar (android.icu.util.JapaneseCalendar)2 AttributedCharacterIterator (java.text.AttributedCharacterIterator)2 ChineseDateFormatSymbols (android.icu.text.ChineseDateFormatSymbols)1 DateFormatSymbols (android.icu.text.DateFormatSymbols)1 TimeZone (android.icu.util.TimeZone)1 ULocale (android.icu.util.ULocale)1 IOException (java.io.IOException)1 CharacterIterator (java.text.CharacterIterator)1