Source Code Cross Referenced for TestMessageFormat.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) 2004-2006, International Business Machines
004:         * Corporation and others.  All Rights Reserved.
005:         **********************************************************************
006:         * Author: Alan Liu
007:         * Created: April 6, 2004
008:         * Since: ICU 3.0
009:         **********************************************************************
010:         */
011:        package com.ibm.icu.dev.test.format;
012:
013:        import java.text.ChoiceFormat;
014:        import java.text.FieldPosition;
015:        import java.text.Format;
016:        import java.text.ParseException;
017:        import java.text.ParsePosition;
018:        import java.util.Date;
019:        import java.util.Locale;
020:
021:        import com.ibm.icu.text.DecimalFormat;
022:        import com.ibm.icu.text.MessageFormat;
023:        import com.ibm.icu.text.NumberFormat;
024:        import com.ibm.icu.text.DateFormat;
025:        import com.ibm.icu.text.UFormat;
026:        import com.ibm.icu.util.ULocale;
027:
028:        public class TestMessageFormat extends com.ibm.icu.dev.test.TestFmwk {
029:
030:            public static void main(String[] args) throws Exception {
031:                new TestMessageFormat().run(args);
032:            }
033:
034:            public void TestBug3() {
035:                double myNumber = -123456;
036:                DecimalFormat form = null;
037:                Locale locale[] = {
038:                        new Locale("ar", "", ""),
039:                        new Locale("be", "", ""),
040:                        new Locale("bg", "", ""),
041:                        new Locale("ca", "", ""),
042:                        new Locale("cs", "", ""),
043:                        new Locale("da", "", ""),
044:                        new Locale("de", "", ""),
045:                        new Locale("de", "AT", ""),
046:                        new Locale("de", "CH", ""),
047:                        new Locale("el", "", ""), // 10
048:                        new Locale("en", "CA", ""),
049:                        new Locale("en", "GB", ""),
050:                        new Locale("en", "IE", ""),
051:                        new Locale("en", "US", ""),
052:                        new Locale("es", "", ""),
053:                        new Locale("et", "", ""),
054:                        new Locale("fi", "", ""),
055:                        new Locale("fr", "", ""),
056:                        new Locale("fr", "BE", ""),
057:                        new Locale("fr", "CA", ""), // 20
058:                        new Locale("fr", "CH", ""),
059:                        new Locale("he", "", ""),
060:                        new Locale("hr", "", ""),
061:                        new Locale("hu", "", ""),
062:                        new Locale("is", "", ""),
063:                        new Locale("it", "", ""),
064:                        new Locale("it", "CH", ""),
065:                        new Locale("ja", "", ""),
066:                        new Locale("ko", "", ""),
067:                        new Locale("lt", "", ""), // 30
068:                        new Locale("lv", "", ""), new Locale("mk", "", ""),
069:                        new Locale("nl", "", ""),
070:                        new Locale("nl", "BE", ""),
071:                        new Locale("no", "", ""),
072:                        new Locale("pl", "", ""),
073:                        new Locale("pt", "", ""),
074:                        new Locale("ro", "", ""),
075:                        new Locale("ru", "", ""),
076:                        new Locale("sh", "", ""), // 40
077:                        new Locale("sk", "", ""), new Locale("sl", "", ""),
078:                        new Locale("sq", "", ""), new Locale("sr", "", ""),
079:                        new Locale("sv", "", ""), new Locale("tr", "", ""),
080:                        new Locale("uk", "", ""), new Locale("zh", "", ""),
081:                        new Locale("zh", "TW", "") // 49
082:                };
083:                StringBuffer buffer = new StringBuffer();
084:                ParsePosition parsePos = new ParsePosition(0);
085:                int i;
086:                for (i = 0; i < 49; i++) {
087:                    //        form = (DecimalFormat)NumberFormat.getCurrencyInstance(locale[i]);
088:                    form = (DecimalFormat) NumberFormat.getInstance(locale[i]);
089:                    if (form == null) {
090:                        errln("Number format creation failed for "
091:                                + locale[i].getDisplayName());
092:                        continue;
093:                    }
094:                    FieldPosition pos = new FieldPosition(0);
095:                    buffer.setLength(0);
096:                    form.format(myNumber, buffer, pos);
097:                    parsePos.setIndex(0);
098:                    Object result = form.parse(buffer.toString(), parsePos);
099:                    logln(locale[i].getDisplayName() + " -> " + result);
100:                    if (parsePos.getIndex() != buffer.length()) {
101:                        errln("Number format parse failed.");
102:                    }
103:                }
104:            }
105:
106:            public void TestBug1() {
107:                final double limit[] = { 0.0, 1.0, 2.0 };
108:                final String formats[] = { "0.0<=Arg<1.0", "1.0<=Arg<2.0",
109:                        "2.0<-Arg" };
110:                ChoiceFormat cf = new ChoiceFormat(limit, formats);
111:                FieldPosition status = new FieldPosition(0);
112:                assertEquals("ChoiceFormat.format", formats[1], cf.format(1));
113:            }
114:
115:            public void TestBug2() {
116:                String result;
117:                // {sfb} use double format in pattern, so result will match (not strictly necessary)
118:                final String pattern = "There {0,choice,0.0#are no files|1.0#is one file|1.0<are {0, number} files} on disk {1}. ";
119:                logln("The input pattern : " + pattern);
120:                try {
121:                    MessageFormat fmt = new MessageFormat(pattern);
122:                    assertEquals("toPattern", pattern, fmt.toPattern());
123:                } catch (IllegalArgumentException e) {
124:                    errln("MessageFormat pattern creation failed.");
125:                }
126:            }
127:
128:            public void TestPattern() // aka PatternTest()
129:            {
130:                Object testArgs[] = { new Double(1), new Double(3456), "Disk",
131:                        new Date(1000000000L) };
132:                String testCases[] = {
133:                        "Quotes '', '{', 'a' {0} '{0}'",
134:                        "Quotes '', '{', 'a' {0,number} '{0}'",
135:                        "'{'1,number,'#',##} {1,number,'#',##}",
136:                        "There are {1} files on {2} at {3}.",
137:                        "On {2}, there are {1} files, with {0,number,currency}.",
138:                        "'{1,number,percent}', {1,number,percent},",
139:                        "'{1,date,full}', {1,date,full},",
140:                        "'{3,date,full}', {3,date,full},",
141:                        "'{1,number,#,##}' {1,number,#,##}", };
142:
143:                String testResultPatterns[] = {
144:                        "Quotes '', '{', a {0} '{'0}",
145:                        "Quotes '', '{', a {0,number} '{'0}",
146:                        "'{'1,number,#,##} {1,number,'#'#,##}",
147:                        "There are {1} files on {2} at {3}.",
148:                        "On {2}, there are {1} files, with {0,number,currency}.",
149:                        "'{'1,number,percent}, {1,number,percent},",
150:                        "'{'1,date,full}, {1,date,full},",
151:                        "'{'3,date,full}, {3,date,full},",
152:                        "'{'1,number,#,##} {1,number,#,##}" };
153:
154:                String testResultStrings[] = { "Quotes ', {, a 1 {0}",
155:                        "Quotes ', {, a 1 {0}", "{1,number,#,##} #34,56",
156:                        "There are 3,456 files on Disk at 1/12/70 5:46 AM.",
157:                        "On Disk, there are 3,456 files, with $1.00.",
158:                        "{1,number,percent}, 345,600%,",
159:                        "{1,date,full}, Wednesday, December 31, 1969,",
160:                        "{3,date,full}, Monday, January 12, 1970,",
161:                        "{1,number,#,##} 34,56" };
162:
163:                for (int i = 0; i < 9; ++i) {
164:                    //it_out << "\nPat in:  " << testCases[i]);
165:
166:                    String buffer;
167:                    MessageFormat form = null;
168:                    try {
169:                        form = new MessageFormat(testCases[i], Locale.US);
170:                    } catch (IllegalArgumentException e1) {
171:                        errln("MessageFormat for " + testCases[i]
172:                                + " creation failed.");
173:                        continue;
174:                    }
175:                    assertEquals("\"" + testCases[i] + "\".toPattern()",
176:                            testResultPatterns[i], form.toPattern());
177:
178:                    //it_out << "Pat out: " << form.toPattern(buffer));
179:                    StringBuffer result = new StringBuffer();
180:                    int count = 4;
181:                    FieldPosition fieldpos = new FieldPosition(0);
182:                    form.format(testArgs, result, fieldpos);
183:                    assertEquals("format", testResultStrings[i], result
184:                            .toString());
185:
186:                    //it_out << "Result:  " << result);
187:                    //#if 0
188:                    //        /* TODO: Look at this test and see if this is still a valid test */
189:                    //        logln("---------------- test parse ----------------");
190:                    //
191:                    //        form.toPattern(buffer);
192:                    //        logln("MSG pattern for parse: " + buffer);
193:                    //
194:                    //        int parseCount = 0;
195:                    //        Formattable* values = form.parse(result, parseCount, success);
196:                    //        if (U_FAILURE(success)) {
197:                    //            errln("MessageFormat failed test #5");
198:                    //            logln(String("MessageFormat failed test #5 with error code ")+(int)success);
199:                    //        } else if (parseCount != count) {
200:                    //            errln("MSG count not %d as expected. Got %d", count, parseCount);
201:                    //        }
202:                    //        UBool failed = FALSE;
203:                    //        for (int j = 0; j < parseCount; ++j) {
204:                    //             if (values == 0 || testArgs[j] != values[j]) {
205:                    //                errln(((String)"MSG testargs[") + j + "]: " + toString(testArgs[j]));
206:                    //                errln(((String)"MSG values[") + j + "]  : " + toString(values[j]));
207:                    //                failed = TRUE;
208:                    //             }
209:                    //        }
210:                    //        if (failed)
211:                    //            errln("MessageFormat failed test #6");
212:                    //#endif
213:                }
214:            }
215:
216:            public void TestSample() // aka sample()
217:            {
218:                MessageFormat form = null;
219:                StringBuffer buffer2 = new StringBuffer();
220:                try {
221:                    form = new MessageFormat("There are {0} files on {1}");
222:                } catch (IllegalArgumentException e1) {
223:                    errln("Sample message format creation failed.");
224:                    return;
225:                }
226:                Object testArgs1[] = { "abc", "def" };
227:                FieldPosition fieldpos = new FieldPosition(0);
228:                assertEquals("format", "There are abc files on def", form
229:                        .format(testArgs1, buffer2, fieldpos).toString());
230:            }
231:
232:            public void TestStaticFormat() {
233:                Object arguments[] = { new Integer(7), new Date(871068000000L),
234:                        "a disturbance in the Force" };
235:
236:                assertEquals(
237:                        "format",
238:                        "At 12:20:00 PM on Aug 8, 1997, there was a disturbance in the Force on planet 7.",
239:                        MessageFormat
240:                                .format(
241:                                        "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
242:                                        arguments));
243:            }
244:
245:            static final int FieldPosition_DONT_CARE = -1;
246:
247:            public void TestSimpleFormat() {
248:                Object testArgs1[] = { new Integer(0), "MyDisk" };
249:                Object testArgs2[] = { new Integer(1), "MyDisk" };
250:                Object testArgs3[] = { new Integer(12), "MyDisk" };
251:
252:                MessageFormat form = new MessageFormat(
253:                        "The disk \"{1}\" contains {0} file(s).");
254:
255:                StringBuffer string = new StringBuffer();
256:                FieldPosition ignore = new FieldPosition(
257:                        FieldPosition_DONT_CARE);
258:                form.format(testArgs1, string, ignore);
259:                assertEquals("format",
260:                        "The disk \"MyDisk\" contains 0 file(s).", string
261:                                .toString());
262:
263:                string.setLength(0);
264:                form.format(testArgs2, string, ignore);
265:                assertEquals("format",
266:                        "The disk \"MyDisk\" contains 1 file(s).", string
267:                                .toString());
268:
269:                string.setLength(0);
270:                form.format(testArgs3, string, ignore);
271:                assertEquals("format",
272:                        "The disk \"MyDisk\" contains 12 file(s).", string
273:                                .toString());
274:            }
275:
276:            public void TestMsgFormatChoice() {
277:                MessageFormat form = new MessageFormat(
278:                        "The disk \"{1}\" contains {0}.");
279:                double filelimits[] = { 0, 1, 2 };
280:                String filepart[] = { "no files", "one file",
281:                        "{0,number} files" };
282:                ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
283:                form.setFormat(1, fileform); // NOT zero, see below
284:
285:                FieldPosition ignore = new FieldPosition(
286:                        FieldPosition_DONT_CARE);
287:                StringBuffer string = new StringBuffer();
288:                Object testArgs1[] = { new Integer(0), "MyDisk" };
289:                form.format(testArgs1, string, ignore);
290:                assertEquals("format#1",
291:                        "The disk \"MyDisk\" contains no files.", string
292:                                .toString());
293:
294:                string.setLength(0);
295:                Object testArgs2[] = { new Integer(1), "MyDisk" };
296:                form.format(testArgs2, string, ignore);
297:                assertEquals("format#2",
298:                        "The disk \"MyDisk\" contains one file.", string
299:                                .toString());
300:
301:                string.setLength(0);
302:                Object testArgs3[] = { new Integer(1273), "MyDisk" };
303:                form.format(testArgs3, string, ignore);
304:                assertEquals("format#3",
305:                        "The disk \"MyDisk\" contains 1,273 files.", string
306:                                .toString());
307:            }
308:
309:            //---------------------------------
310:            //  API Tests
311:            //---------------------------------
312:
313:            public void TestClone() {
314:                MessageFormat x = new MessageFormat(
315:                        "There are {0} files on {1}");
316:                MessageFormat z = new MessageFormat(
317:                        "There are {0} files on {1} created");
318:                MessageFormat y = null;
319:                y = (MessageFormat) x.clone();
320:                if (x.equals(y) && !x.equals(z) && !y.equals(z))
321:                    logln("First test (operator ==): Passed!");
322:                else {
323:                    errln("First test (operator ==): Failed!");
324:                }
325:                if ((x.equals(y) && y.equals(x))
326:                        && (!x.equals(z) && !z.equals(x))
327:                        && (!y.equals(z) && !z.equals(y)))
328:                    logln("Second test (equals): Passed!");
329:                else {
330:                    errln("Second test (equals): Failed!");
331:                }
332:
333:            }
334:
335:            public void TestEquals() {
336:                MessageFormat x = new MessageFormat(
337:                        "There are {0} files on {1}");
338:                MessageFormat y = new MessageFormat(
339:                        "There are {0} files on {1}");
340:                if (!x.equals(y)) {
341:                    errln("First test (operator ==): Failed!");
342:                }
343:
344:            }
345:
346:            public void TestNotEquals() {
347:                MessageFormat x = new MessageFormat(
348:                        "There are {0} files on {1}");
349:                MessageFormat y = new MessageFormat(
350:                        "There are {0} files on {1}");
351:                y.setLocale(Locale.FRENCH);
352:                if (x.equals(y)) {
353:                    errln("First test (operator !=): Failed!");
354:                }
355:                y = new MessageFormat("There are {0} files on {1}");
356:                y.applyPattern("There are {0} files on {1} the disk");
357:                if (x.equals(y)) {
358:                    errln("Second test (operator !=): Failed!");
359:                }
360:            }
361:
362:            public void TestHashCode() {
363:                ULocale save = ULocale.getDefault();
364:                ULocale.setDefault(ULocale.US);
365:
366:                MessageFormat x = new MessageFormat(
367:                        "There are {0} files on {1}");
368:                MessageFormat z = new MessageFormat(
369:                        "There are {0} files on {1}");
370:                MessageFormat y = null;
371:                y = (MessageFormat) x.clone();
372:                if (x.hashCode() != y.hashCode())
373:                    errln("FAIL: identical objects have different hashcodes");
374:                if (x.hashCode() != z.hashCode())
375:                    errln("FAIL: identical objects have different hashcodes");
376:
377:                /* These are not errors
378:                    y.setLocale(ULocale.FRENCH);
379:                    if (x.hashCode() == y.hashCode())
380:                        errln("FAIL: different objects have same hashcodes. Locale ignored");
381:
382:                    z.applyPattern("There are {0} files on {1} the disk");
383:                    if (x.hashCode() == z.hashCode())
384:                        errln("FAIL: different objects have same hashcodes. Pattern ignored");
385:                 */
386:
387:                ULocale.setDefault(save);
388:            }
389:
390:            public void TestSetLocale() {
391:                Object arguments[] = { new Double(456.83),
392:                        new Date(871068000000L), "deposit" };
393:
394:                StringBuffer result = new StringBuffer();
395:
396:                //String formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
397:                String formatStr = "At <time> on {1,date}, you made a {2} of {0,number,currency}.";
398:                // {sfb} to get $, would need Locale::US, not Locale::ENGLISH
399:                // Just use unlocalized currency symbol.
400:                //String compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of $456.83.";
401:                String compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of ";
402:                compareStrEng += '\u00a4';
403:                compareStrEng += "456.83.";
404:                // {sfb} to get DM, would need Locale::GERMANY, not Locale::GERMAN
405:                // Just use unlocalized currency symbol.
406:                //String compareStrGer = "At <time> on 08.08.1997, you made a deposit of 456,83 DM.";
407:                String compareStrGer = "At <time> on 08.08.1997, you made a deposit of ";
408:                compareStrGer += '\u00a4';
409:                compareStrGer += " 456,83.";
410:
411:                MessageFormat msg = new MessageFormat(formatStr, Locale.ENGLISH);
412:                result.setLength(0);
413:                FieldPosition pos = new FieldPosition(0);
414:                result = msg.format(arguments, result, pos);
415:                assertEquals("format", compareStrEng, result.toString());
416:
417:                msg.setLocale(Locale.ENGLISH);
418:                assertEquals("getLocale", Locale.ENGLISH, msg.getLocale());
419:
420:                msg.setLocale(Locale.GERMAN);
421:                assertEquals("getLocale", Locale.GERMAN, msg.getLocale());
422:
423:                msg.applyPattern(formatStr);
424:                result.setLength(0);
425:                result = msg.format(arguments, result, pos);
426:                assertEquals("format", compareStrGer, result.toString());
427:
428:                //Cover getULocale()
429:                logln("Testing set/get ULocale ...");
430:                msg.setLocale(ULocale.ENGLISH);
431:                assertEquals("getULocale", ULocale.ENGLISH, msg.getULocale());
432:
433:                msg.setLocale(ULocale.GERMAN);
434:                assertEquals("getULocale", ULocale.GERMAN, msg.getULocale());
435:
436:                msg.applyPattern(formatStr);
437:                result.setLength(0);
438:                result = msg.format(arguments, result, pos);
439:                assertEquals("format", compareStrGer, result.toString());
440:            }
441:
442:            public void TestFormat() {
443:                final Object ft_arr[] = { new Date(871068000000L) };
444:
445:                StringBuffer result = new StringBuffer();
446:
447:                //String formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
448:                String formatStr = "On {0,date}, it began.";
449:                String compareStr = "On Aug 8, 1997, it began.";
450:
451:                MessageFormat msg = new MessageFormat(formatStr);
452:                FieldPosition fp = new FieldPosition(0);
453:
454:                try {
455:                    msg.format(new Date(871068000000L), result, fp);
456:                    errln("*** MSG format without expected error code.");
457:                } catch (Exception e1) {
458:                }
459:
460:                result.setLength(0);
461:                result = msg.format(ft_arr, result, fp);
462:                assertEquals("format", compareStr, result.toString());
463:            }
464:
465:            public void TestParse() {
466:                String msgFormatString = "{0} =sep= {1}";
467:                MessageFormat msg = new MessageFormat(msgFormatString);
468:                String source = "abc =sep= def";
469:                String tmp1, tmp2;
470:
471:                try {
472:                    Object[] fmt_arr = msg.parse(source);
473:                    if (fmt_arr.length != 2) {
474:                        errln("*** MSG parse (ustring, count, err) count err.");
475:                    } else {
476:                        if (fmt_arr.length != 2) {
477:                            errln("*** MSG parse (ustring, parsepos., count) count err.");
478:                        } else {
479:                            assertEquals("parse()[0]", "abc", fmt_arr[0]);
480:                            assertEquals("parse()[1]", "def", fmt_arr[1]);
481:                        }
482:                    }
483:                } catch (ParseException e1) {
484:                    errln("*** MSG parse (ustring, count, err) error.");
485:                }
486:
487:                ParsePosition pp = new ParsePosition(0);
488:
489:                Object[] fmt_arr = msg.parse(source, pp);
490:                if (pp.getIndex() == 0 || fmt_arr == null) {
491:                    errln("*** MSG parse (ustring, parsepos., count) error.");
492:                } else {
493:                    if (fmt_arr.length != 2) {
494:                        errln("*** MSG parse (ustring, parsepos., count) count err.");
495:                    } else {
496:                        assertEquals("parse()[0]", "abc", fmt_arr[0]);
497:                        assertEquals("parse()[1]", "def", fmt_arr[1]);
498:                    }
499:                }
500:
501:                pp.setIndex(0);
502:                Object[] fmta;
503:
504:                fmta = (Object[]) msg.parseObject(source, pp);
505:                if (pp.getIndex() == 0) {
506:                    errln("*** MSG parse (ustring, Object, parsepos ) error.");
507:                } else {
508:                    if (fmta.length != 2) {
509:                        errln("*** MSG parse (ustring, count, err) count err.");
510:                    } else {
511:                        if (fmt_arr.length != 2) {
512:                            errln("*** MSG parse (ustring, parsepos., count) count err.");
513:                        } else {
514:                            assertEquals("parse()[0]", "abc", fmt_arr[0]);
515:                            assertEquals("parse()[1]", "def", fmt_arr[1]);
516:                        }
517:                    }
518:                }
519:            }
520:
521:            /**
522:             * Of course, in Java there is no adopt, but we retain the same
523:             * method name. [alan]
524:             */
525:            public void TestAdopt() {
526:                String formatStr = "{0,date},{1},{2,number}";
527:                String formatStrChange = "{0,number},{1,number},{2,date}";
528:                MessageFormat msg = new MessageFormat(formatStr);
529:                MessageFormat msgCmp = new MessageFormat(formatStr);
530:                Format[] formats = msg.getFormats();
531:                Format[] formatsCmp = msgCmp.getFormats();
532:                Format[] formatsChg = null;
533:                Format[] formatsAct = null;
534:                Format a = null;
535:                Format b = null;
536:                String patCmp;
537:                String patAct;
538:                Format[] formatsToAdopt = null;
539:
540:                if (formats == null || formatsCmp == null
541:                        || (formats.length <= 0)
542:                        || (formats.length != formatsCmp.length)) {
543:                    errln("Error getting Formats");
544:                    return;
545:                }
546:
547:                int i;
548:
549:                for (i = 0; i < formats.length; i++) {
550:                    a = formats[i];
551:                    b = formatsCmp[i];
552:                    if ((a != null) && (b != null)) {
553:                        if (!a.equals(b)) {
554:                            errln("a != b");
555:                            return;
556:                        }
557:                    } else if ((a != null) || (b != null)) {
558:                        errln("(a != null) || (b != null)");
559:                        return;
560:                    }
561:                }
562:
563:                msg.applyPattern(formatStrChange); //set msg formats to something different
564:                formatsChg = msg.getFormats(); // tested function
565:                if (formatsChg == null || (formatsChg.length != formats.length)) {
566:                    errln("Error getting Formats");
567:                    return;
568:                }
569:
570:                boolean diff;
571:                diff = true;
572:                for (i = 0; i < formats.length; i++) {
573:                    a = formatsChg[i];
574:                    b = formatsCmp[i];
575:                    if ((a != null) && (b != null)) {
576:                        if (a.equals(b)) {
577:                            logln("formatsChg == formatsCmp at index " + i);
578:                            diff = false;
579:                        }
580:                    }
581:                }
582:                if (!diff) {
583:                    errln("*** MSG getFormats diff err.");
584:                    return;
585:                }
586:
587:                logln("MSG getFormats tested.");
588:
589:                msg.setFormats(formatsCmp); //tested function
590:
591:                formatsAct = msg.getFormats();
592:                if (formatsAct == null || (formatsAct.length <= 0)
593:                        || (formatsAct.length != formatsCmp.length)) {
594:                    errln("Error getting Formats");
595:                    return;
596:                }
597:
598:                assertEquals("msgCmp.toPattern()", formatStr, msgCmp
599:                        .toPattern());
600:                assertEquals("msg.toPattern()", formatStr, msg.toPattern());
601:
602:                for (i = 0; i < formatsAct.length; i++) {
603:                    a = formatsAct[i];
604:                    b = formatsCmp[i];
605:                    if ((a != null) && (b != null)) {
606:                        if (!a.equals(b)) {
607:                            errln("formatsAct != formatsCmp at index " + i);
608:                            return;
609:                        }
610:                    } else if ((a != null) || (b != null)) {
611:                        errln("(a != null) || (b != null)");
612:                        return;
613:                    }
614:                }
615:                logln("MSG setFormats tested.");
616:
617:                //----
618:
619:                msg.applyPattern(formatStrChange); //set msg formats to something different
620:
621:                formatsToAdopt = new Format[formatsCmp.length];
622:                if (formatsToAdopt == null) {
623:                    errln("memory allocation error");
624:                    return;
625:                }
626:
627:                for (i = 0; i < formatsCmp.length; i++) {
628:                    if (formatsCmp[i] == null) {
629:                        formatsToAdopt[i] = null;
630:                    } else {
631:                        formatsToAdopt[i] = (Format) formatsCmp[i].clone();
632:                        if (formatsToAdopt[i] == null) {
633:                            errln("Can't clone format at index " + i);
634:                            return;
635:                        }
636:                    }
637:                }
638:                msg.setFormats(formatsToAdopt); // function to test
639:
640:                assertEquals("msgCmp.toPattern()", formatStr, msgCmp
641:                        .toPattern());
642:                assertEquals("msg.toPattern()", formatStr, msg.toPattern());
643:
644:                formatsAct = msg.getFormats();
645:                if (formatsAct == null || (formatsAct.length <= 0)
646:                        || (formatsAct.length != formatsCmp.length)) {
647:                    errln("Error getting Formats");
648:                    return;
649:                }
650:
651:                for (i = 0; i < formatsAct.length; i++) {
652:                    a = formatsAct[i];
653:                    b = formatsCmp[i];
654:                    if ((a != null) && (b != null)) {
655:                        if (!a.equals(b)) {
656:                            errln("a != b");
657:                            return;
658:                        }
659:                    } else if ((a != null) || (b != null)) {
660:                        errln("(a != null) || (b != null)");
661:                        return;
662:                    }
663:                }
664:                logln("MSG adoptFormats tested.");
665:
666:                //---- adoptFormat
667:
668:                msg.applyPattern(formatStrChange); //set msg formats to something different
669:
670:                formatsToAdopt = new Format[formatsCmp.length];
671:                if (formatsToAdopt == null) {
672:                    errln("memory allocation error");
673:                    return;
674:                }
675:
676:                for (i = 0; i < formatsCmp.length; i++) {
677:                    if (formatsCmp[i] == null) {
678:                        formatsToAdopt[i] = null;
679:                    } else {
680:                        formatsToAdopt[i] = (Format) formatsCmp[i].clone();
681:                        if (formatsToAdopt[i] == null) {
682:                            errln("Can't clone format at index " + i);
683:                            return;
684:                        }
685:                    }
686:                }
687:
688:                for (i = 0; i < formatsCmp.length; i++) {
689:                    msg.setFormat(i, formatsToAdopt[i]); // function to test
690:                }
691:
692:                assertEquals("msgCmp.toPattern()", formatStr, msgCmp
693:                        .toPattern());
694:                assertEquals("msg.toPattern()", formatStr, msg.toPattern());
695:
696:                formatsAct = msg.getFormats();
697:                if (formatsAct == null || (formatsAct.length <= 0)
698:                        || (formatsAct.length != formatsCmp.length)) {
699:                    errln("Error getting Formats");
700:                    return;
701:                }
702:
703:                for (i = 0; i < formatsAct.length; i++) {
704:                    a = formatsAct[i];
705:                    b = formatsCmp[i];
706:                    if ((a != null) && (b != null)) {
707:                        if (!a.equals(b)) {
708:                            errln("a != b");
709:                            return;
710:                        }
711:                    } else if ((a != null) || (b != null)) {
712:                        errln("(a != null) || (b != null)");
713:                        return;
714:                    }
715:                }
716:                logln("MSG adoptFormat tested.");
717:            }
718:
719:            /**
720:             * Verify that MessageFormat accomodates more than 10 arguments and
721:             * more than 10 subformats.
722:             */
723:            public void TestUnlimitedArgsAndSubformats() {
724:                final String pattern = "On {0,date} (aka {0,date,short}, aka {0,date,long}) "
725:                        + "at {0,time} (aka {0,time,short}, aka {0,time,long}) "
726:                        + "there were {1,number} werjes "
727:                        + "(a {3,number,percent} increase over {2,number}) "
728:                        + "despite the {4}''s efforts "
729:                        + "and to delight of {5}, {6}, {7}, {8}, {9}, and {10} {11}.";
730:                try {
731:                    MessageFormat msg = new MessageFormat(pattern);
732:
733:                    final Object ARGS[] = { new Date(10000000000000L),
734:                            new Integer(1303), new Integer(1202),
735:                            new Double(1303.0 / 1202 - 1), "Glimmung",
736:                            "the printers", "Nick", "his father", "his mother",
737:                            "the spiddles", "of course", "Horace" };
738:
739:                    String expected = "On Nov 20, 2286 (aka 11/20/86, aka November 20, 2286) "
740:                            + "at 9:46:40 AM (aka 9:46 AM, aka 9:46:40 AM PST) "
741:                            + "there were 1,303 werjes "
742:                            + "(a 8% increase over 1,202) "
743:                            + "despite the Glimmung's efforts "
744:                            + "and to delight of the printers, Nick, his father, "
745:                            + "his mother, the spiddles, and of course Horace.";
746:                    String result;
747:                    assertEquals("format", expected, msg.format(ARGS));
748:                } catch (IllegalArgumentException e1) {
749:                    errln("FAIL: constructor failed");
750:                }
751:            }
752:
753:            // test RBNF extensions to message format
754:            public void TestRBNF() {
755:                // WARNING: this depends on the RBNF formats for en_US
756:                Locale locale = Locale.US;
757:                String[] values = {
758:                // decimal values do not format completely for ordinal or duration, and
759:                        // do not always parse, so do not include them
760:                        "0", "1", "12", "100", "123", "1001", "123,456", "-17", };
761:                String[] formats = {
762:                        "There are {0,spellout} files to search.",
763:                        "There are {0,spellout,%simplified} files to search.",
764:                        "The bogus spellout {0,spellout,%BOGUS} files behaves like the default.",
765:                        "This is the {0,ordinal} file to search.", // TODO fix bug, ordinal does not parse
766:                        "Searching this file will take {0,duration} to complete.",
767:                        "Searching this file will take {0,duration,%with-words} to complete.", };
768:                final NumberFormat numFmt = NumberFormat.getInstance(locale);
769:                Object[] args = new Object[1];
770:                Number num = null;
771:                for (int i = 0; i < formats.length; ++i) {
772:                    MessageFormat fmt = new MessageFormat(formats[i], locale);
773:                    logln("Testing format pattern: '" + formats[i] + "'");
774:                    for (int j = 0; j < values.length; ++j) {
775:                        try {
776:                            num = numFmt.parse(values[j]);
777:                        } catch (Exception e) {
778:                            throw new IllegalStateException(
779:                                    "failed to parse test argument");
780:                        }
781:                        args[0] = num;
782:                        String result = fmt.format(args);
783:                        logln("value: " + num + " --> " + result);
784:
785:                        if (i != 3) { // TODO: fix this, for now skip ordinal parsing (format string at index 3)
786:                            try {
787:                                Object[] parsedArgs = fmt.parse(result);
788:                                if (parsedArgs.length != 1) {
789:                                    errln("parse returned " + parsedArgs.length
790:                                            + " args");
791:                                } else if (!parsedArgs[0].equals(num)) {
792:                                    errln("parsed argument " + parsedArgs[0]
793:                                            + " != " + num);
794:                                }
795:                            } catch (Exception e) {
796:                                errln("parse of '" + result
797:                                        + " returned exception: "
798:                                        + e.getMessage());
799:                            }
800:                        }
801:                    }
802:                }
803:            }
804:
805:            public void TestSetGetFormats() {
806:                Object arguments[] = { new Double(456.83),
807:                        new Date(871068000000L), "deposit" };
808:
809:                StringBuffer result = new StringBuffer();
810:
811:                String formatStr = "At <time> on {1,date}, you made a {2} of {0,number,currency}.";
812:                // original expected format result
813:                String compareStr = "At <time> on Aug 8, 1997, you made a deposit of $456.83.";
814:                // the date being German-style, but the currency being English-style
815:                String compareStr2 = "At <time> on 08.08.1997, you made a deposit of ";
816:                compareStr2 += '\u00a4';
817:                compareStr2 += "456.83.";
818:                // both date and currency formats are German-style
819:                String compareStr3 = "At <time> on 08.08.1997, you made a deposit of ";
820:                compareStr3 += '\u00a4';
821:                compareStr3 += " 456,83.";
822:
823:                MessageFormat msg = new MessageFormat(formatStr, ULocale.US);
824:                result.setLength(0);
825:                FieldPosition pos = new FieldPosition(0);
826:                result = msg.format(arguments, result, pos);
827:                assertEquals("format", compareStr, result.toString());
828:
829:                // constructs a Format array with a English-style Currency formatter
830:                //                            and a German-style Date formatter
831:                //      might not meaningful, just for testing setFormatsByArgIndex
832:                Format[] fmts = new Format[] {
833:                        NumberFormat.getCurrencyInstance(ULocale.ENGLISH),
834:                        DateFormat.getDateInstance(DateFormat.DEFAULT,
835:                                ULocale.GERMAN) };
836:
837:                msg.setFormatsByArgumentIndex(fmts);
838:                result.setLength(0);
839:                pos = new FieldPosition(0);
840:                result = msg.format(arguments, result, pos);
841:                assertEquals("format", compareStr2, result.toString());
842:
843:                // Construct a German-style Currency formatter, replace the corresponding one
844:                // Thus both formatters should format objects with German-style
845:                Format newFmt = NumberFormat
846:                        .getCurrencyInstance(ULocale.GERMAN);
847:                msg.setFormatByArgumentIndex(0, newFmt);
848:                result.setLength(0);
849:                pos = new FieldPosition(0);
850:                result = msg.format(arguments, result, pos);
851:                assertEquals("format", compareStr3, result.toString());
852:
853:                // verify getFormatsByArgumentIndex
854:                //   you should got three formats by that
855:                //          - DecimalFormat     locale: de
856:                //          - SimpleDateFormat  locale: de
857:                //          - null
858:                Format[] fmts2 = msg.getFormatsByArgumentIndex();
859:                assertEquals("1st subformmater: Format Class",
860:                        "com.ibm.icu.text.DecimalFormat", fmts2[0].getClass()
861:                                .getName());
862:                assertEquals("1st subformmater: its Locale", ULocale.GERMAN,
863:                        ((UFormat) fmts2[0]).getLocale(ULocale.VALID_LOCALE));
864:                assertEquals("2nd subformatter: Format Class",
865:                        "com.ibm.icu.text.SimpleDateFormat", fmts2[1]
866:                                .getClass().getName());
867:                assertEquals("2nd subformmater: its Locale", ULocale.GERMAN,
868:                        ((UFormat) fmts2[1]).getLocale(ULocale.VALID_LOCALE));
869:                assertTrue("The third subFormatter is null", null == fmts2[2]);
870:            }
871:
872:            // Test the fix pattern api
873:            public void TestAutoQuoteApostrophe() {
874:                final String[] patterns = { // new pattern, expected pattern
875:                "'", "''", "''", "''", "'{", "'{'", "' {", "'' {", "'a", "''a",
876:                        "'{'a", "'{'a", "'{a'", "'{a'", "'{}", "'{}'", "{'",
877:                        "{'", "{'a", "{'a", "{'a{}'a}'a", "{'a{}'a}''a", "'}'",
878:                        "'}'", "'} '{'}'", "'} '{'}''", "'} {{{''",
879:                        "'} {{{'''", };
880:                for (int i = 0; i < patterns.length; i += 2) {
881:                    assertEquals("[" + (i / 2) + "] \"" + patterns[i] + "\"",
882:                            patterns[i + 1], MessageFormat
883:                                    .autoQuoteApostrophe(patterns[i]));
884:                }
885:            }
886:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.