Skip to content

Commit 243323c

Browse files
committed
Modified sample inputs to make the tests more meaningful.
1 parent be17633 commit 243323c

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

java-strings/src/test/java/com/baeldung/string/conversion/ByteArrayToStringUnitTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,29 @@ public void whenStringConstructorWithStandardCharSet_thenOK() {
6262

6363
@Test
6464
public void whenDecodeWithCharset_thenOK() {
65-
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108,
66-
100, 33 };
67-
final Charset charset = StandardCharsets.UTF_8;
65+
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, -10, 111, 114, 108, -63, 33 };
66+
final Charset charset = StandardCharsets.US_ASCII;
6867

6968
String string = charset.decode(ByteBuffer.wrap(byteArrray)).toString();
7069
System.out.println(string);
7170

72-
assertEquals("Hello World!", string);
71+
assertEquals("Hello �orl�!", string);
7372
}
7473

7574
@Test
7675
public void whenUsingCharsetDecoder_thenOK()
7776
throws CharacterCodingException {
78-
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108,
79-
100, 33 };
80-
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
77+
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, -10, 111, 114, 108, -63, 33};
78+
CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder();
8179

82-
decoder.onMalformedInput(CodingErrorAction.IGNORE)
80+
decoder.onMalformedInput(CodingErrorAction.REPLACE)
8381
.onUnmappableCharacter(CodingErrorAction.REPLACE)
8482
.replaceWith("?");
8583

8684
String string = decoder.decode(ByteBuffer.wrap(byteArrray)).toString();
8785

88-
assertEquals("Hello World!", string);
86+
assertEquals("Hello ?orl?!", string);
8987
}
9088

89+
9190
}

java-strings/src/test/java/com/baeldung/string/conversion/StringToByteArrayUnitTest.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class StringToByteArrayUnitTest {
1919
@Test
2020
public void whenGetBytesWithDefaultCharset_thenOK() {
2121
final String inputString = "Hello World!";
22-
final String defaultCharSet = Charset.defaultCharset().displayName();
22+
final String defaultCharSet = Charset.defaultCharset()
23+
.displayName();
2324

2425
byte[] byteArrray = inputString.getBytes();
2526

@@ -50,17 +51,17 @@ public void whenGetBytesWithNamedCharset_thenOK()
5051

5152
@Test
5253
public void whenGetBytesWithCharset_thenOK() {
53-
final String inputString = "Hello World!";
54-
final Charset charset = Charset.forName("UTF-8");
54+
final String inputString = "Hello ਸੰਸਾਰ!";
55+
final Charset charset = Charset.forName("ASCII");
5556

5657
byte[] byteArrray = inputString.getBytes(charset);
5758

5859
System.out.printf(
5960
"Using Charset:%s, Input String:%s, Output byte array:%s\n",
6061
charset, inputString, Arrays.toString(byteArrray));
6162

62-
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
63-
108, 100, 33 },
63+
assertArrayEquals(
64+
new byte[] { 72, 101, 108, 108, 111, 32, 63, 63, 63, 63, 63, 33 },
6465
byteArrray);
6566
}
6667

@@ -82,37 +83,39 @@ public void whenGetBytesWithStandardCharset_thenOK() {
8283

8384
@Test
8485
public void whenEncodeWithCharset_thenOK() {
85-
final String inputString = "Hello World!";
86-
final Charset charset = StandardCharsets.UTF_8;
86+
final String inputString = "Hello ਸੰਸਾਰ!";
87+
final Charset charset = StandardCharsets.US_ASCII;
8788

88-
byte[] byteArrray = charset.encode(inputString).array();
89+
byte[] byteArrray = charset.encode(inputString)
90+
.array();
8991

9092
System.out.printf(
9193
"Using encode with Charset:%s, Input String:%s, Output byte array:%s\n",
9294
charset, inputString, Arrays.toString(byteArrray));
9395

94-
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
95-
108, 100, 33, 0 },
96+
assertArrayEquals(
97+
new byte[] { 72, 101, 108, 108, 111, 32, 63, 63, 63, 63, 63, 33 },
9698
byteArrray);
9799
}
98100

99101
@Test
100102
public void whenUsingCharsetEncoder_thenOK()
101103
throws CharacterCodingException {
102-
final String inputString = "Hello World!";
103-
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
104+
final String inputString = "Hello ਸੰਸਾਰ!";
105+
CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
104106
encoder.onMalformedInput(CodingErrorAction.IGNORE)
105107
.onUnmappableCharacter(CodingErrorAction.REPLACE)
106108
.replaceWith(new byte[] { 0 });
107109

108-
byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString)).array();
110+
byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString))
111+
.array();
109112

110113
System.out.printf(
111114
"Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n",
112115
encoder, inputString, Arrays.toString(byteArrray));
113116

114-
assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114,
115-
108, 100, 33, 0 },
117+
assertArrayEquals(
118+
new byte[] { 72, 101, 108, 108, 111, 32, 0, 0, 0, 0, 0, 33 },
116119
byteArrray);
117120
}
118121

0 commit comments

Comments
 (0)