public void Test_ConstructorLjava_lang_StringLjava_util_Locale() { // Test for method java.text.MessageFormat(java.lang.String, // java.util.Locale) ILOG.J2CsMapping.Util.Locale mk = new ILOG.J2CsMapping.Util.Locale("mk", "MK"); IBM.ICU.Text.MessageFormat format = new IBM.ICU.Text.MessageFormat( "Date: {0,date} Currency: {1, number, currency} Integer: {2, number, integer}", mk); NUnit.Framework.Assert.IsTrue(format.GetLocale().Equals(mk), "Wrong locale1"); NUnit.Framework.Assert.IsTrue(format.GetFormats()[0].Equals(IBM.ICU.Text.DateFormat .GetDateInstance(IBM.ICU.Text.DateFormat.DEFAULT, mk)), "Wrong locale2"); NUnit.Framework.Assert.IsTrue(format.GetFormats()[1].Equals(IBM.ICU.Text.NumberFormat .GetCurrencyInstance(mk)), "Wrong locale3"); NUnit.Framework.Assert.IsTrue(format.GetFormats()[2].Equals(IBM.ICU.Text.NumberFormat .GetIntegerInstance(mk)), "Wrong locale4"); }
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"); }
public void Test_getFormats() { // Test for method java.text.Format [] // java.text.IBM.ICU.Text.MessageFormat.getFormats() // test with repeating formats and max argument index < max offset Format[] formats = format1.GetFormats(); Format[] correctFormats = new Format[] { IBM.ICU.Text.NumberFormat.GetCurrencyInstance(), IBM.ICU.Text.DateFormat.GetTimeInstance(), IBM.ICU.Text.NumberFormat.GetPercentInstance(), null, new ChoiceFormat("0#off|1#on"), IBM.ICU.Text.DateFormat.GetDateInstance(), }; NUnit.Framework.Assert.AreEqual(correctFormats.Length, formats.Length, "Test1:Returned wrong number of formats:"); for (int i = 0; i < correctFormats.Length; i++) { NUnit.Framework.Assert.AreEqual(correctFormats[i], formats[i], "Test1:wrong format for pattern index " + i + ":"); } // test with max argument index > max offset formats = format2.GetFormats(); correctFormats = new Format[] { IBM.ICU.Text.NumberFormat.GetCurrencyInstance(), IBM.ICU.Text.DateFormat.GetTimeInstance(), IBM.ICU.Text.NumberFormat.GetPercentInstance(), null, new ChoiceFormat("0#off|1#on"), IBM.ICU.Text.DateFormat.GetDateInstance() }; NUnit.Framework.Assert.AreEqual(correctFormats.Length, formats.Length, "Test2:Returned wrong number of formats:"); for (int i_0 = 0; i_0 < correctFormats.Length; i_0++) { NUnit.Framework.Assert.AreEqual(correctFormats[i_0], formats[i_0], "Test2:wrong format for pattern index " + i_0 + ":"); } // test with argument number being zero formats = format3.GetFormats(); NUnit.Framework.Assert.AreEqual(0, formats.Length, "Test3: Returned wrong number of formats:"); }
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 } }
public void Test_setLocaleLjava_util_Locale() { // Test for method void // java.text.IBM.ICU.Text.MessageFormat.setLocale(java.util.Locale) IBM.ICU.Text.MessageFormat format = new IBM.ICU.Text.MessageFormat("date {0,date}"); format.SetLocale(ILOG.J2CsMapping.Util.Locale.CHINA); NUnit.Framework.Assert.AreEqual(ILOG.J2CsMapping.Util.Locale.CHINA, format.GetLocale(), "Wrong locale1"); format.ApplyPattern("{1,date}"); NUnit.Framework.Assert.AreEqual(IBM.ICU.Text.DateFormat.GetDateInstance(IBM.ICU.Text.DateFormat.DEFAULT, ILOG.J2CsMapping.Util.Locale.CHINA), format.GetFormats()[0], "Wrong locale3"); }
public void Test_setFormatsByArgumentIndex_Ljava_text_Format() { // test for method setFormatByArgumentIndex(Format[]) IBM.ICU.Text.MessageFormat f1 = (IBM.ICU.Text.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[] { IBM.ICU.Text.DateFormat.GetTimeInstance(), new ChoiceFormat("0#off|1#on"), IBM.ICU.Text.DateFormat.GetTimeInstance(), IBM.ICU.Text.NumberFormat.GetCurrencyInstance(), new ChoiceFormat("1#few|2#ok|3#a lot") }; f1.SetFormatsByArgumentIndex(correctFormats); Format[] formats = f1.GetFormatsByArgumentIndex(); NUnit.Framework.Assert.AreEqual(correctFormats.Length, formats.Length, "Test1A:Returned wrong number of formats:"); for (int i = 0; i < correctFormats.Length; i++) { NUnit.Framework.Assert.AreEqual(correctFormats[i], formats[i], "Test1B:wrong format for argument index " + i + ":"); } // compare getFormats() results after calls to // setFormatByArgumentIndex() formats = f1.GetFormats(); correctFormats = new Format[] { IBM.ICU.Text.NumberFormat.GetCurrencyInstance(), IBM.ICU.Text.DateFormat.GetTimeInstance(), IBM.ICU.Text.DateFormat.GetTimeInstance(), new ChoiceFormat("1#few|2#ok|3#a lot"), new ChoiceFormat("0#off|1#on"), IBM.ICU.Text.DateFormat.GetTimeInstance(), }; NUnit.Framework.Assert.AreEqual(correctFormats.Length, formats.Length, "Test1C:Returned wrong number of formats:"); for (int i_0 = 0; i_0 < correctFormats.Length; i_0++) { NUnit.Framework.Assert.AreEqual(correctFormats[i_0], formats[i_0], "Test1D:wrong format for pattern index " + i_0 + ":"); } // test setting argumentIndexes that are not used IBM.ICU.Text.MessageFormat f2 = (IBM.ICU.Text.MessageFormat)format2.Clone(); Format[] inputFormats = new Format[] { IBM.ICU.Text.DateFormat.GetDateInstance(), new ChoiceFormat("0#off|1#on"), IBM.ICU.Text.NumberFormat.GetPercentInstance(), IBM.ICU.Text.NumberFormat.GetCurrencyInstance(), IBM.ICU.Text.DateFormat.GetTimeInstance(), null, null, null, IBM.ICU.Text.DateFormat.GetTimeInstance() }; f2.SetFormatsByArgumentIndex(inputFormats); formats = f2.GetFormatsByArgumentIndex(); correctFormats = format2.GetFormatsByArgumentIndex(); NUnit.Framework.Assert.AreEqual(correctFormats.Length, formats.Length, "Test2A:Returned wrong number of formats:"); for (int i_1 = 0; i_1 < correctFormats.Length; i_1++) { NUnit.Framework.Assert.AreEqual(correctFormats[i_1], formats[i_1], "Test2B:wrong format for argument index " + i_1 + ":"); } formats = f2.GetFormats(); correctFormats = new Format[] { IBM.ICU.Text.NumberFormat.GetCurrencyInstance(), IBM.ICU.Text.DateFormat.GetTimeInstance(), IBM.ICU.Text.DateFormat.GetDateInstance(), null, new ChoiceFormat("0#off|1#on"), IBM.ICU.Text.DateFormat.GetDateInstance() }; NUnit.Framework.Assert.AreEqual(correctFormats.Length, formats.Length, "Test2C:Returned wrong number of formats:"); for (int i_2 = 0; i_2 < correctFormats.Length; i_2++) { NUnit.Framework.Assert.AreEqual(correctFormats[i_2], formats[i_2], "Test2D:wrong format for pattern index " + i_2 + ":"); } // test exceeding the argumentIndex number IBM.ICU.Text.MessageFormat f3 = (IBM.ICU.Text.MessageFormat)format3.Clone(); f3.SetFormatsByArgumentIndex(inputFormats); formats = f3.GetFormatsByArgumentIndex(); NUnit.Framework.Assert.AreEqual(0, formats.Length, "Test3A:Returned wrong number of formats:"); formats = f3.GetFormats(); NUnit.Framework.Assert.AreEqual(0, formats.Length, "Test3B:Returned wrong number of formats:"); }
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 } }