1818import static org .junit .jupiter .api .Assumptions .assumeTrue ;
1919
2020import java .io .BufferedReader ;
21+ import java .io .IOException ;
2122import java .io .InputStreamReader ;
2223import java .lang .ProcessBuilder .Redirect ;
24+ import java .nio .file .FileVisitResult ;
2325import java .nio .file .Files ;
2426import java .nio .file .Path ;
27+ import java .nio .file .SimpleFileVisitor ;
28+ import java .nio .file .attribute .BasicFileAttributes ;
2529import java .rmi .NotBoundException ;
2630import java .rmi .RemoteException ;
2731import java .rmi .registry .LocateRegistry ;
4044import org .junit .jupiter .api .BeforeEach ;
4145import org .junit .jupiter .api .RepeatedTest ;
4246import org .junit .jupiter .api .Test ;
43- import org .junit .jupiter .api .io .TempDir ;
4447
4548import clipboard .ClipboardCommands ;
4649
5457public 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