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.

PackFileTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2021 Qualcomm Innovation Center, Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.internal.storage.file;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertThrows;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.File;
  16. import org.eclipse.jgit.internal.storage.pack.PackExt;
  17. import org.junit.Test;
  18. public class PackFileTest {
  19. private static final String TEST_ID = "0123456789012345678901234567890123456789";
  20. private static final String PREFIX = "pack-";
  21. private static final String OLD_PREFIX = "old-";
  22. private static final String OLD_PACK = PREFIX + TEST_ID + "." + OLD_PREFIX
  23. + PackExt.PACK.getExtension();
  24. private static final File TEST_PACK_DIR = new File(
  25. "/path/to/repo.git/objects/pack");
  26. private static final File TEST_PRESERVED_DIR = new File(TEST_PACK_DIR,
  27. "preserved");
  28. private static final PackFile TEST_PACKFILE_NO_EXT = new PackFile(
  29. new File(TEST_PACK_DIR, PREFIX + TEST_ID));
  30. @Test
  31. public void idIsSameFromFileOrDirAndName() throws Exception {
  32. File pack = new File(TEST_PACK_DIR, PREFIX + TEST_ID);
  33. PackFile pf = new PackFile(pack);
  34. PackFile pfFromDirAndName = new PackFile(TEST_PACK_DIR,
  35. PREFIX + TEST_ID);
  36. assertEquals(pf.getId(), pfFromDirAndName.getId());
  37. }
  38. @Test
  39. public void idIsSameFromFileWithOrWithoutExt() throws Exception {
  40. PackFile packWithExt = new PackFile(new File(TEST_PACK_DIR,
  41. PREFIX + TEST_ID + "." + PackExt.PACK.getExtension()));
  42. assertEquals(packWithExt.getId(), TEST_PACKFILE_NO_EXT.getId());
  43. }
  44. @Test
  45. public void idIsSameFromFileWithOrWithoutPrefix() throws Exception {
  46. PackFile packWithoutPrefix = new PackFile(
  47. new File(TEST_PACK_DIR, TEST_ID));
  48. assertEquals(packWithoutPrefix.getId(), TEST_PACKFILE_NO_EXT.getId());
  49. }
  50. @Test
  51. public void canCreatePreservedFromFile() throws Exception {
  52. PackFile preserved = new PackFile(
  53. new File(TEST_PRESERVED_DIR, OLD_PACK));
  54. assertTrue(preserved.getName().contains(OLD_PACK));
  55. assertEquals(preserved.getId(), TEST_ID);
  56. assertEquals(preserved.getPackExt(), PackExt.PACK);
  57. }
  58. @Test
  59. public void canCreatePreservedFromDirAndName() throws Exception {
  60. PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
  61. assertTrue(preserved.getName().contains(OLD_PACK));
  62. assertEquals(preserved.getId(), TEST_ID);
  63. assertEquals(preserved.getPackExt(), PackExt.PACK);
  64. }
  65. @Test
  66. public void cannotCreatePreservedNoExtFromNonPreservedNoExt()
  67. throws Exception {
  68. assertThrows(IllegalArgumentException.class, () -> TEST_PACKFILE_NO_EXT
  69. .createPreservedForDirectory(TEST_PRESERVED_DIR));
  70. }
  71. @Test
  72. public void canCreateAnyExtFromAnyExt() throws Exception {
  73. for (PackExt from : PackExt.values()) {
  74. PackFile dotFrom = TEST_PACKFILE_NO_EXT.create(from);
  75. for (PackExt to : PackExt.values()) {
  76. PackFile dotTo = dotFrom.create(to);
  77. File expected = new File(TEST_PACK_DIR,
  78. PREFIX + TEST_ID + "." + to.getExtension());
  79. assertEquals(dotTo.getPackExt(), to);
  80. assertEquals(dotFrom.getId(), dotTo.getId());
  81. assertEquals(expected.getName(), dotTo.getName());
  82. }
  83. }
  84. }
  85. @Test
  86. public void canCreatePreservedFromAnyExt() throws Exception {
  87. for (PackExt ext : PackExt.values()) {
  88. PackFile nonPreserved = TEST_PACKFILE_NO_EXT.create(ext);
  89. PackFile preserved = nonPreserved
  90. .createPreservedForDirectory(TEST_PRESERVED_DIR);
  91. File expected = new File(TEST_PRESERVED_DIR,
  92. PREFIX + TEST_ID + "." + OLD_PREFIX + ext.getExtension());
  93. assertEquals(preserved.getName(), expected.getName());
  94. assertEquals(preserved.getId(), TEST_ID);
  95. assertEquals(preserved.getPackExt(), nonPreserved.getPackExt());
  96. }
  97. }
  98. @Test
  99. public void canCreateAnyPreservedExtFromAnyPreservedExt() throws Exception {
  100. // Preserved PackFiles must have an extension
  101. PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
  102. for (PackExt from : PackExt.values()) {
  103. PackFile preservedWithExt = preserved.create(from);
  104. for (PackExt to : PackExt.values()) {
  105. PackFile preservedNewExt = preservedWithExt.create(to);
  106. File expected = new File(TEST_PRESERVED_DIR, PREFIX + TEST_ID
  107. + "." + OLD_PREFIX + to.getExtension());
  108. assertEquals(preservedNewExt.getPackExt(), to);
  109. assertEquals(preservedWithExt.getId(), preservedNewExt.getId());
  110. assertEquals(preservedNewExt.getName(), expected.getName());
  111. }
  112. }
  113. }
  114. @Test
  115. public void canCreateNonPreservedFromAnyPreservedExt() throws Exception {
  116. // Preserved PackFiles must have an extension
  117. PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
  118. for (PackExt ext : PackExt.values()) {
  119. PackFile preservedWithExt = preserved.create(ext);
  120. PackFile nonPreserved = preservedWithExt
  121. .createForDirectory(TEST_PACK_DIR);
  122. File expected = new File(TEST_PACK_DIR,
  123. PREFIX + TEST_ID + "." + ext.getExtension());
  124. assertEquals(nonPreserved.getName(), expected.getName());
  125. assertEquals(nonPreserved.getId(), TEST_ID);
  126. assertEquals(nonPreserved.getPackExt(),
  127. preservedWithExt.getPackExt());
  128. }
  129. }
  130. }