Exemplo n.º 1
0
        public void Test_ConstructorLjava_lang_String()
        {
            // Test for method java.text.MessageFormat(java.lang.String)
            IBM.ICU.Text.MessageFormat format = new IBM.ICU.Text.MessageFormat(
                "abc {4,time} def {3,date} ghi {2,number} jkl {1,choice,0#low|1#high} mnop {0}");
            NUnit.Framework.Assert.IsTrue((Object)format.GetType() == (Object)typeof(IBM.ICU.Text.MessageFormat), "Not a IBM.ICU.Text.MessageFormat");
            ILOG.J2CsMapping.Formatting.Format[] formats = format.GetFormats();
            NUnit.Framework.Assert.IsNotNull(formats, "null formats");
            NUnit.Framework.Assert.IsTrue(formats.Length >= 5, "Wrong format count: " + formats.Length);
            NUnit.Framework.Assert.IsTrue(formats[0].Equals(IBM.ICU.Text.DateFormat.GetTimeInstance()), "Wrong time format");
            NUnit.Framework.Assert.IsTrue(formats[1].Equals(IBM.ICU.Text.DateFormat.GetDateInstance()), "Wrong date format");
            NUnit.Framework.Assert.IsTrue(formats[2].Equals(IBM.ICU.Text.NumberFormat.GetInstance()), "Wrong number format");
            NUnit.Framework.Assert.IsTrue(formats[3].Equals(new ChoiceFormat("0.0#low|1.0#high")), "Wrong choice format");
            NUnit.Framework.Assert.IsNull(formats[4], "Wrong string format");

            DateTime      date   = DateTime.Now;
            FieldPosition pos    = new FieldPosition(-1);
            StringBuilder buffer = new StringBuilder();

            format.Format(new Object[] { "123", (double )(1.6d), (double )(7.2d),
                                         date, date }, buffer, pos);
            String result = buffer.ToString();

            buffer.Length = 0;
            buffer.Append("abc ");
            buffer.Append(IBM.ICU.Text.DateFormat.GetTimeInstance().Format(date));
            buffer.Append(" def ");
            buffer.Append(IBM.ICU.Text.DateFormat.GetDateInstance().Format(date));
            buffer.Append(" ghi ");
            buffer.Append(IBM.ICU.Text.NumberFormat.GetInstance().Format((double)(7.2d)));
            buffer.Append(" jkl high mnop 123");
            NUnit.Framework.Assert.IsTrue(result.Equals(buffer.ToString()), "Wrong answer:\n" + result + "\n" + buffer);

            NUnit.Framework.Assert.AreEqual("Test message", new IBM.ICU.Text.MessageFormat(
                                                "Test message").FormatObject(new Object[0]), "Simple string");

            result = new IBM.ICU.Text.MessageFormat("Don't").FormatObject(new Object[0]);
            NUnit.Framework.Assert.IsTrue("Dont".Equals(result), "Should not throw IllegalArgumentException: " + result);

            try
            {
                new IBM.ICU.Text.MessageFormat("Invalid {1,foobar} format descriptor!");
                NUnit.Framework.Assert.Fail("Expected test_ConstructorLjava_lang_String to throw IAE.");
            }
            catch (ArgumentException ex)
            {
                // expected
            }

            try
            {
                new IBM.ICU.Text.MessageFormat(
                    "Invalid {1,date,invalid-spec} format descriptor!");
            }
            catch (ArgumentException ex_0)
            {
                // expected
            }

            //CheckSerialization(new IBM.ICU.Text.MessageFormat(""));
            //CheckSerialization(new IBM.ICU.Text.MessageFormat("noargs"));
            //CheckSerialization(new IBM.ICU.Text.MessageFormat("{0}"));
            //CheckSerialization(new IBM.ICU.Text.MessageFormat("a{0}"));
            //CheckSerialization(new IBM.ICU.Text.MessageFormat("{0}b"));
            //CheckSerialization(new IBM.ICU.Text.MessageFormat("a{0}b"));

            // Regression for HARMONY-65
            try
            {
                new IBM.ICU.Text.MessageFormat("{0,number,integer");
                NUnit.Framework.Assert.Fail("Assert 0: Failed to detect unmatched brackets.");
            }
            catch (ArgumentException e)
            {
                // expected
            }
        }