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