private void checkSerialization(MessageFormat format) { try { ByteArrayOutputStream ba = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(ba); out.writeObject(format); out.close(); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(ba.toByteArray())); MessageFormat read = (MessageFormat) in.readObject(); assertTrue("Not equal: " + format.toPattern(), format.equals(read)); } catch (IOException e) { fail("Format: " + format.toPattern() + " caused IOException: " + e); } catch (ClassNotFoundException e) { fail("Format: " + format.toPattern() + " caused ClassNotFoundException: " + e); } }
@TestTargetNew( level = TestLevel.COMPLETE, method = "applyPattern", args = {java.lang.String.class}) public void test_applyPatternLjava_lang_String_AndroidFailure() { MessageFormat format = new MessageFormat("test"); format.setLocale(Locale.FRENCH); // use French since English has the // same LONG and FULL time patterns format.applyPattern("{0,time, Full}"); assertEquals("Wrong full time pattern", "{0,time,full}", format.toPattern()); }
/** @tests java.text.MessageFormat#toPattern() */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "toPattern", args = {}) public void test_toPattern() { // Test for method java.lang.String java.text.MessageFormat.toPattern() String pattern = "[{0}]"; MessageFormat mf = new MessageFormat(pattern); assertTrue("Wrong pattern", mf.toPattern().equals(pattern)); // Regression for HARMONY-59 new MessageFormat("CHOICE {1,choice}").toPattern(); }
@Override public String[] unparse(LoadContext context, CDOMObject cdo) { MapChanges<CaseInsensitiveString, MessageFormat> changes = context.getObjectContext().getMapChanges(cdo, MapKey.INFO); if (changes == null || changes.isEmpty()) { return null; } Set<String> set = new TreeSet<>(); for (CaseInsensitiveString key : changes.getAdded().keySet()) { MessageFormat value = changes.getAdded().get(key); set.add( new StringBuilder() .append(key) .append(Constants.PIPE) .append(value.toPattern()) .toString()); } return set.toArray(new String[set.size()]); }
/** Test extended and built in formats. */ @Test public void testExtendedAndBuiltInFormats() { final Calendar cal = Calendar.getInstance(); cal.set(2007, Calendar.JANUARY, 23, 18, 33, 05); final Object[] args = new Object[] {"John Doe", cal.getTime(), Double.valueOf("12345.67")}; final String builtinsPattern = "DOB: {1,date,short} Salary: {2,number,currency}"; final String extendedPattern = "Name: {0,upper} "; final String pattern = extendedPattern + builtinsPattern; final HashSet<Locale> testLocales = new HashSet<Locale>(); testLocales.addAll(Arrays.asList(DateFormat.getAvailableLocales())); testLocales.retainAll(Arrays.asList(NumberFormat.getAvailableLocales())); testLocales.add(null); for (final Locale locale : testLocales) { final MessageFormat builtins = createMessageFormat(builtinsPattern, locale); final String expectedPattern = extendedPattern + builtins.toPattern(); DateFormat df = null; NumberFormat nf = null; ExtendedMessageFormat emf = null; if (locale == null) { df = DateFormat.getDateInstance(DateFormat.SHORT); nf = NumberFormat.getCurrencyInstance(); emf = new ExtendedMessageFormat(pattern, registry); } else { df = DateFormat.getDateInstance(DateFormat.SHORT, locale); nf = NumberFormat.getCurrencyInstance(locale); emf = new ExtendedMessageFormat(pattern, locale, registry); } final StringBuilder expected = new StringBuilder(); expected.append("Name: "); expected.append(args[0].toString().toUpperCase()); expected.append(" DOB: "); expected.append(df.format(args[1])); expected.append(" Salary: "); expected.append(nf.format(args[2])); assertPatternsEqual( "pattern comparison for locale " + locale, expectedPattern, emf.toPattern()); assertEquals(String.valueOf(locale), expected.toString(), emf.format(args)); } }
/** * Create an ExtendedMessageFormat for the specified pattern and locale and check the formated * output matches the expected result for the parameters. * * @param pattern string * @param registryUnused map (currently unused) * @param args Object[] * @param locale Locale */ private void checkBuiltInFormat( final String pattern, final Map<String, ?> registryUnused, final Object[] args, final Locale locale) { final StringBuilder buffer = new StringBuilder(); buffer.append("Pattern=["); buffer.append(pattern); buffer.append("], locale=["); buffer.append(locale); buffer.append("]"); final MessageFormat mf = createMessageFormat(pattern, locale); // System.out.println(buffer + ", result=[" + mf.format(args) +"]"); ExtendedMessageFormat emf = null; if (locale == null) { emf = new ExtendedMessageFormat(pattern); } else { emf = new ExtendedMessageFormat(pattern, locale); } assertEquals("format " + buffer.toString(), mf.format(args), emf.format(args)); assertPatternsEqual("toPattern " + buffer.toString(), mf.toPattern(), emf.toPattern()); }
/** @tests java.text.MessageFormat#applyPattern(java.lang.String) */ @TestTargetNew( level = TestLevel.COMPLETE, method = "applyPattern", args = {java.lang.String.class}) public void test_applyPatternLjava_lang_String() { // Test for method void // java.text.MessageFormat.applyPattern(java.lang.String) MessageFormat format = new MessageFormat("test"); format.applyPattern("xx {0}"); assertEquals("Invalid number", "xx 46", format.format(new Object[] {new Integer(46)})); Date date = new Date(); String result = format.format(new Object[] {date}); String expected = "xx " + DateFormat.getInstance().format(date); assertTrue("Invalid date:\n" + result + "\n" + expected, result.equals(expected)); format = new MessageFormat("{0,date}{1,time}{2,number,integer}"); format.applyPattern("nothing"); assertEquals("Found formats", "nothing", format.toPattern()); format.applyPattern("{0}"); assertNull("Wrong format", format.getFormats()[0]); assertEquals("Wrong pattern", "{0}", format.toPattern()); format.applyPattern("{0, \t\u001ftime }"); assertTrue("Wrong time format", format.getFormats()[0].equals(DateFormat.getTimeInstance())); assertEquals("Wrong time pattern", "{0,time}", format.toPattern()); format.applyPattern("{0,Time, Short\n}"); assertTrue( "Wrong short time format", format.getFormats()[0].equals(DateFormat.getTimeInstance(DateFormat.SHORT))); assertEquals("Wrong short time pattern", "{0,time,short}", format.toPattern()); format.applyPattern("{0,TIME,\nmedium }"); assertTrue( "Wrong medium time format", format.getFormats()[0].equals(DateFormat.getTimeInstance(DateFormat.MEDIUM))); assertEquals("Wrong medium time pattern", "{0,time}", format.toPattern()); format.applyPattern("{0,time,LONG}"); assertTrue( "Wrong long time format", format.getFormats()[0].equals(DateFormat.getTimeInstance(DateFormat.LONG))); assertEquals("Wrong long time pattern", "{0,time,long}", format.toPattern()); format.setLocale(Locale.FRENCH); // use French since English has the // same LONG and FULL time patterns format.applyPattern("{0,time, Full}"); assertTrue( "Wrong full time format", format.getFormats()[0].equals(DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH))); // Outsourced to _AndroidFailure: // // assertEquals("Wrong full time pattern", "{0,time,full}", format // .toPattern()); format.setLocale(Locale.getDefault()); format.applyPattern("{0, date}"); assertTrue("Wrong date format", format.getFormats()[0].equals(DateFormat.getDateInstance())); assertEquals("Wrong date pattern", "{0,date}", format.toPattern()); format.applyPattern("{0, date, short}"); assertTrue( "Wrong short date format", format.getFormats()[0].equals(DateFormat.getDateInstance(DateFormat.SHORT))); assertEquals("Wrong short date pattern", "{0,date,short}", format.toPattern()); format.applyPattern("{0, date, medium}"); assertTrue( "Wrong medium date format", format.getFormats()[0].equals(DateFormat.getDateInstance(DateFormat.MEDIUM))); assertEquals("Wrong medium date pattern", "{0,date}", format.toPattern()); format.applyPattern("{0, date, long}"); assertTrue( "Wrong long date format", format.getFormats()[0].equals(DateFormat.getDateInstance(DateFormat.LONG))); assertEquals("Wrong long date pattern", "{0,date,long}", format.toPattern()); format.applyPattern("{0, date, full}"); assertTrue( "Wrong full date format", format.getFormats()[0].equals(DateFormat.getDateInstance(DateFormat.FULL))); assertEquals("Wrong full date pattern", "{0,date,full}", format.toPattern()); format.applyPattern("{0, date, MMM d {hh:mm:ss}}"); assertEquals( "Wrong time/date format", " MMM d {hh:mm:ss}", ((SimpleDateFormat) (format.getFormats()[0])).toPattern()); assertEquals("Wrong time/date pattern", "{0,date, MMM d {hh:mm:ss}}", format.toPattern()); format.applyPattern("{0, number}"); assertTrue( "Wrong number format", format.getFormats()[0].equals(NumberFormat.getNumberInstance())); assertEquals("Wrong number pattern", "{0,number}", format.toPattern()); format.applyPattern("{0, number, currency}"); assertTrue( "Wrong currency number format", format.getFormats()[0].equals(NumberFormat.getCurrencyInstance())); assertEquals("Wrong currency number pattern", "{0,number,currency}", format.toPattern()); format.applyPattern("{0, number, percent}"); assertTrue( "Wrong percent number format", format.getFormats()[0].equals(NumberFormat.getPercentInstance())); assertEquals("Wrong percent number pattern", "{0,number,percent}", format.toPattern()); format.applyPattern("{0, number, integer}"); NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(0); nf.setParseIntegerOnly(true); assertTrue("Wrong integer number format", format.getFormats()[0].equals(nf)); assertEquals("Wrong integer number pattern", "{0,number,integer}", format.toPattern()); format.applyPattern("{0, number, {'#'}##0.0E0}"); /* * TODO validate these assertions String actual = * ((DecimalFormat)(format.getFormats()[0])).toPattern(); * assertEquals("Wrong pattern number format", "' {#}'##0.0E0", actual); * assertEquals("Wrong pattern number pattern", "{0,number,' * {#}'##0.0E0}", format.toPattern()); * */ format.applyPattern("{0, choice,0#no|1#one|2#{1,number}}"); assertEquals( "Wrong choice format", "0.0#no|1.0#one|2.0#{1,number}", ((ChoiceFormat) format.getFormats()[0]).toPattern()); assertEquals( "Wrong choice pattern", "{0,choice,0.0#no|1.0#one|2.0#{1,number}}", format.toPattern()); assertEquals( "Wrong formatted choice", "3.6", format.format(new Object[] {new Integer(2), new Float(3.6)})); try { format.applyPattern("WRONG MESSAGE FORMAT {0,number,{}"); fail("Expected IllegalArgumentException for invalid pattern"); } catch (IllegalArgumentException e) { } // Regression for HARMONY-65 MessageFormat mf = new MessageFormat("{0,number,integer}"); String badpattern = "{0,number,#"; try { mf.applyPattern(badpattern); fail("Assert 0: Failed to detect unmatched brackets."); } catch (IllegalArgumentException e) { // expected } }