/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/message/param/IntegerParamDescriptorTest.java

http://mobicents.googlecode.com/ · Java · 137 lines · 97 code · 19 blank · 21 comment · 2 complexity · da832768b8ee7c4f11785f26820fc8cb 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.param;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertNotNull;
  25. import java.io.ByteArrayOutputStream;
  26. import org.testng.annotations.Test;
  27. import org.mobicents.protocols.smpp.message.param.IntegerParamDescriptor;
  28. import org.mobicents.protocols.smpp.util.PacketDecoderImpl;
  29. import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
  30. import org.mobicents.protocols.smpp.util.SMPPIO;
  31. @Test
  32. public class IntegerParamDescriptorTest {
  33. private IntegerParamDescriptor integer1 = new IntegerParamDescriptor(1);
  34. private IntegerParamDescriptor integer2 = new IntegerParamDescriptor(2);
  35. private IntegerParamDescriptor integer4 = new IntegerParamDescriptor(4);
  36. private IntegerParamDescriptor integer8 = new IntegerParamDescriptor(8);
  37. public void testSizeOf() throws Exception {
  38. doSizeOf(new IntegerParamDescriptor(1), 1);
  39. doSizeOf(new IntegerParamDescriptor(2), 2);
  40. doSizeOf(new IntegerParamDescriptor(4), 4);
  41. doSizeOf(new IntegerParamDescriptor(8), 8);
  42. }
  43. public void testWriteObject() throws Exception {
  44. doWriteObject(integer1, 0L);
  45. doWriteObject(integer1, 127L);
  46. doWriteObject(integer1, 255L);
  47. doWriteObject(integer2, 0L);
  48. doWriteObject(integer2, (long) Short.MAX_VALUE);
  49. doWriteObject(integer2, 65535L);
  50. doWriteObject(integer4, 0L);
  51. doWriteObject(integer4, (long) Integer.MAX_VALUE);
  52. doWriteObject(integer4, 4294967295L);
  53. doWriteObject(integer8, 0L);
  54. doWriteObject(integer8, Long.MAX_VALUE);
  55. }
  56. public void testReadObject() throws Exception {
  57. doReadObject(integer1, 0L);
  58. doReadObject(integer1, 127L);
  59. doReadObject(integer1, 255L);
  60. doReadObject(integer2, 0L);
  61. doReadObject(integer2, (long) Short.MAX_VALUE);
  62. doReadObject(integer2, 65535L);
  63. doReadObject(integer4, 0L);
  64. doReadObject(integer4, (long) Integer.MAX_VALUE);
  65. doReadObject(integer4, 4294967295L);
  66. doReadObject(integer8, 0L);
  67. doReadObject(integer8, Long.MAX_VALUE);
  68. }
  69. private void doSizeOf(IntegerParamDescriptor descriptor, int intSize) throws Exception {
  70. Integer value = new Integer(231);
  71. assertEquals(descriptor.sizeOf(value), intSize);
  72. assertEquals(descriptor.sizeOf(null), intSize);
  73. }
  74. private void doWriteObject(IntegerParamDescriptor descriptor, long value) throws Exception {
  75. ByteArrayOutputStream out = new ByteArrayOutputStream();
  76. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  77. descriptor.writeObject(value, encoder);
  78. byte[] array = out.toByteArray();
  79. assertEquals(array.length, descriptor.sizeOf(value));
  80. switch (descriptor.sizeOf(value)) {
  81. case 8:
  82. assertEquals(SMPPIO.readInt8(array, 0), value);
  83. break;
  84. case 4:
  85. assertEquals(SMPPIO.readUInt4(array, 0), value);
  86. break;
  87. case 2:
  88. assertEquals(SMPPIO.readUInt2(array, 0), (int) value);
  89. break;
  90. default:
  91. assertEquals((int) array[0] & 0xff, (int) value);
  92. break;
  93. }
  94. }
  95. private void doReadObject(IntegerParamDescriptor descriptor, long value) throws Exception {
  96. ByteArrayOutputStream out = new ByteArrayOutputStream();
  97. switch (descriptor.sizeOf(value)) {
  98. case 8:
  99. SMPPIO.writeLong(value, out);
  100. break;
  101. case 4:
  102. SMPPIO.writeLongInt(value, out);
  103. break;
  104. case 2:
  105. SMPPIO.writeShort((int) value, out);
  106. break;
  107. default:
  108. SMPPIO.writeByte((int) value, out);
  109. break;
  110. }
  111. byte[] array = out.toByteArray();
  112. PacketDecoderImpl decoder = new PacketDecoderImpl(array);
  113. Number number = (Number) descriptor.readObject(decoder, -1);
  114. assertNotNull(number);
  115. assertEquals(number.longValue(), value);
  116. assertEquals(decoder.getParsePosition(), descriptor.sizeOf(value));
  117. }
  118. }