Skip to content

Commit 92db85c

Browse files
committed
Stop swallowing System.err in JUnit tests
test_printStackTrace overrides System.err with its own print stream to check functionality. But these tests did not restore System.err, so all System.err output for the rest of the test suite was lost into the last of these ByteArrayOutputStreams that was created. The tests themselves have little intrinsic value as they really test nothing of SWT itself, but it does help achieve code coverage. This was discovered when working on #2684 as I could not see the output when running all the tests. Since we no longer swallow errors, the "For some reason, printing to System.err[...]" comment is resolved and we can print leaks to stderr again.
1 parent c308fd1 commit 92db85c

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/AllNonBrowserTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ public static void afterClass() {
111111
}
112112

113113
for (Error leak : leakedResources) {
114-
// For some reason, printing to System.err in JUnit test has no effect
115-
leak.printStackTrace (System.out);
114+
leak.printStackTrace ();
116115
}
117116

118117
if (0 != leakedResources.size ()) {

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/SwtTestUtil.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import static org.junit.jupiter.api.Assertions.assertTrue;
2020
import static org.junit.jupiter.api.Assertions.fail;
2121

22+
import java.io.ByteArrayOutputStream;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.io.PrintStream;
26+
import java.nio.charset.StandardCharsets;
2527
import java.nio.file.Files;
2628
import java.nio.file.Path;
2729
import java.util.HashMap;
@@ -764,4 +766,26 @@ static <T> T runOperationInThread(int timeoutMs, ExceptionalSupplier<T> supplier
764766
T result = (T) supplierValue[0];
765767
return result;
766768
}
769+
770+
/**
771+
* Capture any output on System.err
772+
*
773+
* This method does not capture output on stderr from C level, such as
774+
* Gdk-CRITICAL messages.
775+
*
776+
* @param runnable to run while capturing output
777+
* @return output on System.err
778+
*/
779+
public static String runWithCapturedStderr(Runnable runnable) {
780+
PrintStream originalErr = System.err;
781+
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
782+
System.setErr(new PrintStream(errContent, true, StandardCharsets.UTF_8));
783+
try {
784+
runnable.run();
785+
return errContent.toString(StandardCharsets.UTF_8);
786+
787+
} finally {
788+
System.setErr(originalErr);
789+
}
790+
}
767791
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_SWTError.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertTrue;
1919

20-
import java.io.ByteArrayOutputStream;
21-
import java.io.PrintStream;
22-
2320
import org.eclipse.swt.SWT;
2421
import org.eclipse.swt.SWTError;
2522
import org.junit.jupiter.api.Test;
@@ -63,21 +60,17 @@ public void test_getMessage() {
6360
public void test_printStackTrace() {
6461

6562
// test default SWTError
66-
67-
ByteArrayOutputStream out = new ByteArrayOutputStream();
68-
System.setErr(new PrintStream(out));
69-
SWTError error = new SWTError();
70-
error.printStackTrace();
71-
assertTrue(out.size() > 0);
72-
assertTrue(new String(out.toByteArray()).contains("test_printStackTrace"));
63+
SWTError error1 = new SWTError();
64+
String stderr = SwtTestUtil.runWithCapturedStderr(() -> {
65+
error1.printStackTrace();
66+
});
67+
assertTrue(stderr.contains("test_printStackTrace"));
7368

7469
// test SWTError with code
75-
76-
out = new ByteArrayOutputStream();
77-
System.setErr(new PrintStream(out));
78-
error = new SWTError(SWT.ERROR_INVALID_ARGUMENT);
79-
error.printStackTrace();
80-
assertTrue(out.size() > 0);
81-
assertTrue(new String(out.toByteArray()).contains("test_printStackTrace"));
70+
SWTError error2 = new SWTError(SWT.ERROR_INVALID_ARGUMENT);
71+
stderr = SwtTestUtil.runWithCapturedStderr(() -> {
72+
error2.printStackTrace();
73+
});
74+
assertTrue(stderr.contains("test_printStackTrace"));
8275
}
8376
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_SWTException.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertTrue;
1919

20-
import java.io.ByteArrayOutputStream;
21-
import java.io.PrintStream;
22-
2320
import org.eclipse.swt.SWT;
2421
import org.eclipse.swt.SWTException;
2522
import org.junit.jupiter.api.Test;
@@ -64,21 +61,17 @@ public void test_getMessage() {
6461
public void test_printStackTrace() {
6562

6663
// test default SWTException
67-
68-
ByteArrayOutputStream out = new ByteArrayOutputStream();
69-
System.setErr(new PrintStream(out));
70-
SWTException error = new SWTException();
71-
error.printStackTrace();
72-
assertTrue(out.size() > 0);
73-
assertTrue(new String(out.toByteArray()).contains("test_printStackTrace"));
64+
SWTException error1 = new SWTException();
65+
String stderr = SwtTestUtil.runWithCapturedStderr(() -> {
66+
error1.printStackTrace();
67+
});
68+
assertTrue(stderr.contains("test_printStackTrace"));
7469

7570
// test SWTException with code
76-
77-
out = new ByteArrayOutputStream();
78-
System.setErr(new PrintStream(out));
79-
error = new SWTException(SWT.ERROR_INVALID_ARGUMENT);
80-
error.printStackTrace();
81-
assertTrue(out.size() > 0);
82-
assertTrue(new String(out.toByteArray()).contains("test_printStackTrace"));
71+
SWTException error2 = new SWTException(SWT.ERROR_INVALID_ARGUMENT);
72+
stderr = SwtTestUtil.runWithCapturedStderr(() -> {
73+
error2.printStackTrace();
74+
});
75+
assertTrue(stderr.contains("test_printStackTrace"));
8376
}
8477
}

0 commit comments

Comments
 (0)