Skip to content

Commit e0be5f5

Browse files
committed
Migrate BasicAliasTest and SyncAliasTest to JUnit 5 #903
This change migrates the alias tests (BasicAliasTest and SyncAliasTest) to JUnit 5. It replaces the usage of the WorkspaceTestRule with the WorkspaceResetExtension as. The deletion of files and file stores via the WorkspaceTestRule is replaced with dedicated file delete operations exactly where they are required. The creation of temporary file stores is replaced with the usage of temporary directories that are automatically cleaned up via the according JUnit 5 extension. The recursive folder deletion implementation from the SessionCustomizationUtil is used by generalizing it to the FileSystemHelper class. Contributes to #903
1 parent 3c40369 commit e0be5f5

File tree

7 files changed

+199
-180
lines changed

7 files changed

+199
-180
lines changed

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/alias/BasicAliasTest.java

Lines changed: 117 additions & 121 deletions
Large diffs are not rendered by default.

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/alias/SyncAliasTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@
3030
import org.eclipse.core.runtime.IStatus;
3131
import org.eclipse.core.runtime.NullProgressMonitor;
3232
import org.eclipse.core.runtime.Status;
33-
import org.eclipse.core.tests.resources.WorkspaceTestRule;
34-
import org.junit.Rule;
35-
import org.junit.Test;
33+
import org.eclipse.core.tests.resources.util.WorkspaceResetExtension;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.extension.ExtendWith;
3636

3737
/**
3838
* Tests out of sync cases and refreshLocal in the presence of duplicate
3939
* resources.
4040
*/
41+
@ExtendWith(WorkspaceResetExtension.class)
4142
public class SyncAliasTest {
4243

43-
@Rule
44-
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();
45-
4644
/**
4745
* Tests synchronization in presence of nested projects.
4846
* See bug 244315 for details.

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTestUtil.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.OutputStream;
3030
import java.nio.charset.StandardCharsets;
3131
import java.nio.file.Files;
32+
import java.nio.file.Path;
3233
import java.nio.file.attribute.FileTime;
3334
import java.util.ArrayList;
3435
import java.util.List;
@@ -769,4 +770,19 @@ public static String[] findAvailableDevices() {
769770
return devices;
770771
}
771772

773+
/**
774+
* Wraps the given path into a canonical IPath.
775+
*/
776+
public static IPath wrapInCanonicalIPath(Path path) throws IOException {
777+
return IPath.fromOSString(path.toFile().getCanonicalPath());
778+
}
779+
780+
/**
781+
* Returns a file store for the given path referring to the local file system.
782+
*/
783+
public static IFileStore getFileStore(Path path) throws IOException {
784+
IPath canonicalIPath = wrapInCanonicalIPath(path);
785+
return EFS.getLocalFileSystem().getStore(canonicalIPath);
786+
}
787+
772788
}

runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/FileSystemHelper.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.harness;
1515

16+
import static java.util.Comparator.reverseOrder;
1617
import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;
1718
import static org.eclipse.core.tests.harness.TestHarnessPlugin.log;
1819

1920
import java.io.BufferedReader;
2021
import java.io.File;
2122
import java.io.IOException;
2223
import java.io.InputStreamReader;
23-
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import org.eclipse.core.runtime.ILog;
2427
import org.eclipse.core.runtime.IPath;
2528
import org.eclipse.core.runtime.IStatus;
2629
import org.eclipse.core.runtime.Platform;
2730
import org.eclipse.core.runtime.Status;
31+
import org.junit.function.ThrowingRunnable;
2832

2933
/**
3034
* Home for file system-related utility methods.
@@ -192,4 +196,59 @@ public static boolean canCreateSymLinks() throws IOException {
192196
return canCreateSymLinks.booleanValue();
193197
}
194198

199+
/**
200+
* Deletes the file or folder at the given path with all its contents.
201+
*
202+
* @param path the path of the file or folder to delete
203+
* @throws IOException if traversing the file tree for deletion fails
204+
*/
205+
public static void deleteRecursively(Path path) throws IOException {
206+
Files.walk(path) //
207+
.sorted(reverseOrder()) //
208+
.forEach(FileSystemHelper::deleteSilently);
209+
}
210+
211+
private static void deleteSilently(Path path) {
212+
try {
213+
Files.delete(path);
214+
} catch (IOException exception) {
215+
ILog.get().log(new Status(IStatus.WARNING, PI_HARNESS,
216+
"Test file or directory could not be removed: " + path, exception));
217+
}
218+
}
219+
220+
/**
221+
* Deletes the file or folder at the given path with all its contents when the
222+
* Java runtime is shut down.
223+
*
224+
* @param path the path of the file or folder to delete on shutdown
225+
*/
226+
public static void deleteOnShutdownRecursively(Path path) {
227+
Runnable deleteDirectory = () -> {
228+
try {
229+
deleteRecursively(path);
230+
} catch (IOException exception) {
231+
ILog.get().log(new Status(IStatus.WARNING, PI_HARNESS, "Error when removing test directory: " + path,
232+
exception));
233+
}
234+
};
235+
Runtime.getRuntime().addShutdownHook(new Thread(deleteDirectory));
236+
}
237+
238+
/**
239+
* Recursively deletes the folder at the given path after executing the given
240+
* runnable.
241+
*
242+
* @param pathToDelete the path of the file or folder to delete
243+
* @param operationToExecute the operation to execute
244+
* @throws Throwable if a throwable is thrown in the operation to execute
245+
*/
246+
public static void deleteAfterExecution(Path pathToDelete, ThrowingRunnable operationToExecute) throws Throwable {
247+
try {
248+
operationToExecute.run();
249+
} finally {
250+
FileSystemHelper.deleteRecursively(pathToDelete);
251+
}
252+
}
253+
195254
}

runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/session/customization/CustomSessionConfigurationImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
package org.eclipse.core.tests.harness.session.customization;
1212

1313
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.eclipse.core.tests.harness.session.customization.SessionCustomizationUtil.deleteOnShutdownRecursively;
14+
import static org.eclipse.core.tests.harness.FileSystemHelper.deleteOnShutdownRecursively;
1515
import static org.junit.Assert.assertTrue;
1616

1717
import java.io.File;

runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/session/customization/CustomSessionWorkspaceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*******************************************************************************/
1111
package org.eclipse.core.tests.harness.session.customization;
1212

13-
import static org.eclipse.core.tests.harness.session.customization.SessionCustomizationUtil.deleteOnShutdownRecursively;
13+
import static org.eclipse.core.tests.harness.FileSystemHelper.deleteOnShutdownRecursively;
1414

1515
import java.io.IOException;
1616
import java.nio.file.Files;

runtime/tests/org.eclipse.core.tests.harness/src/org/eclipse/core/tests/harness/session/customization/SessionCustomizationUtil.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)