Source Code Cross Referenced for IntlTestDateFormat.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) 1996-2006, International Business Machines
004:         *   Corporation and others.  All Rights Reserved.
005:         */
006:
007:        /** 
008:         * Port From:   JDK 1.4b1 : java.text.Format.IntlTestDateFormat
009:         * Source File: java/text/format/IntlTestDateFormat.java
010:         **/package com.ibm.icu.dev.test.format;
011:
012:        import com.ibm.icu.text.DateFormat;
013:        import com.ibm.icu.text.SimpleDateFormat;
014:        import com.ibm.icu.util.ULocale;
015:        import java.text.FieldPosition;
016:        import java.text.ParseException;
017:        import java.util.Random;
018:        import java.util.Date;
019:
020:        public class IntlTestDateFormat extends com.ibm.icu.dev.test.TestFmwk {
021:            // Values in milliseconds (== Date)
022:            private static final long ONESECOND = 1000;
023:            private static final long ONEMINUTE = 60 * ONESECOND;
024:            private static final long ONEHOUR = 60 * ONEMINUTE;
025:            private static final long ONEDAY = 24 * ONEHOUR;
026:            //private static final double ONEYEAR = 365.25 * ONEDAY; // Approximate //The variable is never used
027:
028:            // EModes
029:            //private static final byte GENERIC = 0;
030:            //private static final byte TIME = GENERIC + 1; //The variable is never used
031:            //private static final byte DATE = TIME + 1; //The variable is never used
032:            //private static final byte DATE_TIME = DATE + 1; //The variable is never used
033:
034:            private DateFormat fFormat = null;
035:            private String fTestName = new String("getInstance");
036:            private int fLimit = 3; // How many iterations it should take to reach convergence
037:            private Random random; // initialized in randDouble
038:
039:            public IntlTestDateFormat() {
040:                //Constructure
041:            }
042:
043:            protected void init() throws Exception {
044:                fFormat = DateFormat.getInstance();
045:            }
046:
047:            public static void main(String[] args) throws Exception {
048:                new IntlTestDateFormat().run(args);
049:            }
050:
051:            public void TestULocale() {
052:                localeTest(ULocale.getDefault(), "Default Locale");
053:            }
054:
055:            // This test does round-trip testing (format -> parse -> format -> parse -> etc.) of DateFormat.
056:            public void localeTest(final ULocale locale, final String localeName) {
057:                int timeStyle, dateStyle;
058:
059:                // For patterns including only time information and a timezone, it may take
060:                // up to three iterations, since the timezone may shift as the year number
061:                // is determined.  For other patterns, 2 iterations should suffice.
062:                fLimit = 3;
063:
064:                for (timeStyle = 0; timeStyle < 4; timeStyle++) {
065:                    fTestName = new String("Time test " + timeStyle + " ("
066:                            + localeName + ")");
067:                    try {
068:                        fFormat = DateFormat.getTimeInstance(timeStyle, locale);
069:                    } catch (StringIndexOutOfBoundsException e) {
070:                        errln("FAIL: localeTest time getTimeInstance exception");
071:                        throw e;
072:                    }
073:                    TestFormat();
074:                }
075:
076:                fLimit = 2;
077:
078:                for (dateStyle = 0; dateStyle < 4; dateStyle++) {
079:                    fTestName = new String("Date test " + dateStyle + " ("
080:                            + localeName + ")");
081:                    try {
082:                        fFormat = DateFormat.getDateInstance(dateStyle, locale);
083:                    } catch (StringIndexOutOfBoundsException e) {
084:                        errln("FAIL: localeTest date getTimeInstance exception");
085:                        throw e;
086:                    }
087:                    TestFormat();
088:                }
089:
090:                for (dateStyle = 0; dateStyle < 4; dateStyle++) {
091:                    for (timeStyle = 0; timeStyle < 4; timeStyle++) {
092:                        fTestName = new String("DateTime test " + dateStyle
093:                                + "/" + timeStyle + " (" + localeName + ")");
094:                        try {
095:                            fFormat = DateFormat.getDateTimeInstance(dateStyle,
096:                                    timeStyle, locale);
097:                        } catch (StringIndexOutOfBoundsException e) {
098:                            errln("FAIL: localeTest date/time getDateTimeInstance exception");
099:                            throw e;
100:                        }
101:                        TestFormat();
102:                    }
103:                }
104:            }
105:
106:            public void TestFormat() {
107:                if (fFormat == null) {
108:                    errln("FAIL: DateFormat creation failed");
109:                    return;
110:                }
111:                //        logln("TestFormat: " + fTestName);
112:                Date now = new Date();
113:                tryDate(new Date(0));
114:                tryDate(new Date((long) 1278161801778.0));
115:                tryDate(now);
116:                // Shift 6 months into the future, AT THE SAME TIME OF DAY.
117:                // This will test the DST handling.
118:                tryDate(new Date(now.getTime() + 6 * 30 * ONEDAY));
119:
120:                Date limit = new Date(now.getTime() * 10); // Arbitrary limit
121:                for (int i = 0; i < 2; ++i)
122:                    //            tryDate(new Date(floor(randDouble() * limit)));
123:                    tryDate(new Date((long) (randDouble() * limit.getTime())));
124:            }
125:
126:            private void describeTest() {
127:                if (fFormat == null) {
128:                    errln("FAIL: no DateFormat");
129:                    return;
130:                }
131:
132:                // Assume it's a SimpleDateFormat and get some info
133:                SimpleDateFormat s = (SimpleDateFormat) fFormat;
134:                logln(fTestName + " Pattern " + s.toPattern());
135:            }
136:
137:            private void tryDate(Date theDate) {
138:                final int DEPTH = 10;
139:                Date[] date = new Date[DEPTH];
140:                StringBuffer[] string = new StringBuffer[DEPTH];
141:
142:                int dateMatch = 0;
143:                int stringMatch = 0;
144:                boolean dump = false;
145:                int i;
146:                for (i = 0; i < DEPTH; ++i)
147:                    string[i] = new StringBuffer();
148:                for (i = 0; i < DEPTH; ++i) {
149:                    if (i == 0)
150:                        date[i] = theDate;
151:                    else {
152:                        try {
153:                            date[i] = fFormat.parse(string[i - 1].toString());
154:                        } catch (ParseException e) {
155:                            describeTest();
156:                            errln("********** FAIL: Parse of " + string[i - 1]
157:                                    + " failed for locale: "
158:                                    + fFormat.getLocale(ULocale.ACTUAL_LOCALE));
159:                            dump = true;
160:                            break;
161:                        }
162:                    }
163:                    FieldPosition position = new FieldPosition(0);
164:                    fFormat.format(date[i], string[i], position);
165:                    if (i > 0) {
166:                        if (dateMatch == 0 && date[i] == date[i - 1])
167:                            dateMatch = i;
168:                        else if (dateMatch > 0 && date[i] != date[i - 1]) {
169:                            describeTest();
170:                            errln("********** FAIL: Date mismatch after match.");
171:                            dump = true;
172:                            break;
173:                        }
174:                        if (stringMatch == 0 && string[i] == string[i - 1])
175:                            stringMatch = i;
176:                        else if (stringMatch > 0 && string[i] != string[i - 1]) {
177:                            describeTest();
178:                            errln("********** FAIL: String mismatch after match.");
179:                            dump = true;
180:                            break;
181:                        }
182:                    }
183:                    if (dateMatch > 0 && stringMatch > 0)
184:                        break;
185:                }
186:                if (i == DEPTH)
187:                    --i;
188:
189:                if (stringMatch > fLimit || dateMatch > fLimit) {
190:                    describeTest();
191:                    errln("********** FAIL: No string and/or date match within "
192:                            + fLimit + " iterations.");
193:                    dump = true;
194:                }
195:
196:                if (dump) {
197:                    for (int k = 0; k <= i; ++k) {
198:                        logln("" + k + ": " + date[k] + " F> " + string[k]
199:                                + " P> ");
200:                    }
201:                }
202:            }
203:
204:            // Return a random double from 0.01 to 1, inclusive
205:            private double randDouble() {
206:                if (random == null) {
207:                    random = createRandom();
208:                }
209:                // Assume 8-bit (or larger) rand values.  Also assume
210:                // that the system rand() function is very poor, which it always is.
211:                //        double d;
212:                //        int i;
213:                //        do {
214:                //            for (i=0; i < sizeof(double); ++i)
215:                //            {
216:                //                char poke = (char*)&d;
217:                //                poke[i] = (rand() & 0xFF);
218:                //            }
219:                //        } while (TPlatformUtilities.isNaN(d) || TPlatformUtilities.isInfinite(d));
220:
221:                //        if (d < 0.0) d = -d;
222:                //        if (d > 0.0)
223:                //        {
224:                //            double e = floor(log10(d));
225:                //            if (e < -2.0) d *= pow(10.0, -e-2);
226:                //            else if (e > -1.0) d /= pow(10.0, e+1);
227:                //        }
228:                //        return d;
229:                return random.nextDouble();
230:            }
231:
232:            public void TestAvailableLocales() {
233:                final ULocale[] locales = DateFormat.getAvailableULocales();
234:                long count = locales.length;
235:                logln("" + count + " available locales");
236:                if (locales != null && count != 0) {
237:                    StringBuffer all = new StringBuffer();
238:                    for (int i = 0; i < count; ++i) {
239:                        if (i != 0)
240:                            all.append(", ");
241:                        all.append(locales[i].getDisplayName());
242:                    }
243:                    logln(all.toString());
244:                } else
245:                    errln("********** FAIL: Zero available locales or null array pointer");
246:            }
247:
248:            public void TestMonster() {
249:                if (isQuick()) {
250:                    logln("Skipping test (use -e for exhaustive)");
251:                    return;
252:                }
253:                final ULocale[] locales = DateFormat.getAvailableULocales();
254:                long count = locales.length;
255:                if (locales != null && count != 0) {
256:                    for (int i = 0; i < count; ++i) {
257:                        String name = locales[i].getDisplayName();
258:                        logln("Testing " + name + "...");
259:                        try {
260:                            localeTest(locales[i], name);
261:                        } catch (Exception e) {
262:                            errln("FAIL: TestMonster localeTest exception" + e);
263:                        }
264:                    }
265:                }
266:            }
267:        }
268:
269:        //eof
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.