Source Code Cross Referenced for DateTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » util » 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 » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.luni.tests.java.util;
019:
020:        import java.util.Calendar;
021:        import java.util.Date;
022:        import java.util.GregorianCalendar;
023:        import java.util.TimeZone;
024:
025:        public class DateTest extends junit.framework.TestCase {
026:
027:            /**
028:             * @tests java.util.Date#Date()
029:             */
030:            public void test_Constructor() {
031:                // Test for method java.util.Date()
032:                GregorianCalendar gc = new GregorianCalendar(1998,
033:                        Calendar.OCTOBER, 13, 19, 9);
034:                long oldTime = gc.getTime().getTime();
035:                long now = new Date().getTime();
036:                assertTrue("Created incorrect date: " + oldTime + " now: "
037:                        + now, oldTime < now);
038:            }
039:
040:            /**
041:             * @tests java.util.Date#Date(int, int, int)
042:             */
043:            public void test_ConstructorIII() {
044:                // Test for method java.util.Date(int, int, int)
045:                Date d1 = new Date(70, 0, 1); // the epoch + local time
046:
047:                // the epoch + local time
048:                Date d2 = new Date(0 + d1.getTimezoneOffset() * 60 * 1000);
049:
050:                assertTrue("Created incorrect date", d1.equals(d2));
051:
052:                Date date = new Date(99, 5, 22);
053:                Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 22);
054:                assertTrue("Wrong time zone", date.equals(cal.getTime()));
055:            }
056:
057:            /**
058:             * @tests java.util.Date#Date(int, int, int, int, int)
059:             */
060:            public void test_ConstructorIIIII() {
061:                // Test for method java.util.Date(int, int, int, int, int)
062:
063:                // the epoch + local time + (1 hour and 1 minute)
064:                Date d1 = new Date(70, 0, 1, 1, 1);
065:
066:                // the epoch + local time + (1 hour and 1 minute)
067:                Date d2 = new Date(0 + d1.getTimezoneOffset() * 60 * 1000 + 60
068:                        * 60 * 1000 + 60 * 1000);
069:
070:                assertTrue("Created incorrect date", d1.equals(d2));
071:            }
072:
073:            /**
074:             * @tests java.util.Date#Date(int, int, int, int, int, int)
075:             */
076:            public void test_ConstructorIIIIII() {
077:                // Test for method java.util.Date(int, int, int, int, int, int)
078:
079:                // the epoch + local time + (1 hour and 1 minute + 1 second)
080:                Date d1 = new Date(70, 0, 1, 1, 1, 1);
081:
082:                // the epoch + local time + (1 hour and 1 minute + 1 second)
083:                Date d2 = new Date(0 + d1.getTimezoneOffset() * 60 * 1000 + 60
084:                        * 60 * 1000 + 60 * 1000 + 1000);
085:
086:                assertTrue("Created incorrect date", d1.equals(d2));
087:            }
088:
089:            /**
090:             * @tests java.util.Date#Date(long)
091:             */
092:            public void test_ConstructorJ() {
093:                // Test for method java.util.Date(long)
094:                assertTrue("Used to test", true);
095:            }
096:
097:            /**
098:             * @tests java.util.Date#Date(java.lang.String)
099:             */
100:            public void test_ConstructorLjava_lang_String() {
101:                // Test for method java.util.Date(java.lang.String)
102:                Date d1 = new Date("January 1, 1970, 00:00:00 GMT"); // the epoch
103:                Date d2 = new Date(0); // the epoch
104:                assertTrue("Created incorrect date", d1.equals(d2));
105:
106:                try {
107:                    // Regression for HARMONY-238
108:                    new Date(null);
109:                    fail("Constructor Date((String)null) should "
110:                            + "throw IllegalArgumentException");
111:                } catch (IllegalArgumentException e) {
112:                    // expected
113:                }
114:            }
115:
116:            /**
117:             * @tests java.util.Date#after(java.util.Date)
118:             */
119:            public void test_afterLjava_util_Date() {
120:                // Test for method boolean java.util.Date.after(java.util.Date)
121:                Date d1 = new Date(0);
122:                Date d2 = new Date(1900000);
123:                assertTrue("Older was returned as newer", d2.after(d1));
124:                assertTrue("Newer was returned as older", !d1.after(d2));
125:            }
126:
127:            /**
128:             * @tests java.util.Date#before(java.util.Date)
129:             */
130:            public void test_beforeLjava_util_Date() {
131:                // Test for method boolean java.util.Date.before(java.util.Date)
132:                Date d1 = new Date(0);
133:                Date d2 = new Date(1900000);
134:                assertTrue("Older was returned as newer", !d2.before(d1));
135:                assertTrue("Newer was returned as older", d1.before(d2));
136:            }
137:
138:            /**
139:             * @tests java.util.Date#clone()
140:             */
141:            public void test_clone() {
142:                // Test for method java.lang.Object java.util.Date.clone()
143:                Date d1 = new Date(100000);
144:                Date d2 = (Date) d1.clone();
145:                assertTrue(
146:                        "Cloning date results in same reference--new date is equivalent",
147:                        d1 != d2);
148:                assertTrue("Cloning date results unequal date", d1.equals(d2));
149:            }
150:
151:            /**
152:             * @tests java.util.Date#compareTo(java.util.Date)
153:             */
154:            public void test_compareToLjava_util_Date() {
155:                // Test for method int java.util.Date.compareTo(java.util.Date)
156:                final int someNumber = 10000;
157:                Date d1 = new Date(someNumber);
158:                Date d2 = new Date(someNumber);
159:                Date d3 = new Date(someNumber + 1);
160:                Date d4 = new Date(someNumber - 1);
161:                assertEquals("Comparing a date to itself did not answer zero",
162:                        0, d1.compareTo(d1));
163:                assertEquals("Comparing equal dates did not answer zero", 0, d1
164:                        .compareTo(d2));
165:                assertEquals(
166:                        "date1.compareTo(date2), where date1 > date2, did not result in 1",
167:                        1, d1.compareTo(d4));
168:                assertEquals(
169:                        "date1.compareTo(date2), where date1 < date2, did not result in -1",
170:                        -1, d1.compareTo(d3));
171:
172:            }
173:
174:            /**
175:             * @tests java.util.Date#equals(java.lang.Object)
176:             */
177:            public void test_equalsLjava_lang_Object() {
178:                // Test for method boolean java.util.Date.equals(java.lang.Object)
179:                Date d1 = new Date(0);
180:                Date d2 = new Date(1900000);
181:                Date d3 = new Date(1900000);
182:                assertTrue("Equality test failed", d2.equals(d3));
183:                assertTrue("Equality test failed", !d1.equals(d2));
184:            }
185:
186:            /**
187:             * @tests java.util.Date#getDate()
188:             */
189:            public void test_getDate() {
190:                // Test for method int java.util.Date.getDate()
191:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
192:                        9).getTime();
193:                assertEquals("Returned incorrect date", 13, d.getDate());
194:            }
195:
196:            /**
197:             * @tests java.util.Date#getDay()
198:             */
199:            public void test_getDay() {
200:                // Test for method int java.util.Date.getDay()
201:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
202:                        9).getTime();
203:                assertEquals("Returned incorrect day", 2, d.getDay());
204:            }
205:
206:            /**
207:             * @tests java.util.Date#getHours()
208:             */
209:            public void test_getHours() {
210:                // Test for method int java.util.Date.getHours()
211:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
212:                        9).getTime();
213:                assertEquals("Returned incorrect hours", 19, d.getHours());
214:            }
215:
216:            /**
217:             * @tests java.util.Date#getMinutes()
218:             */
219:            public void test_getMinutes() {
220:                // Test for method int java.util.Date.getMinutes()
221:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
222:                        9).getTime();
223:                assertEquals("Returned incorrect minutes", 9, d.getMinutes());
224:            }
225:
226:            /**
227:             * @tests java.util.Date#getMonth()
228:             */
229:            public void test_getMonth() {
230:                // Test for method int java.util.Date.getMonth()
231:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
232:                        9).getTime();
233:                assertEquals("Returned incorrect month", 9, d.getMonth());
234:            }
235:
236:            /**
237:             * @tests java.util.Date#getSeconds()
238:             */
239:            public void test_getSeconds() {
240:                // Test for method int java.util.Date.getSeconds()
241:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
242:                        9).getTime();
243:                assertEquals("Returned incorrect seconds", 0, d.getSeconds());
244:            }
245:
246:            /**
247:             * @tests java.util.Date#getTime()
248:             */
249:            public void test_getTime() {
250:                // Test for method long java.util.Date.getTime()
251:                Date d1 = new Date(0);
252:                Date d2 = new Date(1900000);
253:                assertEquals("Returned incorrect time", 1900000, d2.getTime());
254:                assertEquals("Returned incorrect time", 0, d1.getTime());
255:            }
256:
257:            /**
258:             * @tests java.util.Date#getTimezoneOffset()
259:             */
260:            public void test_getTimezoneOffset() {
261:                // Test for method int java.util.Date.getTimezoneOffset()
262:                assertTrue("Used to test", true);
263:            }
264:
265:            /**
266:             * @tests java.util.Date#getYear()
267:             */
268:            public void test_getYear() {
269:                // Test for method int java.util.Date.getYear()
270:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
271:                        9).getTime();
272:                assertEquals("Returned incorrect year", 98, d.getYear());
273:            }
274:
275:            /**
276:             * @tests java.util.Date#hashCode()
277:             */
278:            public void test_hashCode() {
279:                // Test for method int java.util.Date.hashCode()
280:                Date d1 = new Date(0);
281:                Date d2 = new Date(1900000);
282:                assertEquals("Returned incorrect hash", 1900000, d2.hashCode());
283:                assertEquals("Returned incorrect hash", 0, d1.hashCode());
284:            }
285:
286:            /**
287:             * @tests java.util.Date#parse(java.lang.String)
288:             */
289:            public void test_parseLjava_lang_String() {
290:                // Test for method long java.util.Date.parse(java.lang.String)
291:                Date d = new Date(Date.parse("13 October 1998"));
292:                GregorianCalendar cal = new GregorianCalendar();
293:                cal.setTime(d);
294:                assertEquals("Parsed incorrect month", 9, cal
295:                        .get(Calendar.MONTH));
296:                assertEquals("Parsed incorrect year", 1998, cal
297:                        .get(Calendar.YEAR));
298:                assertEquals("Parsed incorrect date", 13, cal
299:                        .get(Calendar.DATE));
300:
301:                d = new Date(Date.parse("Jan-12 1999"));
302:                assertTrue("Wrong parsed date 1", d
303:                        .equals(new GregorianCalendar(1999, 0, 12).getTime()));
304:                d = new Date(Date.parse("Jan12-1999"));
305:                assertTrue("Wrong parsed date 2", d
306:                        .equals(new GregorianCalendar(1999, 0, 12).getTime()));
307:                d = new Date(Date.parse("Jan12 69-1"));
308:                cal.setTimeZone(TimeZone.getTimeZone("GMT"));
309:                cal.clear();
310:                cal.set(1969, Calendar.JANUARY, 12, 1, 0);
311:                assertTrue("Wrong parsed date 3", d.equals(cal.getTime()));
312:                d = new Date(Date.parse("6:45:13 3/2/1200 MST"));
313:                cal.setTimeZone(TimeZone.getTimeZone("MST"));
314:                cal.clear();
315:                cal.set(1200, 2, 2, 6, 45, 13);
316:                assertTrue("Wrong parsed date 4", d.equals(cal.getTime()));
317:                d = new Date(Date.parse("Mon, 22 Nov 1999 12:52:06 GMT"));
318:                cal.setTimeZone(TimeZone.getTimeZone("GMT"));
319:                cal.clear();
320:                cal.set(1999, Calendar.NOVEMBER, 22, 12, 52, 06);
321:                assertTrue("Wrong parsed date 5", d.equals(cal.getTime()));
322:
323:                try {
324:                    // Regression for HARMONY-259
325:                    Date.parse(null);
326:                    fail("Date.parse(null) should throw IllegalArgumentException");
327:                } catch (IllegalArgumentException e) {
328:                    // expected
329:                }
330:
331:                // Regression for HARMONY-102
332:                assertEquals("Assert 0: parse failure", -5400000, Date
333:                        .parse("Sat, 1 Jan 1970 +0130 00:00:00"));
334:                assertEquals("Assert 1: parse failure", 858600000, Date
335:                        .parse("00:00:00 GMT +0130 Sat, 11 Jan 1970"));
336:            }
337:
338:            /**
339:             * @tests java.util.Date#setDate(int)
340:             */
341:            public void test_setDateI() {
342:                // Test for method void java.util.Date.setDate(int)
343:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
344:                        9).getTime();
345:                d.setDate(23);
346:                assertEquals("Set incorrect date", 23, d.getDate());
347:            }
348:
349:            /**
350:             * @tests java.util.Date#setHours(int)
351:             */
352:            public void test_setHoursI() {
353:                // Test for method void java.util.Date.setHours(int)
354:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
355:                        9).getTime();
356:                d.setHours(23);
357:                assertEquals("Set incorrect hours", 23, d.getHours());
358:            }
359:
360:            /**
361:             * @tests java.util.Date#setMinutes(int)
362:             */
363:            public void test_setMinutesI() {
364:                // Test for method void java.util.Date.setMinutes(int)
365:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
366:                        9).getTime();
367:                d.setMinutes(45);
368:                assertEquals("Set incorrect mins", 45, d.getMinutes());
369:            }
370:
371:            /**
372:             * @tests java.util.Date#setMonth(int)
373:             */
374:            public void test_setMonthI() {
375:                // Test for method void java.util.Date.setMonth(int)
376:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
377:                        9).getTime();
378:                d.setMonth(0);
379:                assertEquals("Set incorrect month", 0, d.getMonth());
380:            }
381:
382:            /**
383:             * @tests java.util.Date#setSeconds(int)
384:             */
385:            public void test_setSecondsI() {
386:                // Test for method void java.util.Date.setSeconds(int)
387:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
388:                        9).getTime();
389:                d.setSeconds(13);
390:                assertEquals("Set incorrect seconds", 13, d.getSeconds());
391:            }
392:
393:            /**
394:             * @tests java.util.Date#setTime(long)
395:             */
396:            public void test_setTimeJ() {
397:                // Test for method void java.util.Date.setTime(long)
398:                Date d1 = new Date(0);
399:                Date d2 = new Date(1900000);
400:                d1.setTime(900);
401:                d2.setTime(890000);
402:                assertEquals("Returned incorrect time", 890000, d2.getTime());
403:                assertEquals("Returned incorrect time", 900, d1.getTime());
404:            }
405:
406:            /**
407:             * @tests java.util.Date#setYear(int)
408:             */
409:            public void test_setYearI() {
410:                // Test for method void java.util.Date.setYear(int)
411:                Date d = new GregorianCalendar(1998, Calendar.OCTOBER, 13, 19,
412:                        9).getTime();
413:                d.setYear(8);
414:                assertEquals("Set incorrect year", 8, d.getYear());
415:            }
416:
417:            /**
418:             * @tests java.util.Date#toGMTString()
419:             */
420:            public void test_toGMTString() {
421:                // Test for method java.lang.String java.util.Date.toGMTString()
422:                assertEquals("Did not convert epoch to GMT string correctly",
423:                        "1 Jan 1970 00:00:00 GMT", new Date(0).toGMTString());
424:                assertEquals(
425:                        "Did not convert epoch + 1yr to GMT string correctly",
426:                        "1 Jan 1971 00:00:00 GMT", new Date((long) 365 * 24
427:                                * 60 * 60 * 1000).toGMTString());
428:            }
429:
430:            /**
431:             * @tests java.util.Date#toString()
432:             */
433:            public void test_toString() {
434:                // Test for method java.lang.String java.util.Date.toString()
435:                Calendar cal = Calendar.getInstance();
436:                cal.set(Calendar.DATE, 1);
437:                cal.set(Calendar.MONTH, Calendar.JANUARY);
438:                cal.set(Calendar.YEAR, 1970);
439:                cal.set(Calendar.HOUR_OF_DAY, 0);
440:                cal.set(Calendar.MINUTE, 0);
441:                cal.set(Calendar.SECOND, 0);
442:                Date d = cal.getTime();
443:                String result = d.toString();
444:                assertTrue("Incorrect result: " + d, result
445:                        .startsWith("Thu Jan 01 00:00:00")
446:                        && result.endsWith("1970"));
447:
448:                TimeZone tz = TimeZone.getDefault();
449:                TimeZone.setDefault(TimeZone.getTimeZone("EST"));
450:                try {
451:                    Date d1 = new Date(0);
452:                    assertTrue("Returned incorrect string: " + d1, d1
453:                            .toString().startsWith("Wed Dec 31 19:00:00")
454:                            && d1.toString().endsWith("1969"));
455:                } finally {
456:                    TimeZone.setDefault(tz);
457:                }
458:            }
459:
460:            /**
461:             * @tests java.util.Date#UTC(int, int, int, int, int, int)
462:             */
463:            public void test_UTCIIIIII() {
464:                // Test for method long java.util.Date.UTC(int, int, int, int, int, int)
465:                assertTrue("Returned incorrect UTC value for epoch", Date.UTC(
466:                        70, 0, 1, 0, 0, 0) == (long) 0);
467:                assertTrue("Returned incorrect UTC value for epoch +1yr", Date
468:                        .UTC(71, 0, 1, 0, 0, 0) == (long) 365 * 24 * 60 * 60
469:                        * 1000);
470:            }
471:
472:            /**
473:             * Sets up the fixture, for example, open a network connection. This method
474:             * is called before a test is executed.
475:             */
476:            protected void setUp() {
477:            }
478:
479:            /**
480:             * Tears down the fixture, for example, close a network connection. This
481:             * method is called after a test is executed.
482:             */
483:            protected void tearDown() {
484:            }
485:        }
w__w__w_._ja_v___a__2__s__.___com___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.