Exemplo n.º 1
0
        public void Test_format_Object()
        {
            // Regression for HARMONY-1875
            ILOG.J2CsMapping.Util.Locale.SetDefault(ILOG.J2CsMapping.Util.Locale.CANADA);
            IBM.ICU.Util.TimeZone.SetDefault(IBM.ICU.Util.TimeZone.GetTimeZone("UTC"));
            String pat    = "text here {0, date, yyyyyyyyy } and here";
            String etalon = "text here  000002007  and here";

            IBM.ICU.Text.MessageFormat obj = new IBM.ICU.Text.MessageFormat(pat);
            NUnit.Framework.Assert.AreEqual(etalon, obj.FormatObject(new Object[] { new DateTime((1198141737640L) * 10000) }));

            NUnit.Framework.Assert.AreEqual("{0}", IBM.ICU.Text.MessageFormat.Format("{0}", (Object[])null));
            NUnit.Framework.Assert.AreEqual("nullABC", IBM.ICU.Text.MessageFormat.Format("{0}{1}", new String[] { null, "ABC" }));
        }
Exemplo n.º 2
0
 public void Test_clone()
 {
     // Test for method java.lang.Object java.text.IBM.ICU.Text.MessageFormat.clone()
     IBM.ICU.Text.MessageFormat format = new IBM.ICU.Text.MessageFormat("'{'choice'}'{0}");
     IBM.ICU.Text.MessageFormat clone  = (IBM.ICU.Text.MessageFormat)format.Clone();
     NUnit.Framework.Assert.IsTrue(format.Equals(clone), "Clone not equal");
     NUnit.Framework.Assert.AreEqual("{choice}{0}", format.FormatObject(new Object[] { }), "Wrong answer");
     clone.SetFormat(0, IBM.ICU.Text.DateFormat.GetInstance());
     NUnit.Framework.Assert.IsTrue(!format.Equals(clone), "Clone shares format data");
     format = (IBM.ICU.Text.MessageFormat)clone.Clone();
     Format[] formats = clone.GetFormats();
     ((IBM.ICU.Text.SimpleDateFormat)formats[0]).ApplyPattern("adk123");
     NUnit.Framework.Assert.IsTrue(!format.Equals(clone), "Clone shares format data");
 }
Exemplo n.º 3
0
        public void Test_format_Ljava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition()
        {
            // Test for method java.lang.StringBuffer
            // java.text.IBM.ICU.Text.MessageFormat.format(java.lang.Object [],
            // java.lang.StringBuffer, java.text.FieldPosition)
            IBM.ICU.Text.MessageFormat format = new IBM.ICU.Text.MessageFormat("{1,number,integer}");
            StringBuilder buffer = new StringBuilder();

            format.Format(new Object[] { "0", (double)(53.863d) }, buffer,
                          new FieldPosition(0));
            NUnit.Framework.Assert.AreEqual("54", buffer.ToString(), "Wrong result");
            format.ApplyPattern("{0,choice,0#zero|1#one '{1,choice,2#two {2,time}}'}");
            DateTime date     = DateTime.Now;
            String   expected = "one two "
                                + IBM.ICU.Text.DateFormat.GetTimeInstance().Format(date);
            String result = format.FormatObject(new Object[] { (double )(1.6d),
                                                               ((int)(3)), date });

            NUnit.Framework.Assert.IsTrue(expected.Equals(result), "Choice not recursive:\n" + expected + "\n" + result);
        }
