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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.eclipse.jgit.lib.ObjectId;
  18. import org.junit.Test;
  19. public class PackFileTest {
  20. private static final ObjectId TEST_OID = ObjectId
  21. .fromString("0123456789012345678901234567890123456789");
  22. private static final String TEST_ID = TEST_OID.name();
  23. private static final String PREFIX = "pack-";
  24. private static final String OLD_PREFIX = "old-";
  25. private static final String OLD_PACK = PREFIX + TEST_ID + "." + OLD_PREFIX
  26. + PackExt.PACK.getExtension();
  27. private static final File TEST_PACK_DIR = new File(
  28. "/path/to/repo.git/objects/pack");
  29. private static final File TEST_PRESERVED_DIR = new File(TEST_PACK_DIR,
  30. "preserved");
  31. private static final PackFile TEST_PACKFILE_NO_EXT = new PackFile(
  32. new File(TEST_PACK_DIR, PREFIX + TEST_ID));
  33. @Test
  34. public void objectsAreSameFromAnyConstructor() throws Exception {
  35. String name = PREFIX + TEST_ID + "." + PackExt.PACK.getExtension();
  36. File pack = new File(TEST_PACK_DIR, name);
  37. PackFile pf = new PackFile(pack);
  38. PackFile pfFromDirAndName = new PackFile(TEST_PACK_DIR, name);
  39. assertPackFilesEqual(pf, pfFromDirAndName);
  40. PackFile pfFromOIdAndExt = new PackFile(TEST_PACK_DIR, TEST_OID,
  41. PackExt.PACK);
  42. assertPackFilesEqual(pf, pfFromOIdAndExt);
  43. PackFile pfFromIdAndExt = new PackFile(TEST_PACK_DIR, TEST_ID,
  44. PackExt.PACK);
  45. assertPackFilesEqual(pf, pfFromIdAndExt);
  46. }
  47. @Test
  48. public void idIsSameFromFileWithOrWithoutExt() throws Exception {
  49. PackFile packWithExt = new PackFile(new File(TEST_PACK_DIR,
  50. PREFIX + TEST_ID + "." + PackExt.PACK.getExtension()));
  51. assertEquals(packWithExt.getId(), TEST_PACKFILE_NO_EXT.getId());
  52. }
  53. @Test
  54. public void idIsSameFromFileWithOrWithoutPrefix() throws Exception {
  55. PackFile packWithoutPrefix = new PackFile(
  56. new File(TEST_PACK_DIR, TEST_ID));
  57. assertEquals(packWithoutPrefix.getId(), TEST_PACKFILE_NO_EXT.getId());
  58. }
  59. @Test
  60. public void canCreatePreservedFromFile() throws Exception {
  61. PackFile preserved = new PackFile(
  62. new File(TEST_PRESERVED_DIR, OLD_PACK));
  63. assertTrue(preserved.getName().contains(OLD_PACK));
  64. assertEquals(preserved.getId(), TEST_ID);
  65. assertEquals(preserved.getPackExt(), PackExt.PACK);
  66. }
  67. @Test
  68. public void canCreatePreservedFromDirAndName() throws Exception {
  69. PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
  70. assertTrue(preserved.getName().contains(OLD_PACK));
  71. assertEquals(preserved.getId(), TEST_ID);
  72. assertEquals(preserved.getPackExt(), PackExt.PACK);
  73. }
  74. @Test
  75. public void cannotCreatePreservedNoExtFromNonPreservedNoExt()
  76. throws Exception {
  77. assertThrows(IllegalArgumentException.class, () -> TEST_PACKFILE_NO_EXT
  78. .createPreservedForDirectory(TEST_PRESERVED_DIR));
  79. }
  80. @Test
  81. public void canCreateAnyExtFromAnyExt() throws Exception {
  82. for (PackExt from : PackExt.values()) {
  83. PackFile dotFrom = TEST_PACKFILE_NO_EXT.create(from);
  84. for (PackExt to : PackExt.values()) {
  85. PackFile dotTo = dotFrom.create(to);
  86. File expected = new File(TEST_PACK_DIR,
  87. PREFIX + TEST_ID + "." + to.getExtension());
  88. assertEquals(dotTo.getPackExt(), to);
  89. assertEquals(dotFrom.getId(), dotTo.getId());
  90. assertEquals(expected.getName(), dotTo.getName());
  91. }
  92. }
  93. }
  94. @Test
  95. public void canCreatePreservedFromAnyExt() throws Exception {
  96. for (PackExt ext : PackExt.values()) {
  97. PackFile nonPreserved = TEST_PACKFILE_NO_EXT.create(ext);
  98. PackFile preserved = nonPreserved
  99. .createPreservedForDirectory(TEST_PRESERVED_DIR);
  100. File expected = new File(TEST_PRESERVED_DIR,
  101. PREFIX + TEST_ID + "." + OLD_PREFIX + ext.getExtension());
  102. assertEquals(preserved.getName(), expected.getName());
  103. assertEquals(preserved.getId(), TEST_ID);
  104. assertEquals(preserved.getPackExt(), nonPreserved.getPackExt());
  105. }
  106. }
  107. @Test
  108. public void canCreateAnyPreservedExtFromAnyPreservedExt() throws Exception {
  109. // Preserved PackFiles must have an extension
  110. PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
  111. for (PackExt from : PackExt.values()) {
  112. PackFile preservedWithExt = preserved.create(from);
  113. for (PackExt to : PackExt.values()) {
  114. PackFile preservedNewExt = preservedWithExt.create(to);
  115. File expected = new File(TEST_PRESERVED_DIR, PREFIX + TEST_ID
  116. + "." + OLD_PREFIX + to.getExtension());
  117. assertEquals(preservedNewExt.getPackExt(), to);
  118. assertEquals(preservedWithExt.getId(), preservedNewExt.getId());
  119. assertEquals(preservedNewExt.getName(), expected.getName());
  120. }
  121. }
  122. }
  123. @Test
  124. public void canCreateNonPreservedFromAnyPreservedExt() throws Exception {
  125. // Preserved PackFiles must have an extension
  126. PackFile preserved = new PackFile(TEST_PRESERVED_DIR, OLD_PACK);
  127. for (PackExt ext : PackExt.values()) {
  128. PackFile preservedWithExt = preserved.create(ext);
  129. PackFile nonPreserved = preservedWithExt
  130. .createForDirectory(TEST_PACK_DIR);
  131. File expected = new File(TEST_PACK_DIR,
  132. PREFIX + TEST_ID + "." + ext.getExtension());
  133. assertEquals(nonPreserved.getName(), expected.getName());
  134. assertEquals(nonPreserved.getId(), TEST_ID);
  135. assertEquals(nonPreserved.getPackExt(),
  136. preservedWithExt.getPackExt());
  137. }
  138. }
  139. private void assertPackFilesEqual(PackFile p1, PackFile p2) {
  140. // for test purposes, considered equal if id, name, and ext are equal
  141. assertEquals(p1.getId(), p2.getId());
  142. assertEquals(p1.getPackExt(), p2.getPackExt());
  143. assertEquals(p1.getName(), p2.getName());
  144. }
  145. }