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