Skip to content

Commit 94c901d

Browse files
authored
Replace System.out with logger in unit tests
1 parent b04f20d commit 94c901d

File tree

1 file changed

+21
-21
lines changed
  • core-java-modules/core-java-string-conversions-4/EBCDIC_to_ASCII

1 file changed

+21
-21
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,76 @@
1+
import org.slf4j.Logger;
2+
import org.slf4j.LoggerFactory;
13
import java.io.*;
24
import java.nio.charset.Charset;
35
import java.nio.charset.StandardCharsets;
46

57
public class EBCDICConversionTest {
6-
8+
private static final Logger logger = LoggerFactory.getLogger(EBCDICConversionTest.class);
9+
710
public static void main(String[] args) throws Exception {
811
testBasicEBCDICToAsciiExample();
912
testStepByStepConversion();
1013
testFileBasedConversion();
1114
testStreamingConversion();
12-
System.out.println("✅ All tests passed!");
15+
logger.info("✅ All tests passed!");
1316
}
14-
17+
1518
static void assertEquals(String expected, String actual) {
1619
if (!expected.equals(actual)) {
1720
throw new AssertionError("Expected: [" + expected + "], but got: [" + actual + "]");
1821
}
1922
}
20-
23+
2124
static void testBasicEBCDICToAsciiExample() {
2225
// Example: EBCDIC bytes for "ABC" (in Cp037)
2326
byte[] ebcdicBytes = new byte[] { (byte) 0xC1, (byte) 0xC2, (byte) 0xC3 };
2427
String text = new String(ebcdicBytes, Charset.forName("Cp037"));
25-
System.out.println("Decoded text: " + text);
28+
logger.info("Decoded text: {}", text);
2629
assertEquals("ABC", text);
2730
}
28-
31+
2932
static void testStepByStepConversion() {
3033
// Example EBCDIC bytes ("Hello" in Cp037)
3134
byte[] ebcdicData = { (byte) 0xC8, (byte) 0x85, (byte) 0x93, (byte) 0x93, (byte) 0x96 };
3235
String unicodeText = new String(ebcdicData, Charset.forName("Cp037"));
3336
assertEquals("Hello", unicodeText);
34-
37+
3538
byte[] asciiData = unicodeText.getBytes(StandardCharsets.US_ASCII);
3639
assertEquals("Hello", new String(asciiData, StandardCharsets.US_ASCII));
37-
38-
System.out.println("Step-by-step conversion OK: " + new String(asciiData));
40+
logger.info("Step-by-step conversion OK: {}", new String(asciiData));
3941
}
40-
42+
4143
static void testFileBasedConversion() throws Exception {
4244
File tempFile = File.createTempFile("input", ".ebc");
4345
tempFile.deleteOnExit();
44-
46+
4547
// "TEST" in Cp037 (all uppercase)
4648
byte[] ebcdicData = { (byte) 0xE3, (byte) 0xC5, (byte) 0xE2, (byte) 0xE3 };
4749
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
4850
fos.write(ebcdicData);
4951
}
50-
52+
5153
byte[] rawBytes = new FileInputStream(tempFile).readAllBytes();
5254
String unicodeText = new String(rawBytes, Charset.forName("Cp037"));
5355
assertEquals("TEST", unicodeText);
54-
56+
5557
byte[] asciiData = unicodeText.getBytes(StandardCharsets.US_ASCII);
5658
assertEquals("TEST", new String(asciiData, StandardCharsets.US_ASCII));
57-
58-
System.out.println("File-based conversion OK: " + unicodeText);
59+
logger.info("File-based conversion OK: {}", unicodeText);
5960
}
60-
61+
6162
static void testStreamingConversion() throws IOException {
6263
File input = File.createTempFile("input", ".ebc");
6364
File output = File.createTempFile("output", ".txt");
6465
input.deleteOnExit();
6566
output.deleteOnExit();
66-
67+
6768
// "JAVA" in Cp037 (all uppercase)
6869
byte[] ebcdicData = { (byte) 0xD1, (byte) 0xC1, (byte) 0xE5, (byte) 0xC1 };
6970
try (FileOutputStream fos = new FileOutputStream(input)) {
7071
fos.write(ebcdicData);
7172
}
72-
73+
7374
try (
7475
InputStreamReader reader = new InputStreamReader(
7576
new FileInputStream(input),
@@ -86,13 +87,12 @@ static void testStreamingConversion() throws IOException {
8687
writer.write(buffer, 0, length);
8788
}
8889
}
89-
90+
9091
String result = new String(
9192
new FileInputStream(output).readAllBytes(),
9293
StandardCharsets.US_ASCII
9394
);
9495
assertEquals("JAVA", result);
95-
96-
System.out.println("Streaming conversion OK: " + result);
96+
logger.info("Streaming conversion OK: {}", result);
9797
}
9898
}

0 commit comments

Comments
 (0)