PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/util/SMPPIOTest.java

http://mobicents.googlecode.com/
Java | 183 lines | 134 code | 28 blank | 21 comment | 1 complexity | 6ff7bc54fc59a47de82e4f050ec00b55 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.util;
 24
 25import static org.testng.Assert.assertEquals;
 26
 27import java.io.ByteArrayOutputStream;
 28import java.util.Arrays;
 29
 30import org.mobicents.protocols.smpp.util.SMPPIO;
 31import org.testng.annotations.Test;
 32
 33@Test
 34public class SMPPIOTest {
 35
 36    public void testWriteByte() throws Exception {
 37        testWriteByte(0, allZero(1));
 38        testWriteByte(127, new byte[] {0x7f});
 39        testWriteByte(255, allFs(1));
 40    }
 41    
 42    public void testWriteShort() throws Exception {
 43        testWriteShort(0, allZero(2));
 44        testWriteShort(32768, new byte[] { (byte) 0x80, 0});
 45        testWriteShort(65535, allFs(2));
 46    }
 47    
 48    public void testWriteInt() throws Exception {
 49        testWriteInt(0, allZero(4));
 50        testWriteInt(20401123, new byte[] {0x1, 0x37, 0x4b, (byte) 0xe3});
 51        byte[] expected = allFs(4);
 52        expected[0] = 0x7f;
 53        testWriteInt(Integer.MAX_VALUE, expected);
 54    }
 55
 56    public void testWriteLongInt() throws Exception {
 57        testWriteLongInt(0, allZero(4));
 58        testWriteLongInt(20401123, new byte[] {0x1, 0x37, 0x4b, (byte) 0xe3});
 59        testWriteLongInt(4294967295L, allFs(4));
 60    }
 61    
 62    public void testWriteLong() throws Exception {
 63        testWriteLong(0L, allZero(8));
 64        byte[] expected = allFs(8);
 65        expected[0] = 0x7f;
 66        testWriteLong(Long.MAX_VALUE, expected);
 67    }
 68    
 69    public void testBytesToByte() {
 70        testBytesToByte(allZero(1), 0);
 71        testBytesToByte(new byte[] {0x7f}, 127);
 72        testBytesToByte(allFs(2), 255);
 73    }
 74    
 75    public void testBytesToShort() {
 76        testBytesToShort(allZero(2), 0);
 77        testBytesToShort(new byte[] {(byte) 0xb2, (byte) 0x99}, 45721);
 78        testBytesToShort(allFs(2), 65535);
 79    }
 80    
 81    public void testBytesToInt() {
 82        testBytesToInt(allZero(4), 0);
 83        testBytesToInt(new byte[] {0x78, (byte) 0x88, (byte) 0x88, (byte) 0x88},
 84                2022213768);
 85        byte[] allF = allFs(4);
 86        allF[0] = 0x7f;
 87        testBytesToInt(allF, Integer.MAX_VALUE);
 88    }
 89
 90    public void testBytesToLongInt() {
 91        testBytesToLongInt(allZero(4), 0L);
 92        testBytesToLongInt(
 93                new byte[] {0x78, (byte) 0x88, (byte) 0x88, (byte) 0x88},
 94                2022213768L);
 95        testBytesToLongInt(allFs(4), 4294967295L);
 96    }
 97
 98    public void testBytesToLong() {
 99        testBytesToLong(allZero(8), 0L);
100        byte[] allF = allFs(8);
101        allF[0] = 0x7f;
102        testBytesToLong(allF, Long.MAX_VALUE);
103    }
104    
105    private void testWriteByte(int value, byte[] expected) throws Exception {
106        ByteArrayOutputStream out = new ByteArrayOutputStream();
107        SMPPIO.writeByte(value, out);
108        byte[] array = out.toByteArray();
109        compareArrays(expected, array);
110    }
111    
112    private void testWriteShort(int value, byte[] expected) throws Exception {
113        ByteArrayOutputStream out = new ByteArrayOutputStream();
114        SMPPIO.writeShort(value, out);
115        byte[] array = out.toByteArray();
116        compareArrays(expected, array);
117    }
118    
119    private void testWriteInt(int value, byte[] expected) throws Exception {
120        ByteArrayOutputStream out = new ByteArrayOutputStream();
121        SMPPIO.writeInt(value, out);
122        byte[] array = out.toByteArray();
123        compareArrays(expected, array);
124    }
125    
126    private void testWriteLongInt(long value, byte[] expected) throws Exception {
127        ByteArrayOutputStream out = new ByteArrayOutputStream();
128        SMPPIO.writeLongInt(value, out);
129        byte[] array = out.toByteArray();
130        compareArrays(expected, array);
131    }
132    
133    private void testWriteLong(long value, byte[] expected) throws Exception {
134        ByteArrayOutputStream out = new ByteArrayOutputStream();
135        SMPPIO.writeLong(value, out);
136        byte[] array = out.toByteArray();
137        compareArrays(expected, array);
138    }
139
140    private void testBytesToByte(byte[] array, int expected) {
141        int actual = SMPPIO.readUInt1(array, 0);
142        assertEquals(actual, expected);
143    }
144    
145    private void testBytesToShort(byte[] array, int expected) {
146        int actual = SMPPIO.readUInt2(array, 0);
147        assertEquals(actual, expected);
148    }
149    
150    private void testBytesToInt(byte[] array, int expected) {
151        int actual = SMPPIO.readInt4(array, 0);
152        assertEquals(actual, expected);
153    }
154    
155    private void testBytesToLongInt(byte[] array, long expected) {
156        long actual = SMPPIO.readUInt4(array, 0);
157        assertEquals(actual, expected);
158    }
159    
160    private void testBytesToLong(byte[] array, long expected) {
161        long actual = SMPPIO.readInt8(array, 0);
162        assertEquals(actual, expected);
163    }
164    
165    private void compareArrays(byte[] expected, byte[] actual) {
166        assertEquals(actual.length, expected.length);
167        for (int i = 0; i < expected.length; i++) {
168            assertEquals(actual[i], expected[i]);
169        }
170    }
171
172    private byte[] allZero(int size) {
173        byte[] array = new byte[size];
174        Arrays.fill(array, (byte) 0);
175        return array;
176    }
177    
178    private byte[] allFs(int size) {
179        byte[] array = new byte[size];
180        Arrays.fill(array, (byte) 0xff);
181        return array;
182    }
183}