< prev index next >

test/java/text/Format/MessageFormat/MessageRegression.java

Print this page




 119         }
 120     }
 121     /* @bug 4031438
 122      * More robust message formats.
 123      */
 124     public void Test4031438() {
 125         Locale locale = Locale.getDefault();
 126         if (!TestUtils.usesAsciiDigits(locale)) {
 127             logln("Skipping this test because locale is " + locale);
 128             return;
 129         }
 130 
 131         String pattern1 = "Impossible {1} has occurred -- status code is {0} and message is {2}.";
 132         String pattern2 = "Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.";
 133 
 134         MessageFormat messageFormatter = new MessageFormat("");
 135 
 136         try {
 137             logln("Apply with pattern : " + pattern1);
 138             messageFormatter.applyPattern(pattern1);
 139             Object[] params = {new Integer(7)};
 140             String tempBuffer = messageFormatter.format(params);
 141             if (!tempBuffer.equals("Impossible {1} has occurred -- status code is 7 and message is {2}."))
 142                 errln("Tests arguments < substitution failed. Formatted text=" +
 143                       "<" + tempBuffer + ">");
 144             logln("Formatted with 7 : " + tempBuffer);
 145             ParsePosition status = new ParsePosition(0);
 146             Object[] objs = messageFormatter.parse(tempBuffer, status);
 147             if (objs[params.length] != null)
 148                 errln("Parse failed with more than expected arguments");
 149             for (int i = 0; i < objs.length; i++) {
 150                 if (objs[i] != null && !objs[i].toString().equals(params[i].toString())) {
 151                     errln("Parse failed on object " + objs[i] + " at index : " + i);
 152                 }
 153             }
 154             tempBuffer = messageFormatter.format(null);
 155             if (!tempBuffer.equals("Impossible {1} has occurred -- status code is {0} and message is {2}."))
 156                 errln("Tests with no arguments failed");
 157             logln("Formatted with null : " + tempBuffer);
 158             logln("Apply with pattern : " + pattern2);
 159             messageFormatter.applyPattern(pattern2);


 438             logln(i + ". pattern :\"" + mf.toPattern() + "\"");
 439             log(" \"" + formatted + "\" parsed as ");
 440             if (objs == null) logln("  null");
 441             else logln("  " + objs[0]);
 442         }
 443     }
 444     /* @bug 4118594
 445      * MessageFormat.parse fails for some patterns.
 446      */
 447     public void Test4118594()
 448     {
 449         MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
 450         String forParsing = "x, y, z";
 451         Object[] objs = mf.parse(forParsing, new ParsePosition(0));
 452         logln("pattern: \"" + mf.toPattern() + "\"");
 453         logln("text for parsing: \"" + forParsing + "\"");
 454         if (!objs[0].toString().equals("z"))
 455             errln("argument0: \"" + objs[0] + "\"");
 456         mf.setLocale(Locale.US);
 457         mf.applyPattern("{0,number,#.##}, {0,number,#.#}");
 458         Object[] oldobjs = {new Double(3.1415)};
 459         String result = mf.format( oldobjs );
 460         logln("pattern: \"" + mf.toPattern() + "\"");
 461         logln("text for parsing: \"" + result + "\"");
 462         // result now equals "3.14, 3.1"
 463         if (!result.equals("3.14, 3.1"))
 464             errln("result = " + result);
 465         Object[] newobjs = mf.parse(result, new ParsePosition(0));
 466         // newobjs now equals {new Double(3.1)}
 467         if (((Double)newobjs[0]).doubleValue() != 3.1)
 468             errln( "newobjs[0] = " + newobjs[0]);
 469     }
 470     /* @bug 4105380
 471      * When using ChoiceFormat, MessageFormat is not good for I18n.
 472      */
 473     public void Test4105380()
 474     {
 475         String patternText1 = "The disk \"{1}\" contains {0}.";
 476         String patternText2 = "There are {0} on the disk \"{1}\"";
 477         MessageFormat form1 = new MessageFormat(patternText1);
 478         MessageFormat form2 = new MessageFormat(patternText2);
 479         double[] filelimits = {0,1,2};
 480         String[] filepart = {"no files","one file","{0,number} files"};
 481         ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 482         form1.setFormat(1, fileform);
 483         form2.setFormat(0, fileform);
 484         Object[] testArgs = {new Long(12373), "MyDisk"};
 485         logln(form1.format(testArgs));
 486         logln(form2.format(testArgs));
 487     }
 488     /* @bug 4120552
 489      * MessageFormat.parse incorrectly sets errorIndex.
 490      */
 491     public void Test4120552()
 492     {
 493         MessageFormat mf = new MessageFormat("pattern");
 494         String texts[] = {"pattern", "pat", "1234"};
 495         logln("pattern: \"" + mf.toPattern() + "\"");
 496         for (int i = 0; i < texts.length; i++) {
 497             ParsePosition pp = new ParsePosition(0);
 498             Object[] objs = mf.parse(texts[i], pp);
 499             log("  text for parsing: \"" + texts[i] + "\"");
 500             if (objs == null) {
 501                 logln("  (incorrectly formatted string)");
 502                 if (pp.getErrorIndex() == -1)
 503                     errln("Incorrect error index: " + pp.getErrorIndex());
 504             } else {


 514      * understand single quotes.
 515      */
 516     public void Test4142938() {
 517         String pat = "''Vous'' {0,choice,0#n''|1#}avez s\u00E9lectionne\u00E9 " +
 518             "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} " +
 519             "personnel{0,choice,0#s|1#|2#s}.";
 520         MessageFormat mf = new MessageFormat(pat);
 521 
 522         String[] PREFIX = {
 523             "'Vous' n'avez s\u00E9lectionne\u00E9 aucun clients personnels.",
 524             "'Vous' avez s\u00E9lectionne\u00E9 ",
 525             "'Vous' avez s\u00E9lectionne\u00E9 "
 526         };
 527         String[] SUFFIX = {
 528             null,
 529             " client personnel.",
 530             " clients personnels."
 531         };
 532 
 533         for (int i=0; i<3; i++) {
 534             String out = mf.format(new Object[]{new Integer(i)});
 535             if (SUFFIX[i] == null) {
 536                 if (!out.equals(PREFIX[i]))
 537                     errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"");
 538             }
 539             else {
 540                 if (!out.startsWith(PREFIX[i]) ||
 541                     !out.endsWith(SUFFIX[i]))
 542                     errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
 543                           SUFFIX[i] + "\"");
 544             }
 545         }
 546     }
 547 
 548     /**
 549      * @bug 4142938
 550      * Test the applyPattern and toPattern handling of single quotes
 551      * by ChoiceFormat.  (This is in here because this was a bug reported
 552      * against MessageFormat.)  The single quote is used to quote the
 553      * pattern characters '|', '#', '<', and '\u2264'.  Two quotes in a row
 554      * is a quote literal.


 590     public void Test4112104() {
 591         MessageFormat format = new MessageFormat("");
 592         try {
 593             // This should NOT throw an exception
 594             if (format.equals(null)) {
 595                 // It also should return false
 596                 errln("MessageFormat.equals(null) returns false");
 597             }
 598         }
 599         catch (NullPointerException e) {
 600             errln("MessageFormat.equals(null) throws " + e);
 601         }
 602     }
 603 
 604     /**
 605      * @bug 4169959
 606      * MessageFormat does not format null objects. CANNOT REPRODUCE THIS BUG.
 607      */
 608     public void Test4169959() {
 609         // This works
 610         logln(MessageFormat.format( "This will {0}",
 611                                     new String[]{"work"} ) );
 612 
 613         // This fails
 614         logln(MessageFormat.format( "This will {0}",
 615                                     new Object[]{ null } ) );
 616     }
 617 
 618     public void test4232154() {
 619         boolean gotException = false;
 620         try {
 621             MessageFormat format = new MessageFormat("The date is {0:date}");
 622         } catch (Exception e) {
 623             gotException = true;
 624             if (!(e instanceof IllegalArgumentException)) {
 625                 throw new RuntimeException("got wrong exception type");
 626             }
 627             if ("argument number too large at ".equals(e.getMessage())) {
 628                 throw new RuntimeException("got wrong exception message");
 629             }
 630         }
 631         if (!gotException) {


 119         }
 120     }
 121     /* @bug 4031438
 122      * More robust message formats.
 123      */
 124     public void Test4031438() {
 125         Locale locale = Locale.getDefault();
 126         if (!TestUtils.usesAsciiDigits(locale)) {
 127             logln("Skipping this test because locale is " + locale);
 128             return;
 129         }
 130 
 131         String pattern1 = "Impossible {1} has occurred -- status code is {0} and message is {2}.";
 132         String pattern2 = "Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.";
 133 
 134         MessageFormat messageFormatter = new MessageFormat("");
 135 
 136         try {
 137             logln("Apply with pattern : " + pattern1);
 138             messageFormatter.applyPattern(pattern1);
 139             Object[] params = {7};
 140             String tempBuffer = messageFormatter.format(params);
 141             if (!tempBuffer.equals("Impossible {1} has occurred -- status code is 7 and message is {2}."))
 142                 errln("Tests arguments < substitution failed. Formatted text=" +
 143                       "<" + tempBuffer + ">");
 144             logln("Formatted with 7 : " + tempBuffer);
 145             ParsePosition status = new ParsePosition(0);
 146             Object[] objs = messageFormatter.parse(tempBuffer, status);
 147             if (objs[params.length] != null)
 148                 errln("Parse failed with more than expected arguments");
 149             for (int i = 0; i < objs.length; i++) {
 150                 if (objs[i] != null && !objs[i].toString().equals(params[i].toString())) {
 151                     errln("Parse failed on object " + objs[i] + " at index : " + i);
 152                 }
 153             }
 154             tempBuffer = messageFormatter.format(null);
 155             if (!tempBuffer.equals("Impossible {1} has occurred -- status code is {0} and message is {2}."))
 156                 errln("Tests with no arguments failed");
 157             logln("Formatted with null : " + tempBuffer);
 158             logln("Apply with pattern : " + pattern2);
 159             messageFormatter.applyPattern(pattern2);


 438             logln(i + ". pattern :\"" + mf.toPattern() + "\"");
 439             log(" \"" + formatted + "\" parsed as ");
 440             if (objs == null) logln("  null");
 441             else logln("  " + objs[0]);
 442         }
 443     }
 444     /* @bug 4118594
 445      * MessageFormat.parse fails for some patterns.
 446      */
 447     public void Test4118594()
 448     {
 449         MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
 450         String forParsing = "x, y, z";
 451         Object[] objs = mf.parse(forParsing, new ParsePosition(0));
 452         logln("pattern: \"" + mf.toPattern() + "\"");
 453         logln("text for parsing: \"" + forParsing + "\"");
 454         if (!objs[0].toString().equals("z"))
 455             errln("argument0: \"" + objs[0] + "\"");
 456         mf.setLocale(Locale.US);
 457         mf.applyPattern("{0,number,#.##}, {0,number,#.#}");
 458         Object[] oldobjs = {3.1415};
 459         String result = mf.format( oldobjs );
 460         logln("pattern: \"" + mf.toPattern() + "\"");
 461         logln("text for parsing: \"" + result + "\"");
 462         // result now equals "3.14, 3.1"
 463         if (!result.equals("3.14, 3.1"))
 464             errln("result = " + result);
 465         Object[] newobjs = mf.parse(result, new ParsePosition(0));
 466         // newobjs now equals {new Double(3.1)}
 467         if (((Double)newobjs[0]).doubleValue() != 3.1)
 468             errln( "newobjs[0] = " + newobjs[0]);
 469     }
 470     /* @bug 4105380
 471      * When using ChoiceFormat, MessageFormat is not good for I18n.
 472      */
 473     public void Test4105380()
 474     {
 475         String patternText1 = "The disk \"{1}\" contains {0}.";
 476         String patternText2 = "There are {0} on the disk \"{1}\"";
 477         MessageFormat form1 = new MessageFormat(patternText1);
 478         MessageFormat form2 = new MessageFormat(patternText2);
 479         double[] filelimits = {0,1,2};
 480         String[] filepart = {"no files","one file","{0,number} files"};
 481         ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 482         form1.setFormat(1, fileform);
 483         form2.setFormat(0, fileform);
 484         Object[] testArgs = {12373L, "MyDisk"};
 485         logln(form1.format(testArgs));
 486         logln(form2.format(testArgs));
 487     }
 488     /* @bug 4120552
 489      * MessageFormat.parse incorrectly sets errorIndex.
 490      */
 491     public void Test4120552()
 492     {
 493         MessageFormat mf = new MessageFormat("pattern");
 494         String texts[] = {"pattern", "pat", "1234"};
 495         logln("pattern: \"" + mf.toPattern() + "\"");
 496         for (int i = 0; i < texts.length; i++) {
 497             ParsePosition pp = new ParsePosition(0);
 498             Object[] objs = mf.parse(texts[i], pp);
 499             log("  text for parsing: \"" + texts[i] + "\"");
 500             if (objs == null) {
 501                 logln("  (incorrectly formatted string)");
 502                 if (pp.getErrorIndex() == -1)
 503                     errln("Incorrect error index: " + pp.getErrorIndex());
 504             } else {


 514      * understand single quotes.
 515      */
 516     public void Test4142938() {
 517         String pat = "''Vous'' {0,choice,0#n''|1#}avez s\u00E9lectionne\u00E9 " +
 518             "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} " +
 519             "personnel{0,choice,0#s|1#|2#s}.";
 520         MessageFormat mf = new MessageFormat(pat);
 521 
 522         String[] PREFIX = {
 523             "'Vous' n'avez s\u00E9lectionne\u00E9 aucun clients personnels.",
 524             "'Vous' avez s\u00E9lectionne\u00E9 ",
 525             "'Vous' avez s\u00E9lectionne\u00E9 "
 526         };
 527         String[] SUFFIX = {
 528             null,
 529             " client personnel.",
 530             " clients personnels."
 531         };
 532 
 533         for (int i=0; i<3; i++) {
 534             String out = mf.format(new Object[]{i});
 535             if (SUFFIX[i] == null) {
 536                 if (!out.equals(PREFIX[i]))
 537                     errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"");
 538             }
 539             else {
 540                 if (!out.startsWith(PREFIX[i]) ||
 541                     !out.endsWith(SUFFIX[i]))
 542                     errln("" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
 543                           SUFFIX[i] + "\"");
 544             }
 545         }
 546     }
 547 
 548     /**
 549      * @bug 4142938
 550      * Test the applyPattern and toPattern handling of single quotes
 551      * by ChoiceFormat.  (This is in here because this was a bug reported
 552      * against MessageFormat.)  The single quote is used to quote the
 553      * pattern characters '|', '#', '<', and '\u2264'.  Two quotes in a row
 554      * is a quote literal.


 590     public void Test4112104() {
 591         MessageFormat format = new MessageFormat("");
 592         try {
 593             // This should NOT throw an exception
 594             if (format.equals(null)) {
 595                 // It also should return false
 596                 errln("MessageFormat.equals(null) returns false");
 597             }
 598         }
 599         catch (NullPointerException e) {
 600             errln("MessageFormat.equals(null) throws " + e);
 601         }
 602     }
 603 
 604     /**
 605      * @bug 4169959
 606      * MessageFormat does not format null objects. CANNOT REPRODUCE THIS BUG.
 607      */
 608     public void Test4169959() {
 609         // This works
 610         logln(MessageFormat.format( "This will {0}", "work"));

 611 
 612         // This fails
 613         logln(MessageFormat.format( "This will {0}",
 614                                     new Object[]{ null } ) );
 615     }
 616 
 617     public void test4232154() {
 618         boolean gotException = false;
 619         try {
 620             MessageFormat format = new MessageFormat("The date is {0:date}");
 621         } catch (Exception e) {
 622             gotException = true;
 623             if (!(e instanceof IllegalArgumentException)) {
 624                 throw new RuntimeException("got wrong exception type");
 625             }
 626             if ("argument number too large at ".equals(e.getMessage())) {
 627                 throw new RuntimeException("got wrong exception message");
 628             }
 629         }
 630         if (!gotException) {
< prev index next >