Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public static void afterClass() {
}

for (Error leak : leakedResources) {
// For some reason, printing to System.err in JUnit test has no effect
leak.printStackTrace (System.out);
leak.printStackTrace ();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @SyntevoAlex I found out where System.err was going.

}

if (0 != leakedResources.size ()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
Expand Down Expand Up @@ -764,4 +766,26 @@ static <T> T runOperationInThread(int timeoutMs, ExceptionalSupplier<T> supplier
T result = (T) supplierValue[0];
return result;
}

/**
* Capture any output on System.err
*
* This method does not capture output on stderr from C level, such as
* Gdk-CRITICAL messages.
*
* @param runnable to run while capturing output
* @return output on System.err
*/
public static String runWithCapturedStderr(Runnable runnable) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I've recently relied on AutoCloseable a lot for testing; it allows some good way of wrapping state relying less on Runnable.

For this case, it could go that way

class CaptureStderr implements AutoCloseable, Supplier<String> {
  ByteArrayOutputStream errContent
  CaptureStderr() {
     errContent = new ByteArrayOutputStream();
	 System.setErr(new PrintStream(errContent, true, StandardCharsets.UTF_8));
  }
  @Override
  public void close() {
    System.setErr(stderr);
    errContent.close();
  }
  @Override
  public String get() {
    return errContent.toString(StandardCharsets.UTF_8);
  }
}

and then tests can be written

try (new captureStdErr = new CaptureStdErr()) {
   // do stuff
  assertTrue(captureStdErr.get().contains("blah");
}

PrintStream originalErr = System.err;
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(errContent, true, StandardCharsets.UTF_8));
try {
runnable.run();
return errContent.toString(StandardCharsets.UTF_8);

} finally {
System.setErr(originalErr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,21 +60,17 @@ public void test_getMessage() {
public void test_printStackTrace() {

// test default SWTError

ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setErr(new PrintStream(out));
SWTError error = new SWTError();
error.printStackTrace();
assertTrue(out.size() > 0);
assertTrue(new String(out.toByteArray()).contains("test_printStackTrace"));
SWTError error1 = new SWTError();
String stderr = SwtTestUtil.runWithCapturedStderr(() -> {
error1.printStackTrace();
});
assertTrue(stderr.contains("test_printStackTrace"));

// test SWTError with code

out = new ByteArrayOutputStream();
System.setErr(new PrintStream(out));
error = new SWTError(SWT.ERROR_INVALID_ARGUMENT);
error.printStackTrace();
assertTrue(out.size() > 0);
assertTrue(new String(out.toByteArray()).contains("test_printStackTrace"));
SWTError error2 = new SWTError(SWT.ERROR_INVALID_ARGUMENT);
stderr = SwtTestUtil.runWithCapturedStderr(() -> {
error2.printStackTrace();
});
assertTrue(stderr.contains("test_printStackTrace"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -64,21 +61,17 @@ public void test_getMessage() {
public void test_printStackTrace() {

// test default SWTException

ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setErr(new PrintStream(out));
SWTException error = new SWTException();
error.printStackTrace();
assertTrue(out.size() > 0);
assertTrue(new String(out.toByteArray()).contains("test_printStackTrace"));
SWTException error1 = new SWTException();
String stderr = SwtTestUtil.runWithCapturedStderr(() -> {
error1.printStackTrace();
});
assertTrue(stderr.contains("test_printStackTrace"));

// test SWTException with code

out = new ByteArrayOutputStream();
System.setErr(new PrintStream(out));
error = new SWTException(SWT.ERROR_INVALID_ARGUMENT);
error.printStackTrace();
assertTrue(out.size() > 0);
assertTrue(new String(out.toByteArray()).contains("test_printStackTrace"));
SWTException error2 = new SWTException(SWT.ERROR_INVALID_ARGUMENT);
stderr = SwtTestUtil.runWithCapturedStderr(() -> {
error2.printStackTrace();
});
assertTrue(stderr.contains("test_printStackTrace"));
}
}
Loading