18
18
import static org .junit .jupiter .api .Assumptions .assumeTrue ;
19
19
20
20
import java .io .BufferedReader ;
21
+ import java .io .IOException ;
21
22
import java .io .InputStreamReader ;
22
23
import java .lang .ProcessBuilder .Redirect ;
24
+ import java .nio .file .FileVisitResult ;
23
25
import java .nio .file .Files ;
24
26
import java .nio .file .Path ;
27
+ import java .nio .file .SimpleFileVisitor ;
28
+ import java .nio .file .attribute .BasicFileAttributes ;
25
29
import java .rmi .NotBoundException ;
26
30
import java .rmi .RemoteException ;
27
31
import java .rmi .registry .LocateRegistry ;
40
44
import org .junit .jupiter .api .BeforeEach ;
41
45
import org .junit .jupiter .api .RepeatedTest ;
42
46
import org .junit .jupiter .api .Test ;
43
- import org .junit .jupiter .api .io .TempDir ;
44
47
45
48
import clipboard .ClipboardCommands ;
46
49
54
57
public class Test_org_eclipse_swt_dnd_Clipboard {
55
58
56
59
private static final int DEFAULT_TIMEOUT_MS = 10000 ;
57
- @ TempDir
58
- static Path tempFolder ;
59
60
static int uniqueId = 1 ;
60
61
private Display display ;
61
62
private Shell shell ;
@@ -64,6 +65,7 @@ public class Test_org_eclipse_swt_dnd_Clipboard {
64
65
private RTFTransfer rtfTransfer ;
65
66
private ClipboardCommands remote ;
66
67
private Process remoteClipboardProcess ;
68
+ private Path remoteClipboardTempDir ;
67
69
68
70
@ BeforeEach
69
71
public void setUp () {
@@ -139,21 +141,24 @@ private void startRemoteClipboardCommands() throws Exception {
139
141
* If the ClipboardTest starts to get more complicated, or other tests want to
140
142
* replicate this design element, then refactoring this is an option.
141
143
*/
144
+ remoteClipboardTempDir = Files .createTempDirectory ("swt-test-Clipboard" );
142
145
List .of ( //
143
146
"ClipboardTest" , //
144
147
"ClipboardCommands" , //
145
148
"ClipboardCommandsImpl" , //
146
149
"ClipboardTest$LocalHostOnlySocketFactory" //
147
150
).forEach ((f ) -> {
148
151
// 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" ));
150
154
});
151
155
152
156
String javaHome = System .getProperty ("java.home" );
153
157
String javaExe = javaHome + "/bin/java" + (SwtTestUtil .isWindowsOS ? ".exe" : "" );
154
158
assertTrue (Files .exists (Path .of (javaExe )));
155
159
156
- ProcessBuilder pb = new ProcessBuilder (javaExe , "clipboard.ClipboardTest" ).directory (tempFolder .toFile ());
160
+ ProcessBuilder pb = new ProcessBuilder (javaExe , "clipboard.ClipboardTest" )
161
+ .directory (remoteClipboardTempDir .toFile ());
157
162
pb .inheritIO ();
158
163
pb .redirectOutput (Redirect .PIPE );
159
164
remoteClipboardProcess = pb .start ();
@@ -213,6 +218,41 @@ private void startRemoteClipboardCommands() throws Exception {
213
218
}
214
219
215
220
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 {
216
256
try {
217
257
if (remote != null ) {
218
258
remote .stop ();
0 commit comments