Source Code Cross Referenced for TestDateDiffFunction.java in  » Database-DBMS » axion » org » axiondb » functions » 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 » Database DBMS » axion » org.axiondb.functions 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: TestDateDiffFunction.java,v 1.8 2005/03/04 09:09:17 ahimanikya Exp $
003:         * =======================================================================
004:         * Copyright (c) 2004 Axion Development Team.  All rights reserved.
005:         *  
006:         * Redistribution and use in source and binary forms, with or without 
007:         * modification, are permitted provided that the following conditions 
008:         * are met:
009:         * 
010:         * 1. Redistributions of source code must retain the above 
011:         *    copyright notice, this list of conditions and the following 
012:         *    disclaimer. 
013:         *   
014:         * 2. Redistributions in binary form must reproduce the above copyright 
015:         *    notice, this list of conditions and the following disclaimer in 
016:         *    the documentation and/or other materials provided with the 
017:         *    distribution. 
018:         *   
019:         * 3. The names "Tigris", "Axion", nor the names of its contributors may 
020:         *    not be used to endorse or promote products derived from this 
021:         *    software without specific prior written permission. 
022:         *  
023:         * 4. Products derived from this software may not be called "Axion", nor 
024:         *    may "Tigris" or "Axion" appear in their names without specific prior
025:         *    written permission.
026:         *   
027:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
028:         * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
029:         * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030:         * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
031:         * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
032:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
033:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
034:         * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
035:         * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
036:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
037:         * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038:         * =======================================================================
039:         */
040:
041:        package org.axiondb.functions;
042:
043:        import java.sql.Date;
044:        import java.sql.Timestamp;
045:        import java.util.Calendar;
046:        import java.util.HashMap;
047:
048:        import org.axiondb.ColumnIdentifier;
049:        import org.axiondb.RowDecorator;
050:        import org.axiondb.engine.rows.SimpleRow;
051:        import org.axiondb.types.DateType;
052:        import org.axiondb.types.TimestampType;
053:
054:        import junit.framework.Test;
055:        import junit.framework.TestSuite;
056:
057:        /**
058:         * @author radval
059:         *
060:         * To change the template for this generated type comment go to
061:         * Window - Preferences - Java - Code Generation - Code and Comments
062:         */
063:        public class TestDateDiffFunction extends BaseFunctionTest {
064:
065:            public TestDateDiffFunction(String testName) {
066:                super (testName);
067:            }
068:
069:            public void setUp() throws Exception {
070:                super .setUp();
071:                TimestampType.setTimeZone("GMT");
072:            }
073:
074:            /* (non-Javadoc)
075:             * @see org.axiondb.functions.BaseFunctionTest#makeFunction()
076:             */
077:            protected ConcreteFunction makeFunction() {
078:                return new DateDiffFunction();
079:            }
080:
081:            public static Test suite() {
082:                TestSuite suite = new TestSuite(TestDateDiffFunction.class);
083:                return suite;
084:            }
085:
086:            public void testYearsBetweenDates() throws Exception {
087:                DateDiffFunction function = new DateDiffFunction();
088:                ColumnIdentifier intervalType = new ColumnIdentifier(
089:                        "intervalType");
090:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
091:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
092:
093:                function.addArgument(intervalType);
094:                function.addArgument(timestamp1);
095:                function.addArgument(timestamp2);
096:
097:                HashMap map = new HashMap();
098:                map.put(intervalType, new Integer(0));
099:                map.put(timestamp1, new Integer(1));
100:                map.put(timestamp2, new Integer(2));
101:
102:                //November 30 1973  GMT
103:                Timestamp t1 = new Timestamp(3 * 365 * 24 * 60 * 60 * 1000L
104:                        + //year
105:                        8 * 24 * 60 * 60 * 1000L
106:                        + // leap years '72,'76,'80,'84,'88,'92,'96,2000
107:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
108:                        * 24 * 60 * 60 * 1000L // November 30
109:                );
110:
111:                //April 25 2004  GMT
112:                Timestamp t2 = new Timestamp(34 * 365 * 24 * 60 * 60 * 1000L + //year
113:                        9 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000,2004
114:                        (31 + 28 + 31 + 25) * 24 * 60 * 60 * 1000L // April 25
115:                );
116:
117:                Calendar c1 = Calendar.getInstance(TimestampType.getTimeZone());
118:                c1.setTimeInMillis(t1.getTime());
119:
120:                Calendar c2 = Calendar.getInstance(TimestampType.getTimeZone());
121:                c2.setTimeInMillis(t2.getTime());
122:
123:                RowDecorator dec = new RowDecorator(map);
124:                dec.setRow(new SimpleRow(new Object[] { "YEAR", t1, t2 }));
125:                Long result = new Long(c2.get(Calendar.YEAR)
126:                        - c1.get(Calendar.YEAR));
127:                assertEquals(result, function.evaluate(dec));
128:            }
129:
130:            public void testYearsBetweenDates2() throws Exception {
131:                DateDiffFunction function = new DateDiffFunction();
132:                ColumnIdentifier intervalType = new ColumnIdentifier(
133:                        "intervalType");
134:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
135:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
136:
137:                function.addArgument(intervalType);
138:                function.addArgument(timestamp1);
139:                function.addArgument(timestamp2);
140:
141:                HashMap map = new HashMap();
142:                map.put(intervalType, new Integer(0));
143:                map.put(timestamp1, new Integer(1));
144:                map.put(timestamp2, new Integer(2));
145:
146:                //November 30 1973  GMT
147:                Timestamp t2 = new Timestamp(3 * 365 * 24 * 60 * 60 * 1000L
148:                        + //year
149:                        8 * 24 * 60 * 60 * 1000L
150:                        + // leap years '72,'76,'80,'84,'88,'92,'96,2000
151:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
152:                        * 24 * 60 * 60 * 1000L // November 30
153:                );
154:
155:                //April 25 2004  GMT
156:                Timestamp t1 = new Timestamp(34 * 365 * 24 * 60 * 60 * 1000L + //year
157:                        9 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000,2004
158:                        (31 + 28 + 31 + 25) * 24 * 60 * 60 * 1000L // April 25
159:                );
160:
161:                Calendar c1 = Calendar.getInstance(TimestampType.getTimeZone());
162:                c1.setTimeInMillis(t1.getTime());
163:
164:                Calendar c2 = Calendar.getInstance(TimestampType.getTimeZone());
165:                c2.setTimeInMillis(t2.getTime());
166:
167:                RowDecorator dec = new RowDecorator(map);
168:                dec.setRow(new SimpleRow(new Object[] { "YEAR", t1, t2 }));
169:                Long result = new Long(c2.get(Calendar.YEAR)
170:                        - c1.get(Calendar.YEAR));
171:                assertEquals(result, function.evaluate(dec));
172:            }
173:
174:            public void testWeeksBetweenDates() throws Exception {
175:                DateDiffFunction function = new DateDiffFunction();
176:                ColumnIdentifier intervalType = new ColumnIdentifier(
177:                        "intervalType");
178:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
179:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
180:
181:                function.addArgument(intervalType);
182:                function.addArgument(timestamp1);
183:                function.addArgument(timestamp2);
184:
185:                HashMap map = new HashMap();
186:                map.put(intervalType, new Integer(0));
187:                map.put(timestamp1, new Integer(1));
188:                map.put(timestamp2, new Integer(2));
189:
190:                //November 30 1973  GMT
191:                Timestamp t1 = new Timestamp(3 * 365 * 24 * 60 * 60 * 1000L
192:                        + //year
193:                        8 * 24 * 60 * 60 * 1000L
194:                        + // leap years '72,'76,'80,'84,'88,'92,'96,2000
195:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
196:                        * 24 * 60 * 60 * 1000L // November 30
197:                );
198:
199:                //April 25 2004  GMT
200:                Timestamp t2 = new Timestamp(34 * 365 * 24 * 60 * 60 * 1000L + //year
201:                        9 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000,2004
202:                        (31 + 28 + 31 + 25) * 24 * 60 * 60 * 1000L // April 25
203:                );
204:
205:                RowDecorator dec = new RowDecorator(map);
206:                dec.setRow(new SimpleRow(new Object[] { "WEEK", t1, t2 }));
207:                int i = (t2.getTime() - t1.getTime())
208:                        % (7 * 24 * 60 * 60 * 1000) == 0 ? 0 : 1;
209:                Long result = new Long((t2.getTime() - t1.getTime())
210:                        / (7 * 24 * 60 * 60 * 1000) + i);
211:                assertEquals(result, function.evaluate(dec));
212:            }
213:
214:            public void testWeeksBetweenDates2() throws Exception {
215:                DateDiffFunction function = new DateDiffFunction();
216:                ColumnIdentifier intervalType = new ColumnIdentifier(
217:                        "intervalType");
218:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
219:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
220:
221:                function.addArgument(intervalType);
222:                function.addArgument(timestamp1);
223:                function.addArgument(timestamp2);
224:
225:                HashMap map = new HashMap();
226:                map.put(intervalType, new Integer(0));
227:                map.put(timestamp1, new Integer(1));
228:                map.put(timestamp2, new Integer(2));
229:
230:                //November 30 1973  GMT
231:                Timestamp t2 = new Timestamp(3 * 365 * 24 * 60 * 60 * 1000L
232:                        + //year
233:                        8 * 24 * 60 * 60 * 1000L
234:                        + // leap years '72,'76,'80,'84,'88,'92,'96,2000
235:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
236:                        * 24 * 60 * 60 * 1000L // November 30
237:                );
238:
239:                //April 25 2004  GMT
240:                Timestamp t1 = new Timestamp(34 * 365 * 24 * 60 * 60 * 1000L + //year
241:                        9 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000,2004
242:                        (31 + 28 + 31 + 25) * 24 * 60 * 60 * 1000L // April 25
243:                );
244:
245:                RowDecorator dec = new RowDecorator(map);
246:                dec.setRow(new SimpleRow(new Object[] { "WEEK", t1, t2 }));
247:                int i = (t2.getTime() - t1.getTime())
248:                        % (7 * 24 * 60 * 60 * 1000) == 0 ? 0 : -1;
249:                Long result = new Long((t2.getTime() - t1.getTime())
250:                        / (7 * 24 * 60 * 60 * 1000) + i);
251:                assertEquals(result, function.evaluate(dec));
252:            }
253:
254:            public void testQuartersBetweenDates() throws Exception {
255:                DateDiffFunction function = new DateDiffFunction();
256:                ColumnIdentifier intervalType = new ColumnIdentifier(
257:                        "intervalType");
258:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
259:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
260:
261:                function.addArgument(intervalType);
262:                function.addArgument(timestamp1);
263:                function.addArgument(timestamp2);
264:
265:                HashMap map = new HashMap();
266:                map.put(intervalType, new Integer(0));
267:                map.put(timestamp1, new Integer(1));
268:                map.put(timestamp2, new Integer(2));
269:
270:                // November 30 1973  GMT
271:                Timestamp t1 = new Timestamp(3 * 365 * 24 * 60 * 60 * 1000L
272:                        + //year
273:                        8 * 24 * 60 * 60 * 1000L
274:                        + // leap years '72,'76,'80,'84,'88,'92,'96,2000
275:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
276:                        * 24 * 60 * 60 * 1000L // November 30
277:                );
278:
279:                //April 25 2004  GMT
280:                Timestamp t2 = new Timestamp(34 * 365 * 24 * 60 * 60 * 1000L + //year
281:                        9 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000,2004
282:                        (31 + 28 + 31 + 25) * 24 * 60 * 60 * 1000L // April 25
283:                );
284:
285:                //      dumb way to calculate months difference but this is another way
286:                //to verify , we are doing a better way in datediff function
287:                Calendar c1 = Calendar.getInstance(TimestampType.getTimeZone());
288:                c1.setTimeInMillis(t1.getTime());
289:                c1.clear(Calendar.MILLISECOND);
290:                c1.clear(Calendar.SECOND);
291:                c1.clear(Calendar.MINUTE);
292:                c1.clear(Calendar.HOUR_OF_DAY);
293:                c1.clear(Calendar.DATE);
294:
295:                Calendar c2 = Calendar.getInstance(TimestampType.getTimeZone());
296:                c2.setTimeInMillis(t2.getTime());
297:                c2.clear(Calendar.MILLISECOND);
298:                c2.clear(Calendar.SECOND);
299:                c2.clear(Calendar.MINUTE);
300:                c2.clear(Calendar.HOUR_OF_DAY);
301:                c2.clear(Calendar.DATE);
302:
303:                int months = 0;
304:                while (c1.before(c2)) {
305:                    c1.add(Calendar.MONTH, 1);
306:                    months++;
307:                }
308:
309:                RowDecorator dec = new RowDecorator(map);
310:                dec.setRow(new SimpleRow(new Object[] { "QUARTER", t1, t2 }));
311:                Long result = new Long(months / 3);
312:                assertEquals(result, function.evaluate(dec));
313:            }
314:
315:            public void testMonthsBetweenDates() throws Exception {
316:                DateDiffFunction function = new DateDiffFunction();
317:                ColumnIdentifier intervalType = new ColumnIdentifier(
318:                        "intervalType");
319:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
320:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
321:
322:                function.addArgument(intervalType);
323:                function.addArgument(timestamp1);
324:                function.addArgument(timestamp2);
325:
326:                HashMap map = new HashMap();
327:                map.put(intervalType, new Integer(0));
328:                map.put(timestamp1, new Integer(1));
329:                map.put(timestamp2, new Integer(2));
330:
331:                //August 22 1990  GMT
332:                Timestamp t2 = new Timestamp(20 * 365 * 24 * 60 * 60 * 1000L
333:                        + //year
334:                        8 * 24 * 60 * 60 * 1000L
335:                        + // leap years '72,'76,'80,'84,'88,'92,'96,2000
336:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 22) * 24 * 60 * 60
337:                        * 1000L // August 22
338:                );
339:
340:                //July 23 2002  GMT
341:                Timestamp t1 = new Timestamp(32 * 365 * 24 * 60 * 60 * 1000L
342:                        + //year
343:                        8 * 24 * 60 * 60 * 1000L
344:                        + // leap years '72,'76,'80,'84,'88,'92,'96,2000
345:                        (31 + 28 + 31 + 30 + 31 + 30 + 23) * 24 * 60 * 60
346:                        * 1000L // //July 23
347:                );
348:
349:                //dumb way to calculate months difference but this is another way
350:                //to verify , we are doing a better way in datediff function
351:                Calendar c1 = Calendar.getInstance(TimestampType.getTimeZone());
352:                c1.setTimeInMillis(t1.getTime());
353:                c1.clear(Calendar.MILLISECOND);
354:                c1.clear(Calendar.SECOND);
355:                c1.clear(Calendar.MINUTE);
356:                c1.clear(Calendar.HOUR_OF_DAY);
357:                c1.clear(Calendar.DATE);
358:
359:                Calendar c2 = Calendar.getInstance(TimestampType.getTimeZone());
360:                c2.setTimeInMillis(t2.getTime());
361:                c2.clear(Calendar.MILLISECOND);
362:                c2.clear(Calendar.SECOND);
363:                c2.clear(Calendar.MINUTE);
364:                c2.clear(Calendar.HOUR_OF_DAY);
365:                c2.clear(Calendar.DATE);
366:
367:                int months = 0;
368:                while (c2.before(c1)) {
369:                    c2.add(Calendar.MONTH, 1);
370:                    months++;
371:                }
372:
373:                RowDecorator dec = new RowDecorator(map);
374:                dec.setRow(new SimpleRow(new Object[] { "MONTH", t1, t2 }));
375:                Long result = new Long(-months);
376:                assertEquals(result, function.evaluate(dec));
377:
378:                dec.setRow(new SimpleRow(new Object[] { "MONTH", t2, t2 }));
379:                assertEquals(new Long(0), function.evaluate(dec));
380:            }
381:
382:            public void testDaysBetweenDates() throws Exception {
383:                DateDiffFunction function = new DateDiffFunction();
384:                ColumnIdentifier intervalType = new ColumnIdentifier(
385:                        "intervalType");
386:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
387:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
388:
389:                function.addArgument(intervalType);
390:                function.addArgument(timestamp1);
391:                function.addArgument(timestamp2);
392:
393:                HashMap map = new HashMap();
394:                map.put(intervalType, new Integer(0));
395:                map.put(timestamp1, new Integer(1));
396:                map.put(timestamp2, new Integer(2));
397:
398:                //August 15 1947  GMT
399:                Timestamp t1 = new Timestamp(-1
400:                        * (23 * 365 * 24 * 60 * 60 * 1000L + //year
401:                                6 * 24 * 60 * 60 * 1000L + // leap years '48,'52,'56,'60,'64,'68,
402:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 15) * 24 * 60 * 60
403:                                * 1000L) // August 15
404:                );
405:
406:                //July 23 2000  GMT
407:                Timestamp t2 = new Timestamp(30 * 365 * 24 * 60 * 60 * 1000L
408:                        + //year
409:                        8 * 24 * 60 * 60 * 1000L
410:                        + // leap years '72,'76,'80,'84,'88,'92,'96,2000
411:                        (31 + 28 + 31 + 30 + 31 + 30 + 23) * 24 * 60 * 60
412:                        * 1000L // //July 23
413:                );
414:
415:                RowDecorator dec = new RowDecorator(map);
416:                dec.setRow(new SimpleRow(new Object[] { "DAY", t1, t2 }));
417:                Long result = new Long((t2.getTime() - t1.getTime())
418:                        / (24 * 60 * 60 * 1000));
419:                assertEquals(result, function.evaluate(dec));
420:            }
421:
422:            public void testHoursBetweenDates() throws Exception {
423:                DateDiffFunction function = new DateDiffFunction();
424:                ColumnIdentifier intervalType = new ColumnIdentifier(
425:                        "intervalType");
426:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
427:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
428:
429:                function.addArgument(intervalType);
430:                function.addArgument(timestamp1);
431:                function.addArgument(timestamp2);
432:
433:                HashMap map = new HashMap();
434:                map.put(intervalType, new Integer(0));
435:                map.put(timestamp1, new Integer(1));
436:                map.put(timestamp2, new Integer(2));
437:
438:                //December 15 1999 11:11:23 GMT
439:                Timestamp t1 = new Timestamp(
440:                        29 * 365 * 24
441:                                * 60
442:                                * 60
443:                                * 1000L
444:                                + //year
445:                                7
446:                                * 24
447:                                * 60
448:                                * 60
449:                                * 1000L
450:                                + // leap years '72,'76,'80,'84,'88,'92,'96
451:                                (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30
452:                                        + 31 + 30 + 15) * 24 * 60 * 60 * 1000L + // December 15
453:                                11 * 60 * 60 * 1000L + //Hours
454:                                11 * 60 * 1000L + //minutes
455:                                23 * 1000L //seconds
456:                );
457:
458:                //April 23 2004 21:22:21  GMT
459:                Timestamp t2 = new Timestamp(34 * 365 * 24 * 60 * 60 * 1000L + //year
460:                        9 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000,2004
461:                        (31 + 28 + 31 + 23) * 24 * 60 * 60 * 1000L + //April 23
462:                        21 * 60 * 60 * 1000L + //Hours
463:                        21 * 60 * 1000L + //minutes
464:                        21 * 1000L //seconds
465:                );
466:
467:                RowDecorator dec = new RowDecorator(map);
468:                dec.setRow(new SimpleRow(new Object[] { "HOUR", t1, t2 }));
469:                Long result = new Long((t2.getTime() - t1.getTime())
470:                        / (60 * 60 * 1000));
471:                assertEquals(result, function.evaluate(dec));
472:            }
473:
474:            public void testMinutesBetweenDates() throws Exception {
475:                DateDiffFunction function = new DateDiffFunction();
476:                ColumnIdentifier intervalType = new ColumnIdentifier(
477:                        "intervalType");
478:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
479:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
480:
481:                function.addArgument(intervalType);
482:                function.addArgument(timestamp1);
483:                function.addArgument(timestamp2);
484:
485:                HashMap map = new HashMap();
486:                map.put(intervalType, new Integer(0));
487:                map.put(timestamp1, new Integer(1));
488:                map.put(timestamp2, new Integer(2));
489:
490:                //August 15 2000 12:04:22 GMT
491:                Timestamp t1 = new Timestamp(30 * 365 * 24 * 60 * 60 * 1000L
492:                        + //year
493:                        8 * 24 * 60 * 60 * 1000L
494:                        + // leap years '72,'76,'80,'84,'88,'92,'96, 2000
495:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 15) * 24 * 60 * 60
496:                        * 1000L + // August 15
497:                        12 * 60 * 60 * 1000L + //Hours
498:                        04 * 60 * 1000L + //minutes
499:                        22 * 1000L //seconds
500:                );
501:
502:                //April 23 2000 11:06:22  GMT
503:                Timestamp t2 = new Timestamp(30 * 365 * 24 * 60 * 60 * 1000L + //year
504:                        8 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000
505:                        (31 + 28 + 31 + 23) * 24 * 60 * 60 * 1000L + //April 23
506:                        11 * 60 * 60 * 1000L + //Hours
507:                        06 * 60 * 1000L + //minutes
508:                        22 * 1000L //seconds
509:                );
510:
511:                RowDecorator dec = new RowDecorator(map);
512:                dec.setRow(new SimpleRow(new Object[] { "MINUTE", t1, t2 }));
513:                Long result = new Long((t2.getTime() - t1.getTime())
514:                        / (60 * 1000));
515:                assertEquals(result, function.evaluate(dec));
516:            }
517:
518:            public void testSecondsBetweenDates() throws Exception {
519:                DateDiffFunction function = new DateDiffFunction();
520:                ColumnIdentifier intervalType = new ColumnIdentifier(
521:                        "intervalType");
522:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
523:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
524:
525:                function.addArgument(intervalType);
526:                function.addArgument(timestamp1);
527:                function.addArgument(timestamp2);
528:
529:                HashMap map = new HashMap();
530:                map.put(intervalType, new Integer(0));
531:                map.put(timestamp1, new Integer(1));
532:                map.put(timestamp2, new Integer(2));
533:
534:                //May 15 2000 12:04:22 GMT
535:                Timestamp t1 = new Timestamp(30 * 365 * 24 * 60 * 60 * 1000L + //year
536:                        8 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96, 2000
537:                        (31 + 28 + 31 + 30 + 15) * 24 * 60 * 60 * 1000L + // May 15
538:                        12 * 60 * 60 * 1000L + //Hours
539:                        04 * 60 * 1000L + //minutes
540:                        22 * 1000L //seconds
541:                );
542:
543:                //April 23 2000 11:06:22  GMT
544:                Timestamp t2 = new Timestamp(30 * 365 * 24 * 60 * 60 * 1000L + //year
545:                        8 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000
546:                        (31 + 28 + 31 + 23) * 24 * 60 * 60 * 1000L + //April 23
547:                        11 * 60 * 60 * 1000L + //Hours
548:                        06 * 60 * 1000L + //minutes
549:                        22 * 1000L //seconds
550:                );
551:
552:                RowDecorator dec = new RowDecorator(map);
553:                dec.setRow(new SimpleRow(new Object[] { "SECOND", t1, t2 }));
554:                Long result = new Long((t2.getTime() - t1.getTime()) / (1000));
555:                assertEquals(result, function.evaluate(dec));
556:            }
557:
558:            public void testMilliSecondsBetweenDates() throws Exception {
559:                DateDiffFunction function = new DateDiffFunction();
560:                ColumnIdentifier intervalType = new ColumnIdentifier(
561:                        "intervalType");
562:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
563:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
564:
565:                function.addArgument(intervalType);
566:                function.addArgument(timestamp1);
567:                function.addArgument(timestamp2);
568:
569:                HashMap map = new HashMap();
570:                map.put(intervalType, new Integer(0));
571:                map.put(timestamp1, new Integer(1));
572:                map.put(timestamp2, new Integer(2));
573:
574:                //      April 15 2000 12:04:22 GMT
575:                Timestamp t1 = new Timestamp(30 * 365 * 24 * 60 * 60 * 1000L + //year
576:                        8 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96, 2000
577:                        (31 + 28 + 31 + 15) * 24 * 60 * 60 * 1000L + // April 15
578:                        12 * 60 * 60 * 1000L + //Hours
579:                        04 * 60 * 1000L + //minutes
580:                        22 * 1000L //seconds
581:                );
582:
583:                //April 23 2000 11:06:22  GMT
584:                Timestamp t2 = new Timestamp(30 * 365 * 24 * 60 * 60 * 1000L + //year
585:                        8 * 24 * 60 * 60 * 1000L + // leap years '72,'76,'80,'84,'88,'92,'96,2000
586:                        (31 + 28 + 31 + 23) * 24 * 60 * 60 * 1000L + //April 23
587:                        11 * 60 * 60 * 1000L + //Hours
588:                        06 * 60 * 1000L + //minutes
589:                        22 * 1000L //seconds
590:                );
591:
592:                RowDecorator dec = new RowDecorator(map);
593:                dec
594:                        .setRow(new SimpleRow(new Object[] { "MILLISECOND", t1,
595:                                t2 }));
596:                Long result = new Long(t2.getTime() - t1.getTime());
597:                assertEquals(result, function.evaluate(dec));
598:            }
599:
600:            public void testNullTimestampInputYieldsNull() {
601:                DateDiffFunction function = new DateDiffFunction();
602:                ColumnIdentifier intervalType = new ColumnIdentifier(
603:                        "intervalType");
604:                ColumnIdentifier timestamp1 = new ColumnIdentifier("timestamp1");
605:                ColumnIdentifier timestamp2 = new ColumnIdentifier("timestamp2");
606:
607:                function.addArgument(intervalType);
608:                function.addArgument(timestamp1);
609:                function.addArgument(timestamp2);
610:
611:                HashMap map = new HashMap();
612:                map.put(intervalType, new Integer(0));
613:                map.put(timestamp1, new Integer(1));
614:                map.put(timestamp2, new Integer(2));
615:
616:                //August 15 1947  GMT
617:                final Timestamp ts = new Timestamp(-1
618:                        * (23 * 365 * 24 * 60 * 60 * 1000L + //year
619:                                6 * 24 * 60 * 60 * 1000L + // leap years '48,'52,'56,'60,'64,'68,
620:                        (31 + 28 + 31 + 30 + 31 + 30 + 31 + 15) * 24 * 60 * 60
621:                                * 1000L) // August 15
622:                );
623:
624:                RowDecorator dec = new RowDecorator(map);
625:                dec.setRow(new SimpleRow(new Object[] { "DAY", ts, null }));
626:                try {
627:                    assertNull(
628:                            "Expected null return for DateDiff with null input for t2.",
629:                            function.evaluate(dec));
630:                } catch (Exception e) {
631:                    fail("Null for input t2 of DateDiff should not have thrown an Exception: "
632:                            + e);
633:                }
634:                // Now test with reversed inputs:  t1 = null, t2 = (some valid timestamp)
635:                dec.setRow(new SimpleRow(new Object[] { "DAY", null, ts }));
636:                try {
637:                    assertNull(
638:                            "Expected null return for DateDiff with null input for t1.",
639:                            function.evaluate(dec));
640:                } catch (Exception e) {
641:                    fail("Null for input t1 of DateDiff should not have thrown an Exception: "
642:                            + e);
643:                }
644:
645:                // Now test with both inputs null
646:                dec.setRow(new SimpleRow(new Object[] { "DAY", null, null }));
647:                try {
648:                    assertNull(
649:                            "Expected null return for DateDiff with both inputs null.",
650:                            function.evaluate(dec));
651:                } catch (Exception e) {
652:                    fail("Nulls for both inputs of DateDiff should not have thrown an Exception: "
653:                            + e);
654:                }
655:            }
656:
657:            public void testInvalidArguments() {
658:                DateDiffFunction function = new DateDiffFunction();
659:                ColumnIdentifier intervalType = new ColumnIdentifier(
660:                        "intervalType");
661:                ColumnIdentifier interval = new ColumnIdentifier("interval");
662:                ColumnIdentifier timestamp = new ColumnIdentifier("timestamp");
663:
664:                function.addArgument(intervalType);
665:                function.addArgument(interval);
666:                function.addArgument(timestamp);
667:
668:                HashMap map = new HashMap();
669:                map.put(intervalType, new Integer(0));
670:                map.put(interval, new Integer(1));
671:                map.put(timestamp, new Integer(2));
672:
673:                RowDecorator dec = new RowDecorator(map);
674:                Date input = new Date(DateType.normalizeToUTCZeroHour(System
675:                        .currentTimeMillis()));
676:                dec.setRow(new SimpleRow(
677:                        new Object[] { "DAY", "integer", input }));
678:                try {
679:                    function.evaluate(dec);
680:                    fail("Expected conversion error");
681:                } catch (Exception e) {
682:                    // Expected
683:                }
684:
685:                dec.setRow(new SimpleRow(new Object[] { "DAY",
686:                        new Object[] { "integer" }, input }));
687:                try {
688:                    function.evaluate(dec);
689:                    fail("Expected conversion error");
690:                } catch (Exception e) {
691:                    // Expected
692:                }
693:
694:                dec.setRow(new SimpleRow(new Object[] { "DAY", new Integer(1),
695:                        "input" }));
696:                try {
697:                    function.evaluate(dec);
698:                    fail("Expected conversion error");
699:                } catch (Exception e) {
700:                    // Expected
701:                }
702:
703:                dec.setRow(new SimpleRow(new Object[] { "DAY", new Integer(1),
704:                        new Object[] { "input" } }));
705:                try {
706:                    function.evaluate(dec);
707:                    fail("Expected conversion error");
708:                } catch (Exception e) {
709:                    // Expected
710:                }
711:
712:            }
713:
714:            public void testMakeNewInstance() {
715:                DateDiffFunction function = new DateDiffFunction();
716:                assertTrue(function.makeNewInstance() instanceof  DateDiffFunction);
717:                assertTrue(function.makeNewInstance() != function
718:                        .makeNewInstance());
719:            }
720:
721:            public void testInvalid() throws Exception {
722:                DateDiffFunction function = new DateDiffFunction();
723:                assertTrue(!function.isValid());
724:            }
725:
726:            public void testValid() throws Exception {
727:                DateDiffFunction function = new DateDiffFunction();
728:                function.addArgument(new ColumnIdentifier("foo"));
729:                function.addArgument(new ColumnIdentifier("bar"));
730:                function.addArgument(new ColumnIdentifier("foobar"));
731:                assertTrue(function.isValid());
732:            }
733:
734:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.