PageRenderTime 27ms CodeModel.GetById 21ms app.highlight 4ms RepoModel.GetById 1ms 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
 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
23package org.mobicents.protocols.smpp.message.param;
24
25import static org.testng.Assert.assertEquals;
26
27import java.io.ByteArrayOutputStream;
28import java.util.BitSet;
29
30import org.testng.annotations.Test;
31
32import org.mobicents.protocols.smpp.message.param.BitmaskParamDescriptor;
33import org.mobicents.protocols.smpp.message.param.ParamDescriptor;
34import org.mobicents.protocols.smpp.util.PacketDecoderImpl;
35import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
36
37@Test
38public class BitmaskParamDescriptorTest {
39    
40    private ParamDescriptor descriptor = new BitmaskParamDescriptor();
41
42    public void testWriteObjectWithNoBitsSet() throws Exception {
43        BitSet bitset = new BitSet();
44        ByteArrayOutputStream out = new ByteArrayOutputStream();
45        PacketEncoderImpl encoder = new PacketEncoderImpl(out);
46        descriptor.writeObject(bitset, encoder);
47        byte[] actual = out.toByteArray();
48        assertEquals(actual.length, 1);
49        assertEquals(actual, new byte[] { 0 });
50    }
51    
52    public void testWriteObject() throws Exception {
53        BitSet bitset = new BitSet();
54        bitset.set(3);
55        bitset.set(4);
56        bitset.set(6);
57        bitset.set(7);
58        ByteArrayOutputStream out = new ByteArrayOutputStream();
59        PacketEncoderImpl encoder = new PacketEncoderImpl(out);
60        descriptor.writeObject(bitset, encoder);
61        byte[] actual = out.toByteArray();
62        assertEquals(actual.length, 1);
63        assertEquals(actual, new byte[] { (byte) 0xd8 });
64    }
65    
66    public void testReadObjectWithNoBitsSet() throws Exception {
67        PacketDecoderImpl decoder = new PacketDecoderImpl(new byte[] {0});
68        BitSet bitset = (BitSet) descriptor.readObject(decoder, 1);
69        assertEquals(bitset.length(), 0);
70        assertEquals(bitset.nextSetBit(0), -1);
71        assertEquals(decoder.getParsePosition(), 1);
72    }
73    
74    public void testReadObjectWithSize1() throws Exception {
75        // We're reading from an offset here, the first byte should be ignored.
76        PacketDecoderImpl decoder =
77            new PacketDecoderImpl(new byte[] {0, 0x7f, 0x7f, 0x77}, 3);
78        BitSet bitset = (BitSet) descriptor.readObject(decoder, 1);
79        assertEquals(bitset.nextSetBit(0), 0);
80        assertEquals(bitset.nextSetBit(1), 1);
81        assertEquals(bitset.nextSetBit(2), 2);
82        assertEquals(bitset.nextSetBit(3), 4);
83        assertEquals(bitset.nextSetBit(5), 5);
84        assertEquals(bitset.nextSetBit(6), 6);
85        assertEquals(bitset.nextSetBit(7), -1);
86        assertEquals(decoder.getParsePosition(), 4);
87    }
88}