/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/encoding/BaseAlphabetEncodingTest.java

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