alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (SqlDateTest.groovy)

This example Groovy source code file (SqlDateTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

groovytestcase, groovytestcase, sqldatetest, sqldatetest

The Groovy SqlDateTest.groovy source code

package groovy;

import groovy.time.TimeCategory

class SqlDateTest extends GroovyTestCase {

    void testIncrement() {
        def nowMillis = System.currentTimeMillis()
        def sqlDate = new java.sql.Date(nowMillis)
        def nowOffset = TimeCategory.getDaylightSavingsOffset(sqlDate).toMilliseconds()
        sqlDate++
        def incOffset = TimeCategory.getDaylightSavingsOffset(sqlDate).toMilliseconds()
        assertTrue "incrementing a java.sql.Date returned an incorrect type: ${sqlDate.class}", sqlDate instanceof java.sql.Date

        // difference adjusted for daylight savings
        def diff = (sqlDate.getTime() + incOffset) - (nowMillis + nowOffset)
        assertEquals "incrementing a java.sql.Date did not work properly", 1000 * 60 * 60 * 24, diff
    }

      void testDecrement() {
        def nowMillis = System.currentTimeMillis()
        def sqlDate = new java.sql.Date(nowMillis)
        def nowOffset = TimeCategory.getDaylightSavingsOffset(sqlDate).toMilliseconds()
        sqlDate--
        def decOffset = TimeCategory.getDaylightSavingsOffset(sqlDate).toMilliseconds()
        assertTrue "decrementing a java.sql.Date returned an incorrect type: ${sqlDate.class}", sqlDate instanceof java.sql.Date
        
        // difference adjusted for daylight savings
        def diff = (nowMillis + nowOffset) - (sqlDate.getTime() + decOffset)

        assertEquals "decrementing a java.sql.Date did not work properly", 1000 * 60 * 60 * 24, diff
    }
      
    void testPlusOperator() {
        def nowMillis = System.currentTimeMillis()
        def sqlDate = new java.sql.Date(nowMillis)
        def nowOffset = TimeCategory.getDaylightSavingsOffset(sqlDate).toMilliseconds()
        sqlDate += 1
        def incOffset = TimeCategory.getDaylightSavingsOffset(sqlDate).toMilliseconds()

        assertTrue  "the plus operator applied to a java.sql.Date returned an incorrect type: ${sqlDate.class}", sqlDate instanceof java.sql.Date
        
        // difference adjusted for daylight savings
        def diff = (sqlDate.getTime() + incOffset) - (nowMillis + nowOffset)
        assertEquals "decrementing a java.sql.Date did not work properly", 1000 * 60 * 60 * 24, diff
    }
    
    void testMinusOperator() {
        def nowMillis = System.currentTimeMillis()
        def sqlDate = new java.sql.Date(nowMillis)
        def nowOffset = TimeCategory.getDaylightSavingsOffset(sqlDate).toMilliseconds()
        sqlDate -= 1
        def decOffset = TimeCategory.getDaylightSavingsOffset(sqlDate).toMilliseconds()

        assertTrue  "the minus operator applied to a java.sql.Date returned an incorrect type: ${sqlDate.class}", sqlDate instanceof java.sql.Date
        
        // difference adjusted for daylight savings
        def diff = (nowMillis + nowOffset) - (sqlDate.getTime() + decOffset)
        assertEquals "decrementing a java.sql.Date did not work properly", 1000 * 60 * 60 * 24, diff
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy SqlDateTest.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.