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

http://mobicents.googlecode.com/ · Java · 183 lines · 134 code · 28 blank · 21 comment · 1 complexity · 6ff7bc54fc59a47de82e4f050ec00b55 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 java.io.ByteArrayOutputStream;
  25. import java.util.Arrays;
  26. import org.mobicents.protocols.smpp.util.SMPPIO;
  27. import org.testng.annotations.Test;
  28. @Test
  29. public class SMPPIOTest {
  30. public void testWriteByte() throws Exception {
  31. testWriteByte(0, allZero(1));
  32. testWriteByte(127, new byte[] {0x7f});
  33. testWriteByte(255, allFs(1));
  34. }
  35. public void testWriteShort() throws Exception {
  36. testWriteShort(0, allZero(2));
  37. testWriteShort(32768, new byte[] { (byte) 0x80, 0});
  38. testWriteShort(65535, allFs(2));
  39. }
  40. public void testWriteInt() throws Exception {
  41. testWriteInt(0, allZero(4));
  42. testWriteInt(20401123, new byte[] {0x1, 0x37, 0x4b, (byte) 0xe3});
  43. byte[] expected = allFs(4);
  44. expected[0] = 0x7f;
  45. testWriteInt(Integer.MAX_VALUE, expected);
  46. }
  47. public void testWriteLongInt() throws Exception {
  48. testWriteLongInt(0, allZero(4));
  49. testWriteLongInt(20401123, new byte[] {0x1, 0x37, 0x4b, (byte) 0xe3});
  50. testWriteLongInt(4294967295L, allFs(4));
  51. }
  52. public void testWriteLong() throws Exception {
  53. testWriteLong(0L, allZero(8));
  54. byte[] expected = allFs(8);
  55. expected[0] = 0x7f;
  56. testWriteLong(Long.MAX_VALUE, expected);
  57. }
  58. public void testBytesToByte() {
  59. testBytesToByte(allZero(1), 0);
  60. testBytesToByte(new byte[] {0x7f}, 127);
  61. testBytesToByte(allFs(2), 255);
  62. }
  63. public void testBytesToShort() {
  64. testBytesToShort(allZero(2), 0);
  65. testBytesToShort(new byte[] {(byte) 0xb2, (byte) 0x99}, 45721);
  66. testBytesToShort(allFs(2), 65535);
  67. }
  68. public void testBytesToInt() {
  69. testBytesToInt(allZero(4), 0);
  70. testBytesToInt(new byte[] {0x78, (byte) 0x88, (byte) 0x88, (byte) 0x88},
  71. 2022213768);
  72. byte[] allF = allFs(4);
  73. allF[0] = 0x7f;
  74. testBytesToInt(allF, Integer.MAX_VALUE);
  75. }
  76. public void testBytesToLongInt() {
  77. testBytesToLongInt(allZero(4), 0L);
  78. testBytesToLongInt(
  79. new byte[] {0x78, (byte) 0x88, (byte) 0x88, (byte) 0x88},
  80. 2022213768L);
  81. testBytesToLongInt(allFs(4), 4294967295L);
  82. }
  83. public void testBytesToLong() {
  84. testBytesToLong(allZero(8), 0L);
  85. byte[] allF = allFs(8);
  86. allF[0] = 0x7f;
  87. testBytesToLong(allF, Long.MAX_VALUE);
  88. }
  89. private void testWriteByte(int value, byte[] expected) throws Exception {
  90. ByteArrayOutputStream out = new ByteArrayOutputStream();
  91. SMPPIO.writeByte(value, out);
  92. byte[] array = out.toByteArray();
  93. compareArrays(expected, array);
  94. }
  95. private void testWriteShort(int value, byte[] expected) throws Exception {
  96. ByteArrayOutputStream out = new ByteArrayOutputStream();
  97. SMPPIO.writeShort(value, out);
  98. byte[] array = out.toByteArray();
  99. compareArrays(expected, array);
  100. }
  101. private void testWriteInt(int value, byte[] expected) throws Exception {
  102. ByteArrayOutputStream out = new ByteArrayOutputStream();
  103. SMPPIO.writeInt(value, out);
  104. byte[] array = out.toByteArray();
  105. compareArrays(expected, array);
  106. }
  107. private void testWriteLongInt(long value, byte[] expected) throws Exception {
  108. ByteArrayOutputStream out = new ByteArrayOutputStream();
  109. SMPPIO.writeLongInt(value, out);
  110. byte[] array = out.toByteArray();
  111. compareArrays(expected, array);
  112. }
  113. private void testWriteLong(long value, byte[] expected) throws Exception {
  114. ByteArrayOutputStream out = new ByteArrayOutputStream();
  115. SMPPIO.writeLong(value, out);
  116. byte[] array = out.toByteArray();
  117. compareArrays(expected, array);
  118. }
  119. private void testBytesToByte(byte[] array, int expected) {
  120. int actual = SMPPIO.readUInt1(array, 0);
  121. assertEquals(actual, expected);
  122. }
  123. private void testBytesToShort(byte[] array, int expected) {
  124. int actual = SMPPIO.readUInt2(array, 0);
  125. assertEquals(actual, expected);
  126. }
  127. private void testBytesToInt(byte[] array, int expected) {
  128. int actual = SMPPIO.readInt4(array, 0);
  129. assertEquals(actual, expected);
  130. }
  131. private void testBytesToLongInt(byte[] array, long expected) {
  132. long actual = SMPPIO.readUInt4(array, 0);
  133. assertEquals(actual, expected);
  134. }
  135. private void testBytesToLong(byte[] array, long expected) {
  136. long actual = SMPPIO.readInt8(array, 0);
  137. assertEquals(actual, expected);
  138. }
  139. private void compareArrays(byte[] expected, byte[] actual) {
  140. assertEquals(actual.length, expected.length);
  141. for (int i = 0; i < expected.length; i++) {
  142. assertEquals(actual[i], expected[i]);
  143. }
  144. }
  145. private byte[] allZero(int size) {
  146. byte[] array = new byte[size];
  147. Arrays.fill(array, (byte) 0);
  148. return array;
  149. }
  150. private byte[] allFs(int size) {
  151. byte[] array = new byte[size];
  152. Arrays.fill(array, (byte) 0xff);
  153. return array;
  154. }
  155. }