/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. package org.mobicents.protocols.smpp.util;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertFalse;
  25. import static org.testng.Assert.assertTrue;
  26. import java.util.Calendar;
  27. import java.util.Date;
  28. import java.util.GregorianCalendar;
  29. import java.util.SimpleTimeZone;
  30. import java.util.TimeZone;
  31. import org.mobicents.protocols.smpp.util.SMPPDate;
  32. import org.testng.annotations.Test;
  33. @Test
  34. public class AbsoluteSMPPDateTest {
  35. private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
  36. public void testAbsoluteDateInUTC() {
  37. Calendar cal = new GregorianCalendar();
  38. Date now = new Date();
  39. cal.setTime(now);
  40. cal.setTimeZone(UTC);
  41. SMPPDate d = SMPPDate.getAbsoluteInstance(cal, true);
  42. assertFalse(d.isRelative());
  43. assertTrue(d.isAbsolute());
  44. assertEquals(d.getYear(), cal.get(Calendar.YEAR));
  45. assertEquals(d.getMonth(), cal.get(Calendar.MONTH) + 1);
  46. assertEquals(d.getDay(), cal.get(Calendar.DAY_OF_MONTH));
  47. assertEquals(d.getHour(), cal.get(Calendar.HOUR_OF_DAY));
  48. assertEquals(d.getMinute(), cal.get(Calendar.MINUTE));
  49. assertEquals(d.getSecond(), cal.get(Calendar.SECOND));
  50. assertEquals(d.getTenth(), cal.get(Calendar.MILLISECOND) / 100);
  51. assertEquals(d.getTimeZone(), cal.getTimeZone());
  52. assertEquals(d.getSign(), '+');
  53. }
  54. public void testDaylightSavingsTimeZone() throws Exception {
  55. // Create two similar time zones, each one is 1 hour behind
  56. // UTC in standard time and one hour ahead of UTC in daylight
  57. // savings. One of them will be in daylight savings "now", the
  58. // other will not.
  59. Calendar dstStart = Calendar.getInstance();
  60. dstStart.add(Calendar.MONTH, -2);
  61. Calendar dstEnd = Calendar.getInstance();
  62. dstEnd.add(Calendar.MONTH, 4);
  63. SimpleTimeZone aheadInDst = new SimpleTimeZone(
  64. -3600000,
  65. "AheadInDst",
  66. dstStart.get(Calendar.MONTH),
  67. dstStart.get(Calendar.DAY_OF_WEEK),
  68. -1,
  69. 3600000,
  70. dstEnd.get(Calendar.MONTH),
  71. dstEnd.get(Calendar.DAY_OF_WEEK),
  72. -1,
  73. 3600000,
  74. 7200000);
  75. SimpleTimeZone behindWhenNotInDst = new SimpleTimeZone(
  76. -3600000,
  77. "BehindNotInDst",
  78. dstEnd.get(Calendar.MONTH),
  79. dstEnd.get(Calendar.DAY_OF_WEEK),
  80. -1,
  81. 3600000,
  82. dstStart.get(Calendar.MONTH),
  83. dstStart.get(Calendar.DAY_OF_WEEK),
  84. -1,
  85. 3600000,
  86. 7200000);
  87. assertTrue(aheadInDst.inDaylightTime(new Date()));
  88. assertFalse(behindWhenNotInDst.inDaylightTime(new Date()));
  89. assertTrue(aheadInDst.getRawOffset() < 0);
  90. assertTrue(behindWhenNotInDst.getRawOffset() < 0);
  91. assertTrue(aheadInDst.getOffset(System.currentTimeMillis()) > 0);
  92. assertTrue(behindWhenNotInDst.getOffset(System.currentTimeMillis()) < 0);
  93. Calendar now = Calendar.getInstance(aheadInDst);
  94. SMPPDate date = SMPPDate.getAbsoluteInstance(now, true);
  95. assertTrue(date.hasTimezone());
  96. assertEquals(date.getUtcOffset(), 4);
  97. assertEquals(date.getSign(), '+');
  98. now = Calendar.getInstance(behindWhenNotInDst);
  99. date = SMPPDate.getAbsoluteInstance(now, true);
  100. assertTrue(date.hasTimezone());
  101. assertEquals(date.getUtcOffset(), 4);
  102. assertEquals(date.getSign(), '-');
  103. }
  104. public void testEqualsAndHashCode() {
  105. Calendar now = new GregorianCalendar();
  106. Calendar other = new GregorianCalendar();
  107. other.set(Calendar.YEAR, 1956);
  108. SMPPDate now1WithTz = SMPPDate.getAbsoluteInstance(now);
  109. SMPPDate now2WithTz = SMPPDate.getAbsoluteInstance(now);
  110. SMPPDate now3NoTz = SMPPDate.getAbsoluteInstance(now, false);
  111. SMPPDate otherWithTz = SMPPDate.getAbsoluteInstance(other);
  112. assertEquals(now1WithTz, now1WithTz);
  113. assertEquals(now2WithTz, now2WithTz);
  114. assertEquals(now2WithTz, now1WithTz);
  115. assertEquals(now1WithTz, now2WithTz);
  116. assertEquals(now2WithTz.hashCode(), now1WithTz.hashCode());
  117. assertEquals(now3NoTz, now3NoTz);
  118. assertFalse(now1WithTz.equals(now3NoTz));
  119. assertFalse(now3NoTz.equals(now2WithTz));
  120. assertFalse(now3NoTz.hashCode() == now1WithTz.hashCode());
  121. assertFalse(now1WithTz.equals(otherWithTz));
  122. assertFalse(now2WithTz.equals(otherWithTz));
  123. assertFalse(otherWithTz.equals(now1WithTz));
  124. assertFalse(otherWithTz.equals(now2WithTz));
  125. assertFalse(now1WithTz.hashCode() == otherWithTz.hashCode());
  126. assertFalse(now2WithTz.hashCode() == otherWithTz.hashCode());
  127. }
  128. }