Modified sample inputs to make the tests more meaningful.

This commit is contained in:
chandra 2018-11-12 21:36:21 -05:00
parent be17633174
commit 243323c55f
2 changed files with 26 additions and 24 deletions

View File

@ -62,30 +62,29 @@ public class ByteArrayToStringUnitTest {
@Test @Test
public void whenDecodeWithCharset_thenOK() { public void whenDecodeWithCharset_thenOK() {
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, byte[] byteArrray = { 72, 101, 108, 108, 111, 32, -10, 111, 114, 108, -63, 33 };
100, 33 }; final Charset charset = StandardCharsets.US_ASCII;
final Charset charset = StandardCharsets.UTF_8;
String string = charset.decode(ByteBuffer.wrap(byteArrray)).toString(); String string = charset.decode(ByteBuffer.wrap(byteArrray)).toString();
System.out.println(string); System.out.println(string);
assertEquals("Hello World!", string); assertEquals("Hello <EFBFBD>orl<EFBFBD>!", string);
} }
@Test @Test
public void whenUsingCharsetDecoder_thenOK() public void whenUsingCharsetDecoder_thenOK()
throws CharacterCodingException { throws CharacterCodingException {
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, byte[] byteArrray = { 72, 101, 108, 108, 111, 32, -10, 111, 114, 108, -63, 33};
100, 33 }; CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder();
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE) decoder.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("?"); .replaceWith("?");
String string = decoder.decode(ByteBuffer.wrap(byteArrray)).toString(); String string = decoder.decode(ByteBuffer.wrap(byteArrray)).toString();
assertEquals("Hello World!", string); assertEquals("Hello ?orl?!", string);
} }
} }

View File

@ -19,7 +19,8 @@ public class StringToByteArrayUnitTest {
@Test @Test
public void whenGetBytesWithDefaultCharset_thenOK() { public void whenGetBytesWithDefaultCharset_thenOK() {
final String inputString = "Hello World!"; final String inputString = "Hello World!";
final String defaultCharSet = Charset.defaultCharset().displayName(); final String defaultCharSet = Charset.defaultCharset()
.displayName();
byte[] byteArrray = inputString.getBytes(); byte[] byteArrray = inputString.getBytes();
@ -50,8 +51,8 @@ public class StringToByteArrayUnitTest {
@Test @Test
public void whenGetBytesWithCharset_thenOK() { public void whenGetBytesWithCharset_thenOK() {
final String inputString = "Hello World!"; final String inputString = "Hello ਸੰਸਾਰ!";
final Charset charset = Charset.forName("UTF-8"); final Charset charset = Charset.forName("ASCII");
byte[] byteArrray = inputString.getBytes(charset); byte[] byteArrray = inputString.getBytes(charset);
@ -59,8 +60,8 @@ public class StringToByteArrayUnitTest {
"Using Charset:%s, Input String:%s, Output byte array:%s\n", "Using Charset:%s, Input String:%s, Output byte array:%s\n",
charset, inputString, Arrays.toString(byteArrray)); charset, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, assertArrayEquals(
108, 100, 33 }, new byte[] { 72, 101, 108, 108, 111, 32, 63, 63, 63, 63, 63, 33 },
byteArrray); byteArrray);
} }
@ -82,37 +83,39 @@ public class StringToByteArrayUnitTest {
@Test @Test
public void whenEncodeWithCharset_thenOK() { public void whenEncodeWithCharset_thenOK() {
final String inputString = "Hello World!"; final String inputString = "Hello ਸੰਸਾਰ!";
final Charset charset = StandardCharsets.UTF_8; final Charset charset = StandardCharsets.US_ASCII;
byte[] byteArrray = charset.encode(inputString).array(); byte[] byteArrray = charset.encode(inputString)
.array();
System.out.printf( System.out.printf(
"Using encode with Charset:%s, Input String:%s, Output byte array:%s\n", "Using encode with Charset:%s, Input String:%s, Output byte array:%s\n",
charset, inputString, Arrays.toString(byteArrray)); charset, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, assertArrayEquals(
108, 100, 33, 0 }, new byte[] { 72, 101, 108, 108, 111, 32, 63, 63, 63, 63, 63, 33 },
byteArrray); byteArrray);
} }
@Test @Test
public void whenUsingCharsetEncoder_thenOK() public void whenUsingCharsetEncoder_thenOK()
throws CharacterCodingException { throws CharacterCodingException {
final String inputString = "Hello World!"; final String inputString = "Hello ਸੰਸਾਰ!";
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
encoder.onMalformedInput(CodingErrorAction.IGNORE) encoder.onMalformedInput(CodingErrorAction.IGNORE)
.onUnmappableCharacter(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith(new byte[] { 0 }); .replaceWith(new byte[] { 0 });
byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString)).array(); byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString))
.array();
System.out.printf( System.out.printf(
"Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n", "Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n",
encoder, inputString, Arrays.toString(byteArrray)); encoder, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, assertArrayEquals(
108, 100, 33, 0 }, new byte[] { 72, 101, 108, 108, 111, 32, 0, 0, 0, 0, 0, 33 },
byteArrray); byteArrray);
} }