PageRenderTime 48ms CodeModel.GetById 9ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/util/AbsoluteSMPPDateTest.java

http://mobicents.googlecode.com/
Java | 144 lines | 103 code | 16 blank | 25 comment | 3 complexity | 64d168eadba985924e400fb646c476db MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.smpp.util;
 24
 25import static org.testng.Assert.assertEquals;
 26import static org.testng.Assert.assertFalse;
 27import static org.testng.Assert.assertTrue;
 28
 29import java.util.Calendar;
 30import java.util.Date;
 31import java.util.GregorianCalendar;
 32import java.util.SimpleTimeZone;
 33import java.util.TimeZone;
 34
 35import org.mobicents.protocols.smpp.util.SMPPDate;
 36import org.testng.annotations.Test;
 37
 38@Test
 39public class AbsoluteSMPPDateTest {
 40
 41    private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
 42    
 43    public void testAbsoluteDateInUTC() {
 44        Calendar cal = new GregorianCalendar();
 45        Date now = new Date();
 46        cal.setTime(now);
 47        cal.setTimeZone(UTC);
 48        
 49        SMPPDate d = SMPPDate.getAbsoluteInstance(cal, true);
 50        assertFalse(d.isRelative());
 51        assertTrue(d.isAbsolute());
 52        assertEquals(d.getYear(), cal.get(Calendar.YEAR));
 53        assertEquals(d.getMonth(), cal.get(Calendar.MONTH) + 1);
 54        assertEquals(d.getDay(), cal.get(Calendar.DAY_OF_MONTH));
 55        assertEquals(d.getHour(), cal.get(Calendar.HOUR_OF_DAY));
 56        assertEquals(d.getMinute(), cal.get(Calendar.MINUTE));
 57        assertEquals(d.getSecond(), cal.get(Calendar.SECOND));
 58        assertEquals(d.getTenth(), cal.get(Calendar.MILLISECOND) / 100);
 59        assertEquals(d.getTimeZone(), cal.getTimeZone());
 60        assertEquals(d.getSign(), '+');
 61    }
 62    
 63    public void testDaylightSavingsTimeZone() throws Exception {
 64        // Create two similar time zones, each one is 1 hour behind
 65        // UTC in standard time and one hour ahead of UTC in daylight
 66        // savings. One of them will be in daylight savings "now", the
 67        // other will not.
 68        Calendar dstStart = Calendar.getInstance();
 69        dstStart.add(Calendar.MONTH, -2);
 70        Calendar dstEnd = Calendar.getInstance();
 71        dstEnd.add(Calendar.MONTH, 4);
 72        SimpleTimeZone aheadInDst = new SimpleTimeZone(
 73                -3600000,
 74                "AheadInDst",
 75                dstStart.get(Calendar.MONTH),
 76                dstStart.get(Calendar.DAY_OF_WEEK),
 77                -1,
 78                3600000,
 79                dstEnd.get(Calendar.MONTH),
 80                dstEnd.get(Calendar.DAY_OF_WEEK),
 81                -1,
 82                3600000,
 83                7200000);
 84        SimpleTimeZone behindWhenNotInDst = new SimpleTimeZone(
 85                -3600000,
 86                "BehindNotInDst",
 87                dstEnd.get(Calendar.MONTH),
 88                dstEnd.get(Calendar.DAY_OF_WEEK),
 89                -1,
 90                3600000,
 91                dstStart.get(Calendar.MONTH),
 92                dstStart.get(Calendar.DAY_OF_WEEK),
 93                -1,
 94                3600000,
 95                7200000);
 96
 97        assertTrue(aheadInDst.inDaylightTime(new Date()));
 98        assertFalse(behindWhenNotInDst.inDaylightTime(new Date()));
 99        assertTrue(aheadInDst.getRawOffset() < 0);
100        assertTrue(behindWhenNotInDst.getRawOffset() < 0);
101        assertTrue(aheadInDst.getOffset(System.currentTimeMillis()) > 0);
102        assertTrue(behindWhenNotInDst.getOffset(System.currentTimeMillis()) < 0);
103        
104        Calendar now = Calendar.getInstance(aheadInDst);
105        SMPPDate date = SMPPDate.getAbsoluteInstance(now, true);
106        assertTrue(date.hasTimezone());
107        assertEquals(date.getUtcOffset(), 4);
108        assertEquals(date.getSign(), '+');
109        
110        now = Calendar.getInstance(behindWhenNotInDst);
111        date = SMPPDate.getAbsoluteInstance(now, true);
112        assertTrue(date.hasTimezone());
113        assertEquals(date.getUtcOffset(), 4);
114        assertEquals(date.getSign(), '-');
115    }
116    
117    public void testEqualsAndHashCode() {
118        Calendar now = new GregorianCalendar();
119        Calendar other = new GregorianCalendar();
120        other.set(Calendar.YEAR, 1956);
121        SMPPDate now1WithTz = SMPPDate.getAbsoluteInstance(now);
122        SMPPDate now2WithTz = SMPPDate.getAbsoluteInstance(now);
123        SMPPDate now3NoTz = SMPPDate.getAbsoluteInstance(now, false);
124        SMPPDate otherWithTz = SMPPDate.getAbsoluteInstance(other);
125
126        assertEquals(now1WithTz, now1WithTz);
127        assertEquals(now2WithTz, now2WithTz);
128        assertEquals(now2WithTz, now1WithTz);
129        assertEquals(now1WithTz, now2WithTz);
130        assertEquals(now2WithTz.hashCode(), now1WithTz.hashCode());
131
132        assertEquals(now3NoTz, now3NoTz);
133        assertFalse(now1WithTz.equals(now3NoTz));
134        assertFalse(now3NoTz.equals(now2WithTz));
135        assertFalse(now3NoTz.hashCode() == now1WithTz.hashCode());
136        
137        assertFalse(now1WithTz.equals(otherWithTz));
138        assertFalse(now2WithTz.equals(otherWithTz));
139        assertFalse(otherWithTz.equals(now1WithTz));
140        assertFalse(otherWithTz.equals(now2WithTz));
141        assertFalse(now1WithTz.hashCode() == otherWithTz.hashCode());
142        assertFalse(now2WithTz.hashCode() == otherWithTz.hashCode());
143    }
144}