@Test
  public void testOverriddenBuiltinFormat() {
    final Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JANUARY, 23);
    final Object[] args = new Object[] {cal.getTime()};
    final Locale[] availableLocales = DateFormat.getAvailableLocales();
    final Map<String, ? extends FormatFactory> dateRegistry =
        Collections.singletonMap("date", new OverrideShortDateFormatFactory());

    // check the non-overridden builtins:
    checkBuiltInFormat("1: {0,date}", dateRegistry, args, availableLocales);
    checkBuiltInFormat("2: {0,date,medium}", dateRegistry, args, availableLocales);
    checkBuiltInFormat("3: {0,date,long}", dateRegistry, args, availableLocales);
    checkBuiltInFormat("4: {0,date,full}", dateRegistry, args, availableLocales);
    checkBuiltInFormat("5: {0,date,d MMM yy}", dateRegistry, args, availableLocales);

    // check the overridden format:
    for (int i = -1; i < availableLocales.length; i++) {
      final Locale locale = i < 0 ? null : availableLocales[i];
      final MessageFormat dateDefault = createMessageFormat("{0,date}", locale);
      final String pattern = "{0,date,short}";
      final ExtendedMessageFormat dateShort =
          new ExtendedMessageFormat(pattern, locale, dateRegistry);
      assertEquals(
          "overridden date,short format", dateDefault.format(args), dateShort.format(args));
      assertEquals("overridden date,short pattern", pattern, dateShort.toPattern());
    }
  }
 /** Test extended formats. */
 @Test
 public void testExtendedFormats() {
   final String pattern = "Lower: {0,lower} Upper: {1,upper}";
   final ExtendedMessageFormat emf = new ExtendedMessageFormat(pattern, registry);
   assertPatternsEqual("TOPATTERN", pattern, emf.toPattern());
   assertEquals("Lower: foo Upper: BAR", emf.format(new Object[] {"foo", "bar"}));
   assertEquals("Lower: foo Upper: BAR", emf.format(new Object[] {"Foo", "Bar"}));
   assertEquals("Lower: foo Upper: BAR", emf.format(new Object[] {"FOO", "BAR"}));
   assertEquals("Lower: foo Upper: BAR", emf.format(new Object[] {"FOO", "bar"}));
   assertEquals("Lower: foo Upper: BAR", emf.format(new Object[] {"foo", "BAR"}));
 }
  /** 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());
 }