1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-20 14:54:31 +01:00

AbstractWithPreferencesTest: Clean up files after every test

Previously, this used the DeleteFilesOnShutdown class and a shutdown
hook, which would delete the files only after shutdown. However, the
shutdown handler would be re-added for every testcase, potentially
leading to a lot of threads trying to delete the same files.

This uses an alternative: Just keep a list of files to delete inside the
testcase and use an @After handler to delete the files directly after
each usecase.
This commit is contained in:
Matthijs Kooijman 2020-05-06 20:28:53 +02:00 committed by Cristian Maglie
parent 5700d2b539
commit 0e4c900252

View File

@ -29,17 +29,27 @@
package processing.app;
import cc.arduino.files.DeleteFilesOnShutdown;
import org.junit.Before;
import org.junit.After;
import processing.app.helpers.FileUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Random;
import java.util.List;
import java.util.LinkedList;
public abstract class AbstractWithPreferencesTest {
/**
* Files or directories that will be deleted after each test.
* Subclasses can add files here in @Test or @Before functions.
*/
protected List<File> deleteAfter = new LinkedList<File>();
@Before
public void init() throws Exception {
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
BaseNoGui.initPlatform();
BaseNoGui.getPlatform().init();
PreferencesData.init(null);
@ -48,7 +58,13 @@ public abstract class AbstractWithPreferencesTest {
BaseNoGui.initPackages();
Base.untitledFolder = FileUtils.createTempFolder("untitled" + new Random().nextInt(Integer.MAX_VALUE), ".tmp");
DeleteFilesOnShutdown.add(Base.untitledFolder);
deleteAfter.add(Base.untitledFolder);
}
@After
public void cleanup() throws IOException {
for (File f : deleteAfter)
FileUtils.recursiveDelete(f);
deleteAfter = new LinkedList<File>();
}
}