Package ch.pollet.jzic.datetime

Source Code of ch.pollet.jzic.datetime.DateTimeTest

package ch.pollet.jzic.datetime;

import ch.pollet.jzic.datetime.DateTimeException;
import ch.pollet.jzic.datetime.DateTime;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author Christophe Pollet
*/
public class DateTimeTest {
    @Test
    public void testCompareTo() {
        DateTime dt1 = new DateTime();
        DateTime dt2 = new DateTime();

        dt1.setYear(2009);
        dt1.setMonth(9);
        dt1.setDay(17);
        dt1.setHour(12);
        dt1.setMinute(00);
        dt1.setSecond(00);
        dt1.setType(DateTime.Type.UTC);

        dt2.setYear(2008);
        dt2.setMonth(9);
        dt2.setDay(17);
        dt2.setHour(12);
        dt2.setMinute(00);
        dt2.setSecond(00);
        dt2.setType(DateTime.Type.UTC);

        assertEquals(1, dt1.compareTo(dt2));
        assertEquals(-1, dt2.compareTo(dt1));

        dt2.setYear(2009);
        dt2.setMonth(8);

        assertEquals(1, dt1.compareTo(dt2));
        assertEquals(-1, dt2.compareTo(dt1));

        dt2.setMonth(9);

        assertEquals(0, dt1.compareTo(dt2));
        assertEquals(0, dt2.compareTo(dt1));

        try {
            dt1.compareTo(null);
            fail("Expected ClassCastException");
        } catch (ClassCastException ex) {
        }

        try {
            dt1.compareTo(new Object());
            fail("Expected ClassCastException");
        } catch (ClassCastException ex) {
        }
    }

