1515
1616import org .eclipse .core .resources .IProject ;
1717import org .eclipse .core .resources .IResource ;
18+ import org .eclipse .core .runtime .CoreException ;
1819import org .junit .jupiter .api .AfterEach ;
1920import org .junit .jupiter .api .BeforeEach ;
2021import org .junit .jupiter .api .TestInfo ;
@@ -35,8 +36,38 @@ public void setUpProject(TestInfo testInfo) throws Exception {
3536
3637 @ AfterEach
3738 public void tearDownProject () throws Exception {
38- if (project != null ) {
39- project .delete (IResource .FORCE , null );
39+ if (project != null && project .exists ()) {
40+ deleteProjectWithRetries (project , 10 , 500 );
41+ }
42+ }
43+
44+ /**
45+ * Mitigation for potential
46+ * <code>java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process</code>
47+ * when deleting a project.
48+ */
49+ private static void deleteProjectWithRetries (IProject project , int maxAttempts , long delayMillis )
50+ throws CoreException {
51+ for (int attempt = 1 ; attempt <= maxAttempts ; attempt ++) {
52+ try {
53+ if (!project .exists ()) {
54+ break ;
55+ }
56+ project .close (null );
57+ project .delete (IResource .FORCE | IResource .ALWAYS_DELETE_PROJECT_CONTENT , null );
58+ break ;
59+ } catch (CoreException ex ) {
60+ if (attempt == maxAttempts ) {
61+ throw ex ;
62+ }
63+ try {
64+ Thread .sleep (delayMillis );
65+ } catch (InterruptedException ie ) {
66+ Thread .currentThread ().interrupt ();
67+ ex .printStackTrace ();
68+ break ;
69+ }
70+ }
4071 }
4172 }
4273}
0 commit comments