Source Code Cross Referenced for TestFormatter.java in  » 6.0-JDK-Modules » j2me » javax » microedition » global » 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 » 6.0 JDK Modules » j2me » javax.microedition.global 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *   
003:         *
004:         * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License version
009:         * 2 only, as published by the Free Software Foundation.
010:         * 
011:         * This program is distributed in the hope that it will be useful, but
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014:         * General Public License version 2 for more details (a copy is
015:         * included at /legal/license.txt).
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * version 2 along with this work; if not, write to the Free Software
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA
021:         * 
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023:         * Clara, CA 95054 or visit www.sun.com if you need additional
024:         * information or have any questions.
025:         */
026:
027:        package javax.microedition.global;
028:
029:        import com.sun.midp.i3test.*;
030:        import java.util.*;
031:
032:        /**
033:         * i3test for Formatter
034:         */
035:        public class TestFormatter extends TestCase {
036:            /**
037:             * Format Currency with code
038:             */
039:            public void testCurrencyWithCode() {
040:                Formatter f = new Formatter();
041:                String result = f.formatCurrency(12.34, "USD");
042:                assertEquals("$12.34", result);
043:                result = f.formatCurrency(-12.34, "USD");
044:                assertEquals("($12.34)", result);
045:            }
046:
047:            /**
048:             * Format Currency with nonsupported code
049:             */
050:            public void testCurrencyWithNonSupportedCode() {
051:                Formatter f = new Formatter();
052:                String result = f.formatCurrency(12.34, "LIT");
053:                assertEquals("LIT12.34", result);
054:                result = f.formatCurrency(-12.34, "LIT");
055:                assertEquals("(LIT12.34)", result);
056:            }
057:
058:            /**
059:             * Test unsupported locale
060:             */
061:            public void testUnsupportedLocale() {
062:                try {
063:                    Formatter f = new Formatter("uk-UK");
064:
065:                } catch (UnsupportedLocaleException ue) {
066:                    assertTrue(true);
067:                    return;
068:                }
069:                fail("Exception wasn't thrown");
070:            }
071:
072:            /**
073:             * Format supported Currency
074:             */
075:            public void testFormatSupportedCurrency() {
076:                try {
077:                    Formatter f = new Formatter("sk-SK");
078:                    String result = f.formatCurrency(12.50);
079:                    assertEquals("12.50 Sk", result);
080:                    Formatter f1 = new Formatter("en-US");
081:                    result = f1.formatCurrency(12.50);
082:                    assertEquals("$12.50", result);
083:                    result = f1.formatCurrency(-12.50);
084:                    assertEquals("($12.50)", result);
085:                    Formatter f2 = new Formatter("en");
086:                    result = f2.formatCurrency(12.50);
087:                    assertEquals("US$12.50", result);
088:                    result = f2.formatCurrency(-12.50);
089:                    assertEquals("-US$12.50", result);
090:                } catch (UnsupportedLocaleException ue) {
091:                    ue.printStackTrace();
092:                    fail();
093:                }
094:            }
095:
096:            /**
097:             * Format Rounding Currency Down
098:             */
099:            public void testRoundingCurrencyDown() {
100:                try {
101:                    Formatter f = new Formatter("sk-SK");
102:                    String result = f.formatCurrency(12.502);
103:                    assertEquals("12.50 Sk", result);
104:                } catch (UnsupportedLocaleException ue) {
105:                    ue.printStackTrace();
106:                    fail();
107:                }
108:            }
109:
110:            /**
111:             * Format Rounding Currency Up
112:             */
113:            public void testRoundingCurrencyUp() {
114:                try {
115:                    Formatter f = new Formatter("sk-SK");
116:                    String result = f.formatCurrency(12.597);
117:                    assertEquals("12.60 Sk", result);
118:                } catch (UnsupportedLocaleException ue) {
119:                    ue.printStackTrace();
120:                    fail();
121:                }
122:            }
123:
124:            /**
125:             * Format Numbers
126:             */
127:            public void testNumber() {
128:                Formatter f = new Formatter("en-US");
129:                String result = f.formatNumber(1234567890);
130:                assertEquals("1,234,567,890", result);
131:                result = f.formatNumber(12345.67890);
132:                assertEquals("12,345.68", result);
133:                result = f.formatNumber(12345.67890, 4);
134:                assertEquals("12,345.6789", result);
135:                Formatter f1 = new Formatter("zh-CN");
136:                result = f1.formatNumber(1234567890);
137:                assertEquals("1,234,567,890", result);
138:                result = f1.formatNumber(12345.67890);
139:                assertEquals("12,345.68", result);
140:                result = f1.formatNumber(12345.67890, 4);
141:                assertEquals("12,345.6789", result);
142:                Formatter f2 = new Formatter();
143:                result = f2.formatNumber(1234567890);
144:                assertEquals("1,234,567,890", result);
145:                result = f2.formatNumber(12345.67890);
146:                assertEquals("12,345.68", result);
147:                result = f2.formatNumber(12345.67890, 4);
148:                assertEquals("12,345.6789", result);
149:            }
150:
151:            /**
152:             * Format Percents
153:             */
154:            public void testPercent() {
155:                Formatter f = new Formatter("en-US");
156:                float fl_value = 0.123456f;
157:                String result = f.formatPercentage(123);
158:                assertEquals("123%", result);
159:                result = f.formatPercentage(fl_value, 3);
160:                assertEquals("12.346%", result);
161:                result = f.formatPercentage(1234567890);
162:                assertEquals("1,234,567,890%", result);
163:                Formatter f1 = new Formatter("zh-CN");
164:                result = f1.formatPercentage(1234567890);
165:                assertEquals("1,234,567,890%", result);
166:                result = f1.formatPercentage(fl_value, 4);
167:                assertEquals("12.3456%", result);
168:                result = f1.formatPercentage(-fl_value, 4);
169:                assertEquals("-12.3456%", result);
170:            }
171:
172:            /**
173:             * Format Date & Time
174:             */
175:            public void testDateTime() {
176:                long constTime = 1128522923951l;
177:                String result;
178:                String month_en = "October";
179:                String month_sk = "okt\u00f3ber";
180:                String month_cs = "\u0159\u00edjna";
181:                Formatter f = new Formatter("en-US");
182:                Formatter f1 = new Formatter("sk-SK");
183:                Formatter f2 = new Formatter("cs-CZ");
184:                Calendar c = Calendar.getInstance();
185:                Date date = c.getTime();
186:
187:                c.setTimeZone(TimeZone.getTimeZone("GMT-04:00"));
188:
189:                date.setTime(constTime);
190:                c.setTime(date);
191:
192:                result = f.formatDateTime(c, Formatter.DATE_SHORT);
193:                assertEquals("10/5/05", result);
194:                result = f1.formatDateTime(c, Formatter.DATE_SHORT);
195:                assertEquals("5.10.2005", result);
196:                result = f2.formatDateTime(c, Formatter.DATE_SHORT);
197:                assertEquals("5.10.05", result);
198:
199:                result = f.formatDateTime(c, Formatter.DATE_LONG);
200:                assertEquals(month_en + " 5, 2005", result);
201:                result = f1.formatDateTime(c, Formatter.DATE_LONG);
202:                assertEquals("5. " + month_sk + " 2005", result);
203:                result = f2.formatDateTime(c, Formatter.DATE_LONG);
204:                assertEquals("5. " + month_cs + " 2005", result);
205:
206:                result = f.formatDateTime(c, Formatter.TIME_SHORT);
207:                assertEquals("9:35 AM", result);
208:                result = f1.formatDateTime(c, Formatter.TIME_SHORT);
209:                assertEquals("9:35", result);
210:                result = f2.formatDateTime(c, Formatter.TIME_SHORT);
211:                assertEquals("9:35", result);
212:
213:                result = f.formatDateTime(c, Formatter.TIME_LONG);
214:                assertEquals("9:35:23 AM -04:00", result);
215:                result = f1.formatDateTime(c, Formatter.TIME_LONG);
216:                assertEquals("9:35:23 -04:00", result);
217:                result = f2.formatDateTime(c, Formatter.TIME_LONG);
218:                assertEquals("9:35:23 -04:00", result);
219:
220:                result = f.formatDateTime(c, Formatter.DATETIME_SHORT);
221:                assertEquals("9:35 AM 10/5/05", result);
222:                result = f1.formatDateTime(c, Formatter.DATETIME_SHORT);
223:                assertEquals("9:35 5.10.2005", result);
224:                result = f2.formatDateTime(c, Formatter.DATETIME_SHORT);
225:                assertEquals("9:35 5.10.05", result);
226:
227:                result = f.formatDateTime(c, Formatter.DATETIME_LONG);
228:                assertEquals("9:35:23 AM -04:00 " + month_en + " 5, 2005",
229:                        result);
230:                result = f1.formatDateTime(c, Formatter.DATETIME_LONG);
231:                assertEquals("9:35:23 -04:00 5. " + month_sk + " 2005", result);
232:                result = f2.formatDateTime(c, Formatter.DATETIME_LONG);
233:                assertEquals("9:35:23 -04:00 5. " + month_cs + " 2005", result);
234:            }
235:
236:            /**
237:             * Format Message
238:             */
239:            public void testMessageFormat() {
240:                Formatter f = new Formatter("en-US");
241:                String result;
242:
243:                /* Format message with just one parameter */
244:                result = f.formatMessage("This is {0} parameter",
245:                        new String[] { "first" });
246:                assertEquals("This is first parameter", result);
247:
248:                /* Format message with more than one parameter */
249:                result = f.formatMessage("{0} {1} {2} {3} {4} {5} {6} {7}",
250:                        new String[] { "first", "second", "third", "fourth",
251:                                "fifth", "sixth", "seventh", "eighth" });
252:                assertEquals(
253:                        "first second third fourth fifth sixth seventh eighth",
254:                        result);
255:
256:                /* Format message containing escaped parenthesis */
257:                result = f.formatMessage("This is {{ {0} parenthesis",
258:                        new String[] { "left" });
259:                assertEquals("This is { left parenthesis", result);
260:
261:                /* Format message containing right escaped parenthesis */
262:                result = f.formatMessage("This is } {0} parenthesis",
263:                        new String[] { "right" });
264:                assertEquals("This is } right parenthesis", result);
265:
266:                /* Test when placeholders doesn't match number of parameters */
267:                try {
268:                    result = f.formatMessage("{0} {1} {2}",
269:                            new String[] { "first" });
270:                    fail("IllegalArgumentException wasn't thrown.");
271:                } catch (IllegalArgumentException e) {
272:                    assertTrue(true);
273:                }
274:
275:                /* Test when there are more parameters than placeholders */
276:                result = null;
277:                try {
278:                    result = f.formatMessage("{0}", new String[] { "first",
279:                            "second", "third" });
280:                } catch (IllegalArgumentException e) {
281:                    fail();
282:                }
283:                assertEquals("first", result);
284:
285:                /* Placeholders can repeat */
286:                result = f.formatMessage("{0} {1} {0} {0}", new String[] {
287:                        "first", "second" });
288:                assertEquals("first second first first", result);
289:
290:                /* Test leading zero example: <code>{05}</code> */
291:                result = f.formatMessage("{00} {01}", new String[] { "first",
292:                        "second" });
293:                assertEquals("first second", result);
294:
295:                /*
296:                 * If a placeholder doesn't contain number, then
297:                 * IllegalArgumentException is thrown
298:                 */
299:                try {
300:                    result = f
301:                            .formatMessage("{ciao}", new String[] { "first" });
302:                    fail("IllegalArgumentException wasn't thrown.");
303:                } catch (IllegalArgumentException e) {
304:                    assertTrue(true);
305:                }
306:
307:                /* Check if NPE is thrown when parameters == null */
308:                String[] params = null;
309:                try {
310:                    result = f.formatMessage("{0} {1}", params);
311:                    fail("NullPointerException wasn't thrown.");
312:                } catch (NullPointerException npe) {
313:                    assertTrue(true);
314:                }
315:
316:                /* Check if NPE is thrown when template == null */
317:                String template = null;
318:                try {
319:                    result = f
320:                            .formatMessage(template, new String[] { "first" });
321:                    fail("NullPointerException wasn't thrown.");
322:                } catch (NullPointerException npe) {
323:                    assertTrue(true);
324:                }
325:            }
326:
327:            /**
328:             * creates an array containing all the supported Formatters and 'null'
329:             * for locale-neutral formatting
330:             * @return Formatter[]  - created array
331:             */
332:            private Formatter[] getFormatters() {
333:
334:                String[] suppLocales = Formatter.getSupportedLocales();
335:                Formatter[] ret = new Formatter[suppLocales.length + 1];
336:                int i;
337:
338:                try {
339:                    for (i = 0; i < suppLocales.length; i++) {
340:                        ret[i] = new Formatter(suppLocales[i]);
341:                    }
342:
343:                    ret[i++] = new Formatter(null);
344:                } catch (Exception e) {
345:                    return null;
346:                }
347:                return ret;
348:            }
349:
350:            /**
351:             * Format NAN, infinity value for Currency
352:             */
353:            public void testNaN() {
354:
355:                double[] val = { Double.NaN, Double.POSITIVE_INFINITY,
356:                        Double.NEGATIVE_INFINITY };
357:                String[] currencyCode = { "XXX", "YYY", "ZZZ" };
358:
359:                Formatter[] formatter = getFormatters();
360:
361:                for (int i = 0; i < formatter.length; i++) {
362:                    for (int j = 0; j < val.length; j++) {
363:                        String result = formatter[i].formatCurrency(val[j],
364:                                currencyCode[j]);
365:                        if (result.indexOf(currencyCode[j]) != -1) {
366:                            assertTrue(false);
367:                        } else {
368:                            assertTrue(true);
369:                        }
370:                    }
371:                }
372:            }
373:
374:            /**
375:             * fill suite with test methods
376:             */
377:            public void runTests() {
378:
379:                declare("testMessageFormat");
380:                testMessageFormat();
381:
382:                declare("testDateTime");
383:                testDateTime();
384:
385:                declare("testPercent");
386:                testPercent();
387:                declare("testNumber");
388:                testNumber();
389:
390:                declare("testCurrencyWithCode");
391:                testCurrencyWithCode();
392:
393:                declare("testUnsupportedLocale");
394:                testUnsupportedLocale();
395:                declare("testFormatSupportedCurrency");
396:                testFormatSupportedCurrency();
397:
398:                declare("testCurrencyWithNonSupportedCode");
399:                testCurrencyWithNonSupportedCode();
400:
401:                declare("testRoundingCurrencyDown");
402:                testRoundingCurrencyDown();
403:
404:                declare("testRoundingCurrencyUp");
405:                testRoundingCurrencyUp();
406:
407:                declare("testNaN");
408:                testNaN();
409:            }
410:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.