/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/message/PacketTests.java

http://mobicents.googlecode.com/ · Java · 164 lines · 102 code · 21 blank · 41 comment · 2 complexity · ed2d4338f4d8531d407098412d36e763 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.message;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertFalse;
  25. import static org.testng.Assert.assertNotNull;
  26. import java.io.ByteArrayOutputStream;
  27. import java.util.Random;
  28. import org.testng.annotations.BeforeClass;
  29. import org.testng.annotations.Test;
  30. import org.mobicents.protocols.smpp.message.BindReceiver;
  31. import org.mobicents.protocols.smpp.message.BindTransceiver;
  32. import org.mobicents.protocols.smpp.message.SMPPPacket;
  33. import org.mobicents.protocols.smpp.message.tlv.Tag;
  34. import org.mobicents.protocols.smpp.util.PacketDecoderImpl;
  35. import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
  36. /**
  37. * Test that the value reported by <code>getLength</code> matches the actual
  38. * length a packet serializes to and deserializes from.
  39. *
  40. * @author Oran Kelly &lt;orank@users.sf.net&gt;
  41. */
  42. public abstract class PacketTests<T extends SMPPPacket> {
  43. private Random random;
  44. @Test
  45. public void testEqualsIsFalseForNullObject() throws Exception {
  46. T packet = getPacketType().newInstance();
  47. assertFalse(packet.equals(null));
  48. }
  49. @Test
  50. public void testEqualsIsFalseForWrongPacketType() throws Exception {
  51. T packet = getPacketType().newInstance();
  52. SMPPPacket other;
  53. if (packet instanceof BindReceiver) {
  54. other = new BindTransceiver();
  55. } else {
  56. other = new BindReceiver();
  57. }
  58. assertFalse(packet.equals(other));
  59. }
  60. @Test
  61. public void testEqualsIsTrueForClonedPacket() throws Exception {
  62. T packet = getPacketType().newInstance();
  63. Object cloned = packet.clone();
  64. assertNotNull(cloned);
  65. assertEquals(cloned, packet);
  66. }
  67. @Test
  68. public void testSizesMatchWithSerializationWithDefaultInitalisation() throws Exception {
  69. T packet = getPacketType().newInstance();
  70. testGetLengthMatchesReality(packet);
  71. }
  72. @Test
  73. public void testSizesMatchWithSerializationWithFieldsSet() throws Exception {
  74. T packet = getInitialisedPacket();
  75. setCommonFields(packet);
  76. testGetLengthMatchesReality(packet);
  77. }
  78. /**
  79. * Test that after serializing and deserializing a packet, the mandatory
  80. * parameters all match.
  81. * @throws Exception
  82. */
  83. @Test
  84. public void testDeserializedFieldsMatchOriginalPacket() throws Exception {
  85. T original = getInitialisedPacket();
  86. setCommonFields(original);
  87. T decodedPacket = getPacketType().newInstance();
  88. ByteArrayOutputStream out = new ByteArrayOutputStream();
  89. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  90. original.writeTo(encoder);
  91. PacketDecoderImpl decoder = new PacketDecoderImpl(out.toByteArray());
  92. decodedPacket.readFrom(decoder);
  93. assertEquals(decodedPacket, original);
  94. }
  95. @Test
  96. public void testPacketWithNonZeroStatusIsOnlyAHeader() throws Exception {
  97. T original = getInitialisedPacket();
  98. setCommonFields(original);
  99. T decodedPacket = getPacketType().newInstance();
  100. ByteArrayOutputStream out = new ByteArrayOutputStream();
  101. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  102. original.writeTo(encoder);
  103. PacketDecoderImpl decoder = new PacketDecoderImpl(
  104. out.toByteArray());
  105. decodedPacket.readFrom(decoder);
  106. assertEquals(original, decodedPacket);
  107. }
  108. protected abstract Class<T> getPacketType();
  109. protected abstract T getInitialisedPacket();
  110. @BeforeClass
  111. protected void initRandom() {
  112. random = new Random();
  113. }
  114. private void setCommonFields(SMPPPacket packet) {
  115. packet.setSequenceNum((long) random.nextInt(Integer.MAX_VALUE));
  116. packet.setCommandStatus(0);
  117. // Add a couple of TLVs
  118. packet.setTLV(Tag.DEST_ADDR_SUBUNIT, new Integer(234));
  119. packet.setTLV(Tag.ITS_SESSION_INFO, new byte[] {1, 2});
  120. }
  121. /**
  122. * Test an individual packet. This method serializes the packet to a byte
  123. * array and then deserializes a second packet from that byte array. It then
  124. * asserts that <code>getLength</code> on the original packet matches the
  125. * length of the byte array and that the length of the byte array matches
  126. * the value returned from <code>getLength</code> on the deserialized
  127. * packet.
  128. */
  129. private void testGetLengthMatchesReality(SMPPPacket packet) throws Exception {
  130. ByteArrayOutputStream out = new ByteArrayOutputStream();
  131. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  132. packet.writeTo(encoder);
  133. byte[] array = out.toByteArray();
  134. System.out.println(array.length);
  135. System.out.println(packet.getLength());
  136. assertEquals(array.length, packet.getLength());
  137. SMPPPacket deserialized = getPacketType().newInstance();
  138. PacketDecoderImpl decoder = new PacketDecoderImpl(array);
  139. deserialized.readFrom(decoder);
  140. assertEquals(packet.getLength(), array.length, packet.getClass().getName());
  141. assertEquals(array.length, deserialized.getLength(), packet.getClass().getName());
  142. }
  143. }