Source Code Cross Referenced for MessageRegression.java in  » Internationalization-Localization » icu4j » com » ibm » icu » dev » test » format » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.icu.dev.test.format 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         **********************************************************************
003:         * Copyright (c) 2005, International Business Machines
004:         * Corporation and others.  All Rights Reserved.
005:         **********************************************************************
006:         * Author: Alan Liu
007:         * Created: April 12, 2004
008:         * Since: ICU 3.0
009:         **********************************************************************
010:         */
011:        /**
012:         * MessageRegression.java
013:         *
014:         * @test 1.29 01/03/12
015:         * @bug 4031438 4058973 4074764 4094906 4104976 4105380 4106659 4106660 4106661
016:         * 4111739 4112104 4113018 4114739 4114743 4116444 4118592 4118594 4120552
017:         * 4142938 4169959 4232154 4293229
018:         * @summary Regression tests for MessageFormat and associated classes
019:         */package com.ibm.icu.dev.test.format;
020:
021:        import java.io.ByteArrayInputStream;
022:        import java.io.ByteArrayOutputStream;
023:        import java.io.IOException;
024:        import java.io.ObjectInputStream;
025:        import java.io.ObjectOutputStream;
026:        import java.text.ChoiceFormat;
027:        import java.text.ParsePosition;
028:        import java.util.Date;
029:        import java.util.Locale;
030:
031:        import com.ibm.icu.text.MessageFormat;
032:
033:        public class MessageRegression extends com.ibm.icu.dev.test.TestFmwk {
034:
035:            public static void main(String[] args) throws Exception {
036:                new MessageRegression().run(args);
037:            }
038:
039:            /* @bug 4074764
040:             * Null exception when formatting pattern with MessageFormat
041:             * with no parameters.
042:             */
043:            public void Test4074764() {
044:                String[] pattern = { "Message without param",
045:                        "Message with param:{0}",
046:                        "Longer Message with param {0}" };
047:                //difference between the two param strings are that
048:                //in the first one, the param position is within the
049:                //length of the string without param while it is not so
050:                //in the other case.
051:
052:                MessageFormat messageFormatter = new MessageFormat("");
053:
054:                try {
055:                    //Apply pattern with param and print the result
056:                    messageFormatter.applyPattern(pattern[1]);
057:                    Object[] params = { new String("BUG"), new Date() };
058:                    String tempBuffer = messageFormatter.format(params);
059:                    if (!tempBuffer.equals("Message with param:BUG"))
060:                        errln("MessageFormat with one param test failed.");
061:                    logln("Formatted with one extra param : " + tempBuffer);
062:
063:                    //Apply pattern without param and print the result
064:                    messageFormatter.applyPattern(pattern[0]);
065:                    tempBuffer = messageFormatter.format(null);
066:                    if (!tempBuffer.equals("Message without param"))
067:                        errln("MessageFormat with no param test failed.");
068:                    logln("Formatted with no params : " + tempBuffer);
069:
070:                    tempBuffer = messageFormatter.format(params);
071:                    if (!tempBuffer.equals("Message without param"))
072:                        errln("Formatted with arguments > subsitution failed. result = "
073:                                + tempBuffer.toString());
074:                    logln("Formatted with extra params : " + tempBuffer);
075:                    //This statement gives an exception while formatting...
076:                    //If we use pattern[1] for the message with param,
077:                    //we get an NullPointerException in MessageFormat.java(617)
078:                    //If we use pattern[2] for the message with param,
079:                    //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
080:                    //Both are due to maxOffset not being reset to -1
081:                    //in applyPattern() when the pattern does not
082:                    //contain any param.
083:                } catch (Exception foo) {
084:                    errln("Exception when formatting with no params.");
085:                }
086:            }
087:
088:            /* @bug 4058973
089:             * MessageFormat.toPattern has weird rounding behavior.
090:             */
091:            public void Test4058973() {
092:
093:                MessageFormat fmt = new MessageFormat(
094:                        "{0,choice,0#no files|1#one file|1< {0,number,integer} files}");
095:                String pat = fmt.toPattern();
096:                if (!pat
097:                        .equals("{0,choice,0.0#no files|1.0#one file|1.0< {0,number,integer} files}")) {
098:                    errln("MessageFormat.toPattern failed");
099:                }
100:            }
101:
102:            /* @bug 4031438
103:             * More robust message formats.
104:             */
105:            public void Test4031438() {
106:                String pattern1 = "Impossible {1} has occurred -- status code is {0} and message is {2}.";
107:                String pattern2 = "Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.";
108:
109:                MessageFormat messageFormatter = new MessageFormat("");
110:
111:                try {
112:                    logln("Apply with pattern : " + pattern1);
113:                    messageFormatter.applyPattern(pattern1);
114:                    Object[] params = { new Integer(7) };
115:                    String tempBuffer = messageFormatter.format(params);
116:                    if (!tempBuffer
117:                            .equals("Impossible {1} has occurred -- status code is 7 and message is {2}."))
118:                        errln("Tests arguments < substitution failed");
119:                    logln("Formatted with 7 : " + tempBuffer);
120:                    ParsePosition status = new ParsePosition(0);
121:                    Object[] objs = messageFormatter.parse(tempBuffer, status);
122:                    if (objs[params.length] != null)
123:                        errln("Parse failed with more than expected arguments");
124:                    for (int i = 0; i < objs.length; i++) {
125:                        if (objs[i] != null
126:                                && !objs[i].toString().equals(
127:                                        params[i].toString())) {
128:                            errln("Parse failed on object " + objs[i]
129:                                    + " at index : " + i);
130:                        }
131:                    }
132:                    tempBuffer = messageFormatter.format(null);
133:                    if (!tempBuffer
134:                            .equals("Impossible {1} has occurred -- status code is {0} and message is {2}."))
135:                        errln("Tests with no arguments failed");
136:                    logln("Formatted with null : " + tempBuffer);
137:                    logln("Apply with pattern : " + pattern2);
138:                    messageFormatter.applyPattern(pattern2);
139:                    tempBuffer = messageFormatter.format(params);
140:                    if (!tempBuffer
141:                            .equals("Double ' Quotes 7 test and quoted {1} test plus other {2} stuff."))
142:                        errln("quote format test (w/ params) failed.");
143:                    logln("Formatted with params : " + tempBuffer);
144:                    tempBuffer = messageFormatter.format(null);
145:                    if (!tempBuffer
146:                            .equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff."))
147:                        errln("quote format test (w/ null) failed.");
148:                    logln("Formatted with null : " + tempBuffer);
149:                    logln("toPattern : " + messageFormatter.toPattern());
150:                } catch (Exception foo) {
151:                    warnln("Exception when formatting in bug 4031438. "
152:                            + foo.getMessage());
153:                }
154:            }
155:
156:            public void Test4052223() {
157:                ParsePosition pos = new ParsePosition(0);
158:                if (pos.getErrorIndex() != -1) {
159:                    errln("ParsePosition.getErrorIndex initialization failed.");
160:                }
161:                MessageFormat fmt = new MessageFormat(
162:                        "There are {0} apples growing on the {1} tree.");
163:                String str = new String(
164:                        "There is one apple growing on the peach tree.");
165:                Object[] objs = fmt.parse(str, pos);
166:                logln("unparsable string , should fail at "
167:                        + pos.getErrorIndex());
168:                if (pos.getErrorIndex() == -1)
169:                    errln("Bug 4052223 failed : parsing string " + str);
170:                pos.setErrorIndex(4);
171:                if (pos.getErrorIndex() != 4)
172:                    errln("setErrorIndex failed, got " + pos.getErrorIndex()
173:                            + " instead of 4");
174:                ChoiceFormat f = new ChoiceFormat(
175:                        "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.");
176:                pos.setIndex(0);
177:                pos.setErrorIndex(-1);
178:                Number obj = f.parse("are negative", pos);
179:                if (pos.getErrorIndex() != -1 && obj.doubleValue() == -1.0)
180:                    errln("Parse with \"are negative\" failed, at "
181:                            + pos.getErrorIndex());
182:                pos.setIndex(0);
183:                pos.setErrorIndex(-1);
184:                obj = f.parse("are no or fraction ", pos);
185:                if (pos.getErrorIndex() != -1 && obj.doubleValue() == 0.0)
186:                    errln("Parse with \"are no or fraction\" failed, at "
187:                            + pos.getErrorIndex());
188:                pos.setIndex(0);
189:                pos.setErrorIndex(-1);
190:                obj = f.parse("go postal", pos);
191:                if (pos.getErrorIndex() == -1
192:                        && !Double.isNaN(obj.doubleValue()))
193:                    errln("Parse with \"go postal\" failed, at "
194:                            + pos.getErrorIndex());
195:            }
196:
197:            /* @bug 4104976
198:             * ChoiceFormat.equals(null) throws NullPointerException
199:             */
200:            public void Test4104976() {
201:                double[] limits = { 1, 20 };
202:                String[] formats = { "xyz", "abc" };
203:                ChoiceFormat cf = new ChoiceFormat(limits, formats);
204:                try {
205:                    log("Compares to null is always false, returned : ");
206:                    logln(cf.equals(null) ? "TRUE" : "FALSE");
207:                } catch (Exception foo) {
208:                    errln("ChoiceFormat.equals(null) throws exception.");
209:                }
210:            }
211:
212:            /* @bug 4106659
213:             * ChoiceFormat.ctor(double[], String[]) doesn't check
214:             * whether lengths of input arrays are equal.
215:             */
216:            public void Test4106659() {
217:                double[] limits = { 1, 2, 3 };
218:                String[] formats = { "one", "two" };
219:                ChoiceFormat cf = null;
220:                try {
221:                    cf = new ChoiceFormat(limits, formats);
222:                } catch (Exception foo) {
223:                    logln("ChoiceFormat constructor should check for the array lengths");
224:                    cf = null;
225:                }
226:                if (cf != null)
227:                    errln(cf.format(5));
228:            }
229:
230:            /* @bug 4106660
231:             * ChoiceFormat.ctor(double[], String[]) allows unordered double array.
232:             * This is not a bug, added javadoc to emphasize the use of limit
233:             * array must be in ascending order.
234:             */
235:            public void Test4106660() {
236:                double[] limits = { 3, 1, 2 };
237:                String[] formats = { "Three", "One", "Two" };
238:                ChoiceFormat cf = new ChoiceFormat(limits, formats);
239:                double d = 5.0;
240:                String str = cf.format(d);
241:                if (!str.equals("Two"))
242:                    errln("format(" + d + ") = " + cf.format(d));
243:            }
244:
245:            /* @bug 4111739
246:             * MessageFormat is incorrectly serialized/deserialized.
247:             */
248:            public void Test4111739() {
249:                MessageFormat format1 = null;
250:                MessageFormat format2 = null;
251:                ObjectOutputStream ostream = null;
252:                ByteArrayOutputStream baos = null;
253:                ObjectInputStream istream = null;
254:
255:                try {
256:                    baos = new ByteArrayOutputStream();
257:                    ostream = new ObjectOutputStream(baos);
258:                } catch (IOException e) {
259:                    errln("Unexpected exception : " + e.getMessage());
260:                    return;
261:                }
262:
263:                try {
264:                    format1 = new MessageFormat("pattern{0}");
265:                    ostream.writeObject(format1);
266:                    ostream.flush();
267:
268:                    byte bytes[] = baos.toByteArray();
269:
270:                    istream = new ObjectInputStream(new ByteArrayInputStream(
271:                            bytes));
272:                    format2 = (MessageFormat) istream.readObject();
273:                } catch (Exception e) {
274:                    errln("Unexpected exception : " + e.getMessage());
275:                }
276:
277:                if (!format1.equals(format2)) {
278:                    errln("MessageFormats before and after serialization are not"
279:                            + " equal\nformat1 = "
280:                            + format1
281:                            + "("
282:                            + format1.toPattern()
283:                            + ")\nformat2 = "
284:                            + format2
285:                            + "(" + format2.toPattern() + ")");
286:                } else {
287:                    logln("Serialization for MessageFormat is OK.");
288:                }
289:            }
290:
291:            /* @bug 4114743
292:             * MessageFormat.applyPattern allows illegal patterns.
293:             */
294:            public void Test4114743() {
295:                String originalPattern = "initial pattern";
296:                MessageFormat mf = new MessageFormat(originalPattern);
297:                try {
298:                    String illegalPattern = "ab { '}' de";
299:                    mf.applyPattern(illegalPattern);
300:                    errln("illegal pattern: \"" + illegalPattern + "\"");
301:                } catch (IllegalArgumentException foo) {
302:                    if (!originalPattern.equals(mf.toPattern()))
303:                        errln("pattern after: \"" + mf.toPattern() + "\"");
304:                }
305:            }
306:
307:            /* @bug 4116444
308:             * MessageFormat.parse has different behavior in case of null.
309:             */
310:            public void Test4116444() {
311:                String[] patterns = { "", "one", "{0,date,short}" };
312:                MessageFormat mf = new MessageFormat("");
313:
314:                for (int i = 0; i < patterns.length; i++) {
315:                    String pattern = patterns[i];
316:                    mf.applyPattern(pattern);
317:                    try {
318:                        Object[] array = mf.parse(null, new ParsePosition(0));
319:                        logln("pattern: \"" + pattern + "\"");
320:                        log(" parsedObjects: ");
321:                        if (array != null) {
322:                            log("{");
323:                            for (int j = 0; j < array.length; j++) {
324:                                if (array[j] != null)
325:                                    err("\"" + array[j].toString() + "\"");
326:                                else
327:                                    log("null");
328:                                if (j < array.length - 1)
329:                                    log(",");
330:                            }
331:                            log("}");
332:                        } else {
333:                            log("null");
334:                        }
335:                        logln("");
336:                    } catch (Exception e) {
337:                        errln("pattern: \"" + pattern + "\"");
338:                        errln("  Exception: " + e.getMessage());
339:                    }
340:                }
341:
342:            }
343:
344:            /* @bug 4114739 (FIX and add javadoc)
345:             * MessageFormat.format has undocumented behavior about empty format objects.
346:             */
347:            public void Test4114739() {
348:
349:                MessageFormat mf = new MessageFormat("<{0}>");
350:                Object[] objs1 = null;
351:                Object[] objs2 = {};
352:                Object[] objs3 = { null };
353:                try {
354:                    logln("pattern: \"" + mf.toPattern() + "\"");
355:                    log("format(null) : ");
356:                    logln("\"" + mf.format(objs1) + "\"");
357:                    log("format({})   : ");
358:                    logln("\"" + mf.format(objs2) + "\"");
359:                    log("format({null}) :");
360:                    logln("\"" + mf.format(objs3) + "\"");
361:                } catch (Exception e) {
362:                    errln("Exception thrown for null argument tests.");
363:                }
364:            }
365:
366:            /* @bug 4113018
367:             * MessageFormat.applyPattern works wrong with illegal patterns.
368:             */
369:            public void Test4113018() {
370:                String originalPattern = "initial pattern";
371:                MessageFormat mf = new MessageFormat(originalPattern);
372:                String illegalPattern = "format: {0, xxxYYY}";
373:                logln("pattern before: \"" + mf.toPattern() + "\"");
374:                logln("illegal pattern: \"" + illegalPattern + "\"");
375:                try {
376:                    mf.applyPattern(illegalPattern);
377:                    errln("Should have thrown IllegalArgumentException for pattern : "
378:                            + illegalPattern);
379:                } catch (IllegalArgumentException e) {
380:                    if (!originalPattern.equals(mf.toPattern()))
381:                        errln("pattern after: \"" + mf.toPattern() + "\"");
382:                }
383:            }
384:
385:            /* @bug 4106661
386:             * ChoiceFormat is silent about the pattern usage in javadoc.
387:             */
388:            public void Test4106661() {
389:                ChoiceFormat fmt = new ChoiceFormat(
390:                        "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.");
391:                logln("Formatter Pattern : " + fmt.toPattern());
392:
393:                logln("Format with -INF : "
394:                        + fmt.format(Double.NEGATIVE_INFINITY));
395:                logln("Format with -1.0 : " + fmt.format(-1.0));
396:                logln("Format with 0 : " + fmt.format(0));
397:                logln("Format with 0.9 : " + fmt.format(0.9));
398:                logln("Format with 1.0 : " + fmt.format(1));
399:                logln("Format with 1.5 : " + fmt.format(1.5));
400:                logln("Format with 2 : " + fmt.format(2));
401:                logln("Format with 2.1 : " + fmt.format(2.1));
402:                logln("Format with NaN : " + fmt.format(Double.NaN));
403:                logln("Format with +INF : "
404:                        + fmt.format(Double.POSITIVE_INFINITY));
405:            }
406:
407:            /* @bug 4094906
408:             * ChoiceFormat should accept \u221E as eq. to INF.
409:             */
410:            public void Test4094906() {
411:                ChoiceFormat fmt = new ChoiceFormat(
412:                        "-\u221E<are negative|0<are no or fraction|1#is one|1.0<is 1+|\u221E<are many.");
413:                if (!fmt
414:                        .toPattern()
415:                        .startsWith(
416:                                "-\u221E<are negative|0.0<are no or fraction|1.0#is one|1.0<is 1+|\u221E<are many."))
417:                    errln("Formatter Pattern : " + fmt.toPattern());
418:                logln("Format with -INF : "
419:                        + fmt.format(Double.NEGATIVE_INFINITY));
420:                logln("Format with -1.0 : " + fmt.format(-1.0));
421:                logln("Format with 0 : " + fmt.format(0));
422:                logln("Format with 0.9 : " + fmt.format(0.9));
423:                logln("Format with 1.0 : " + fmt.format(1));
424:                logln("Format with 1.5 : " + fmt.format(1.5));
425:                logln("Format with 2 : " + fmt.format(2));
426:                logln("Format with +INF : "
427:                        + fmt.format(Double.POSITIVE_INFINITY));
428:            }
429:
430:            /* @bug 4118592
431:             * MessageFormat.parse fails with ChoiceFormat.
432:             */
433:            public void Test4118592() {
434:                MessageFormat mf = new MessageFormat("");
435:                String pattern = "{0,choice,1#YES|2#NO}";
436:                String prefix = "";
437:                for (int i = 0; i < 5; i++) {
438:                    String formatted = prefix + "YES";
439:                    mf.applyPattern(prefix + pattern);
440:                    prefix += "x";
441:                    Object[] objs = mf.parse(formatted, new ParsePosition(0));
442:                    logln(i + ". pattern :\"" + mf.toPattern() + "\"");
443:                    log(" \"" + formatted + "\" parsed as ");
444:                    if (objs == null)
445:                        logln("  null");
446:                    else
447:                        logln("  " + objs[0]);
448:                }
449:            }
450:
451:            /* @bug 4118594
452:             * MessageFormat.parse fails for some patterns.
453:             */
454:            public void Test4118594() {
455:                MessageFormat mf = new MessageFormat("{0}, {0}, {0}");
456:                String forParsing = "x, y, z";
457:                Object[] objs = mf.parse(forParsing, new ParsePosition(0));
458:                logln("pattern: \"" + mf.toPattern() + "\"");
459:                logln("text for parsing: \"" + forParsing + "\"");
460:                if (!objs[0].toString().equals("z"))
461:                    errln("argument0: \"" + objs[0] + "\"");
462:                mf.setLocale(Locale.US);
463:                mf.applyPattern("{0,number,#.##}, {0,number,#.#}");
464:                Object[] oldobjs = { new Double(3.1415) };
465:                String result = mf.format(oldobjs);
466:                logln("pattern: \"" + mf.toPattern() + "\"");
467:                logln("text for parsing: \"" + result + "\"");
468:                // result now equals "3.14, 3.1"
469:                if (!result.equals("3.14, 3.1"))
470:                    errln("result = " + result);
471:                Object[] newobjs = mf.parse(result, new ParsePosition(0));
472:                // newobjs now equals {new Double(3.1)}
473:                if (((Number) newobjs[0]).doubleValue() != 3.1) // was (Double) [alan]
474:                    errln("newobjs[0] = " + newobjs[0]);
475:            }
476:
477:            /* @bug 4105380
478:             * When using ChoiceFormat, MessageFormat is not good for I18n.
479:             */
480:            public void Test4105380() {
481:                String patternText1 = "The disk \"{1}\" contains {0}.";
482:                String patternText2 = "There are {0} on the disk \"{1}\"";
483:                MessageFormat form1 = new MessageFormat(patternText1);
484:                MessageFormat form2 = new MessageFormat(patternText2);
485:                double[] filelimits = { 0, 1, 2 };
486:                String[] filepart = { "no files", "one file",
487:                        "{0,number} files" };
488:                ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
489:                form1.setFormat(1, fileform);
490:                form2.setFormat(0, fileform);
491:                Object[] testArgs = { new Long(12373), "MyDisk" };
492:                logln(form1.format(testArgs));
493:                logln(form2.format(testArgs));
494:            }
495:
496:            /* @bug 4120552
497:             * MessageFormat.parse incorrectly sets errorIndex.
498:             */
499:            public void Test4120552() {
500:                MessageFormat mf = new MessageFormat("pattern");
501:                String texts[] = { "pattern", "pat", "1234" };
502:                logln("pattern: \"" + mf.toPattern() + "\"");
503:                for (int i = 0; i < texts.length; i++) {
504:                    ParsePosition pp = new ParsePosition(0);
505:                    Object[] objs = mf.parse(texts[i], pp);
506:                    log("  text for parsing: \"" + texts[i] + "\"");
507:                    if (objs == null) {
508:                        logln("  (incorrectly formatted string)");
509:                        if (pp.getErrorIndex() == -1)
510:                            errln("Incorrect error index: "
511:                                    + pp.getErrorIndex());
512:                    } else {
513:                        logln("  (correctly formatted string)");
514:                    }
515:                }
516:            }
517:
518:            /**
519:             * @bug 4142938
520:             * MessageFormat handles single quotes in pattern wrong.
521:             * This is actually a problem in ChoiceFormat; it doesn't
522:             * understand single quotes.
523:             */
524:            public void Test4142938() {
525:                String pat = "''Vous'' {0,choice,0#n''|1#}avez s\u00E9lectionne\u00E9 "
526:                        + "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} "
527:                        + "personnel{0,choice,0#s|1#|2#s}.";
528:                MessageFormat mf = new MessageFormat(pat);
529:
530:                String[] PREFIX = {
531:                        "'Vous' n'avez s\u00E9lectionne\u00E9 aucun clients personnels.",
532:                        "'Vous' avez s\u00E9lectionne\u00E9 ",
533:                        "'Vous' avez s\u00E9lectionne\u00E9 " };
534:                String[] SUFFIX = { null, " client personnel.",
535:                        " clients personnels." };
536:
537:                for (int i = 0; i < 3; i++) {
538:                    String out = mf.format(new Object[] { new Integer(i) });
539:                    if (SUFFIX[i] == null) {
540:                        if (!out.equals(PREFIX[i]))
541:                            errln("" + i + ": Got \"" + out + "\"; Want \""
542:                                    + PREFIX[i] + "\"");
543:                    } else {
544:                        if (!out.startsWith(PREFIX[i])
545:                                || !out.endsWith(SUFFIX[i]))
546:                            errln("" + i + ": Got \"" + out + "\"; Want \""
547:                                    + PREFIX[i] + "\"...\"" + SUFFIX[i] + "\"");
548:                    }
549:                }
550:            }
551:
552:            /**
553:             * @bug 4142938
554:             * Test the applyPattern and toPattern handling of single quotes
555:             * by ChoiceFormat.  (This is in here because this was a bug reported
556:             * against MessageFormat.)  The single quote is used to quote the
557:             * pattern characters '|', '#', '<', and '\u2264'.  Two quotes in a row
558:             * is a quote literal.
559:             */
560:            public void TestChoicePatternQuote() {
561:                String[] DATA = {
562:                        // Pattern                  0 value           1 value
563:                        "0#can''t|1#can", "can't", "can",
564:                        "0#'pound(#)=''#'''|1#xyz", "pound(#)='#'", "xyz",
565:                        "0#'1<2 | 1\u22641'|1#''", "1<2 | 1\u22641", "'", };
566:                for (int i = 0; i < DATA.length; i += 3) {
567:                    try {
568:                        ChoiceFormat cf = new ChoiceFormat(DATA[i]);
569:                        for (int j = 0; j <= 1; ++j) {
570:                            String out = cf.format(j);
571:                            if (!out.equals(DATA[i + 1 + j]))
572:                                errln("Fail: Pattern \"" + DATA[i] + "\" x "
573:                                        + j + " -> " + out + "; want \""
574:                                        + DATA[i + 1 + j] + '"');
575:                        }
576:                        String pat = cf.toPattern();
577:                        String pat2 = new ChoiceFormat(pat).toPattern();
578:                        if (!pat.equals(pat2))
579:                            errln("Fail: Pattern \"" + DATA[i]
580:                                    + "\" x toPattern -> \"" + pat + '"');
581:                        else
582:                            logln("Ok: Pattern \"" + DATA[i]
583:                                    + "\" x toPattern -> \"" + pat + '"');
584:                    } catch (IllegalArgumentException e) {
585:                        errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
586:                    }
587:                }
588:            }
589:
590:            /**
591:             * @bug 4112104
592:             * MessageFormat.equals(null) throws a NullPointerException.  The JLS states
593:             * that it should return false.
594:             */
595:            public void Test4112104() {
596:                MessageFormat format = new MessageFormat("");
597:                try {
598:                    // This should NOT throw an exception
599:                    if (format.equals(null)) {
600:                        // It also should return false
601:                        errln("MessageFormat.equals(null) returns false");
602:                    }
603:                } catch (NullPointerException e) {
604:                    errln("MessageFormat.equals(null) throws " + e);
605:                }
606:            }
607:
608:            /**
609:             * @bug 4169959
610:             * MessageFormat does not format null objects. CANNOT REPRODUCE THIS BUG.
611:             */
612:            public void Test4169959() {
613:                // This works
614:                logln(MessageFormat.format("This will {0}",
615:                        new String[] { "work" }));
616:
617:                // This fails
618:                logln(MessageFormat.format("This will {0}",
619:                        new Object[] { null }));
620:            }
621:
622:            public void test4232154() {
623:                boolean gotException = false;
624:                try {
625:                    MessageFormat format = new MessageFormat(
626:                            "The date is {0:date}");
627:                } catch (Exception e) {
628:                    gotException = true;
629:                    if (!(e instanceof  IllegalArgumentException)) {
630:                        throw new RuntimeException("got wrong exception type");
631:                    }
632:                    if ("argument number too large at ".equals(e.getMessage())) {
633:                        throw new RuntimeException(
634:                                "got wrong exception message");
635:                    }
636:                }
637:                if (!gotException) {
638:                    throw new RuntimeException(
639:                            "didn't get exception for invalid input");
640:                }
641:            }
642:
643:            public void test4293229() {
644:                MessageFormat format = new MessageFormat("'''{'0}'' '''{0}'''");
645:                Object[] args = { null };
646:                String expected = "'{0}' '{0}'";
647:                String result = format.format(args);
648:                if (!result.equals(expected)) {
649:                    throw new RuntimeException(
650:                            "wrong format result - expected \"" + expected
651:                                    + "\", got \"" + result + "\"");
652:                }
653:            }
654:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.