/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/encoding/BaseAlphabetEncodingTest.java
Java | 153 lines | 108 code | 24 blank | 21 comment | 3 complexity | ee00276a2afcf30833101a24692f4548 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.encoding; 24 25import static org.testng.Assert.assertEquals; 26import static org.testng.Assert.assertNotNull; 27import static org.testng.Assert.assertTrue; 28 29import java.io.UnsupportedEncodingException; 30 31import org.mobicents.protocols.smpp.encoding.AlphabetEncoding; 32import org.testng.annotations.Test; 33 34public abstract class BaseAlphabetEncodingTest<T extends AlphabetEncoding> { 35 36 @Test 37 public void testDecodeNullArray() throws Exception { 38 AlphabetEncoding encoding = getEncodingToTest(); 39 String decoded = encoding.decode(null); 40 assertNotNull(decoded); 41 assertEquals(decoded, ""); 42 } 43 44 @Test(expectedExceptions = {NullPointerException.class}) 45 public void testDecodeNullArrayWithOffsetAndLength() throws Exception { 46 AlphabetEncoding encoding = getEncodingToTest(); 47 encoding.decode(null, 1, 5); 48 } 49 50 @Test 51 public void testDecodeFullArray() throws Exception { 52 AlphabetEncoding encoding = getEncodingToTest(); 53 TestData testData = getArrayToDecode(); 54 String decoded = encoding.decode(testData.getBytes()); 55 assertNotNull(decoded); 56 assertEquals(decoded, testData.string); 57 } 58 59 @Test 60 public void testDecodePartialArray() throws Exception { 61 AlphabetEncoding encoding = getEncodingToTest(); 62 int charSize = encoding.getEncodedSize("a"); 63 TestData testData = getArrayToDecode(); 64 assertTrue(testData.bytes.length > 6); 65 String decoded = encoding.decode( 66 testData.getBytes(), 1 * charSize, 3 * charSize); 67 assertNotNull(decoded); 68 assertEquals(decoded, testData.string.substring(1, 4)); 69 } 70 71 @Test 72 public void testEncodeNullReturnsZeroLengthArray() throws Exception { 73 AlphabetEncoding encoding = getEncodingToTest(); 74 byte[] encoded = encoding.encode(null); 75 assertNotNull(encoded); 76 assertEquals(encoded.length, 0); 77 } 78 79 @Test 80 public void testEncodeWithAllCharactersSupported() throws Exception { 81 AlphabetEncoding encoding = getEncodingToTest(); 82 TestData testData = getFullySupportedStringToEncode(); 83 byte[] encoded = encoding.encode(testData.string); 84 assertNotNull(encoded); 85 assertEquals(encoded, testData.getBytes()); 86 } 87 88 @Test 89 public void testEncodeWithUnsupportedCharacters() throws Exception { 90 AlphabetEncoding encoding = getEncodingToTest(); 91 TestData testData = getPartiallySupportedStringToEncode(); 92 byte[] encoded = encoding.encode(testData.string); 93 assertNotNull(encoded); 94 assertEquals(encoded, testData.getBytes()); 95 } 96 97 @Test 98 public void testGetEncodingIsNotNull() throws Exception { 99 AlphabetEncoding encoding = getEncodingToTest(); 100 if (encoding.getCharset() != null) { 101 new String(new byte[] {64}, encoding.getCharset()); 102 } 103 } 104 105 @Test 106 public void testEncodedSizeIsGreaterThanZeroForStringWithContent() throws Exception { 107 AlphabetEncoding encoding = getEncodingToTest(); 108 assertTrue(encoding.getEncodedSize("test") > 0); 109 } 110 111 @Test 112 public void testEncodedSizeIsZeroForEmptyString() throws Exception { 113 AlphabetEncoding encoding = getEncodingToTest(); 114 assertEquals(encoding.getEncodedSize(""), 0); 115 } 116 117 @Test 118 public void testEncodedSizeIsZeroForNull() throws Exception { 119 AlphabetEncoding encoding = getEncodingToTest(); 120 assertEquals(encoding.getEncodedSize(null), 0); 121 } 122 123 protected abstract T getEncodingToTest() throws UnsupportedEncodingException; 124 125 protected abstract TestData getArrayToDecode(); 126 127 protected abstract TestData getFullySupportedStringToEncode(); 128 129 protected abstract TestData getPartiallySupportedStringToEncode(); 130 131 protected class TestData { 132 int[] bytes; 133 String string; 134 135 public TestData(int[] expectedBytes, String actualString) { 136 this.bytes = expectedBytes; 137 this.string = actualString; 138 } 139 140 public TestData(String expectedString, int[] actualBytes) { 141 this.string = expectedString; 142 this.bytes = actualBytes; 143 } 144 145 public byte[] getBytes() { 146 byte[] byteArray = new byte[bytes.length]; 147 for (int i = 0; i < bytes.length; i++) { 148 byteArray[i] = (byte) bytes[i]; 149 } 150 return byteArray; 151 } 152 } 153}