Skip to content

Commit

Permalink
Migrate BasicAliasTest and SyncAliasTest to JUnit 5 #903
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HeikoKlare committed Jul 12, 2024
1 parent 3c40369 commit e0be5f5
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 180 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,17 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.tests.resources.WorkspaceTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.eclipse.core.tests.resources.util.WorkspaceResetExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Tests out of sync cases and refreshLocal in the presence of duplicate
* resources.
*/
@ExtendWith(WorkspaceResetExtension.class)
public class SyncAliasTest {

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

/**
* Tests synchronization in presence of nested projects.
* See bug 244315 for details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -769,4 +770,19 @@ public static String[] findAvailableDevices() {
return devices;
}

/**
* Wraps the given path into a canonical IPath.
*/
public static IPath wrapInCanonicalIPath(Path path) throws IOException {
return IPath.fromOSString(path.toFile().getCanonicalPath());
}

/**
* Returns a file store for the given path referring to the local file system.
*/
public static IFileStore getFileStore(Path path) throws IOException {
IPath canonicalIPath = wrapInCanonicalIPath(path);
return EFS.getLocalFileSystem().getStore(canonicalIPath);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@
*******************************************************************************/
package org.eclipse.core.tests.harness;

import static java.util.Comparator.reverseOrder;
import static org.eclipse.core.tests.harness.TestHarnessPlugin.PI_HARNESS;
import static org.eclipse.core.tests.harness.TestHarnessPlugin.log;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.junit.function.ThrowingRunnable;

/**
* Home for file system-related utility methods.
Expand Down Expand Up @@ -192,4 +196,59 @@ public static boolean canCreateSymLinks() throws IOException {
return canCreateSymLinks.booleanValue();
}

/**
* Deletes the file or folder at the given path with all its contents.
*
* @param path the path of the file or folder to delete
* @throws IOException if traversing the file tree for deletion fails
*/
public static void deleteRecursively(Path path) throws IOException {
Files.walk(path) //
.sorted(reverseOrder()) //
.forEach(FileSystemHelper::deleteSilently);
}

private static void deleteSilently(Path path) {
try {
Files.delete(path);
} catch (IOException exception) {
ILog.get().log(new Status(IStatus.WARNING, PI_HARNESS,
"Test file or directory could not be removed: " + path, exception));
}
}

/**
* Deletes the file or folder at the given path with all its contents when the
* Java runtime is shut down.
*
* @param path the path of the file or folder to delete on shutdown
*/
public static void deleteOnShutdownRecursively(Path path) {
Runnable deleteDirectory = () -> {
try {
deleteRecursively(path);
} catch (IOException exception) {
ILog.get().log(new Status(IStatus.WARNING, PI_HARNESS, "Error when removing test directory: " + path,
exception));
}
};
Runtime.getRuntime().addShutdownHook(new Thread(deleteDirectory));
}

/**
* Recursively deletes the folder at the given path after executing the given
* runnable.
*
* @param pathToDelete the path of the file or folder to delete
* @param operationToExecute the operation to execute
* @throws Throwable if a throwable is thrown in the operation to execute
*/
public static void deleteAfterExecution(Path pathToDelete, ThrowingRunnable operationToExecute) throws Throwable {
try {
operationToExecute.run();
} finally {
FileSystemHelper.deleteRecursively(pathToDelete);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package org.eclipse.core.tests.harness.session.customization;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.core.tests.harness.session.customization.SessionCustomizationUtil.deleteOnShutdownRecursively;
import static org.eclipse.core.tests.harness.FileSystemHelper.deleteOnShutdownRecursively;
import static org.junit.Assert.assertTrue;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*******************************************************************************/
package org.eclipse.core.tests.harness.session.customization;

import static org.eclipse.core.tests.harness.session.customization.SessionCustomizationUtil.deleteOnShutdownRecursively;
import static org.eclipse.core.tests.harness.FileSystemHelper.deleteOnShutdownRecursively;

import java.io.IOException;
import java.nio.file.Files;
Expand Down

This file was deleted.

0 comments on commit e0be5f5

Please sign in to comment.