AlphaNumericCodec implements Codec { public String encode(int number) { int i = number; if(i == 0) { return Character.toString(CHARSET[0]); } StringBuilder stringBuilder = new StringBuilder(); while(i > 0) { int remainder = i % ALPHABET_LENGTH; i /= ALPHABET_LENGTH; stringBuilder.append(CHARSET[remainder]); } return stringBuilder.reverse().toString(); }  String encode(int number); @Override int decode(String s);  }
@Test public void cbEncode() { String s = alphaNumericCodec.encode(125); assertEquals("cb", s); }
@Test public void e9aEncode() { String s = alphaNumericCodec.encode(19158); assertEquals("e9a", s); }
@Test public void testEncodeSingleLowerCaseChar() { int encodeValue = 0; for (char letter = 97; letter <= 122; letter++) { String expected = Character.toString(letter); assertEquals("The numeric value " + encodeValue + " should be encoded to '" + expected + "'", expected, alphaNumericCodec.encode(encodeValue++)); } }
@Test public void testEncodeSingleUpperCaseChar() { int encodeValue = 26; for (char letter = 65; letter <= 90; letter++) { String expected = Character.toString(letter); assertEquals("The numeric value " + encodeValue + " should be encoded to '" + expected + "'", expected, alphaNumericCodec.encode(encodeValue++)); } }
@Test public void testNumber() { int encodeValue = 52; for(int i = 0; i<10; i++) { String expected = String.valueOf(i); assertEquals("The numeric value " + encodeValue + " should be encoded to '" + expected + "'", expected, alphaNumericCodec.encode(encodeValue++)); } }
@Test public void testEncodeTwoUpperCaseProgression() { assertEquals("ba", alphaNumericCodec.encode(62)); assertEquals("bb", alphaNumericCodec.encode(63)); assertEquals("bc", alphaNumericCodec.encode(64)); assertEquals("b9", alphaNumericCodec.encode(123)); assertEquals("ca", alphaNumericCodec.encode(124)); assertEquals("c9", alphaNumericCodec.encode(185)); }
@Test public void testBiggestPossibleInteger() { assertEquals("cvuMLb", alphaNumericCodec.encode(Integer.MAX_VALUE)); }
@Test public void testSmallestPossibleIntegerEncodesToBlank() { assertEquals("", alphaNumericCodec.encode(Integer.MIN_VALUE)); }
@Test public void testNegativeIntegerEncodesToBlank() { assertEquals("", alphaNumericCodec.encode(-1)); }