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 package org.eclipse.jgit.lfs.lib;
45
46 import static java.nio.charset.StandardCharsets.US_ASCII;
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertFalse;
49 import static org.junit.Assert.assertNotEquals;
50 import static org.junit.Assert.assertTrue;
51 import static org.junit.Assert.fail;
52
53 import java.io.ByteArrayOutputStream;
54 import java.io.IOException;
55 import java.io.OutputStreamWriter;
56 import java.nio.ByteBuffer;
57 import java.nio.charset.Charset;
58 import java.nio.file.Files;
59 import java.nio.file.Path;
60 import java.util.Locale;
61
62 import org.eclipse.jgit.junit.JGitTestUtil;
63 import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
64 import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
65 import org.eclipse.jgit.lib.Constants;
66 import org.eclipse.jgit.util.FileUtils;
67 import org.junit.AfterClass;
68 import org.junit.BeforeClass;
69 import org.junit.Test;
70
71
72
73
74 public class LongObjectIdTest {
75 private static Path tmp;
76
77 @BeforeClass
78 public static void setup() throws IOException {
79 tmp = Files.createTempDirectory("jgit_test_");
80 }
81
82 @AfterClass
83 public static void tearDown() throws IOException {
84 FileUtils.delete(tmp.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
85 }
86
87 @Test
88 public void test001_toString() {
89 final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a";
90 final LongObjectId oid = LongObjectId.fromString(x);
91 assertEquals(x, oid.name());
92 }
93
94 @Test
95 public void test002_toString() {
96 final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf";
97 final LongObjectId oid = LongObjectId.fromString(x);
98 assertEquals(x, oid.name());
99 }
100
101 @Test
102 public void test003_equals() {
103 final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a";
104 final LongObjectId a = LongObjectId.fromString(x);
105 final LongObjectId b = LongObjectId.fromString(x);
106 assertEquals(a.hashCode(), b.hashCode());
107 assertEquals("a and b should be equal", b, a);
108 }
109
110 @Test
111 public void test004_isId() {
112 assertTrue("valid id", LongObjectId.isId(
113 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"));
114 }
115
116 @Test
117 public void test005_notIsId() {
118 assertFalse("bob is not an id", LongObjectId.isId("bob"));
119 }
120
121 @Test
122 public void test006_notIsId() {
123 assertFalse("63 digits is not an id", LongObjectId.isId(
124 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0"));
125 }
126
127 @Test
128 public void test007_isId() {
129 assertTrue("uppercase is accepted", LongObjectId.isId(
130 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2dEb7ab0A"));
131 }
132
133 @Test
134 public void test008_notIsId() {
135 assertFalse("g is not a valid hex digit", LongObjectId.isId(
136 "g367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"));
137 }
138
139 @Test
140 public void test009_toString() {
141 final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf";
142 final LongObjectId oid = LongObjectId.fromString(x);
143 assertEquals(x, LongObjectId.toString(oid));
144 }
145
146 @Test
147 public void test010_toString() {
148 final String x = "0000000000000000000000000000000000000000000000000000000000000000";
149 assertEquals(x, LongObjectId.toString(null));
150 }
151
152 @Test
153 public void test011_toString() {
154 final String x = "0123456789ABCDEFabcdef01234567890123456789ABCDEFabcdef0123456789";
155 final LongObjectId oid = LongObjectId.fromString(x);
156 assertEquals(x.toLowerCase(Locale.ROOT), oid.name());
157 }
158
159 @Test
160 public void testGetByte() {
161 byte[] raw = new byte[32];
162 for (int i = 0; i < 32; i++)
163 raw[i] = (byte) (0xa0 + i);
164 LongObjectId id = LongObjectId.fromRaw(raw);
165
166 assertEquals(raw[0] & 0xff, id.getFirstByte());
167 assertEquals(raw[0] & 0xff, id.getByte(0));
168 assertEquals(raw[1] & 0xff, id.getByte(1));
169 assertEquals(raw[1] & 0xff, id.getSecondByte());
170
171 for (int i = 2; i < 32; i++) {
172 assertEquals("index " + i, raw[i] & 0xff, id.getByte(i));
173 }
174 try {
175 id.getByte(32);
176 fail("LongObjectId has 32 byte only");
177 } catch (ArrayIndexOutOfBoundsException e) {
178
179 }
180 }
181
182 @Test
183 public void testSetByte() {
184 byte[] exp = new byte[32];
185 for (int i = 0; i < 32; i++) {
186 exp[i] = (byte) (0xa0 + i);
187 }
188
189 MutableLongObjectId id = new MutableLongObjectId();
190 id.fromRaw(exp);
191 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
192
193 id.setByte(0, 0x10);
194 assertEquals(0x10, id.getByte(0));
195 exp[0] = 0x10;
196 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
197
198 for (int p = 1; p < 32; p++) {
199 id.setByte(p, 0x10 + p);
200 assertEquals(0x10 + p, id.getByte(p));
201 exp[p] = (byte) (0x10 + p);
202 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
203 }
204
205 for (int p = 0; p < 32; p++) {
206 id.setByte(p, 0x80 + p);
207 assertEquals(0x80 + p, id.getByte(p));
208 exp[p] = (byte) (0x80 + p);
209 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
210 }
211 }
212
213 @Test
214 public void testZeroId() {
215 AnyLongObjectId zero = new LongObjectId(0L, 0L, 0L, 0L);
216 assertEquals(zero, LongObjectId.zeroId());
217 assertEquals(
218 "0000000000000000000000000000000000000000000000000000000000000000",
219 LongObjectId.zeroId().name());
220 }
221
222 @Test
223 public void testEquals() {
224 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
225 assertTrue("id should equal itself", id1.equals(id1));
226 AnyLongObjectId id2 = new LongObjectId(id1);
227 assertEquals("objects should be equals", id1, id2);
228
229 id2 = LongObjectIdTestUtils.hash("other");
230 assertNotEquals("objects should be not equal", id1, id2);
231 }
232
233 @Test
234 public void testCopyRawBytes() {
235 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
236 AnyLongObjectId id2 = new LongObjectId(id1);
237
238 byte[] buf = new byte[64];
239 id1.copyRawTo(buf, 0);
240 id2.copyRawTo(buf, 32);
241 assertTrue("objects should be equals",
242 LongObjectId.equals(buf, 0, buf, 32));
243 }
244
245 @Test
246 public void testCopyRawLongs() {
247 long[] a = new long[4];
248 a[0] = 1L;
249 a[1] = 2L;
250 a[2] = 3L;
251 a[3] = 4L;
252 AnyLongObjectId id1 = new LongObjectId(a[0], a[1], a[2], a[3]);
253 AnyLongObjectId id2 = LongObjectId.fromRaw(a);
254 assertEquals("objects should be equals", id1, id2);
255 }
256
257 @Test
258 public void testCopyFromStringInvalid() {
259 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
260 try {
261 LongObjectId.fromString(id1.name() + "01234");
262 fail("expected InvalidLongObjectIdException");
263 } catch (InvalidLongObjectIdException e) {
264 assertEquals("Invalid id: " + id1.name() + "01234",
265 e.getMessage());
266 }
267 }
268
269 @Test
270 public void testCopyFromStringByte() {
271 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
272 byte[] buf = new byte[64];
273 Charset cs = US_ASCII;
274 cs.encode(id1.name()).get(buf);
275 AnyLongObjectId id2 = LongObjectId.fromString(buf, 0);
276 assertEquals("objects should be equals", id1, id2);
277 }
278
279 @Test
280 public void testHashFile() throws IOException {
281 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
282 Path f = tmp.resolve("test");
283 JGitTestUtil.write(f.toFile(), "test");
284 AnyLongObjectId id2 = LongObjectIdTestUtils.hash(f);
285 assertEquals("objects should be equals", id1, id2);
286 }
287
288 @Test
289 public void testCompareTo() {
290 AnyLongObjectId id1 = LongObjectId.fromString(
291 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
292 assertEquals(0, id1.compareTo(LongObjectId.fromString(
293 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
294 AnyLongObjectId self = id1;
295 assertEquals(0, id1.compareTo(self));
296
297 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
298 "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
299 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
300 "0123456789abcdef1123456789abcdef0123456789abcdef0123456789abcdef")));
301 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
302 "0123456789abcdef0123456789abcdef1123456789abcdef0123456789abcdef")));
303 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
304 "0123456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef")));
305
306 assertEquals(1, id1.compareTo(LongObjectId.fromString(
307 "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
308 assertEquals(1, id1.compareTo(LongObjectId.fromString(
309 "0123456789abcdef0023456789abcdef0123456789abcdef0123456789abcdef")));
310 assertEquals(1, id1.compareTo(LongObjectId.fromString(
311 "0123456789abcdef0123456789abcdef0023456789abcdef0123456789abcdef")));
312 assertEquals(1, id1.compareTo(LongObjectId.fromString(
313 "0123456789abcdef0123456789abcdef0123456789abcdef0023456789abcdef")));
314 }
315
316 @Test
317 public void testCompareToByte() {
318 AnyLongObjectId id1 = LongObjectId.fromString(
319 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
320 byte[] buf = new byte[32];
321 id1.copyRawTo(buf, 0);
322 assertEquals(0, id1.compareTo(buf, 0));
323
324 LongObjectId
325 .fromString(
326 "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
327 .copyRawTo(buf, 0);
328 assertEquals(-1, id1.compareTo(buf, 0));
329
330 LongObjectId
331 .fromString(
332 "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
333 .copyRawTo(buf, 0);
334 assertEquals(1, id1.compareTo(buf, 0));
335 }
336
337 @Test
338 public void testCompareToLong() {
339 AnyLongObjectId id1 = new LongObjectId(1L, 2L, 3L, 4L);
340 long[] buf = new long[4];
341 id1.copyRawTo(buf, 0);
342 assertEquals(0, id1.compareTo(buf, 0));
343
344 new LongObjectId(2L, 2L, 3L, 4L).copyRawTo(buf, 0);
345 assertEquals(-1, id1.compareTo(buf, 0));
346
347 new LongObjectId(0L, 2L, 3L, 4L).copyRawTo(buf, 0);
348 assertEquals(1, id1.compareTo(buf, 0));
349 }
350
351 @Test
352 public void testCopyToByte() {
353 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
354 byte[] buf = new byte[64];
355 id1.copyTo(buf, 0);
356 assertEquals(id1, LongObjectId.fromString(buf, 0));
357 }
358
359 @Test
360 public void testCopyRawToByteBuffer() {
361 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
362 ByteBuffer buf = ByteBuffer.allocate(32);
363 id1.copyRawTo(buf);
364 assertEquals(id1, LongObjectId.fromRaw(buf.array(), 0));
365 }
366
367 @Test
368 public void testCopyToByteBuffer() {
369 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
370 ByteBuffer buf = ByteBuffer.allocate(64);
371 id1.copyTo(buf);
372 assertEquals(id1, LongObjectId.fromString(buf.array(), 0));
373 }
374
375 @Test
376 public void testCopyRawToOutputStream() throws IOException {
377 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
378 ByteArrayOutputStream os = new ByteArrayOutputStream(32);
379 id1.copyRawTo(os);
380 assertEquals(id1, LongObjectId.fromRaw(os.toByteArray(), 0));
381 }
382
383 @Test
384 public void testCopyToOutputStream() throws IOException {
385 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
386 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
387 id1.copyTo(os);
388 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
389 }
390
391 @Test
392 public void testCopyToWriter() throws IOException {
393 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
394 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
395 try (OutputStreamWriter w = new OutputStreamWriter(os,
396 Constants.CHARSET)) {
397 id1.copyTo(w);
398 }
399 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
400 }
401
402 @Test
403 public void testCopyToWriterWithBuf() throws IOException {
404 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
405 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
406 try (OutputStreamWriter w = new OutputStreamWriter(os,
407 Constants.CHARSET)) {
408 char[] buf = new char[64];
409 id1.copyTo(buf, w);
410 }
411 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
412 }
413
414 @Test
415 public void testCopyToStringBuilder() {
416 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
417 StringBuilder sb = new StringBuilder();
418 char[] buf = new char[64];
419 id1.copyTo(buf, sb);
420 assertEquals(id1, LongObjectId.fromString(sb.toString()));
421 }
422
423 @Test
424 public void testCopy() {
425 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
426 assertEquals(id1.copy(), id1);
427 MutableLongObjectId id2 = new MutableLongObjectId();
428 id2.fromObjectId(id1);
429 assertEquals(id1, id2.copy());
430 }
431 }