/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/message/param/IntegerParamDescriptorTest.java
Java | 137 lines | 97 code | 19 blank | 21 comment | 2 complexity | da832768b8ee7c4f11785f26820fc8cb 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; 26import static org.testng.Assert.assertNotNull; 27 28import java.io.ByteArrayOutputStream; 29 30import org.testng.annotations.Test; 31 32import org.mobicents.protocols.smpp.message.param.IntegerParamDescriptor; 33import org.mobicents.protocols.smpp.util.PacketDecoderImpl; 34import org.mobicents.protocols.smpp.util.PacketEncoderImpl; 35import org.mobicents.protocols.smpp.util.SMPPIO; 36 37@Test 38public class IntegerParamDescriptorTest { 39 40 private IntegerParamDescriptor integer1 = new IntegerParamDescriptor(1); 41 private IntegerParamDescriptor integer2 = new IntegerParamDescriptor(2); 42 private IntegerParamDescriptor integer4 = new IntegerParamDescriptor(4); 43 private IntegerParamDescriptor integer8 = new IntegerParamDescriptor(8); 44 45 public void testSizeOf() throws Exception { 46 doSizeOf(new IntegerParamDescriptor(1), 1); 47 doSizeOf(new IntegerParamDescriptor(2), 2); 48 doSizeOf(new IntegerParamDescriptor(4), 4); 49 doSizeOf(new IntegerParamDescriptor(8), 8); 50 } 51 52 public void testWriteObject() throws Exception { 53 doWriteObject(integer1, 0L); 54 doWriteObject(integer1, 127L); 55 doWriteObject(integer1, 255L); 56 57 doWriteObject(integer2, 0L); 58 doWriteObject(integer2, (long) Short.MAX_VALUE); 59 doWriteObject(integer2, 65535L); 60 61 doWriteObject(integer4, 0L); 62 doWriteObject(integer4, (long) Integer.MAX_VALUE); 63 doWriteObject(integer4, 4294967295L); 64 65 doWriteObject(integer8, 0L); 66 doWriteObject(integer8, Long.MAX_VALUE); 67 } 68 69 public void testReadObject() throws Exception { 70 doReadObject(integer1, 0L); 71 doReadObject(integer1, 127L); 72 doReadObject(integer1, 255L); 73 74 doReadObject(integer2, 0L); 75 doReadObject(integer2, (long) Short.MAX_VALUE); 76 doReadObject(integer2, 65535L); 77 78 doReadObject(integer4, 0L); 79 doReadObject(integer4, (long) Integer.MAX_VALUE); 80 doReadObject(integer4, 4294967295L); 81 82 doReadObject(integer8, 0L); 83 doReadObject(integer8, Long.MAX_VALUE); 84 } 85 86 private void doSizeOf(IntegerParamDescriptor descriptor, int intSize) throws Exception { 87 Integer value = new Integer(231); 88 assertEquals(descriptor.sizeOf(value), intSize); 89 assertEquals(descriptor.sizeOf(null), intSize); 90 } 91 92 private void doWriteObject(IntegerParamDescriptor descriptor, long value) throws Exception { 93 ByteArrayOutputStream out = new ByteArrayOutputStream(); 94 PacketEncoderImpl encoder = new PacketEncoderImpl(out); 95 descriptor.writeObject(value, encoder); 96 byte[] array = out.toByteArray(); 97 assertEquals(array.length, descriptor.sizeOf(value)); 98 switch (descriptor.sizeOf(value)) { 99 case 8: 100 assertEquals(SMPPIO.readInt8(array, 0), value); 101 break; 102 case 4: 103 assertEquals(SMPPIO.readUInt4(array, 0), value); 104 break; 105 case 2: 106 assertEquals(SMPPIO.readUInt2(array, 0), (int) value); 107 break; 108 default: 109 assertEquals((int) array[0] & 0xff, (int) value); 110 break; 111 } 112 } 113 114 private void doReadObject(IntegerParamDescriptor descriptor, long value) throws Exception { 115 ByteArrayOutputStream out = new ByteArrayOutputStream(); 116 switch (descriptor.sizeOf(value)) { 117 case 8: 118 SMPPIO.writeLong(value, out); 119 break; 120 case 4: 121 SMPPIO.writeLongInt(value, out); 122 break; 123 case 2: 124 SMPPIO.writeShort((int) value, out); 125 break; 126 default: 127 SMPPIO.writeByte((int) value, out); 128 break; 129 } 130 byte[] array = out.toByteArray(); 131 PacketDecoderImpl decoder = new PacketDecoderImpl(array); 132 Number number = (Number) descriptor.readObject(decoder, -1); 133 assertNotNull(number); 134 assertEquals(number.longValue(), value); 135 assertEquals(decoder.getParsePosition(), descriptor.sizeOf(value)); 136 } 137}