    @Test
    public void testEquals() {
        DateTime dt1 = new DateTime();
        DateTime dt2 = new DateTime();
        Object o = new Object();

        assertTrue(dt1.equals(dt1));
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        assertFalse(dt1.equals(o));
        assertFalse(dt1.equals(null));

        dt1.setYear(2009);
        dt2.setYear(2008);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        // year
        dt2.setYear(2009);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // month
        dt1.setMonth(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setMonth(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setMonth(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // day
        dt1.setDay(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setDay(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setDay(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // hour
        dt1.setHour(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setHour(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setHour(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // minute
        dt1.setMinute(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setMinute(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setMinute(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // second
        dt1.setSecond(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setSecond(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setSecond(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));
    }

    @Test
    public void testHashCode() {
        DateTime dt1 = new DateTime();
        DateTime dt2 = new DateTime();

        dt1.setYear(2009);
        dt1.setMonth(9);
        dt1.setDay(19);
        dt1.setHour(19);
        dt1.setMinute(33);
        dt1.setSecond(12);
        dt1.setType(DateTime.Type.WALL);

        dt2.setYear(2009);
        dt2.setMonth(9);
        dt2.setDay(19);
        dt2.setHour(19);
        dt2.setMinute(33);
        dt2.setSecond(12);
        dt2.setType(DateTime.Type.WALL);

        assertEquals(dt1.hashCode(), dt2.hashCode());
    }

    @Test
    public void testHas() {
        DateTime dt = new DateTime();
        assertFalse(dt.hasYear());
        assertFalse(dt.hasMonth());
        assertFalse(dt.hasDay());
        assertFalse(dt.hasHour());
        assertFalse(dt.hasMinute());
        assertFalse(dt.hasSecond());

        dt.setYear(1);
        assertTrue(dt.hasYear());

        dt.setMonth(1);
        assertTrue(dt.hasMonth());

        dt.setDay(1);
        assertTrue(dt.hasDay());

        dt.setHour(1);
        assertTrue(dt.hasHour());

        dt.setMinute(1);
        assertTrue(dt.hasMinute());

        dt.setSecond(1);
        assertTrue(dt.hasSecond());

        dt = new DateTime();

        assertFalse(dt.hasYear());
        assertFalse(dt.hasMonth());
        assertFalse(dt.hasDay());

        dt.setDayBase(1);
        assertFalse(dt.hasDay());

        dt.setDayPosition(DateTime.DayPosition.GE);
        assertFalse(dt.hasDay());

        dt.setDayOfWeek(1);
        assertFalse(dt.hasDay());

        dt.setMonth(1);
        assertFalse(dt.hasDay());

        dt.setYear(1);
        assertTrue(dt.hasDay());
    }

    @Test
    public void testToString() {
        DateTime dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(1);
        dt.setDay(2);
        dt.setHour(3);
        dt.setMinute(4);
        dt.setSecond(5);
        dt.setType(DateTime.Type.WALL);
        assertEquals("2009-01-02T03:04:05W", dt.toString());

        dt.setMonth(10);
        dt.setDay(20);
        dt.setHour(30);
        dt.setMinute(40);
        dt.setSecond(50);
        dt.setType(DateTime.Type.STD);
        assertEquals("2009-10-20T30:40:50S", dt.toString());
    }

    @Test
    public void testToUtc() throws DateTimeException {
        DateTime dt = new DateTime();
        DateTime dtExp = new DateTime();

        // 2009-06-14T12:30:30W --> 2009-06-14T12:30:29U
        dt.setYear(2009);
        dt.setMonth(6);
        dt.setDay(14);
        dt.setHour(12);
        dt.setMinute(30);
        dt.setSecond(30);
        dt.setType(DateTime.Type.WALL);
        dt.toUtc(0, 61);
        dtExp.setYear(2009);
        dtExp.setMonth(6);
        dtExp.setDay(14);
        dtExp.setHour(12);
        dtExp.setMinute(29);
        dtExp.setSecond(29);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);

        // 2009-06-30T23:59:59W --> 2009-07-01T00:00:00U
        dt.setYear(2009);
        dt.setMonth(6);
        dt.setDay(30);
        dt.setHour(23);
        dt.setMinute(59);
        dt.setSecond(59);
        dt.setType(DateTime.Type.WALL);
        dt.toUtc(0, -1);
        dtExp.setYear(2009);
        dtExp.setMonth(7);
        dtExp.setDay(1);
        dtExp.setHour(0);
        dtExp.setMinute(0);
        dtExp.setSecond(0);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);

        // 2009-03-01T00:00:00W --> 2009-02-28T23:59:59Z
        dt.setYear(2009);
        dt.setMonth(3);
        dt.setDay(1);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);
        dt.toUtc(0, 1);
        dtExp.setYear(2009);
        dtExp.setMonth(2);
        dtExp.setDay(28);
        dtExp.setHour(23);
        dtExp.setMinute(59);
        dtExp.setSecond(59);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);

        // 2008-03-01T00:00:00W --> 2008-02-28T23:59:59Z
        dt.setYear(2008);
        dt.setMonth(3);
        dt.setDay(1);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);
        dt.toUtc(0, 1);
        dtExp.setYear(2008);
        dtExp.setMonth(2);
        dtExp.setDay(29);
        dtExp.setHour(23);
        dtExp.setMinute(59);
        dtExp.setSecond(59);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);

        // 2000-03-01T00:00:00W --> 2000-02-28T23:59:59Z
        dt.setYear(2000);
        dt.setMonth(3);
        dt.setDay(1);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);
        dt.toUtc(0, 1);
        dtExp.setYear(2000);
        dtExp.setMonth(2);
        dtExp.setDay(29);
        dtExp.setHour(23);
        dtExp.setMinute(59);
        dtExp.setSecond(59);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);

        // 1900-03-01T00:00:00W --> 1900-02-28T23:59:59Z
        dt.setYear(1900);
        dt.setMonth(3);
        dt.setDay(1);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);
        dt.toUtc(0, 1);
        dtExp.setYear(1900);
        dtExp.setMonth(2);
        dtExp.setDay(28);
        dtExp.setHour(23);
        dtExp.setMinute(59);
        dtExp.setSecond(59);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);

        // 2000-03-30T23:59:59S --> 2000-03-31T00:00:00Z
        dt.setYear(2000);
        dt.setMonth(3);
        dt.setDay(30);
        dt.setHour(23);
        dt.setMinute(59);
        dt.setSecond(59);
        dt.setType(DateTime.Type.STD);
        dt.toUtc(-1, 0);
        dtExp.setYear(2000);
        dtExp.setMonth(3);
        dtExp.setDay(31);
        dtExp.setHour(0);
        dtExp.setMinute(0);
        dtExp.setSecond(0);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);

        // 2000-12-29T12:00:00W --> 2001-01-01T12:00:00Z
        dt.setYear(2000);
        dt.setMonth(12);
        dt.setDay(29);
        dt.setHour(12);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.STD);
        dt.toUtc(-259200, 0);
        dtExp.setYear(2001);
        dtExp.setMonth(1);
        dtExp.setDay(1);
        dtExp.setHour(12);
        dtExp.setMinute(0);
        dtExp.setSecond(0);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);

        // 2000-01-01T00:00:00W --> 1999-12-12T23:59:59Z
        dt.setYear(2000);
        dt.setMonth(1);
        dt.setDay(1);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.STD);
        dt.toUtc(1, 0);
        dtExp.setYear(1999);
        dtExp.setMonth(12);
        dtExp.setDay(31);
        dtExp.setHour(23);
        dtExp.setMinute(59);
        dtExp.setSecond(59);
        dtExp.setType(DateTime.Type.UTC);
        assertEquals(dtExp, dt);
    }

    @Test
    public void testComputeDayOfMonth() throws DateTimeException {
        DateTime dt = new DateTime();

        // first monday >= 2009-09-01
        dt.setYear(2009);
        dt.setMonth(9);
        dt.setDayOfWeek(1);
        dt.setDayPosition(DateTime.DayPosition.GE);
        dt.setDayBase(1);
        assertEquals(7, dt.getDay().intValue());

        // first monday >= 2009-09-07
        dt.setYear(2009);
        dt.setMonth(9);
        dt.setDayOfWeek(1);
        dt.setDayPosition(DateTime.DayPosition.GE);
        dt.setDayBase(7);
        assertEquals(7, dt.getDay().intValue());

        // first monday <= 2009-09-31
        dt.setYear(2009);
        dt.setMonth(9);
        dt.setDayOfWeek(1);
        dt.setDayPosition(DateTime.DayPosition.LE);
        dt.setDayBase(31);
        assertEquals(28, dt.getDay().intValue());

        // first monday <= 2009-09-28
        dt.setYear(2009);
        dt.setMonth(9);
        dt.setDayOfWeek(1);
        dt.setDayPosition(DateTime.DayPosition.LE);
        dt.setDayBase(31);
        assertEquals(28, dt.getDay().intValue());

        // last sun of sept
        dt.setYear(2009);
        dt.setMonth(9);
        dt.setDayOfWeek(7);
        dt.setDayPosition(DateTime.DayPosition.LE);
        dt.setDayBase(31);
        assertEquals(27, dt.getDay().intValue());
    }

    @Test
    public void testClone() {
        DateTime dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(1);
        dt.setDay(2);
        dt.setHour(3);
        dt.setMinute(4);
        dt.setSecond(5);
        dt.setType(DateTime.Type.WALL);

        DateTime dtCloned = dt.clone();

        assertFalse(dt == dtCloned);
        assertEquals(dt, dtCloned);
    }
}
TOP

Related Classes of ch.pollet.jzic.datetime.DateTimeTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.