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