public void Test4031438() { String pattern1 = "Impossible {1} has occurred -- status code is {0} and message is {2}."; String pattern2 = "Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'."; MessageFormat messageFormatter = new MessageFormat(""); try { Logln("Apply with pattern : " + pattern1); messageFormatter.ApplyPattern(pattern1); Object[] paramArray = { (int)7 }; String tempBuffer = messageFormatter.Format(paramArray); if (!tempBuffer.Equals("Impossible {1} has occurred -- status code is 7 and message is {2}.")) { Errln("Tests arguments < substitution failed"); } Logln("Formatted with 7 : " + tempBuffer); ParsePosition status = new ParsePosition(0); Object[] objs = messageFormatter.Parse(tempBuffer, status); if (objs[paramArray.Length] != null) { Errln("Parse failed with more than expected arguments"); } for (int i = 0; i < objs.Length; i++) { if (objs[i] != null && !objs[i].ToString().Equals(paramArray[i].ToString())) { Errln("Parse failed on object " + objs[i] + " at index : " + i); } } tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Impossible {1} has occurred -- status code is {0} and message is {2}.")) { Errln("Tests with no arguments failed"); } Logln("Formatted with null : " + tempBuffer); Logln("Apply with pattern : " + pattern2); messageFormatter.ApplyPattern(pattern2); tempBuffer = messageFormatter.Format(paramArray); if (!tempBuffer.Equals("Double ' Quotes 7 test and quoted {1} test plus 'other {2} stuff'.")) { Errln("quote format test (w/ params) failed."); } Logln("Formatted with params : " + tempBuffer); tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Double ' Quotes {0} test and quoted {1} test plus 'other {2} stuff'.")) { Errln("quote format test (w/ null) failed."); } Logln("Formatted with null : " + tempBuffer); Logln("toPattern : " + messageFormatter.ToPattern()); } catch (Exception foo) { Warnln("Exception when formatting in bug 4031438. " + foo.Message); } }
public void Test4074764() { String[] pattern = { "Message without param", "Message with param:{0}", "Longer Message with param {0}" }; //difference between the two param strings are that //in the first one, the param position is within the //length of the string without param while it is not so //in the other case. MessageFormat messageFormatter = new MessageFormat(""); try { //Apply pattern with param and print the result messageFormatter.ApplyPattern(pattern[1]); Object[] paramArray = { "BUG", new DateTime() }; String tempBuffer = messageFormatter.Format(paramArray); if (!tempBuffer.Equals("Message with param:BUG")) { Errln("MessageFormat with one param test failed."); } Logln("Formatted with one extra param : " + tempBuffer); //Apply pattern without param and print the result messageFormatter.ApplyPattern(pattern[0]); tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Message without param")) { Errln("MessageFormat with no param test failed."); } Logln("Formatted with no params : " + tempBuffer); tempBuffer = messageFormatter.Format(paramArray); if (!tempBuffer.Equals("Message without param")) { Errln("Formatted with arguments > subsitution failed. result = " + tempBuffer.ToString()); } Logln("Formatted with extra params : " + tempBuffer); //This statement gives an exception while formatting... //If we use pattern[1] for the message with param, //we get an NullPointerException in MessageFormat.java(617) //If we use pattern[2] for the message with param, //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614) //Both are due to maxOffset not being reset to -1 //in applyPattern() when the pattern does not //contain any param. } catch (Exception foo) { Errln("Exception when formatting with no params."); } }
public void Test4118594() { MessageFormat mf = new MessageFormat("{0}, {0}, {0}"); String forParsing = "x, y, z"; Object[] objs = mf.Parse(forParsing, new ParsePosition(0)); Logln("pattern: \"" + mf.ToPattern() + "\""); Logln("text for parsing: \"" + forParsing + "\""); if (!objs[0].ToString().Equals("z")) { Errln("argument0: \"" + objs[0] + "\""); } mf.SetLocale(new CultureInfo("en-us")); mf.ApplyPattern("{0,number,#.##}, {0,number,#.#}"); Object[] oldobjs = { 3.1415d }; String result = mf.Format(oldobjs); Logln("pattern: \"" + mf.ToPattern() + "\""); Logln("text for parsing: \"" + result + "\""); // result now equals "3.14, 3.1" if (!result.Equals("3.14, 3.1")) { Errln("result = " + result); } Object[] newobjs = mf.Parse(result, new ParsePosition(0)); // newobjs now equals {new Double(3.1)} if (Convert.ToDouble(newobjs[0]) != 3.1) // was (Double) [alan] { Errln("newobjs[0] = " + newobjs[0]); } }
public void Test4116444() { String[] patterns = { "", "one", "{0,date,short}" }; MessageFormat mf = new MessageFormat(""); for (int i = 0; i < patterns.Length; i++) { String pattern = patterns[i]; mf.ApplyPattern(pattern); try { Object[] array = mf.Parse(null, new ParsePosition(0)); Logln("pattern: \"" + pattern + "\""); Log(" parsedObjects: "); if (array != null) { Log("{"); for (int j = 0; j < array.Length; j++) { if (array[j] != null) { Err("\"" + array[j].ToString() + "\""); } else { Log("null"); } if (j < array.Length - 1) { Log(","); } } Log("}"); } else { Log("null"); } Logln(""); } catch (Exception e) { Errln("pattern: \"" + pattern + "\""); Errln(" Exception: " + e.Message); } } }
public void Test4114743() { String originalPattern = "initial pattern"; MessageFormat mf = new MessageFormat(originalPattern); String illegalPattern = "ab { '}' de"; try { mf.ApplyPattern(illegalPattern); Errln("illegal pattern: \"" + illegalPattern + "\""); } catch (ArgumentException foo) { if (illegalPattern.Equals(mf.ToPattern())) { Errln("pattern after: \"" + mf.ToPattern() + "\""); } } }
public void Test4113018() { String originalPattern = "initial pattern"; MessageFormat mf = new MessageFormat(originalPattern); String illegalPattern = "format: {0, xxxYYY}"; Logln("pattern before: \"" + mf.ToPattern() + "\""); Logln("illegal pattern: \"" + illegalPattern + "\""); try { mf.ApplyPattern(illegalPattern); Errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern); } catch (ArgumentException e) { if (illegalPattern.Equals(mf.ToPattern())) { Errln("pattern after: \"" + mf.ToPattern() + "\""); } } }
public void Test4118592() { MessageFormat mf = new MessageFormat(""); String pattern = "{0,choice,1#YES|2#NO}"; String prefix = ""; for (int i = 0; i < 5; i++) { String formatted = prefix + "YES"; mf.ApplyPattern(prefix + pattern); prefix += "x"; Object[] objs = mf.Parse(formatted, new ParsePosition(0)); Logln(i + ". pattern :\"" + mf.ToPattern() + "\""); Log(" \"" + formatted + "\" parsed as "); if (objs == null) { Logln(" null"); } else { Logln(" " + objs[0]); } } }
public void TestBugTestsWithNamesArguments() { { // Taken from Test4031438(). String pattern1 = "Impossible {arg1} has occurred -- status code is {arg0} and message is {arg2}."; String pattern2 = "Double '' Quotes {ARG_ZERO} test and quoted '{ARG_ONE}' test plus 'other {ARG_TWO} stuff'."; MessageFormat messageFormatter = new MessageFormat(""); try { Logln("Apply with pattern : " + pattern1); messageFormatter.ApplyPattern(pattern1); IDictionary <string, object> paramsMap = new Dictionary <string, object>(); paramsMap["arg0"] = 7; String tempBuffer = messageFormatter.Format(paramsMap); if (!tempBuffer.Equals("Impossible {arg1} has occurred -- status code is 7 and message is {arg2}.")) { Errln("Tests arguments < substitution failed"); } Logln("Formatted with 7 : " + tempBuffer); ParsePosition status = new ParsePosition(0); var objs = messageFormatter.ParseToMap(tempBuffer, status); if (objs.Get("arg1") != null || objs.Get("arg2") != null) { Errln("Parse failed with more than expected arguments"); } //for (Iterator keyIter = objs.keySet().iterator(); // keyIter.hasNext();) //{ // String key = (String)keyIter.next(); foreach (var key in objs.Keys) { if (objs.Get(key) != null && !objs.Get(key).ToString().Equals(paramsMap.Get(key).ToString())) { Errln("Parse failed on object " + objs.Get(key) + " with argument name : " + key); } } tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Impossible {arg1} has occurred -- status code is {arg0} and message is {arg2}.")) { Errln("Tests with no arguments failed"); } Logln("Formatted with null : " + tempBuffer); Logln("Apply with pattern : " + pattern2); messageFormatter.ApplyPattern(pattern2); paramsMap.Clear(); paramsMap["ARG_ZERO"] = 7; tempBuffer = messageFormatter.Format(paramsMap); if (!tempBuffer.Equals("Double ' Quotes 7 test and quoted {ARG_ONE} test plus 'other {ARG_TWO} stuff'.")) { Errln("quote format test (w/ params) failed."); } Logln("Formatted with params : " + tempBuffer); tempBuffer = messageFormatter.Format((object)null); if (!tempBuffer.Equals("Double ' Quotes {ARG_ZERO} test and quoted {ARG_ONE} test plus 'other {ARG_TWO} stuff'.")) { Errln("quote format test (w/ null) failed."); } Logln("Formatted with null : " + tempBuffer); Logln("toPattern : " + messageFormatter.ToPattern()); } catch (Exception foo) { Warnln("Exception when formatting in bug 4031438. " + foo.Message); } } { // Taken from Test4052223(). ParsePosition pos = new ParsePosition(0); if (pos.ErrorIndex != -1) { Errln("ParsePosition.getErrorIndex initialization failed."); } MessageFormat fmt = new MessageFormat("There are {numberOfApples} apples growing on the {whatKindOfTree} tree."); String str = "There is one apple growing on the peach tree."; var objs = fmt.ParseToMap(str, pos); Logln("unparsable string , should fail at " + pos.ErrorIndex); if (pos.ErrorIndex == -1) { Errln("Bug 4052223 failed : parsing string " + str); } pos.ErrorIndex = (4); if (pos.ErrorIndex != 4) { Errln("setErrorIndex failed, got " + pos.ErrorIndex + " instead of 4"); } if (objs != null) { Errln("unparsable string, should return null"); } } // ICU4N TODO: Serialization //{ // Taken from Test4111739(). // MessageFormat format1 = null; // MessageFormat format2 = null; // ObjectOutputStream ostream = null; // ByteArrayOutputStream baos = null; // ObjectInputStream istream = null; // try // { // baos = new ByteArrayOutputStream(); // ostream = new ObjectOutputStream(baos); // } // catch (IOException e) // { // Errln("Unexpected exception : " + e.getMessage()); // return; // } // try // { // format1 = new MessageFormat("pattern{argument}"); // ostream.writeObject(format1); // ostream.flush(); // byte bytes[] = baos.toByteArray(); // istream = new ObjectInputStream(new ByteArrayInputStream(bytes)); // format2 = (MessageFormat)istream.readObject(); // } // catch (Exception e) // { // Errln("Unexpected exception : " + e.Message); // } // if (!format1.Equals(format2)) // { // Errln("MessageFormats before and after serialization are not" + // " equal\nformat1 = " + format1 + "(" + format1.ToPattern() + ")\nformat2 = " + // format2 + "(" + format2.ToPattern() + ")"); // } // else // { // Logln("Serialization for MessageFormat is OK."); // } //} { // Taken from Test4116444(). String[] patterns = { "", "one", "{namedArgument,date,short}" }; MessageFormat mf = new MessageFormat(""); for (int i = 0; i < patterns.Length; i++) { String pattern = patterns[i]; mf.ApplyPattern(pattern); try { var objs = mf.ParseToMap(null, new ParsePosition(0)); Logln("pattern: \"" + pattern + "\""); Log(" parsedObjects: "); if (objs != null) { Log("{"); //for (Iterator keyIter = objs.keySet().iterator(); // keyIter.hasNext();) //{ // String key = (String)keyIter.next(); bool first = true; foreach (var key in objs.Keys) { if (first) { first = false; } else { Log(","); } if (objs.Get(key) != null) { Err("\"" + objs.Get(key).ToString() + "\""); } else { Log("null"); } //if (keyIter.hasNext()) //{ // Log(","); //} } Log("}"); } else { Log("null"); } Logln(""); } catch (Exception e) { Errln("pattern: \"" + pattern + "\""); Errln(" Exception: " + e.Message); } } } { // Taken from Test4114739(). MessageFormat mf = new MessageFormat("<{arg}>"); IDictionary <string, object> objs1 = null; IDictionary <string, object> objs2 = new Dictionary <string, object>(); IDictionary <string, object> objs3 = new Dictionary <string, object>(); objs3["arg"] = null; try { Logln("pattern: \"" + mf.ToPattern() + "\""); Log("format(null) : "); Logln("\"" + mf.Format(objs1) + "\""); Log("format({}) : "); Logln("\"" + mf.Format(objs2) + "\""); Log("format({null}) :"); Logln("\"" + mf.Format(objs3) + "\""); } catch (Exception e) { Errln("Exception thrown for null argument tests."); } } { // Taken from Test4118594(). String argName = "something_stupid"; MessageFormat mf = new MessageFormat("{" + argName + "}, {" + argName + "}, {" + argName + "}"); String forParsing = "x, y, z"; var objs = mf.ParseToMap(forParsing, new ParsePosition(0)); Logln("pattern: \"" + mf.ToPattern() + "\""); Logln("text for parsing: \"" + forParsing + "\""); if (!objs.Get(argName).ToString().Equals("z")) { Errln("argument0: \"" + objs.Get(argName) + "\""); } mf.SetLocale(new CultureInfo("en-us")); mf.ApplyPattern("{" + argName + ",number,#.##}, {" + argName + ",number,#.#}"); var oldobjs = new Dictionary <string, object>(); oldobjs[argName] = 3.1415d; String result = mf.Format(oldobjs); Logln("pattern: \"" + mf.ToPattern() + "\""); Logln("text for parsing: \"" + result + "\""); // result now equals "3.14, 3.1" if (!result.Equals("3.14, 3.1")) { Errln("result = " + result); } var newobjs = mf.ParseToMap(result, new ParsePosition(0)); // newobjs now equals {new Double(3.1)} if (Convert.ToDouble(newobjs.Get(argName)) != 3.1) // was (Double) [alan] { Errln("newobjs.get(argName) = " + newobjs.Get(argName)); } } { // Taken from Test4105380(). String patternText1 = "The disk \"{diskName}\" contains {numberOfFiles}."; String patternText2 = "There are {numberOfFiles} on the disk \"{diskName}\""; MessageFormat form1 = new MessageFormat(patternText1); MessageFormat form2 = new MessageFormat(patternText2); double[] filelimits = { 0, 1, 2 }; String[] filepart = { "no files", "one file", "{numberOfFiles,number} files" }; ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); form1.SetFormat(1, fileform); form2.SetFormat(0, fileform); var testArgs = new Dictionary <string, object>(); testArgs["diskName"] = "MyDisk"; testArgs["numberOfFiles"] = 12373L; Logln(form1.Format(testArgs)); Logln(form2.Format(testArgs)); } { // Taken from test4293229(). MessageFormat format = new MessageFormat("'''{'myNamedArgument}'' '''{myNamedArgument}'''"); var args = new Dictionary <string, object>(); String expected = "'{myNamedArgument}' '{myNamedArgument}'"; String result = format.Format(args); if (!result.Equals(expected)) { throw new Exception("wrong format result - expected \"" + expected + "\", got \"" + result + "\""); } } }