[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jibx-cvs
Subject:    [Jibx-cvs] core/build/test/org/jibx/runtime JodaConvertTest.java,
From:       Dennis Sosnoski <dsosnoski () users ! sourceforge ! net>
Date:       2009-04-16 11:32:15
Message-ID: E1LuPpH-0003F5-I3 () fdv4jf1 ! ch3 ! sourceforge ! com
[Download RAW message or body]

Update of /cvsroot/jibx/core/build/test/org/jibx/runtime
In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv12460/test/org/jibx/runtime

Modified Files:
	RuntimeSuite.java 
Added Files:
	JodaConvertTest.java 
Log Message:
Added Joda time conversion tests.

--- NEW FILE: JodaConvertTest.java ---
/*
Copyright (c) 2009, Dennis M. Sosnoski. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
 * Neither the name of JiBX nor the names of its contributors may be used
   to endorse or promote products derived from this software without specific
   prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.jibx.runtime;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import junit.framework.TestCase;

/**
 * Joda date/time conversion tests.
 *
 * @author Dennis M. Sosnoski
 */
public class JodaConvertTest extends TestCase
{
    private static int OFFSET_HOURS = 13;
    private static int OFFSET_MILLIS = OFFSET_HOURS*60*60*1000;
    private static final String OFFSET_TEXT = "+" + OFFSET_HOURS + ":00";
    
    protected void setUp() throws Exception {
        DateTimeZone.setDefault(DateTimeZone.forOffsetHours(OFFSET_HOURS));
    }

    public void testDeserializeLocalDate() throws JiBXException {
        assertNull("Null input", JodaConvert.deserializeLocalDate(null));
        LocalDate date = JodaConvert.deserializeLocalDate("2008-02-28");
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 28, date.getDayOfMonth());
        date = JodaConvert.deserializeLocalDate("2008-02-29Z");
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 29, date.getDayOfMonth());
        date = JodaConvert.deserializeLocalDate("2008-02-29-24:00");
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 29, date.getDayOfMonth());
        try {
            JodaConvert.deserializeUTCDateTime("2007-02-29");
            fail("Invalid day number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateTime("2007-13-29");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateTime("2007-00-29");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
    }
    
    public void testSerializeLocalDate() throws JiBXException {
        assertEquals("2008-02-28", JodaConvert.serializeLocalDate(new LocalDate(2008, \
                2, 28)));
        assertEquals("2008-02-29", JodaConvert.serializeLocalDate(new LocalDate(2008, \
                2, 29)));
        assertEquals("2008-03-01", JodaConvert.serializeLocalDate(new LocalDate(2008, \
3, 1)));  }
    
    public void testDeserializeDefaultDateMidnight() throws JiBXException {
        assertNull("Null input", JodaConvert.deserializeDefaultDateMidnight(null));
        DateMidnight date = JodaConvert.deserializeDefaultDateMidnight("2008-02-28");
        DateTimeZone zone = date.getZone();
        assertEquals(OFFSET_MILLIS, zone.getOffset(date));
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 28, date.getDayOfMonth());
        assertEquals("Wrong value", 0, date.getHourOfDay());
        assertEquals("Wrong value", 0, date.getMinuteOfHour());
        assertEquals("Wrong value", 0, date.getSecondOfMinute());
        date = JodaConvert.deserializeDefaultDateMidnight("2008-02-29Z");
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 29, date.getDayOfMonth());
        assertEquals("Wrong value", 0, date.getHourOfDay());
        assertEquals("Wrong value", 0, date.getMinuteOfHour());
        assertEquals("Wrong value", 0, date.getSecondOfMinute());
        date = JodaConvert.deserializeDefaultDateMidnight("2008-02-29-24:00");
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 29, date.getDayOfMonth());
        assertEquals("Wrong value", 0, date.getHourOfDay());
        assertEquals("Wrong value", 0, date.getMinuteOfHour());
        assertEquals("Wrong value", 0, date.getSecondOfMinute());
        try {
            JodaConvert.deserializeDefaultDateMidnight("2007-02-29");
            fail("Invalid day number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeDefaultDateMidnight("2007-13-29");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeDefaultDateMidnight("2007-00-29");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
    }
    
    public void testDeserializeUTCDateMidnight() throws JiBXException {
        assertNull("Null input", JodaConvert.deserializeUTCDateMidnight(null));
        DateMidnight date = JodaConvert.deserializeUTCDateMidnight("2008-02-28");
        DateTimeZone zone = date.getZone();
        assertEquals("UTC timezone expected", "UTC", zone.getID());
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 28, date.getDayOfMonth());
        assertEquals("Wrong value", 0, date.getHourOfDay());
        assertEquals("Wrong value", 0, date.getMinuteOfHour());
        assertEquals("Wrong value", 0, date.getSecondOfMinute());
        date = JodaConvert.deserializeUTCDateMidnight("2008-02-29Z");
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 29, date.getDayOfMonth());
        assertEquals("Wrong value", 0, date.getHourOfDay());
        assertEquals("Wrong value", 0, date.getMinuteOfHour());
        assertEquals("Wrong value", 0, date.getSecondOfMinute());
        date = JodaConvert.deserializeUTCDateMidnight("2008-02-29-24:00");
        assertEquals("Wrong value", 2008, date.getYear());
        assertEquals("Wrong value", 2, date.getMonthOfYear());
        assertEquals("Wrong value", 29, date.getDayOfMonth());
        assertEquals("Wrong value", 0, date.getHourOfDay());
        assertEquals("Wrong value", 0, date.getMinuteOfHour());
        assertEquals("Wrong value", 0, date.getSecondOfMinute());
        try {
            JodaConvert.deserializeUTCDateMidnight("2007-02-29");
            fail("Invalid day number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateMidnight("2007-13-29");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateMidnight("2007-00-29");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
    }
    
    public void testSerializeUnzonedDateMidnight() throws JiBXException {
        assertEquals("2008-02-28", JodaConvert.serializeUnzonedDateMidnight(new \
                DateMidnight(2008, 2, 28)));
        assertEquals("2008-02-29", JodaConvert.serializeUnzonedDateMidnight(new \
                DateMidnight(2008, 2, 29)));
        assertEquals("2008-03-01", JodaConvert.serializeUnzonedDateMidnight(new \
DateMidnight(2008, 3, 1)));  }
    
    public void testSerializeZonedDateMidnight() throws JiBXException {
        assertEquals("2008-02-28" + OFFSET_TEXT, \
                JodaConvert.serializeZonedDateMidnight(new DateMidnight(2008, 2, \
                28)));
        assertEquals("2008-02-29" + OFFSET_TEXT, \
                JodaConvert.serializeZonedDateMidnight(new DateMidnight(2008, 2, \
                29)));
        assertEquals("2008-03-01" + OFFSET_TEXT, \
JodaConvert.serializeZonedDateMidnight(new DateMidnight(2008, 3, 1)));  }
    
    public void testSerializeUTCDateMidnight() throws JiBXException {
        assertEquals("2008-02-28Z", JodaConvert.serializeUTCDateMidnight(new \
                DateMidnight(2008, 2, 28)));
        assertEquals("2008-02-29Z", JodaConvert.serializeUTCDateMidnight(new \
                DateMidnight(2008, 2, 29)));
        assertEquals("2008-03-01Z", JodaConvert.serializeUTCDateMidnight(new \
                DateMidnight(2008, 3, 1)));
        assertEquals("2008-02-28Z", JodaConvert.serializeUTCDateMidnight(new \
                DateMidnight(2008, 2, 28, DateTimeZone.UTC)));
        assertEquals("2008-02-29Z", JodaConvert.serializeUTCDateMidnight(new \
                DateMidnight(2008, 2, 29, DateTimeZone.UTC)));
        assertEquals("2008-03-01Z", JodaConvert.serializeUTCDateMidnight(new \
DateMidnight(2008, 3, 1, DateTimeZone.UTC)));  }
    
    public void testDeserializeLocalTime() throws JiBXException {
        assertNull("Null input", JodaConvert.deserializeLocalTime(null));
        LocalTime time = JodaConvert.deserializeLocalTime("01:02:03Z");
        assertEquals("Wrong value", 1, time.getHourOfDay());
        assertEquals("Wrong value", 2, time.getMinuteOfHour());
        assertEquals("Wrong value", 3, time.getSecondOfMinute());
        time = JodaConvert.deserializeLocalTime("02:05:06.123-02:00");
        assertEquals("Wrong value", 2, time.getHourOfDay());
        assertEquals("Wrong value", 5, time.getMinuteOfHour());
        assertEquals("Wrong value", 6, time.getSecondOfMinute());
        assertEquals("Wrong value", 123, time.getMillisOfSecond());
        time = JodaConvert.deserializeLocalTime("02:05:06.123");
        assertEquals("Wrong value", 2, time.getHourOfDay());
        assertEquals("Wrong value", 5, time.getMinuteOfHour());
        assertEquals("Wrong value", 6, time.getSecondOfMinute());
        assertEquals("Wrong value", 123, time.getMillisOfSecond());
        try {
            JodaConvert.deserializeLocalTime("24:05:06.123-02:00");
            fail("Invalid hour number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeLocalTime("02:60:47.123-02:00");
            fail("Invalid minute number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeLocalTime("02:05:61.123-02:00");
            fail("Invalid second number");
        } catch (JiBXException ex) {}
    }
    
    public void testSerializeUnzonedLocalTime() throws JiBXException {
        assertEquals("01:02:03", JodaConvert.serializeUnzonedLocalTime(new \
                LocalTime(1, 2, 3)));
        assertEquals("23:59:59", JodaConvert.serializeUnzonedLocalTime(new \
                LocalTime(23, 59, 59)));
        assertEquals("00:00:00.123", JodaConvert.serializeUnzonedLocalTime(new \
LocalTime(0, 0, 0, 123)));  }
    
    public void testSerializeUTCLocalTime() throws JiBXException {
        assertEquals("01:02:03Z", JodaConvert.serializeUTCLocalTime(new LocalTime(1, \
                2, 3)));
        assertEquals("23:59:59Z", JodaConvert.serializeUTCLocalTime(new LocalTime(23, \
                59, 59)));
        assertEquals("00:00:00.123Z", JodaConvert.serializeUTCLocalTime(new \
LocalTime(0, 0, 0, 123)));  }
    
    public void testDeserializeUTCDateTime() throws JiBXException {
        assertNull("Null input", JodaConvert.deserializeUTCDateTime(null));
        DateTime time = JodaConvert.deserializeUTCDateTime("2008-02-28T01:02:03Z");
        DateTimeZone zone = time.getZone();
        assertEquals("UTC timezone expected", "UTC", zone.getID());
        assertEquals("Wrong value", 2008, time.getYear());
        assertEquals("Wrong value", 2, time.getMonthOfYear());
        assertEquals("Wrong value", 28, time.getDayOfMonth());
        assertEquals("Wrong value", 1, time.getHourOfDay());
        assertEquals("Wrong value", 2, time.getMinuteOfHour());
        assertEquals("Wrong value", 3, time.getSecondOfMinute());
        time = JodaConvert.deserializeUTCDateTime("2008-02-29T02:05:06.123-02:00");
        assertEquals("Wrong value", 2008, time.getYear());
        assertEquals("Wrong value", 2, time.getMonthOfYear());
        assertEquals("Wrong value", 29, time.getDayOfMonth());
        assertEquals("Wrong value", 4, time.getHourOfDay());
        assertEquals("Wrong value", 5, time.getMinuteOfHour());
        assertEquals("Wrong value", 6, time.getSecondOfMinute());
        assertEquals("Wrong value", 123, time.getMillisOfSecond());
        try {
            JodaConvert.deserializeUTCDateTime("2007-02-29T02:05:06.123-02:00");
            fail("Invalid day number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateTime("2007-13-29T02:05:06.123-02:00");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateTime("2007-00-29T02:05:06.123-02:00");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateTime("2007-00-29T24:05:06.123-02:00");
            fail("Invalid hour number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateTime("2007-00-29T02:60:60.123-02:00");
            fail("Invalid minute number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateTime("2007-00-29T02:05:60.123-02:00");
            fail("Invalid second number");
        } catch (JiBXException ex) {}
    }
    
    public void testDeserializeDefaultDateTime() throws JiBXException {
        assertNull("Null input", JodaConvert.deserializeDefaultDateTime(null));
        DateTime time = \
JodaConvert.deserializeDefaultDateTime("2008-02-28T01:02:03Z");  DateTimeZone zone = \
time.getZone();  assertEquals(OFFSET_MILLIS, zone.getOffset(time));
        assertEquals("Wrong value", 2008, time.getYear());
        assertEquals("Wrong value", 2, time.getMonthOfYear());
        if (OFFSET_HOURS < -1) {
            assertEquals("Wrong value", 27, time.getDayOfMonth());
            assertEquals("Wrong value", 25+OFFSET_HOURS, time.getHourOfDay());
        } else {
            assertEquals("Wrong value", 28, time.getDayOfMonth());
            assertEquals("Wrong value", 1+OFFSET_HOURS, time.getHourOfDay());
        }
        assertEquals("Wrong value", 2, time.getMinuteOfHour());
        assertEquals("Wrong value", 3, time.getSecondOfMinute());
        time = JodaConvert.deserializeDefaultDateTime("2008-02-29T02:05:06.123-02:00");
  assertEquals("Wrong value", 2008, time.getYear());
        assertEquals("Wrong value", 2, time.getMonthOfYear());
        if (OFFSET_HOURS < -4) {
            assertEquals("Wrong value", 28, time.getDayOfMonth());
            assertEquals("Wrong value", 28+OFFSET_HOURS, time.getHourOfDay());
        } else {
            assertEquals("Wrong value", 29, time.getDayOfMonth());
            assertEquals("Wrong value", 4+OFFSET_HOURS, time.getHourOfDay());
        }
        assertEquals("Wrong value", 5, time.getMinuteOfHour());
        assertEquals("Wrong value", 6, time.getSecondOfMinute());
        assertEquals("Wrong value", 123, time.getMillisOfSecond());
        try {
            JodaConvert.deserializeDefaultDateTime("2007-02-29T02:05:06.123-02:00");
            fail("Invalid day number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeDefaultDateTime("2007-13-29T02:05:06.123-02:00");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeDefaultDateTime("2007-00-29T02:05:06.123-02:00");
            fail("Invalid month number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeUTCDateTime("2007-00-29T24:05:06.123-02:00");
            fail("Invalid hour number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeDefaultDateTime("2007-00-29T02:58:60.123-02:00");
            fail("Invalid minute number");
        } catch (JiBXException ex) {}
        try {
            JodaConvert.deserializeDefaultDateTime("2007-00-29T02:05:60.123-02:00");
            fail("Invalid second number");
        } catch (JiBXException ex) {}
    }
}
Index: RuntimeSuite.java
===================================================================
RCS file: /cvsroot/jibx/core/build/test/org/jibx/runtime/RuntimeSuite.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** RuntimeSuite.java	28 Aug 2008 06:39:47 -0000	1.1
--- RuntimeSuite.java	16 Apr 2009 11:32:13 -0000	1.2
***************
*** 10,13 ****
--- 10,14 ----
          TestSuite suite = new TestSuite();
          suite.addTestSuite(UtilityTest.class);
+         suite.addTestSuite(JodaConvertTest.class);
          suite.addTestSuite(JiBXConstrainedParseExceptionTest.class);
          return suite;


------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Jibx-cvs mailing list
Jibx-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-cvs


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic