You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LocalDiskRepositoryTestCase.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.junit;
  46. import static java.nio.charset.StandardCharsets.UTF_8;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.io.PrintStream;
  52. import java.time.Instant;
  53. import java.util.ArrayList;
  54. import java.util.Collections;
  55. import java.util.HashMap;
  56. import java.util.HashSet;
  57. import java.util.List;
  58. import java.util.Map;
  59. import java.util.Set;
  60. import java.util.TreeSet;
  61. import org.eclipse.jgit.dircache.DirCache;
  62. import org.eclipse.jgit.dircache.DirCacheEntry;
  63. import org.eclipse.jgit.internal.storage.file.FileRepository;
  64. import org.eclipse.jgit.lib.ConfigConstants;
  65. import org.eclipse.jgit.lib.Constants;
  66. import org.eclipse.jgit.lib.ObjectId;
  67. import org.eclipse.jgit.lib.PersonIdent;
  68. import org.eclipse.jgit.lib.Repository;
  69. import org.eclipse.jgit.lib.RepositoryCache;
  70. import org.eclipse.jgit.storage.file.FileBasedConfig;
  71. import org.eclipse.jgit.storage.file.WindowCacheConfig;
  72. import org.eclipse.jgit.util.FS;
  73. import org.eclipse.jgit.util.FileUtils;
  74. import org.eclipse.jgit.util.SystemReader;
  75. import org.junit.After;
  76. import org.junit.Before;
  77. /**
  78. * JUnit TestCase with specialized support for temporary local repository.
  79. * <p>
  80. * A temporary directory is created for each test, allowing each test to use a
  81. * fresh environment. The temporary directory is cleaned up after the test ends.
  82. * <p>
  83. * Callers should not use {@link org.eclipse.jgit.lib.RepositoryCache} from
  84. * within these tests as it may wedge file descriptors open past the end of the
  85. * test.
  86. * <p>
  87. * A system property {@code jgit.junit.usemmap} defines whether memory mapping
  88. * is used. Memory mapping has an effect on the file system, in that memory
  89. * mapped files in Java cannot be deleted as long as the mapped arrays have not
  90. * been reclaimed by the garbage collector. The programmer cannot control this
  91. * with precision, so temporary files may hang around longer than desired during
  92. * a test, or tests may fail altogether if there is insufficient file
  93. * descriptors or address space for the test process.
  94. */
  95. public abstract class LocalDiskRepositoryTestCase {
  96. private static final boolean useMMAP = "true".equals(System
  97. .getProperty("jgit.junit.usemmap"));
  98. /** A fake (but stable) identity for author fields in the test. */
  99. protected PersonIdent author;
  100. /** A fake (but stable) identity for committer fields in the test. */
  101. protected PersonIdent committer;
  102. /**
  103. * A {@link SystemReader} used to coordinate time, envars, etc.
  104. * @since 4.2
  105. */
  106. protected MockSystemReader mockSystemReader;
  107. private final Set<Repository> toClose = new HashSet<>();
  108. private File tmp;
  109. /**
  110. * Setup test
  111. *
  112. * @throws Exception
  113. */
  114. @Before
  115. public void setUp() throws Exception {
  116. tmp = File.createTempFile("jgit_test_", "_tmp");
  117. CleanupThread.deleteOnShutdown(tmp);
  118. if (!tmp.delete() || !tmp.mkdir())
  119. throw new IOException("Cannot create " + tmp);
  120. mockSystemReader = new MockSystemReader();
  121. SystemReader.setInstance(mockSystemReader);
  122. // Measure timer resolution before the test to avoid time critical tests
  123. // are affected by time needed for measurement.
  124. // The MockSystemReader must be configured first since we need to use
  125. // the same one here
  126. FS.getFileStoreAttributes(tmp.toPath().getParent());
  127. FileBasedConfig userConfig = new FileBasedConfig(
  128. new File(tmp, "usergitconfig"), FS.DETECTED);
  129. // We have to set autoDetach to false for tests, because tests expect to be able
  130. // to clean up by recursively removing the repository, and background GC might be
  131. // in the middle of writing or deleting files, which would disrupt this.
  132. userConfig.setBoolean(ConfigConstants.CONFIG_GC_SECTION,
  133. null, ConfigConstants.CONFIG_KEY_AUTODETACH, false);
  134. userConfig.save();
  135. mockSystemReader.setUserGitConfig(userConfig);
  136. ceilTestDirectories(getCeilings());
  137. author = new PersonIdent("J. Author", "jauthor@example.com");
  138. committer = new PersonIdent("J. Committer", "jcommitter@example.com");
  139. final WindowCacheConfig c = new WindowCacheConfig();
  140. c.setPackedGitLimit(128 * WindowCacheConfig.KB);
  141. c.setPackedGitWindowSize(8 * WindowCacheConfig.KB);
  142. c.setPackedGitMMAP(useMMAP);
  143. c.setDeltaBaseCacheLimit(8 * WindowCacheConfig.KB);
  144. c.install();
  145. }
  146. /**
  147. * Get temporary directory.
  148. *
  149. * @return the temporary directory
  150. */
  151. protected File getTemporaryDirectory() {
  152. return tmp.getAbsoluteFile();
  153. }
  154. /**
  155. * Get list of ceiling directories
  156. *
  157. * @return list of ceiling directories
  158. */
  159. protected List<File> getCeilings() {
  160. return Collections.singletonList(getTemporaryDirectory());
  161. }
  162. private void ceilTestDirectories(List<File> ceilings) {
  163. mockSystemReader.setProperty(Constants.GIT_CEILING_DIRECTORIES_KEY, makePath(ceilings));
  164. }
  165. private static String makePath(List<?> objects) {
  166. final StringBuilder stringBuilder = new StringBuilder();
  167. for (Object object : objects) {
  168. if (stringBuilder.length() > 0)
  169. stringBuilder.append(File.pathSeparatorChar);
  170. stringBuilder.append(object.toString());
  171. }
  172. return stringBuilder.toString();
  173. }
  174. /**
  175. * Tear down the test
  176. *
  177. * @throws Exception
  178. */
  179. @After
  180. public void tearDown() throws Exception {
  181. RepositoryCache.clear();
  182. for (Repository r : toClose)
  183. r.close();
  184. toClose.clear();
  185. // Since memory mapping is controlled by the GC we need to
  186. // tell it this is a good time to clean up and unlock
  187. // memory mapped files.
  188. //
  189. if (useMMAP)
  190. System.gc();
  191. if (tmp != null)
  192. recursiveDelete(tmp, false, true);
  193. if (tmp != null && !tmp.exists())
  194. CleanupThread.removed(tmp);
  195. SystemReader.setInstance(null);
  196. }
  197. /**
  198. * Increment the {@link #author} and {@link #committer} times.
  199. */
  200. protected void tick() {
  201. mockSystemReader.tick(5 * 60);
  202. final long now = mockSystemReader.getCurrentTime();
  203. final int tz = mockSystemReader.getTimezone(now);
  204. author = new PersonIdent(author, now, tz);
  205. committer = new PersonIdent(committer, now, tz);
  206. }
  207. /**
  208. * Recursively delete a directory, failing the test if the delete fails.
  209. *
  210. * @param dir
  211. * the recursively directory to delete, if present.
  212. */
  213. protected void recursiveDelete(File dir) {
  214. recursiveDelete(dir, false, true);
  215. }
  216. private static boolean recursiveDelete(final File dir,
  217. boolean silent, boolean failOnError) {
  218. assert !(silent && failOnError);
  219. int options = FileUtils.RECURSIVE | FileUtils.RETRY
  220. | FileUtils.SKIP_MISSING;
  221. if (silent) {
  222. options |= FileUtils.IGNORE_ERRORS;
  223. }
  224. try {
  225. FileUtils.delete(dir, options);
  226. } catch (IOException e) {
  227. reportDeleteFailure(failOnError, dir, e);
  228. return !failOnError;
  229. }
  230. return true;
  231. }
  232. private static void reportDeleteFailure(boolean failOnError, File f,
  233. Exception cause) {
  234. String severity = failOnError ? "ERROR" : "WARNING";
  235. String msg = severity + ": Failed to delete " + f;
  236. if (failOnError) {
  237. fail(msg);
  238. } else {
  239. System.err.println(msg);
  240. }
  241. cause.printStackTrace(new PrintStream(System.err));
  242. }
  243. /** Constant <code>MOD_TIME=1</code> */
  244. public static final int MOD_TIME = 1;
  245. /** Constant <code>SMUDGE=2</code> */
  246. public static final int SMUDGE = 2;
  247. /** Constant <code>LENGTH=4</code> */
  248. public static final int LENGTH = 4;
  249. /** Constant <code>CONTENT_ID=8</code> */
  250. public static final int CONTENT_ID = 8;
  251. /** Constant <code>CONTENT=16</code> */
  252. public static final int CONTENT = 16;
  253. /** Constant <code>ASSUME_UNCHANGED=32</code> */
  254. public static final int ASSUME_UNCHANGED = 32;
  255. /**
  256. * Represent the state of the index in one String. This representation is
  257. * useful when writing tests which do assertions on the state of the index.
  258. * By default information about path, mode, stage (if different from 0) is
  259. * included. A bitmask controls which additional info about
  260. * modificationTimes, smudge state and length is included.
  261. * <p>
  262. * The format of the returned string is described with this BNF:
  263. *
  264. * <pre>
  265. * result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* .
  266. * mode = ", mode:" number .
  267. * stage = ", stage:" number .
  268. * time = ", time:t" timestamp-index .
  269. * smudge = "" | ", smudged" .
  270. * length = ", length:" number .
  271. * sha1 = ", sha1:" hex-sha1 .
  272. * content = ", content:" blob-data .
  273. * </pre>
  274. *
  275. * 'stage' is only presented when the stage is different from 0. All
  276. * reported time stamps are mapped to strings like "t0", "t1", ... "tn". The
  277. * smallest reported time-stamp will be called "t0". This allows to write
  278. * assertions against the string although the concrete value of the time
  279. * stamps is unknown.
  280. *
  281. * @param repo
  282. * the repository the index state should be determined for
  283. * @param includedOptions
  284. * a bitmask constructed out of the constants {@link #MOD_TIME},
  285. * {@link #SMUDGE}, {@link #LENGTH}, {@link #CONTENT_ID} and
  286. * {@link #CONTENT} controlling which info is present in the
  287. * resulting string.
  288. * @return a string encoding the index state
  289. * @throws IllegalStateException
  290. * @throws IOException
  291. */
  292. public static String indexState(Repository repo, int includedOptions)
  293. throws IllegalStateException, IOException {
  294. DirCache dc = repo.readDirCache();
  295. StringBuilder sb = new StringBuilder();
  296. TreeSet<Instant> timeStamps = new TreeSet<>();
  297. // iterate once over the dircache just to collect all time stamps
  298. if (0 != (includedOptions & MOD_TIME)) {
  299. for (int i = 0; i < dc.getEntryCount(); ++i) {
  300. timeStamps.add(dc.getEntry(i).getLastModifiedInstant());
  301. }
  302. }
  303. // iterate again, now produce the result string
  304. for (int i=0; i<dc.getEntryCount(); ++i) {
  305. DirCacheEntry entry = dc.getEntry(i);
  306. sb.append("["+entry.getPathString()+", mode:" + entry.getFileMode());
  307. int stage = entry.getStage();
  308. if (stage != 0)
  309. sb.append(", stage:" + stage);
  310. if (0 != (includedOptions & MOD_TIME)) {
  311. sb.append(", time:t"+
  312. timeStamps.headSet(entry.getLastModifiedInstant())
  313. .size());
  314. }
  315. if (0 != (includedOptions & SMUDGE))
  316. if (entry.isSmudged())
  317. sb.append(", smudged");
  318. if (0 != (includedOptions & LENGTH))
  319. sb.append(", length:"
  320. + Integer.toString(entry.getLength()));
  321. if (0 != (includedOptions & CONTENT_ID))
  322. sb.append(", sha1:" + ObjectId.toString(entry.getObjectId()));
  323. if (0 != (includedOptions & CONTENT)) {
  324. sb.append(", content:"
  325. + new String(repo.open(entry.getObjectId(),
  326. Constants.OBJ_BLOB).getCachedBytes(), UTF_8));
  327. }
  328. if (0 != (includedOptions & ASSUME_UNCHANGED))
  329. sb.append(", assume-unchanged:"
  330. + Boolean.toString(entry.isAssumeValid()));
  331. sb.append("]");
  332. }
  333. return sb.toString();
  334. }
  335. /**
  336. * Creates a new empty bare repository.
  337. *
  338. * @return the newly created bare repository, opened for access. The
  339. * repository will not be closed in {@link #tearDown()}; the caller
  340. * is responsible for closing it.
  341. * @throws IOException
  342. * the repository could not be created in the temporary area
  343. */
  344. protected FileRepository createBareRepository() throws IOException {
  345. return createRepository(true /* bare */);
  346. }
  347. /**
  348. * Creates a new empty repository within a new empty working directory.
  349. *
  350. * @return the newly created repository, opened for access. The repository
  351. * will not be closed in {@link #tearDown()}; the caller is
  352. * responsible for closing it.
  353. * @throws IOException
  354. * the repository could not be created in the temporary area
  355. */
  356. protected FileRepository createWorkRepository() throws IOException {
  357. return createRepository(false /* not bare */);
  358. }
  359. /**
  360. * Creates a new empty repository.
  361. *
  362. * @param bare
  363. * true to create a bare repository; false to make a repository
  364. * within its working directory
  365. * @return the newly created repository, opened for access. The repository
  366. * will not be closed in {@link #tearDown()}; the caller is
  367. * responsible for closing it.
  368. * @throws IOException
  369. * the repository could not be created in the temporary area
  370. * @since 5.3
  371. */
  372. protected FileRepository createRepository(boolean bare)
  373. throws IOException {
  374. return createRepository(bare, false /* auto close */);
  375. }
  376. /**
  377. * Creates a new empty repository.
  378. *
  379. * @param bare
  380. * true to create a bare repository; false to make a repository
  381. * within its working directory
  382. * @param autoClose
  383. * auto close the repository in {@link #tearDown()}
  384. * @return the newly created repository, opened for access
  385. * @throws IOException
  386. * the repository could not be created in the temporary area
  387. * @deprecated use {@link #createRepository(boolean)} instead
  388. */
  389. @Deprecated
  390. public FileRepository createRepository(boolean bare, boolean autoClose)
  391. throws IOException {
  392. File gitdir = createUniqueTestGitDir(bare);
  393. FileRepository db = new FileRepository(gitdir);
  394. assertFalse(gitdir.exists());
  395. db.create(bare);
  396. if (autoClose) {
  397. addRepoToClose(db);
  398. }
  399. return db;
  400. }
  401. /**
  402. * Adds a repository to the list of repositories which is closed at the end
  403. * of the tests
  404. *
  405. * @param r
  406. * the repository to be closed
  407. */
  408. public void addRepoToClose(Repository r) {
  409. toClose.add(r);
  410. }
  411. /**
  412. * Creates a unique directory for a test
  413. *
  414. * @param name
  415. * a subdirectory
  416. * @return a unique directory for a test
  417. * @throws IOException
  418. */
  419. protected File createTempDirectory(String name) throws IOException {
  420. File directory = new File(createTempFile(), name);
  421. FileUtils.mkdirs(directory);
  422. return directory.getCanonicalFile();
  423. }
  424. /**
  425. * Creates a new unique directory for a test repository
  426. *
  427. * @param bare
  428. * true for a bare repository; false for a repository with a
  429. * working directory
  430. * @return a unique directory for a test repository
  431. * @throws IOException
  432. */
  433. protected File createUniqueTestGitDir(boolean bare) throws IOException {
  434. String gitdirName = createTempFile().getPath();
  435. if (!bare)
  436. gitdirName += "/";
  437. return new File(gitdirName + Constants.DOT_GIT);
  438. }
  439. /**
  440. * Allocates a new unique file path that does not exist.
  441. * <p>
  442. * Unlike the standard {@code File.createTempFile} the returned path does
  443. * not exist, but may be created by another thread in a race with the
  444. * caller. Good luck.
  445. * <p>
  446. * This method is inherently unsafe due to a race condition between creating
  447. * the name and the first use that reserves it.
  448. *
  449. * @return a unique path that does not exist.
  450. * @throws IOException
  451. */
  452. protected File createTempFile() throws IOException {
  453. File p = File.createTempFile("tmp_", "", tmp);
  454. if (!p.delete()) {
  455. throw new IOException("Cannot obtain unique path " + tmp);
  456. }
  457. return p;
  458. }
  459. /**
  460. * Run a hook script in the repository, returning the exit status.
  461. *
  462. * @param db
  463. * repository the script should see in GIT_DIR environment
  464. * @param hook
  465. * path of the hook script to execute, must be executable file
  466. * type on this platform
  467. * @param args
  468. * arguments to pass to the hook script
  469. * @return exit status code of the invoked hook
  470. * @throws IOException
  471. * the hook could not be executed
  472. * @throws InterruptedException
  473. * the caller was interrupted before the hook completed
  474. */
  475. protected int runHook(final Repository db, final File hook,
  476. final String... args) throws IOException, InterruptedException {
  477. final String[] argv = new String[1 + args.length];
  478. argv[0] = hook.getAbsolutePath();
  479. System.arraycopy(args, 0, argv, 1, args.length);
  480. final Map<String, String> env = cloneEnv();
  481. env.put("GIT_DIR", db.getDirectory().getAbsolutePath());
  482. putPersonIdent(env, "AUTHOR", author);
  483. putPersonIdent(env, "COMMITTER", committer);
  484. final File cwd = db.getWorkTree();
  485. final Process p = Runtime.getRuntime().exec(argv, toEnvArray(env), cwd);
  486. p.getOutputStream().close();
  487. p.getErrorStream().close();
  488. p.getInputStream().close();
  489. return p.waitFor();
  490. }
  491. private static void putPersonIdent(final Map<String, String> env,
  492. final String type, final PersonIdent who) {
  493. final String ident = who.toExternalString();
  494. final String date = ident.substring(ident.indexOf("> ") + 2);
  495. env.put("GIT_" + type + "_NAME", who.getName());
  496. env.put("GIT_" + type + "_EMAIL", who.getEmailAddress());
  497. env.put("GIT_" + type + "_DATE", date);
  498. }
  499. /**
  500. * Create a string to a UTF-8 temporary file and return the path.
  501. *
  502. * @param body
  503. * complete content to write to the file. If the file should end
  504. * with a trailing LF, the string should end with an LF.
  505. * @return path of the temporary file created within the trash area.
  506. * @throws IOException
  507. * the file could not be written.
  508. */
  509. protected File write(String body) throws IOException {
  510. final File f = File.createTempFile("temp", "txt", tmp);
  511. try {
  512. write(f, body);
  513. return f;
  514. } catch (Error | RuntimeException | IOException e) {
  515. f.delete();
  516. throw e;
  517. }
  518. }
  519. /**
  520. * Write a string as a UTF-8 file.
  521. *
  522. * @param f
  523. * file to write the string to. Caller is responsible for making
  524. * sure it is in the trash directory or will otherwise be cleaned
  525. * up at the end of the test. If the parent directory does not
  526. * exist, the missing parent directories are automatically
  527. * created.
  528. * @param body
  529. * content to write to the file.
  530. * @throws IOException
  531. * the file could not be written.
  532. */
  533. protected void write(File f, String body) throws IOException {
  534. JGitTestUtil.write(f, body);
  535. }
  536. /**
  537. * Read a file's content
  538. *
  539. * @param f
  540. * the file
  541. * @return the content of the file
  542. * @throws IOException
  543. */
  544. protected String read(File f) throws IOException {
  545. return JGitTestUtil.read(f);
  546. }
  547. private static String[] toEnvArray(Map<String, String> env) {
  548. final String[] envp = new String[env.size()];
  549. int i = 0;
  550. for (Map.Entry<String, String> e : env.entrySet())
  551. envp[i++] = e.getKey() + "=" + e.getValue();
  552. return envp;
  553. }
  554. private static HashMap<String, String> cloneEnv() {
  555. return new HashMap<>(System.getenv());
  556. }
  557. private static final class CleanupThread extends Thread {
  558. private static final CleanupThread me;
  559. static {
  560. me = new CleanupThread();
  561. Runtime.getRuntime().addShutdownHook(me);
  562. }
  563. static void deleteOnShutdown(File tmp) {
  564. synchronized (me) {
  565. me.toDelete.add(tmp);
  566. }
  567. }
  568. static void removed(File tmp) {
  569. synchronized (me) {
  570. me.toDelete.remove(tmp);
  571. }
  572. }
  573. private final List<File> toDelete = new ArrayList<>();
  574. @Override
  575. public void run() {
  576. // On windows accidentally open files or memory
  577. // mapped regions may prevent files from being deleted.
  578. // Suggesting a GC increases the likelihood that our
  579. // test repositories actually get removed after the
  580. // tests, even in the case of failure.
  581. System.gc();
  582. synchronized (this) {
  583. boolean silent = false;
  584. boolean failOnError = false;
  585. for (File tmp : toDelete)
  586. recursiveDelete(tmp, silent, failOnError);
  587. }
  588. }
  589. }
  590. }