Skip to content

Commit 749ed1a

Browse files
committed
Avoid failing test because temporary directory cannot be deleted
On Windows the temporary directory sometimes cannot be deleted because the files are still locked and you get an error like: The process cannot access the file because it is being used by another process This change allows the deletion of the temp directory to fail without failing the whole test. In case the temp dir can't be deleted a warning is printed on System.err Fixes #2568
1 parent 254e91f commit 749ed1a

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
import static org.junit.jupiter.api.Assumptions.assumeTrue;
1919

2020
import java.io.BufferedReader;
21+
import java.io.IOException;
2122
import java.io.InputStreamReader;
2223
import java.lang.ProcessBuilder.Redirect;
24+
import java.nio.file.FileVisitResult;
2325
import java.nio.file.Files;
2426
import java.nio.file.Path;
27+
import java.nio.file.SimpleFileVisitor;
28+
import java.nio.file.attribute.BasicFileAttributes;
2529
import java.rmi.NotBoundException;
2630
import java.rmi.RemoteException;
2731
import java.rmi.registry.LocateRegistry;
@@ -40,7 +44,6 @@
4044
import org.junit.jupiter.api.BeforeEach;
4145
import org.junit.jupiter.api.RepeatedTest;
4246
import org.junit.jupiter.api.Test;
43-
import org.junit.jupiter.api.io.TempDir;
4447

4548
import clipboard.ClipboardCommands;
4649

@@ -54,8 +57,6 @@
5457
public class Test_org_eclipse_swt_dnd_Clipboard {
5558

5659
private static final int DEFAULT_TIMEOUT_MS = 10000;
57-
@TempDir
58-
static Path tempFolder;
5960
static int uniqueId = 1;
6061
private Display display;
6162
private Shell shell;
@@ -64,6 +65,7 @@ public class Test_org_eclipse_swt_dnd_Clipboard {
6465
private RTFTransfer rtfTransfer;
6566
private ClipboardCommands remote;
6667
private Process remoteClipboardProcess;
68+
private Path remoteClipboardTempDir;
6769

6870
@BeforeEach
6971
public void setUp() {
@@ -139,21 +141,24 @@ private void startRemoteClipboardCommands() throws Exception {
139141
* If the ClipboardTest starts to get more complicated, or other tests want to
140142
* replicate this design element, then refactoring this is an option.
141143
*/
144+
remoteClipboardTempDir = Files.createTempDirectory("swt-test-Clipboard");
142145
List.of( //
143146
"ClipboardTest", //
144147
"ClipboardCommands", //
145148
"ClipboardCommandsImpl", //
146149
"ClipboardTest$LocalHostOnlySocketFactory" //
147150
).forEach((f) -> {
148151
// extract the files and put them in the temp directory
149-
SwtTestUtil.getPath("/clipboard/" + f + ".class", tempFolder.resolve("clipboard/" + f + ".class"));
152+
SwtTestUtil.getPath("/clipboard/" + f + ".class",
153+
remoteClipboardTempDir.resolve("clipboard/" + f + ".class"));
150154
});
151155

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

156-
ProcessBuilder pb = new ProcessBuilder(javaExe, "clipboard.ClipboardTest").directory(tempFolder.toFile());
160+
ProcessBuilder pb = new ProcessBuilder(javaExe, "clipboard.ClipboardTest")
161+
.directory(remoteClipboardTempDir.toFile());
157162
pb.inheritIO();
158163
pb.redirectOutput(Redirect.PIPE);
159164
remoteClipboardProcess = pb.start();
@@ -213,6 +218,41 @@ private void startRemoteClipboardCommands() throws Exception {
213218
}
214219

215220
private void stopRemoteClipboardCommands() throws Exception {
221+
try {
222+
stopRemoteProcess();
223+
} finally {
224+
deleteRemoteTempDir();
225+
}
226+
}
227+
228+
private void deleteRemoteTempDir() {
229+
if (remoteClipboardTempDir != null) {
230+
// At this point the process is ideally destroyed - or at least the test will
231+
// report a failure if it isn't. Clean up the extracted files, but don't
232+
// fail test if we fail to delete
233+
try {
234+
Files.walkFileTree(remoteClipboardTempDir, new SimpleFileVisitor<Path>() {
235+
@Override
236+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
237+
Files.delete(file);
238+
return FileVisitResult.CONTINUE;
239+
}
240+
241+
@Override
242+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
243+
Files.delete(dir);
244+
return FileVisitResult.CONTINUE;
245+
}
246+
});
247+
} catch (IOException e) {
248+
System.err.println("SWT Warning: Failed to clean up temp directory " + remoteClipboardTempDir
249+
+ " Error:" + e.toString());
250+
e.printStackTrace();
251+
}
252+
}
253+
}
254+
255+
private void stopRemoteProcess() throws RemoteException, InterruptedException {
216256
try {
217257
if (remote != null) {
218258
remote.stop();

0 commit comments

Comments
 (0)