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