diff options
author | Matthias Sohn <matthias.sohn@sap.com> | 2025-01-29 12:28:35 +0000 |
---|---|---|
committer | Gerrit Code Review <support@gerrithub.io> | 2025-01-29 12:28:35 +0000 |
commit | 7c0ae6110c9cad89eb06f0d23b9604f202066545 (patch) | |
tree | 2710c04cc62ebd7f1c261285ad790b423ad19c52 | |
parent | 32b9a9523c2a8100f9ac22b251a10c3afb7d1144 (diff) | |
parent | aadfce7e87d2f3536fdc360f562cd0c190a397cd (diff) | |
download | jgit-7c0ae6110c9cad89eb06f0d23b9604f202066545.tar.gz jgit-7c0ae6110c9cad89eb06f0d23b9604f202066545.zip |
Merge "Use junit TemporaryFolder to cleanup files created by tests"
-rw-r--r-- | org.eclipse.jgit.junit/.settings/.api_filters | 11 | ||||
-rw-r--r-- | org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java | 64 |
2 files changed, 25 insertions, 50 deletions
diff --git a/org.eclipse.jgit.junit/.settings/.api_filters b/org.eclipse.jgit.junit/.settings/.api_filters new file mode 100644 index 0000000000..27815301c2 --- /dev/null +++ b/org.eclipse.jgit.junit/.settings/.api_filters @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<component id="org.eclipse.jgit.junit" version="2"> + <resource path="src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java" type="org.eclipse.jgit.junit.LocalDiskRepositoryTestCase"> + <filter id="336658481"> + <message_arguments> + <message_argument value="org.eclipse.jgit.junit.LocalDiskRepositoryTestCase"/> + <message_argument value="testRoot"/> + </message_arguments> + </filter> + </resource> +</component> diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java index c15fc9921e..0d20f6488a 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java @@ -26,14 +26,13 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Set; import java.util.TreeSet; -import java.util.concurrent.ConcurrentHashMap; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.internal.storage.file.FileRepository; -import org.eclipse.jgit.internal.util.ShutdownHook; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; @@ -48,6 +47,7 @@ import org.eclipse.jgit.util.SystemReader; import org.junit.After; import org.junit.Before; import org.junit.Rule; +import org.junit.rules.TemporaryFolder; import org.junit.rules.TestName; /** @@ -85,6 +85,16 @@ public abstract class LocalDiskRepositoryTestCase { protected MockSystemReader mockSystemReader; private final Set<Repository> toClose = new HashSet<>(); + + /** + * Temporary test root directory for files created by tests. + * @since 7.2 + */ + @Rule + public TemporaryFolder testRoot = new TemporaryFolder(); + + Random rand = new Random(); + private File tmp; private File homeDir; @@ -115,11 +125,8 @@ public abstract class LocalDiskRepositoryTestCase { */ @Before public void setUp() throws Exception { - tmp = File.createTempFile("jgit_" + getTestName() + '_', "_tmp"); - Cleanup.deleteOnShutdown(tmp); - if (!tmp.delete() || !tmp.mkdir()) { - throw new IOException("Cannot create " + tmp); - } + tmp = testRoot.newFolder(getTestName() + rand.nextInt()); + mockSystemReader = new MockSystemReader(); SystemReader.setInstance(mockSystemReader); @@ -220,12 +227,6 @@ public abstract class LocalDiskRepositoryTestCase { System.gc(); } FS.DETECTED.setUserHome(homeDir); - if (tmp != null) { - recursiveDelete(tmp, false, true); - } - if (tmp != null && !tmp.exists()) { - Cleanup.removed(tmp); - } SystemReader.setInstance(null); } @@ -624,41 +625,4 @@ public abstract class LocalDiskRepositoryTestCase { private static HashMap<String, String> cloneEnv() { return new HashMap<>(System.getenv()); } - - private static final class Cleanup { - private static final Cleanup INSTANCE = new Cleanup(); - - static { - ShutdownHook.INSTANCE.register(() -> INSTANCE.onShutdown()); - } - - private final Set<File> toDelete = ConcurrentHashMap.newKeySet(); - - private Cleanup() { - // empty - } - - static void deleteOnShutdown(File tmp) { - INSTANCE.toDelete.add(tmp); - } - - static void removed(File tmp) { - INSTANCE.toDelete.remove(tmp); - } - - private void onShutdown() { - // On windows accidentally open files or memory - // mapped regions may prevent files from being deleted. - // Suggesting a GC increases the likelihood that our - // test repositories actually get removed after the - // tests, even in the case of failure. - System.gc(); - synchronized (this) { - boolean silent = false; - boolean failOnError = false; - for (File tmp : toDelete) - recursiveDelete(tmp, silent, failOnError); - } - } - } } |