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.

GcDeleteEmptyRefsFoldersTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2018 Ericsson and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import static org.junit.Assert.assertFalse;
  12. import static org.junit.Assert.assertTrue;
  13. import java.io.IOException;
  14. import java.nio.file.Files;
  15. import java.nio.file.Path;
  16. import java.nio.file.Paths;
  17. import java.nio.file.attribute.FileTime;
  18. import java.time.Instant;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. public class GcDeleteEmptyRefsFoldersTest extends GcTestCase {
  22. private static final String REF_FOLDER_01 = "A/B/01";
  23. private static final String REF_FOLDER_02 = "C/D/02";
  24. private Path refsDir;
  25. private Path heads;
  26. @Override
  27. @Before
  28. public void setUp() throws Exception {
  29. super.setUp();
  30. refsDir = Paths.get(repo.getDirectory().getAbsolutePath())
  31. .resolve("refs");
  32. heads = refsDir.resolve("heads");
  33. }
  34. @Test
  35. public void emptyRefFoldersAreDeleted() throws Exception {
  36. FileTime fileTime = FileTime.from(Instant.now().minusSeconds(31));
  37. Path refDir01 = Files.createDirectories(heads.resolve(REF_FOLDER_01));
  38. Path refDir02 = Files.createDirectories(heads.resolve(REF_FOLDER_02));
  39. setLastModifiedTime(fileTime, heads, REF_FOLDER_01);
  40. setLastModifiedTime(fileTime, heads, REF_FOLDER_02);
  41. assertTrue(refDir01.toFile().exists());
  42. assertTrue(refDir02.toFile().exists());
  43. gc.gc();
  44. assertFalse(refDir01.toFile().exists());
  45. assertFalse(refDir01.getParent().toFile().exists());
  46. assertFalse(refDir01.getParent().getParent().toFile().exists());
  47. assertFalse(refDir02.toFile().exists());
  48. assertFalse(refDir02.getParent().toFile().exists());
  49. assertFalse(refDir02.getParent().getParent().toFile().exists());
  50. }
  51. @Test
  52. public void emptyRefFoldersSkipFiles() throws Exception {
  53. FileTime fileTime = FileTime.from(Instant.now().minusSeconds(31));
  54. Path refFile = Files.createFile(refsDir.resolve(".DS_Store"));
  55. Path refDir01 = Files.createDirectories(heads.resolve(REF_FOLDER_01));
  56. Path refDir02 = Files.createDirectories(heads.resolve(REF_FOLDER_02));
  57. setLastModifiedTime(fileTime, heads, REF_FOLDER_01);
  58. setLastModifiedTime(fileTime, heads, REF_FOLDER_02);
  59. assertTrue(refDir01.toFile().exists());
  60. assertTrue(refDir02.toFile().exists());
  61. gc.gc();
  62. assertTrue(Files.exists(refFile));
  63. }
  64. private void setLastModifiedTime(FileTime fileTime, Path path, String folder) throws IOException {
  65. long numParents = folder.chars().filter(c -> c == '/').count();
  66. Path folderPath = path.resolve(folder);
  67. for(int folderLevel = 0; folderLevel <= numParents; folderLevel ++ ) {
  68. Files.setLastModifiedTime(folderPath, fileTime);
  69. folderPath = folderPath.getParent();
  70. }
  71. }
  72. @Test
  73. public void emptyRefFoldersAreKeptIfTheyAreTooRecent()
  74. throws Exception {
  75. Path refDir01 = Files.createDirectories(heads.resolve(REF_FOLDER_01));
  76. Path refDir02 = Files.createDirectories(heads.resolve(REF_FOLDER_02));
  77. assertTrue(refDir01.toFile().exists());
  78. assertTrue(refDir02.toFile().exists());
  79. gc.gc();
  80. assertTrue(refDir01.toFile().exists());
  81. assertTrue(refDir02.toFile().exists());
  82. }
  83. @Test
  84. public void nonEmptyRefsFoldersAreKept() throws Exception {
  85. Path refDir01 = Files.createDirectories(heads.resolve(REF_FOLDER_01));
  86. Path refDir02 = Files.createDirectories(heads.resolve(REF_FOLDER_02));
  87. Path ref01 = Files.createFile(refDir01.resolve("ref01"));
  88. Path ref02 = Files.createFile(refDir01.resolve("ref02"));
  89. assertTrue(refDir01.toFile().exists());
  90. assertTrue(refDir02.toFile().exists());
  91. assertTrue(ref01.toFile().exists());
  92. assertTrue(ref02.toFile().exists());
  93. gc.gc();
  94. assertTrue(refDir01.toFile().exists());
  95. assertTrue(refDir02.toFile().exists());
  96. assertTrue(ref01.toFile().exists());
  97. assertTrue(ref02.toFile().exists());
  98. }
  99. }