PageRenderTime 34ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/message/PacketTests.java

http://mobicents.googlecode.com/
Java | 164 lines | 102 code | 21 blank | 41 comment | 2 complexity | ed2d4338f4d8531d407098412d36e763 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;
 24
 25import static org.testng.Assert.assertEquals;
 26import static org.testng.Assert.assertFalse;
 27import static org.testng.Assert.assertNotNull;
 28
 29import java.io.ByteArrayOutputStream;
 30import java.util.Random;
 31
 32import org.testng.annotations.BeforeClass;
 33import org.testng.annotations.Test;
 34
 35import org.mobicents.protocols.smpp.message.BindReceiver;
 36import org.mobicents.protocols.smpp.message.BindTransceiver;
 37import org.mobicents.protocols.smpp.message.SMPPPacket;
 38import org.mobicents.protocols.smpp.message.tlv.Tag;
 39import org.mobicents.protocols.smpp.util.PacketDecoderImpl;
 40import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
 41
 42/**
 43 * Test that the value reported by <code>getLength</code> matches the actual
 44 * length a packet serializes to and deserializes from.
 45 * 
 46 * @author Oran Kelly &lt;orank@users.sf.net&gt;
 47 */
 48public abstract class PacketTests<T extends SMPPPacket> {
 49
 50    private Random random;
 51    
 52    @Test
 53    public void testEqualsIsFalseForNullObject() throws Exception {
 54        T packet = getPacketType().newInstance();
 55        assertFalse(packet.equals(null));
 56    }
 57
 58    @Test
 59    public void testEqualsIsFalseForWrongPacketType() throws Exception {
 60        T packet = getPacketType().newInstance();
 61        SMPPPacket other;
 62        if (packet instanceof BindReceiver) {
 63            other = new BindTransceiver();
 64        } else {
 65            other = new BindReceiver();
 66        }
 67        assertFalse(packet.equals(other));
 68    }
 69    
 70    @Test
 71    public void testEqualsIsTrueForClonedPacket() throws Exception {
 72        T packet = getPacketType().newInstance();
 73        Object cloned = packet.clone();
 74        assertNotNull(cloned);
 75        assertEquals(cloned, packet);
 76    }
 77    
 78    @Test
 79    public void testSizesMatchWithSerializationWithDefaultInitalisation() throws Exception {
 80        T packet = getPacketType().newInstance();
 81        testGetLengthMatchesReality(packet);
 82    }
 83    
 84    @Test
 85    public void testSizesMatchWithSerializationWithFieldsSet() throws Exception {
 86        T packet = getInitialisedPacket();
 87        setCommonFields(packet);
 88        testGetLengthMatchesReality(packet);
 89    }
 90    
 91    /**
 92     * Test that after serializing and deserializing a packet, the mandatory
 93     * parameters all match.
 94     * @throws Exception
 95     */
 96    @Test
 97    public void testDeserializedFieldsMatchOriginalPacket() throws Exception {
 98        T original = getInitialisedPacket();
 99        setCommonFields(original);
100        T decodedPacket = getPacketType().newInstance();
101        ByteArrayOutputStream out = new ByteArrayOutputStream();
102        PacketEncoderImpl encoder = new PacketEncoderImpl(out);
103        original.writeTo(encoder);
104        PacketDecoderImpl decoder = new PacketDecoderImpl(out.toByteArray());
105        decodedPacket.readFrom(decoder);
106        assertEquals(decodedPacket, original);
107    }
108
109    @Test
110    public void testPacketWithNonZeroStatusIsOnlyAHeader() throws Exception {
111        T original = getInitialisedPacket();
112        setCommonFields(original);
113        T decodedPacket = getPacketType().newInstance();
114        ByteArrayOutputStream out = new ByteArrayOutputStream();
115        PacketEncoderImpl encoder = new PacketEncoderImpl(out);
116        original.writeTo(encoder);
117        PacketDecoderImpl decoder = new PacketDecoderImpl(
118                out.toByteArray());
119        decodedPacket.readFrom(decoder);
120        assertEquals(original, decodedPacket);
121    }
122    
123    protected abstract Class<T> getPacketType();
124    
125    protected abstract T getInitialisedPacket();
126    
127    @BeforeClass
128    protected void initRandom() {
129        random = new Random();
130    }
131    
132    private void setCommonFields(SMPPPacket packet) {
133        packet.setSequenceNum((long) random.nextInt(Integer.MAX_VALUE));
134        packet.setCommandStatus(0);
135        // Add a couple of TLVs
136        packet.setTLV(Tag.DEST_ADDR_SUBUNIT, new Integer(234));
137        packet.setTLV(Tag.ITS_SESSION_INFO, new byte[] {1, 2});
138    }
139    
140    /**
141     * Test an individual packet. This method serializes the packet to a byte
142     * array and then deserializes a second packet from that byte array. It then
143     * asserts that <code>getLength</code> on the original packet matches the
144     * length of the byte array and that the length of the byte array matches
145     * the value returned from <code>getLength</code> on the deserialized
146     * packet.
147     */
148    private void testGetLengthMatchesReality(SMPPPacket packet) throws Exception {
149        ByteArrayOutputStream out = new ByteArrayOutputStream();
150        PacketEncoderImpl encoder = new PacketEncoderImpl(out);
151        packet.writeTo(encoder);
152        byte[] array = out.toByteArray();
153        
154        System.out.println(array.length);
155        System.out.println(packet.getLength());
156        
157        assertEquals(array.length, packet.getLength());
158        SMPPPacket deserialized = getPacketType().newInstance();
159        PacketDecoderImpl decoder = new PacketDecoderImpl(array);
160        deserialized.readFrom(decoder);
161        assertEquals(packet.getLength(), array.length, packet.getClass().getName());
162        assertEquals(array.length, deserialized.getLength(), packet.getClass().getName());
163    }
164}