Search in sources :

Example 1 with UFormat

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

the class TestMessageFormat method TestSetGetFormats.

@Test
public void TestSetGetFormats() {
    Object[] arguments = { new Double(456.83), new Date(871068000000L), "deposit" };
    StringBuffer result = new StringBuffer();
    String formatStr = "At <time> on {1,date}, you made a {2} of {0,number,currency}.";
    // original expected format result
    String compareStr = "At <time> on Aug 8, 1997, you made a deposit of $456.83.";
    // the date being German-style, but the currency being English-style
    String compareStr2 = "At <time> on 08.08.1997, you made a deposit of ";
    compareStr2 += '\u00a4';
    compareStr2 += "456.83.";
    // both date and currency formats are German-style
    String compareStr3 = "At <time> on 08.08.1997, you made a deposit of ";
    compareStr3 += "456,83\u00a0";
    compareStr3 += '\u00a4';
    compareStr3 += ".";
    MessageFormat msg = new MessageFormat(formatStr, ULocale.US);
    result.setLength(0);
    FieldPosition pos = new FieldPosition(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStr, result.toString());
    // constructs a Format array with a English-style Currency formatter
    // and a German-style Date formatter
    // might not meaningful, just for testing setFormatsByArgIndex
    Format[] fmts = new Format[] { NumberFormat.getCurrencyInstance(ULocale.ENGLISH), DateFormat.getDateInstance(DateFormat.DEFAULT, ULocale.GERMAN) };
    msg.setFormatsByArgumentIndex(fmts);
    result.setLength(0);
    pos = new FieldPosition(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStr2, result.toString());
    // Construct a German-style Currency formatter, replace the corresponding one
    // Thus both formatters should format objects with German-style
    Format newFmt = NumberFormat.getCurrencyInstance(ULocale.GERMAN);
    msg.setFormatByArgumentIndex(0, newFmt);
    result.setLength(0);
    pos = new FieldPosition(0);
    result = msg.format(arguments, result, pos);
    assertEquals("format", compareStr3, result.toString());
    // verify getFormatsByArgumentIndex
    // you should got three formats by that
    // - DecimalFormat     locale: de
    // - SimpleDateFormat  locale: de
    // - null
    Format[] fmts2 = msg.getFormatsByArgumentIndex();
    assertEquals("1st subformmater: Format Class", "android.icu.text.DecimalFormat", fmts2[0].getClass().getName());
    assertEquals("1st subformmater: its Locale", ULocale.GERMAN, ((UFormat) fmts2[0]).getLocale(ULocale.VALID_LOCALE));
    assertEquals("2nd subformatter: Format Class", "android.icu.text.SimpleDateFormat", fmts2[1].getClass().getName());
    assertEquals("2nd subformmater: its Locale", ULocale.GERMAN, ((UFormat) fmts2[1]).getLocale(ULocale.VALID_LOCALE));
    assertTrue("The third subFormatter is null", null == fmts2[2]);
}
Also used : UFormat(android.icu.text.UFormat) Format(java.text.Format) MessageFormat(android.icu.text.MessageFormat) DateFormat(android.icu.text.DateFormat) NumberFormat(android.icu.text.NumberFormat) DecimalFormat(android.icu.text.DecimalFormat) ChoiceFormat(java.text.ChoiceFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) MessageFormat(android.icu.text.MessageFormat) AttributedString(java.text.AttributedString) FieldPosition(java.text.FieldPosition) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DateFormat (android.icu.text.DateFormat)1 DecimalFormat (android.icu.text.DecimalFormat)1 MessageFormat (android.icu.text.MessageFormat)1 NumberFormat (android.icu.text.NumberFormat)1 SimpleDateFormat (android.icu.text.SimpleDateFormat)1 UFormat (android.icu.text.UFormat)1 AttributedString (java.text.AttributedString)1 ChoiceFormat (java.text.ChoiceFormat)1 FieldPosition (java.text.FieldPosition)1 Format (java.text.Format)1 Date (java.util.Date)1 Test (org.junit.Test)1