Skip to content

Commit d1d5b0b

Browse files
committed
Spotbugs issues fixed
1 parent f101e7e commit d1d5b0b

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

data-loader/cli/src/test/java/com/scalar/db/dataloader/cli/command/dataexport/ConsoleExportProgressReporterTest.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.ByteArrayOutputStream;
88
import java.io.PrintStream;
9+
import java.io.UnsupportedEncodingException;
910
import org.junit.jupiter.api.AfterEach;
1011
import org.junit.jupiter.api.BeforeEach;
1112
import org.junit.jupiter.api.Test;
@@ -16,8 +17,8 @@ class ConsoleExportProgressReporterTest {
1617
private final PrintStream originalOut = System.out;
1718

1819
@BeforeEach
19-
void setUpStreams() {
20-
System.setOut(new PrintStream(outContent));
20+
void setUpStreams() throws UnsupportedEncodingException {
21+
System.setOut(new PrintStream(outContent, true, "UTF-8"));
2122
}
2223

2324
@AfterEach
@@ -26,22 +27,23 @@ void restoreStreams() {
2627
}
2728

2829
@Test
29-
void testStartMessageIncludesExportFilePath() {
30+
void testStartMessageIncludesExportFilePath() throws UnsupportedEncodingException {
3031
new ConsoleExportProgressReporter("output/test.csv");
31-
String output = outContent.toString();
32+
String output = outContent.toString("UTF-8");
3233
assertTrue(output.contains("📤 Starting export"), "Expected start message");
3334
assertTrue(
3435
output.contains("📁 Exporting data to file: output/test.csv"), "Expected file path info");
3536
}
3637

3738
@Test
38-
void testCompletionMessageIncludesFilePathAndDuration() throws InterruptedException {
39+
void testCompletionMessageIncludesFilePathAndDuration()
40+
throws InterruptedException, UnsupportedEncodingException {
3941
ConsoleExportProgressReporter reporter = new ConsoleExportProgressReporter("target/output.csv");
4042

4143
Thread.sleep(100); // Simulate work
4244

4345
reporter.reportCompletion(12345);
44-
String output = outContent.toString();
46+
String output = outContent.toString("UTF-8");
4547

4648
assertTrue(
4749
output.contains("✅ Export completed: 12,345 records exported to target/output.csv"),
@@ -50,30 +52,31 @@ void testCompletionMessageIncludesFilePathAndDuration() throws InterruptedExcept
5052
}
5153

5254
@Test
53-
void testCompletionOnlyPrintedOnce() {
55+
void testCompletionOnlyPrintedOnce() throws UnsupportedEncodingException {
5456
ConsoleExportProgressReporter reporter = new ConsoleExportProgressReporter("target/output.csv");
5557

5658
reporter.reportCompletion(100);
5759
reporter.reportCompletion(999999); // Should be ignored
5860

59-
String output = outContent.toString();
61+
String output = outContent.toString("UTF-8");
6062
int count = output.split("Export completed").length - 1;
6163
assertEquals(1, count, "Expected completion to be printed only once");
6264
}
6365

6466
@Test
65-
void testReportError_shouldPrintErrorMessageWithExceptionMessage() {
67+
void testReportError_shouldPrintErrorMessageWithExceptionMessage()
68+
throws UnsupportedEncodingException {
6669
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
6770
PrintStream originalErr = System.err;
68-
System.setErr(new PrintStream(errContent));
71+
System.setErr(new PrintStream(errContent, true, "UTF-8"));
6972

7073
try {
7174
String errorMessage = "Something went wrong";
7275
Throwable cause = new RuntimeException("Test exception");
7376

7477
ConsoleExportProgressReporter.reportError(errorMessage, cause);
7578

76-
String output = errContent.toString();
79+
String output = errContent.toString("UTF-8");
7780
assertTrue(
7881
output.contains("❌ Export failed: " + errorMessage), "Expected main error message");
7982
assertTrue(output.contains("Cause: " + cause.getMessage()), "Expected exception message");
@@ -83,17 +86,18 @@ void testReportError_shouldPrintErrorMessageWithExceptionMessage() {
8386
}
8487

8588
@Test
86-
void testReportError_shouldPrintMessageWithoutExceptionWhenNull() {
89+
void testReportError_shouldPrintMessageWithoutExceptionWhenNull()
90+
throws UnsupportedEncodingException {
8791
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
8892
PrintStream originalErr = System.err;
89-
System.setErr(new PrintStream(errContent));
93+
System.setErr(new PrintStream(errContent, true, "UTF-8"));
9094

9195
try {
9296
String errorMessage = "Directory not found";
9397

9498
ConsoleExportProgressReporter.reportError(errorMessage, null);
9599

96-
String output = errContent.toString();
100+
String output = errContent.toString("UTF-8");
97101
assertTrue(output.contains("❌ Export failed: " + errorMessage), "Expected error message");
98102
assertFalse(
99103
output.contains("Cause:"), "Should not print exception cause when throwable is null");

data-loader/cli/src/test/java/com/scalar/db/dataloader/cli/command/dataimport/ConsoleImportProgressListenerTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.scalar.db.dataloader.core.dataimport.transactionbatch.ImportTransactionBatchStatus;
1111
import java.io.ByteArrayOutputStream;
1212
import java.io.PrintStream;
13+
import java.io.UnsupportedEncodingException;
1314
import java.time.Duration;
1415
import java.time.Instant;
1516
import java.util.Arrays;
@@ -24,8 +25,8 @@ class ConsoleImportProgressListenerTest {
2425
private ConsoleImportProgressListener listener;
2526

2627
@BeforeEach
27-
void setUp() {
28-
System.setOut(new PrintStream(outContent));
28+
void setUp() throws UnsupportedEncodingException {
29+
System.setOut(new PrintStream(outContent, true, "UTF-8"));
2930
listener = new ConsoleImportProgressListener(Duration.ofMillis(100));
3031
}
3132

@@ -36,7 +37,7 @@ void tearDown() {
3637

3738
@Test
3839
void testOnDataChunkStartedAndCompleted_shouldTrackSuccessAndFailureCounts()
39-
throws InterruptedException {
40+
throws InterruptedException, UnsupportedEncodingException {
4041
int chunkId = 1;
4142
Instant start = Instant.now();
4243
TimeUnit.MILLISECONDS.sleep(50);
@@ -75,7 +76,7 @@ void testOnDataChunkStartedAndCompleted_shouldTrackSuccessAndFailureCounts()
7576
TimeUnit.MILLISECONDS.sleep(200); // Allow render
7677
listener.onAllDataChunksCompleted();
7778

78-
String output = outContent.toString();
79+
String output = outContent.toString("UTF-8");
7980

8081
assertTrue(output.contains("✓ Chunk 1"), "Expected chunk log line");
8182
assertTrue(output.contains("90 records imported successfully"), "Expected success count");
@@ -87,7 +88,7 @@ void testOnDataChunkStartedAndCompleted_shouldTrackSuccessAndFailureCounts()
8788

8889
@Test
8990
void testOnTransactionBatchFailed_shouldAccumulateFailuresInChunkFailureLogs()
90-
throws InterruptedException {
91+
throws InterruptedException, UnsupportedEncodingException {
9192
// Arrange: Build dummy failed batch
9293
ImportTaskResult task =
9394
ImportTaskResult.builder()
@@ -117,7 +118,7 @@ void testOnTransactionBatchFailed_shouldAccumulateFailuresInChunkFailureLogs()
117118
TimeUnit.MILLISECONDS.sleep(500);
118119

119120
// Assert
120-
String output = outContent.toString();
121+
String output = outContent.toString("UTF-8");
121122
System.out.println("OUTPUT:\n" + output); // Useful for debug
122123
assertTrue(
123124
output.contains("❌ Chunk id: 7, Transaction batch id: 2 failed"),
@@ -129,9 +130,10 @@ void testOnTransactionBatchFailed_shouldAccumulateFailuresInChunkFailureLogs()
129130
}
130131

131132
@Test
132-
void testOnAllDataChunksCompleted_shouldShutdownAndPrintFinalSummary() {
133+
void testOnAllDataChunksCompleted_shouldShutdownAndPrintFinalSummary()
134+
throws UnsupportedEncodingException {
133135
listener.onAllDataChunksCompleted();
134-
String output = outContent.toString();
136+
String output = outContent.toString("UTF-8");
135137
assertTrue(output.contains("✅ Import completed"), "Should print completion summary");
136138
}
137139

0 commit comments

Comments
 (0)