PageRenderTime 23ms CodeModel.GetById 9ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 192 lines | 147 code | 17 blank | 28 comment | 6 complexity | d31c4bb9eb8cb278cce589a952bad816 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.fail;
 27
 28import java.text.ParseException;
 29import java.util.Calendar;
 30import java.util.GregorianCalendar;
 31import java.util.SimpleTimeZone;
 32import java.util.TimeZone;
 33
 34import org.mobicents.protocols.smpp.util.SMPPDate;
 35import org.mobicents.protocols.smpp.util.SMPPDateFormat;
 36import org.testng.annotations.Test;
 37
 38@Test
 39public class SMPPDateFormatTest {
 40
 41    private SMPPDateFormat dateFormat = new SMPPDateFormat();
 42    
 43    public void testParse16CharactersAbsoluteFormat() throws Exception {
 44        SMPPDate date =
 45            (SMPPDate) dateFormat.parseObject("061217153023304-");
 46        bothAssertions(date, 2006, 11, 17, 15, 30, 23, 300, -3600000, '-');
 47        date = (SMPPDate) dateFormat.parseObject("010101010101000+");
 48        bothAssertions(date, 2001, 0, 1, 1, 1, 1, 0, 0, '+');
 49        date = (SMPPDate) dateFormat.parseObject("990731235959948+");
 50        bothAssertions(date, 2099, 6, 31, 23, 59, 59, 900, 43200000, '+');
 51    }
 52
 53    public void testParse16CharacterRelativeFormat() throws Exception {
 54        SMPPDate date = (SMPPDate) dateFormat.parseObject("021219212121200R");
 55        dateAssertions(date, 2, 12, 19, 21, 21, 21, 0, 0, 'R');
 56        date = (SMPPDate) dateFormat.parseObject("000000000000000R");
 57        dateAssertions(date, 0, 0, 0, 0, 0, 0, 0, 0, 'R');
 58        date = (SMPPDate) dateFormat.parseObject("999999999999999R");
 59        dateAssertions(date, 99, 99, 99, 99, 99, 99, 0, 0, 'R');
 60    }
 61    
 62    public void testParse12CharacterAbsoluteFormat() throws Exception {
 63        SMPPDate date = (SMPPDate) dateFormat.parseObject("030912131313");
 64        bothAssertions(date, 2003, 8, 12, 13, 13, 13, 0, 0, (char) 0);
 65    }
 66    
 67    public void testParseFailsOnInvalidString() throws Exception {
 68        try {
 69            dateFormat.parseObject(null);
 70            fail("Should not have successfully parsed.");
 71        } catch (NullPointerException x) {
 72            // Pass
 73        }
 74        try {
 75            dateFormat.parseObject("");
 76            fail("Should not have successfully parsed.");
 77        } catch (ParseException x) {
 78            // Pass
 79        }
 80        try {
 81            dateFormat.parseObject("12345");
 82            fail("Should not have successfully parsed.");
 83        } catch (ParseException x) {
 84            // Pass
 85        }
 86        try {
 87            dateFormat.parseObject("abcdefghijklmnop");
 88            fail("Should not have successfully parsed.");
 89        } catch (ParseException x) {
 90            // Pass
 91        }
 92        try {
 93            dateFormat.parseObject("999999999999999-");
 94            fail("Should not have successfully parsed.");
 95        } catch (ParseException x) {
 96            // Pass
 97        }
 98    }
 99
100    public void testFormatAbsolute16() throws Exception {
101        // Get a timezone that is 4 hours behind UTC
102        TimeZone tz = new SimpleTimeZone(-14400000, "UTC-04:00");
103        Calendar cal = new GregorianCalendar(2005, 3, 22, 14, 32, 12);
104        cal.set(Calendar.MILLISECOND, 500);
105        cal.setTimeZone(tz);
106        
107        SMPPDate date = SMPPDate.getAbsoluteInstance(cal, true);
108        assertEquals(dateFormat.format(date), "050422143212516-");
109        
110        // Get a timezone 8 hours ahead of UTC
111        tz = new SimpleTimeZone(28800000, "UTC+08:00");
112        cal.setTimeZone(tz);
113        cal.set(2005, 3, 22, 14, 32, 12);
114        date = SMPPDate.getAbsoluteInstance(cal, true);
115        assertEquals(dateFormat.format(date), "050422143212532+");
116    }
117    
118    public void testFormatAbsolute12() throws Exception {
119        Calendar cal = new GregorianCalendar(2005, 3, 22, 14, 32, 12);
120        cal.set(Calendar.MILLISECOND, 500);
121        SMPPDate date = SMPPDate.getAbsoluteInstance(cal, false);
122        assertEquals(dateFormat.format(date), "050422143212");
123    }
124    
125    public void testFormatRelative16() throws Exception {
126        SMPPDate date = SMPPDate.getRelativeInstance(
127                1, 2, 3, 4, 5, 6);
128        assertEquals(dateFormat.format(date), "010203040506000R");
129    }
130    
131    private void bothAssertions(SMPPDate date,
132            int year,
133            int month,
134            int day,
135            int hour,
136            int min,
137            int sec,
138            int milli,
139            int tzOffset,
140            char sign) {
141        int mult = 1;
142        if (sign != (char) 0 && sign == '-') {
143            mult = -1;
144        }
145        int tz = mult * ((tzOffset / 3600000) * 4);
146        dateAssertions(date, year, month + 1, day, hour, min, sec, milli / 100, tz, sign);
147        calAssertions(date, year, month, day, hour, min, sec, milli, tzOffset, sign);
148    }
149    private void dateAssertions(SMPPDate date,
150            int year,
151            int month,
152            int day,
153            int hour,
154            int min,
155            int sec,
156            int tenths,
157            int tzOffset,
158            char sign) {
159        assertEquals(date.getYear(), year);
160        assertEquals(date.getMonth(), month);
161        assertEquals(date.getDay(), day);
162        assertEquals(date.getHour(), hour);
163        assertEquals(date.getMinute(), min);
164        assertEquals(date.getSecond(), sec);
165        assertEquals(date.getTenth(), tenths);
166        assertEquals(date.getUtcOffset(), tzOffset);
167        assertEquals(date.getSign(), sign);
168    }
169    
170    private void calAssertions(SMPPDate date,
171            int year,
172            int month,
173            int day,
174            int hour,
175            int min,
176            int sec,
177            int milli,
178            int tzOffset,
179            char sign) {
180        Calendar cal = date.getCalendar();
181        assertEquals(cal.get(Calendar.YEAR), year);
182        assertEquals(cal.get(Calendar.MONTH), month);
183        assertEquals(cal.get(Calendar.DAY_OF_MONTH), day);
184        assertEquals(cal.get(Calendar.HOUR_OF_DAY), hour);
185        assertEquals(cal.get(Calendar.MINUTE), min);
186        assertEquals(cal.get(Calendar.SECOND), sec);
187        assertEquals(cal.get(Calendar.MILLISECOND), milli);
188        if (sign != (char) 0) {
189            assertEquals(cal.getTimeZone().getRawOffset(), tzOffset);
190        }
191    }
192}