Exemplo n.º 4
0
        public void Test_applyPatternLjava_lang_String()
        {
            // Test for method void
            // java.text.IBM.ICU.Text.MessageFormat.applyPattern(java.lang.String)
            IBM.ICU.Text.MessageFormat format = new IBM.ICU.Text.MessageFormat("test");
            format.ApplyPattern("xx {0}");
            NUnit.Framework.Assert.AreEqual("xx 46", format.FormatObject(new Object[] { ((int)(46)) }), "Invalid number");
            DateTime date     = DateTime.Now;
            String   result   = format.FormatObject(new Object[] { date });
            String   expected = "xx " + IBM.ICU.Text.DateFormat.GetInstance().Format(date);

            NUnit.Framework.Assert.IsTrue(result.Equals(expected), "Invalid date:\n" + result + "\n" + expected);
            format = new IBM.ICU.Text.MessageFormat("{0,date}{1,time}{2,number,integer}");
            format.ApplyPattern("nothing");
            NUnit.Framework.Assert.AreEqual("nothing", format.ToPattern(), "Found formats");

            format.ApplyPattern("{0}");
            NUnit.Framework.Assert.IsNull(format.GetFormats()[0], "Wrong format");
            NUnit.Framework.Assert.AreEqual("{0}", format.ToPattern(), "Wrong pattern");

            format.ApplyPattern("{0, \t\u001ftime }");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat.GetTimeInstance()), "Wrong time format");
            NUnit.Framework.Assert.AreEqual("{0,time}", format.ToPattern(), "Wrong time pattern");
            format.ApplyPattern("{0,Time, Short\n}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetTimeInstance(IBM.ICU.Text.DateFormat.SHORT)), "Wrong short time format");
            NUnit.Framework.Assert.AreEqual("{0,time,short}", format.ToPattern(), "Wrong short time pattern");
            format.ApplyPattern("{0,TIME,\nmedium  }");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetTimeInstance(IBM.ICU.Text.DateFormat.MEDIUM)), "Wrong medium time format");
            NUnit.Framework.Assert.AreEqual("{0,time}", format.ToPattern(), "Wrong medium time pattern");
            format.ApplyPattern("{0,time,LONG}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetTimeInstance(IBM.ICU.Text.DateFormat.LONG)), "Wrong long time format");
            NUnit.Framework.Assert.AreEqual("{0,time,long}", format.ToPattern(), "Wrong long time pattern");
            format.SetLocale(Locale.FRENCH);  // use French since English has the
            // same LONG and FULL time patterns
            format.ApplyPattern("{0,time, Full}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat.GetTimeInstance(
                                                                            IBM.ICU.Text.DateFormat.FULL, Locale.FRENCH)), "Wrong full time format");
            NUnit.Framework.Assert.AreEqual("{0,time,full}", format.ToPattern(), "Wrong full time pattern");
            format.SetLocale(Locale.GetDefault());

            format.ApplyPattern("{0, date}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat.GetDateInstance()), "Wrong date format");
            NUnit.Framework.Assert.AreEqual("{0,date}", format.ToPattern(), "Wrong date pattern");
            format.ApplyPattern("{0, date, short}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetDateInstance(IBM.ICU.Text.DateFormat.SHORT)), "Wrong short date format");
            NUnit.Framework.Assert.AreEqual("{0,date,short}", format.ToPattern(), "Wrong short date pattern");
            format.ApplyPattern("{0, date, medium}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetDateInstance(IBM.ICU.Text.DateFormat.MEDIUM)), "Wrong medium date format");
            NUnit.Framework.Assert.AreEqual("{0,date}", format.ToPattern(), "Wrong medium date pattern");
            format.ApplyPattern("{0, date, long}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetDateInstance(IBM.ICU.Text.DateFormat.LONG)), "Wrong long date format");
            NUnit.Framework.Assert.AreEqual("{0,date,long}", format.ToPattern(), "Wrong long date pattern");
            format.ApplyPattern("{0, date, full}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat
                                                                        .GetDateInstance(IBM.ICU.Text.DateFormat.FULL)), "Wrong full date format");
            NUnit.Framework.Assert.AreEqual("{0,date,full}", format.ToPattern(), "Wrong full date pattern");

            format.ApplyPattern("{0, date, MMM d {hh:mm:ss}}");
            NUnit.Framework.Assert.AreEqual(" MMM d {hh:mm:ss}", ((IBM.ICU.Text.SimpleDateFormat)(format.GetFormats()[0])).ToPattern(), "Wrong time/date format");
            NUnit.Framework.Assert.AreEqual("{0,date, MMM d {hh:mm:ss}}", format.ToPattern(), "Wrong time/date pattern");

            format.ApplyPattern("{0, number}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.NumberFormat.GetNumberInstance()), "Wrong number format");
            NUnit.Framework.Assert.AreEqual("{0,number}", format.ToPattern(), "Wrong number pattern");
            format.ApplyPattern("{0, number, currency}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.NumberFormat
                                                                        .GetCurrencyInstance()), "Wrong currency number format");
            NUnit.Framework.Assert.AreEqual("{0,number,currency}", format.ToPattern(), "Wrong currency number pattern");
            format.ApplyPattern("{0, number, percent}");
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.NumberFormat.GetPercentInstance()), "Wrong percent number format");
            NUnit.Framework.Assert.AreEqual("{0,number,percent}", format.ToPattern(), "Wrong percent number pattern");
            format.ApplyPattern("{0, number, integer}");
            IBM.ICU.Text.NumberFormat nf = IBM.ICU.Text.NumberFormat.GetInstance();
            nf.SetMaximumFractionDigits(0);
            nf.SetParseIntegerOnly(true);
            NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(nf), "Wrong integer number format");
            NUnit.Framework.Assert.AreEqual("{0,number,integer}", format.ToPattern(), "Wrong integer number pattern");

            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}}");
            NUnit.Framework.Assert.AreEqual("0.0#no|1.0#one|2.0#{1,number}", ((ILOG.J2CsMapping.Formatting.ChoiceFormat)format.GetFormats()[0]).ToPattern(), "Wrong choice format");
            NUnit.Framework.Assert.AreEqual("{0,choice,0.0#no|1.0#one|2.0#{1,number}}", format.ToPattern(), "Wrong choice pattern");
            NUnit.Framework.Assert.AreEqual("3.6", format.FormatObject(new Object[] { ((int)(2)), (float)(3.6d) }), "Wrong formatted choice");

            try
            {
                format.ApplyPattern("WRONG MESSAGE FORMAT {0,number,{}");
                NUnit.Framework.Assert.Fail("Expected IllegalArgumentException for invalid pattern");
            }
            catch (ArgumentException e)
            {
            }

            // Regression for HARMONY-65
            IBM.ICU.Text.MessageFormat mf = new IBM.ICU.Text.MessageFormat("{0,number,integer}");
            String badpattern             = "{0,number,#";

            try
            {
                mf.ApplyPattern(badpattern);
                NUnit.Framework.Assert.Fail("Assert 0: Failed to detect unmatched brackets.");
            }
            catch (ArgumentException e_0)
            {
                // expected
            }
        }