Пример #1
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 + "\"");
                }
            }
        }