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

http://mobicents.googlecode.com/ · Java · 90 lines · 56 code · 13 blank · 21 comment · 0 complexity · d647a57e1bc163a9e64a9bed264c6f5c 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 java.io.ByteArrayOutputStream;
  25. import org.testng.annotations.Test;
  26. import org.mobicents.protocols.smpp.message.param.CStringParamDescriptor;
  27. import org.mobicents.protocols.smpp.util.PacketDecoderImpl;
  28. import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
  29. @Test
  30. public class CStringParamDescriptorTest {
  31. private final String testString = new String("A test string");
  32. private CStringParamDescriptor descriptor = new CStringParamDescriptor();
  33. public void testSizeOf() {
  34. assertEquals(descriptor.sizeOf(null), 1);
  35. assertEquals(descriptor.sizeOf(""), 1);
  36. assertEquals(descriptor.sizeOf(testString), testString.length() + 1);
  37. }
  38. public void testWriteNullString() throws Exception {
  39. ByteArrayOutputStream out = new ByteArrayOutputStream();
  40. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  41. descriptor.writeObject(null, encoder);
  42. byte[] array = out.toByteArray();
  43. assertEquals(array.length, 1);
  44. assertEquals(array[0], (byte) 0);
  45. }
  46. public void testWriteEmptyString() throws Exception {
  47. ByteArrayOutputStream out = new ByteArrayOutputStream();
  48. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  49. descriptor.writeObject("", encoder);
  50. byte[] array = out.toByteArray();
  51. assertEquals(array.length, 1);
  52. assertEquals(array[0], (byte) 0);
  53. }
  54. public void testWriteString() throws Exception {
  55. ByteArrayOutputStream out = new ByteArrayOutputStream();
  56. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  57. descriptor.writeObject(testString, encoder);
  58. byte[] array = out.toByteArray();
  59. assertEquals(array.length, testString.length() + 1);
  60. assertEquals(array[array.length - 1], (byte) 0);
  61. assertEquals(new String(array, 0, array.length - 1, "US-ASCII"), testString);
  62. }
  63. public void testReadEmptyString() throws Exception {
  64. PacketDecoderImpl decoder = new PacketDecoderImpl(new byte[] {0});
  65. String string = (String) descriptor.readObject(decoder, 0);
  66. assertEquals(string, "");
  67. }
  68. public void testReadString() throws Exception {
  69. byte[] array = new byte[testString.length() + 1];
  70. System.arraycopy(testString.getBytes("US-ASCII"), 0, array, 0, testString.length());
  71. array[array.length - 1] = (byte) 0;
  72. PacketDecoderImpl decoder = new PacketDecoderImpl(array);
  73. String string = (String) descriptor.readObject(decoder, 0);
  74. assertEquals(string, testString);
  75. assertEquals(decoder.getParsePosition(), testString.length() + 1);
  76. }
  77. }