PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 88 lines | 55 code | 11 blank | 22 comment | 0 complexity | ef66cbe0043cb4e441439f9061bece17 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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 java.util.BitSet;
  26. import org.testng.annotations.Test;
  27. import org.mobicents.protocols.smpp.message.param.BitmaskParamDescriptor;
  28. import org.mobicents.protocols.smpp.message.param.ParamDescriptor;
  29. import org.mobicents.protocols.smpp.util.PacketDecoderImpl;
  30. import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
  31. @Test
  32. public class BitmaskParamDescriptorTest {
  33. private ParamDescriptor descriptor = new BitmaskParamDescriptor();
  34. public void testWriteObjectWithNoBitsSet() throws Exception {
  35. BitSet bitset = new BitSet();
  36. ByteArrayOutputStream out = new ByteArrayOutputStream();
  37. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  38. descriptor.writeObject(bitset, encoder);
  39. byte[] actual = out.toByteArray();
  40. assertEquals(actual.length, 1);
  41. assertEquals(actual, new byte[] { 0 });
  42. }
  43. public void testWriteObject() throws Exception {
  44. BitSet bitset = new BitSet();
  45. bitset.set(3);
  46. bitset.set(4);
  47. bitset.set(6);
  48. bitset.set(7);
  49. ByteArrayOutputStream out = new ByteArrayOutputStream();
  50. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  51. descriptor.writeObject(bitset, encoder);
  52. byte[] actual = out.toByteArray();
  53. assertEquals(actual.length, 1);
  54. assertEquals(actual, new byte[] { (byte) 0xd8 });
  55. }
  56. public void testReadObjectWithNoBitsSet() throws Exception {
  57. PacketDecoderImpl decoder = new PacketDecoderImpl(new byte[] {0});
  58. BitSet bitset = (BitSet) descriptor.readObject(decoder, 1);
  59. assertEquals(bitset.length(), 0);
  60. assertEquals(bitset.nextSetBit(0), -1);
  61. assertEquals(decoder.getParsePosition(), 1);
  62. }
  63. public void testReadObjectWithSize1() throws Exception {
  64. // We're reading from an offset here, the first byte should be ignored.
  65. PacketDecoderImpl decoder =
  66. new PacketDecoderImpl(new byte[] {0, 0x7f, 0x7f, 0x77}, 3);
  67. BitSet bitset = (BitSet) descriptor.readObject(decoder, 1);
  68. assertEquals(bitset.nextSetBit(0), 0);
  69. assertEquals(bitset.nextSetBit(1), 1);
  70. assertEquals(bitset.nextSetBit(2), 2);
  71. assertEquals(bitset.nextSetBit(3), 4);
  72. assertEquals(bitset.nextSetBit(5), 5);
  73. assertEquals(bitset.nextSetBit(6), 6);
  74. assertEquals(bitset.nextSetBit(7), -1);
  75. assertEquals(decoder.getParsePosition(), 4);
  76. }
  77. }