/**
   * @tests java.text.MessageFormat#setFormat(int, Format) Test of method
   *     java.text.MessageFormat#setFormat(int, Format). Case 1: Compare getFormats() results after
   *     calls to setFormat(). Case 2: Try to call setFormat() using incorrect index.
   */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "setFormat",
      args = {int.class, java.text.Format.class})
  public void test_setFormatILjava_text_Format() {
    try {
      // case 1: Compare getFormats() results after calls to setFormat()
      MessageFormat f1 = (MessageFormat) format1.clone();
      f1.setFormat(0, DateFormat.getTimeInstance());
      f1.setFormat(1, DateFormat.getTimeInstance());
      f1.setFormat(2, NumberFormat.getInstance());
      f1.setFormat(3, new ChoiceFormat("0#off|1#on"));
      f1.setFormat(4, new ChoiceFormat("1#few|2#ok|3#a lot"));
      f1.setFormat(5, DateFormat.getTimeInstance());

      Format[] formats = f1.getFormats();
      formats = f1.getFormats();

      Format[] correctFormats =
          new Format[] {
            DateFormat.getTimeInstance(),
            DateFormat.getTimeInstance(),
            NumberFormat.getInstance(),
            new ChoiceFormat("0#off|1#on"),
            new ChoiceFormat("1#few|2#ok|3#a lot"),
            DateFormat.getTimeInstance()
          };

      assertEquals(
          "Test1A:Returned wrong number of formats:", correctFormats.length, formats.length);
      for (int i = 0; i < correctFormats.length; i++) {
        assertEquals(
            "Test1B:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
      }

      // case 2: Try to setFormat using incorrect index
      try {
        f1.setFormat(-1, DateFormat.getDateInstance());
        fail("Expected ArrayIndexOutOfBoundsException was not thrown");
        f1.setFormat(f1.getFormats().length, DateFormat.getDateInstance());
        fail("Expected ArrayIndexOutOfBoundsException was not thrown");
      } catch (ArrayIndexOutOfBoundsException e) {
        // expected
      }
    } catch (Exception e) {
      fail("Unexpected exception " + e.toString());
    }
  }
  /** @tests java.text.MessageFormat#getFormats() */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "getFormats",
      args = {})
  public void test_getFormats() {
    // Test for method java.text.Format []
    // java.text.MessageFormat.getFormats()

    // test with repeating formats and max argument index < max offset
    Format[] formats = format1.getFormats();
    Format[] correctFormats =
        new Format[] {
          NumberFormat.getCurrencyInstance(),
          DateFormat.getTimeInstance(),
          NumberFormat.getPercentInstance(),
          null,
          new ChoiceFormat("0#off|1#on"),
          DateFormat.getDateInstance(),
        };

    assertEquals("Test1:Returned wrong number of formats:", correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
      assertEquals(
          "Test1:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
    }

    // test with max argument index > max offset
    formats = format2.getFormats();
    correctFormats =
        new Format[] {
          NumberFormat.getCurrencyInstance(),
          DateFormat.getTimeInstance(),
          NumberFormat.getPercentInstance(),
          null,
          new ChoiceFormat("0#off|1#on"),
          DateFormat.getDateInstance()
        };

    assertEquals("Test2:Returned wrong number of formats:", correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
      assertEquals(
          "Test2:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
    }

    // test with argument number being zero
    formats = format3.getFormats();
    assertEquals("Test3: Returned wrong number of formats:", 0, formats.length);
  }
  /** @tests java.text.MessageFormat#MessageFormat(java.lang.String, java.util.Locale) */
  @TestTargetNew(
      level = TestLevel.PARTIAL_COMPLETE,
      notes = "IllegalArgumentException is not verified.",
      method = "MessageFormat",
      args = {java.lang.String.class, java.util.Locale.class})
  public void test_ConstructorLjava_lang_StringLjava_util_Locale() {
    // Test for method java.text.MessageFormat(java.lang.String,
    // java.util.Locale)
    Locale mk = new Locale("mk", "MK");
    MessageFormat format =
        new MessageFormat(
            "Date: {0,date} Currency: {1, number, currency} Integer: {2, number, integer}", mk);

    assertTrue("Wrong locale1", format.getLocale().equals(mk));
    assertTrue(
        "Wrong locale2",
        format.getFormats()[0].equals(DateFormat.getDateInstance(DateFormat.DEFAULT, mk)));
    assertTrue(
        "Wrong locale3", format.getFormats()[1].equals(NumberFormat.getCurrencyInstance(mk)));
    assertTrue("Wrong locale4", format.getFormats()[2].equals(NumberFormat.getIntegerInstance(mk)));
  }
 /** @tests java.text.MessageFormat#setLocale(java.util.Locale) */
 @TestTargetNew(
     level = TestLevel.COMPLETE,
     notes = "",
     method = "setLocale",
     args = {java.util.Locale.class})
 public void test_setLocaleLjava_util_Locale() {
   // Test for method void
   // java.text.MessageFormat.setLocale(java.util.Locale)
   MessageFormat format = new MessageFormat("date {0,date}");
   format.setLocale(Locale.CHINA);
   assertEquals("Wrong locale1", Locale.CHINA, format.getLocale());
   format.applyPattern("{1,date}");
   assertEquals(
       "Wrong locale3",
       DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINA),
       format.getFormats()[0]);
 }
 /** @tests java.text.MessageFormat#clone() */
 @TestTargetNew(
     level = TestLevel.COMPLETE,
     notes = "",
     method = "clone",
     args = {})
 public void test_clone() {
   // Test for method java.lang.Object java.text.MessageFormat.clone()
   MessageFormat format = new MessageFormat("'{'choice'}'{0}");
   MessageFormat clone = (MessageFormat) format.clone();
   assertTrue("Clone not equal", format.equals(clone));
   assertEquals("Wrong answer", "{choice}{0}", format.format(new Object[] {}));
   clone.setFormat(0, DateFormat.getInstance());
   assertTrue("Clone shares format data", !format.equals(clone));
   format = (MessageFormat) clone.clone();
   Format[] formats = clone.getFormats();
   ((SimpleDateFormat) formats[0]).applyPattern("adk123");
   assertTrue("Clone shares format data", !format.equals(clone));
 }
  /**
   * @tests java.text.MessageFormat#setFormats(Format[]) Test of method
   *     java.text.MessageFormat#setFormats(Format[]). Case 1: Test with repeating formats and max
   *     argument index < max offset compare getFormats() results after calls to
   *     setFormats(Format[]) Case 2: Try to pass null argument to setFormats().
   */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "setFormats",
      args = {java.text.Format[].class})
  public void test_setFormats$Ljava_text_Format() {
    try {
      MessageFormat f1 = (MessageFormat) format1.clone();

      // case 1: Test with repeating formats and max argument index < max
      // offset
      // compare getFormats() results after calls to setFormats(Format[])
      Format[] correctFormats =
          new Format[] {
            DateFormat.getTimeInstance(),
            new ChoiceFormat("0#off|1#on"),
            DateFormat.getTimeInstance(),
            NumberFormat.getCurrencyInstance(),
            new ChoiceFormat("1#few|2#ok|3#a lot")
          };

      f1.setFormats(correctFormats);
      Format[] formats = f1.getFormats();

      assertTrue(
          "Test1A:Returned wrong number of formats:", correctFormats.length <= formats.length);
      for (int i = 0; i < correctFormats.length; i++) {
        assertEquals(
            "Test1B:wrong format for argument index " + i + ":", correctFormats[i], formats[i]);
      }

      // case 2: Try to pass null argument to setFormats().
      try {
        f1.setFormats(null);
        fail("Expected exception NullPointerException was not thrown");
      } catch (NullPointerException e) {
        // expected
      }
    } catch (Exception e) {
      fail("Unexpected exception " + e.toString());
    }
  }
  /**
   * Do something TODO.
   *
   * <p>Details of the function.
   */
  private void applyFormats(MessageFormat subFormat) {

    for (Format format : subFormat.getFormats()) {
      if (!(format instanceof ChoiceFormat)) {
        continue;
      }

      ChoiceFormat choice = (ChoiceFormat) format;
      String[] choiceFormats = (String[]) choice.getFormats();
      for (int i = 0; i < choiceFormats.length; i++) {
        String innerFormat = choiceFormats[i];
        if (innerFormat.contains("{")) {
          BeanMessageFormat recursive = new BeanMessageFormat(innerFormat, root);
          choiceFormats[i] = recursive.inner.toPattern();
        }
      }

      choice.setChoices(choice.getLimits(), choiceFormats);
    }
  }
  /** @tests java.text.MessageFormat#MessageFormat(java.lang.String) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      method = "MessageFormat",
      args = {java.lang.String.class})
  public void test_ConstructorLjava_lang_String() {
    // Test for method java.text.MessageFormat(java.lang.String)
    MessageFormat format =
        new MessageFormat(
            "abc {4,time} def {3,date} ghi {2,number} jkl {1,choice,0#low|1#high} mnop {0}");
    assertTrue("Not a MessageFormat", format.getClass() == MessageFormat.class);
    Format[] formats = format.getFormats();
    assertNotNull("null formats", formats);
    assertTrue("Wrong format count: " + formats.length, formats.length >= 5);
    assertTrue("Wrong time format", formats[0].equals(DateFormat.getTimeInstance()));
    assertTrue("Wrong date format", formats[1].equals(DateFormat.getDateInstance()));
    assertTrue("Wrong number format", formats[2].equals(NumberFormat.getInstance()));
    assertTrue("Wrong choice format", formats[3].equals(new ChoiceFormat("0.0#low|1.0#high")));
    assertNull("Wrong string format", formats[4]);

    Date date = new Date();
    FieldPosition pos = new FieldPosition(-1);
    StringBuffer buffer = new StringBuffer();
    format.format(new Object[] {"123", new Double(1.6), new Double(7.2), date, date}, buffer, pos);
    String result = buffer.toString();
    buffer.setLength(0);
    buffer.append("abc ");
    buffer.append(DateFormat.getTimeInstance().format(date));
    buffer.append(" def ");
    buffer.append(DateFormat.getDateInstance().format(date));
    buffer.append(" ghi ");
    buffer.append(NumberFormat.getInstance().format(new Double(7.2)));
    buffer.append(" jkl high mnop 123");
    assertTrue("Wrong answer:\n" + result + "\n" + buffer, result.equals(buffer.toString()));

    assertEquals(
        "Simple string", "Test message", new MessageFormat("Test message").format(new Object[0]));

    result = new MessageFormat("Don't").format(new Object[0]);
    assertTrue("Should not throw IllegalArgumentException: " + result, "Dont".equals(result));

    try {
      new MessageFormat("Invalid {1,foobar} format descriptor!");
      fail("Expected test_ConstructorLjava_lang_String to throw IAE.");
    } catch (IllegalArgumentException ex) {
      // expected
    }

    try {
      new MessageFormat("Invalid {1,date,invalid-spec} format descriptor!");
    } catch (IllegalArgumentException ex) {
      // expected
    }

    checkSerialization(new MessageFormat(""));
    checkSerialization(new MessageFormat("noargs"));
    checkSerialization(new MessageFormat("{0}"));
    checkSerialization(new MessageFormat("a{0}"));
    checkSerialization(new MessageFormat("{0}b"));
    checkSerialization(new MessageFormat("a{0}b"));

    // Regression for HARMONY-65
    try {
      new MessageFormat("{0,number,integer");
      fail("Assert 0: Failed to detect unmatched brackets.");
    } catch (IllegalArgumentException e) {
      // expected
    }
  }
  /** @tests java.text.MessageFormat#setFormatsByArgumentIndex(java.text.Format[]) */
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "setFormatsByArgumentIndex",
      args = {java.text.Format[].class})
  public void test_setFormatsByArgumentIndex$Ljava_text_Format() {
    // test for method setFormatByArgumentIndex(Format[])
    MessageFormat f1 = (MessageFormat) format1.clone();

    // test with repeating formats and max argument index < max offset
    // compare getFormatsByArgumentIndex() results after calls to
    // setFormatsByArgumentIndex(Format[])
    Format[] correctFormats =
        new Format[] {
          DateFormat.getTimeInstance(),
          new ChoiceFormat("0#off|1#on"),
          DateFormat.getTimeInstance(),
          NumberFormat.getCurrencyInstance(),
          new ChoiceFormat("1#few|2#ok|3#a lot")
        };

    f1.setFormatsByArgumentIndex(correctFormats);
    Format[] formats = f1.getFormatsByArgumentIndex();

    assertEquals("Test1A:Returned wrong number of formats:", correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
      assertEquals(
          "Test1B:wrong format for argument index " + i + ":", correctFormats[i], formats[i]);
    }

    // compare getFormats() results after calls to
    // setFormatByArgumentIndex()
    formats = f1.getFormats();
    correctFormats =
        new Format[] {
          NumberFormat.getCurrencyInstance(),
          DateFormat.getTimeInstance(),
          DateFormat.getTimeInstance(),
          new ChoiceFormat("1#few|2#ok|3#a lot"),
          new ChoiceFormat("0#off|1#on"),
          DateFormat.getTimeInstance(),
        };

    assertEquals("Test1C:Returned wrong number of formats:", correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
      assertEquals(
          "Test1D:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
    }

    // test setting argumentIndexes that are not used
    MessageFormat f2 = (MessageFormat) format2.clone();
    Format[] inputFormats =
        new Format[] {
          DateFormat.getDateInstance(),
          new ChoiceFormat("0#off|1#on"),
          NumberFormat.getPercentInstance(),
          NumberFormat.getCurrencyInstance(),
          DateFormat.getTimeInstance(),
          null,
          null,
          null,
          DateFormat.getTimeInstance()
        };
    f2.setFormatsByArgumentIndex(inputFormats);

    formats = f2.getFormatsByArgumentIndex();
    correctFormats = format2.getFormatsByArgumentIndex();

    assertEquals("Test2A:Returned wrong number of formats:", correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
      assertEquals(
          "Test2B:wrong format for argument index " + i + ":", correctFormats[i], formats[i]);
    }

    formats = f2.getFormats();
    correctFormats =
        new Format[] {
          NumberFormat.getCurrencyInstance(),
          DateFormat.getTimeInstance(),
          DateFormat.getDateInstance(),
          null,
          new ChoiceFormat("0#off|1#on"),
          DateFormat.getDateInstance()
        };

    assertEquals("Test2C:Returned wrong number of formats:", correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
      assertEquals(
          "Test2D:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
    }

    // test exceeding the argumentIndex number
    MessageFormat f3 = (MessageFormat) format3.clone();
    f3.setFormatsByArgumentIndex(inputFormats);

    formats = f3.getFormatsByArgumentIndex();
    assertEquals("Test3A:Returned wrong number of formats:", 0, formats.length);

    formats = f3.getFormats();
    assertEquals("Test3B:Returned wrong number of formats:", 0, formats.length);
  }
  /** @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
    }
  }