Skip to content
Merged
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 @@ -18,10 +18,14 @@
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
Expand All @@ -40,7 +44,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import clipboard.ClipboardCommands;

Expand All @@ -54,8 +57,6 @@
public class Test_org_eclipse_swt_dnd_Clipboard {

private static final int DEFAULT_TIMEOUT_MS = 10000;
@TempDir
static Path tempFolder;
static int uniqueId = 1;
private Display display;
private Shell shell;
Expand All @@ -64,6 +65,7 @@ public class Test_org_eclipse_swt_dnd_Clipboard {
private RTFTransfer rtfTransfer;
private ClipboardCommands remote;
private Process remoteClipboardProcess;
private Path remoteClipboardTempDir;

@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -139,21 +141,24 @@ private void startRemoteClipboardCommands() throws Exception {
* If the ClipboardTest starts to get more complicated, or other tests want to
* replicate this design element, then refactoring this is an option.
*/
remoteClipboardTempDir = Files.createTempDirectory("swt-test-Clipboard");
List.of( //
"ClipboardTest", //
"ClipboardCommands", //
"ClipboardCommandsImpl", //
"ClipboardTest$LocalHostOnlySocketFactory" //
).forEach((f) -> {
// extract the files and put them in the temp directory
SwtTestUtil.getPath("/clipboard/" + f + ".class", tempFolder.resolve("clipboard/" + f + ".class"));
SwtTestUtil.getPath("/clipboard/" + f + ".class",
remoteClipboardTempDir.resolve("clipboard/" + f + ".class"));
});

String javaHome = System.getProperty("java.home");
String javaExe = javaHome + "/bin/java" + (SwtTestUtil.isWindowsOS ? ".exe" : "");
assertTrue(Files.exists(Path.of(javaExe)));

ProcessBuilder pb = new ProcessBuilder(javaExe, "clipboard.ClipboardTest").directory(tempFolder.toFile());
ProcessBuilder pb = new ProcessBuilder(javaExe, "clipboard.ClipboardTest")
.directory(remoteClipboardTempDir.toFile());
pb.inheritIO();
pb.redirectOutput(Redirect.PIPE);
remoteClipboardProcess = pb.start();
Expand Down Expand Up @@ -213,6 +218,41 @@ private void startRemoteClipboardCommands() throws Exception {
}

private void stopRemoteClipboardCommands() throws Exception {
try {
stopRemoteProcess();
} finally {
deleteRemoteTempDir();
}
}

private void deleteRemoteTempDir() {
if (remoteClipboardTempDir != null) {
// At this point the process is ideally destroyed - or at least the test will
// report a failure if it isn't. Clean up the extracted files, but don't
// fail test if we fail to delete
try {
Files.walkFileTree(remoteClipboardTempDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
System.err.println("SWT Warning: Failed to clean up temp directory " + remoteClipboardTempDir
+ " Error:" + e.toString());
e.printStackTrace();
}
}
}

private void stopRemoteProcess() throws RemoteException, InterruptedException {
try {
if (remote != null) {
remote.stop();
Expand Down
Loading