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 org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertNotSame;
50 import static org.junit.Assert.assertNull;
51 import static org.junit.Assert.assertTrue;
52 import static org.junit.Assert.fail;
53
54 import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
55 import org.junit.Test;
56
57
58
59
60 public class AbbreviatedLongObjectIdTest {
61 @Test
62 public void testEmpty_FromByteArray() {
63 final AbbreviatedLongObjectId i;
64 i = AbbreviatedLongObjectId.fromString(new byte[] {}, 0, 0);
65 assertNotNull(i);
66 assertEquals(0, i.length());
67 assertFalse(i.isComplete());
68 assertEquals("", i.name());
69 }
70
71 @Test
72 public void testEmpty_FromString() {
73 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId
74 .fromString("");
75 assertNotNull(i);
76 assertEquals(0, i.length());
77 assertFalse(i.isComplete());
78 assertEquals("", i.name());
79 }
80
81 @Test
82 public void testFull_FromByteArray() {
83 final String s = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
84 final byte[] b = org.eclipse.jgit.lib.Constants.encodeASCII(s);
85 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(b,
86 0, b.length);
87 assertNotNull(i);
88 assertEquals(s.length(), i.length());
89 assertTrue(i.isComplete());
90 assertEquals(s, i.name());
91
92 final LongObjectId f = i.toLongObjectId();
93 assertNotNull(f);
94 assertEquals(LongObjectId.fromString(s), f);
95 assertEquals(f.hashCode(), i.hashCode());
96 }
97
98 @Test
99 public void testFull_FromString() {
100 final String s = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
101 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
102 assertNotNull(i);
103 assertEquals(s.length(), i.length());
104 assertTrue(i.isComplete());
105 assertEquals(s, i.name());
106
107 final LongObjectId f = i.toLongObjectId();
108 assertNotNull(f);
109 assertEquals(LongObjectId.fromString(s), f);
110 assertEquals(f.hashCode(), i.hashCode());
111 }
112
113 @Test
114 public void test1_FromString() {
115 final String s = "2";
116 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
117 assertNotNull(i);
118 assertEquals(s.length(), i.length());
119 assertFalse(i.isComplete());
120 assertEquals(s, i.name());
121 assertNull(i.toLongObjectId());
122 }
123
124 @Test
125 public void test2_FromString() {
126 final String s = "27";
127 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
128 assertNotNull(i);
129 assertEquals(s.length(), i.length());
130 assertFalse(i.isComplete());
131 assertEquals(s, i.name());
132 assertNull(i.toLongObjectId());
133 }
134
135 @Test
136 public void test3_FromString() {
137 final String s = "27e";
138 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
139 assertNotNull(i);
140 assertEquals(s.length(), i.length());
141 assertFalse(i.isComplete());
142 assertEquals(s, i.name());
143 assertNull(i.toLongObjectId());
144 }
145
146 @Test
147 public void test4_FromString() {
148 final String s = "27e1";
149 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
150 assertNotNull(i);
151 assertEquals(s.length(), i.length());
152 assertFalse(i.isComplete());
153 assertEquals(s, i.name());
154 assertNull(i.toLongObjectId());
155 }
156
157 @Test
158 public void test5_FromString() {
159 final String s = "27e15";
160 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
161 assertNotNull(i);
162 assertEquals(s.length(), i.length());
163 assertFalse(i.isComplete());
164 assertEquals(s, i.name());
165 assertNull(i.toLongObjectId());
166 }
167
168 @Test
169 public void test6_FromString() {
170 final String s = "27e15b";
171 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
172 assertNotNull(i);
173 assertEquals(s.length(), i.length());
174 assertFalse(i.isComplete());
175 assertEquals(s, i.name());
176 assertNull(i.toLongObjectId());
177 }
178
179 @Test
180 public void test7_FromString() {
181 final String s = "27e15b7";
182 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
183 assertNotNull(i);
184 assertEquals(s.length(), i.length());
185 assertFalse(i.isComplete());
186 assertEquals(s, i.name());
187 assertNull(i.toLongObjectId());
188 }
189
190 @Test
191 public void test8_FromString() {
192 final String s = "27e15b72";
193 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
194 assertNotNull(i);
195 assertEquals(s.length(), i.length());
196 assertFalse(i.isComplete());
197 assertEquals(s, i.name());
198 assertNull(i.toLongObjectId());
199 }
200
201 @Test
202 public void test9_FromString() {
203 final String s = "27e15b729";
204 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
205 assertNotNull(i);
206 assertEquals(s.length(), i.length());
207 assertFalse(i.isComplete());
208 assertEquals(s, i.name());
209 assertNull(i.toLongObjectId());
210 }
211
212 @Test
213 public void test15_FromString() {
214 final String s = "27e15b72937fc8f";
215 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
216 assertNotNull(i);
217 assertEquals(s.length(), i.length());
218 assertFalse(i.isComplete());
219 assertEquals(s, i.name());
220 assertNull(i.toLongObjectId());
221 }
222
223 @Test
224 public void test16_FromString() {
225 final String s = "27e15b72937fc8f5";
226 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
227 assertNotNull(i);
228 assertEquals(s.length(), i.length());
229 assertFalse(i.isComplete());
230 assertEquals(s, i.name());
231 assertNull(i.toLongObjectId());
232 }
233
234 @Test
235 public void test17_FromString() {
236 final String s = "27e15b72937fc8f55";
237 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
238 assertNotNull(i);
239 assertEquals(s.length(), i.length());
240 assertFalse(i.isComplete());
241 assertEquals(s, i.name());
242 assertNull(i.toLongObjectId());
243 }
244
245 @Test
246 public void test33_FromString() {
247 final String s = "27e15b72937fc8f558da24ac3d50ec203";
248 final AbbreviatedLongObjectId i = AbbreviatedLongObjectId.fromString(s);
249 assertNotNull(i);
250 assertEquals(s.length(), i.length());
251 assertFalse(i.isComplete());
252 assertEquals(s, i.name());
253 assertNull(i.toLongObjectId());
254 }
255
256 @Test
257 public void testEquals_Short() {
258 final String s = "27e15b72";
259 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId.fromString(s);
260 final AbbreviatedLongObjectId b = AbbreviatedLongObjectId.fromString(s);
261 assertNotSame(a, b);
262 assertTrue(a.hashCode() == b.hashCode());
263 assertEquals(b, a);
264 assertEquals(a, b);
265 }
266
267 @Test
268 public void testEquals_Full() {
269 final String s = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
270 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId.fromString(s);
271 final AbbreviatedLongObjectId b = AbbreviatedLongObjectId.fromString(s);
272 assertNotSame(a, b);
273 assertTrue(a.hashCode() == b.hashCode());
274 assertEquals(b, a);
275 assertEquals(a, b);
276 }
277
278 @Test
279 public void testNotEquals_SameLength() {
280 final String sa = "27e15b72";
281 final String sb = "27e15b7f";
282 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId
283 .fromString(sa);
284 final AbbreviatedLongObjectId b = AbbreviatedLongObjectId
285 .fromString(sb);
286 assertFalse(a.equals(b));
287 assertFalse(b.equals(a));
288 }
289
290 @Test
291 public void testNotEquals_DiffLength() {
292 final String sa = "27e15b72abcd";
293 final String sb = "27e15b72";
294 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId
295 .fromString(sa);
296 final AbbreviatedLongObjectId b = AbbreviatedLongObjectId
297 .fromString(sb);
298 assertFalse(a.equals(b));
299 assertFalse(b.equals(a));
300 }
301
302 @Test
303 public void testPrefixCompare_Full() {
304 final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
305 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId
306 .fromString(s1);
307 final LongObjectId i1 = LongObjectId.fromString(s1);
308 assertEquals(0, a.prefixCompare(i1));
309 assertTrue(i1.startsWith(a));
310
311 final String s2 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b11";
312 final LongObjectId i2 = LongObjectId.fromString(s2);
313 assertTrue(a.prefixCompare(i2) < 0);
314 assertFalse(i2.startsWith(a));
315
316 final String s3 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b0f";
317 final LongObjectId i3 = LongObjectId.fromString(s3);
318 assertTrue(a.prefixCompare(i3) > 0);
319 assertFalse(i3.startsWith(a));
320 }
321
322 @Test
323 public void testPrefixCompare_1() {
324 final String sa = "2";
325 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId
326 .fromString(sa);
327
328 final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
329 final LongObjectId i1 = LongObjectId.fromString(s1);
330 assertEquals(0, a.prefixCompare(i1));
331 assertTrue(i1.startsWith(a));
332
333 final String s2 = "37e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
334 final LongObjectId i2 = LongObjectId.fromString(s2);
335 assertTrue(a.prefixCompare(i2) < 0);
336 assertFalse(i2.startsWith(a));
337
338 final String s3 = "17e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
339 final LongObjectId i3 = LongObjectId.fromString(s3);
340 assertTrue(a.prefixCompare(i3) > 0);
341 assertFalse(i3.startsWith(a));
342 }
343
344 @Test
345 public void testPrefixCompare_15() {
346 final String sa = "27e15b72937fc8f";
347 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId
348 .fromString(sa);
349
350 final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
351 final LongObjectId i1 = LongObjectId.fromString(s1);
352 assertEquals(0, a.prefixCompare(i1));
353 assertTrue(i1.startsWith(a));
354
355 final String s2 = "27e15b72937fc90558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
356 final LongObjectId i2 = LongObjectId.fromString(s2);
357 assertTrue(a.prefixCompare(i2) < 0);
358 assertFalse(i2.startsWith(a));
359
360 final String s3 = "27e15b72937fc8e558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
361 final LongObjectId i3 = LongObjectId.fromString(s3);
362 assertTrue(a.prefixCompare(i3) > 0);
363 assertFalse(i3.startsWith(a));
364 }
365
366 @Test
367 public void testPrefixCompare_16() {
368 final String sa = "27e15b72937fc8f5";
369 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId
370 .fromString(sa);
371
372 final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
373 final LongObjectId i1 = LongObjectId.fromString(s1);
374 assertEquals(0, a.prefixCompare(i1));
375 assertTrue(i1.startsWith(a));
376
377 final String s2 = "27e15b72937fc8f658da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
378 final LongObjectId i2 = LongObjectId.fromString(s2);
379 assertTrue(a.prefixCompare(i2) < 0);
380 assertFalse(i2.startsWith(a));
381
382 final String s3 = "27e15b72937fc8f458da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
383 final LongObjectId i3 = LongObjectId.fromString(s3);
384 assertTrue(a.prefixCompare(i3) > 0);
385 assertFalse(i3.startsWith(a));
386 }
387
388 @Test
389 public void testPrefixCompare_17() {
390 final String sa = "27e15b72937fc8f55";
391 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId
392 .fromString(sa);
393
394 final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
395 final LongObjectId i1 = LongObjectId.fromString(s1);
396 assertEquals(0, a.prefixCompare(i1));
397 assertTrue(i1.startsWith(a));
398
399 final String s2 = "27e15b72937fc8f568da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
400 final LongObjectId i2 = LongObjectId.fromString(s2);
401 assertTrue(a.prefixCompare(i2) < 0);
402 assertFalse(i2.startsWith(a));
403
404 final String s3 = "27e15b72937fc8f548da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
405 final LongObjectId i3 = LongObjectId.fromString(s3);
406 assertTrue(a.prefixCompare(i3) > 0);
407 assertFalse(i3.startsWith(a));
408 }
409
410 @Test
411 public void testPrefixCompare_33() {
412 final String sa = "27e15b72937fc8f558da24ac3d50ec203";
413 final AbbreviatedLongObjectId a = AbbreviatedLongObjectId
414 .fromString(sa);
415
416 final String s1 = "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10";
417 final LongObjectId i1 = LongObjectId.fromString(s1);
418 assertEquals(0, a.prefixCompare(i1));
419 assertTrue(i1.startsWith(a));
420
421 final String s2 = "27e15b72937fc8f558da24ac3d50ec20402a4cf21e33b87ae8e4ce90e89c4b10";
422 final LongObjectId i2 = LongObjectId.fromString(s2);
423 assertTrue(a.prefixCompare(i2) < 0);
424 assertFalse(i2.startsWith(a));
425
426 final String s3 = "27e15b72937fc8f558da24ac3d50ec20202a4cf21e33b87ae8e4ce90e89c4b10";
427 final LongObjectId i3 = LongObjectId.fromString(s3);
428 assertTrue(a.prefixCompare(i3) > 0);
429 assertFalse(i3.startsWith(a));
430 }
431
432 @Test
433 public void testIsId() {
434
435 assertFalse(AbbreviatedLongObjectId.isId(""));
436 assertFalse(AbbreviatedLongObjectId.isId("a"));
437
438
439 assertFalse(AbbreviatedLongObjectId.isId(LongObjectId
440 .fromString(
441 "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10")
442 .name() + "0"));
443 assertFalse(AbbreviatedLongObjectId.isId(LongObjectId
444 .fromString(
445 "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10")
446 .name() + "c0ffee"));
447
448
449 assertFalse(AbbreviatedLongObjectId.isId("01notahexstring"));
450
451
452 assertTrue(AbbreviatedLongObjectId.isId("ab"));
453 assertTrue(AbbreviatedLongObjectId.isId("abc"));
454 assertTrue(AbbreviatedLongObjectId.isId("abcd"));
455 assertTrue(AbbreviatedLongObjectId.isId("abcd0"));
456 assertTrue(AbbreviatedLongObjectId.isId("abcd09"));
457 assertTrue(AbbreviatedLongObjectId.isId(LongObjectId
458 .fromString(
459 "27e15b72937fc8f558da24ac3d50ec20302a4cf21e33b87ae8e4ce90e89c4b10")
460 .name()));
461 }
462
463 @Test
464 public void testAbbreviate() {
465 AnyLongObjectId id = LongObjectIdTestUtils.hash("test");
466 assertEquals(
467 "abbreviated id should match the id it was abbreviated from", 0,
468 id.abbreviate(10).prefixCompare(id));
469 }
470
471 @Test
472 public void testFromStringByteWrongLength() {
473 byte[] buf = new byte[65];
474 try {
475 AbbreviatedLongObjectId.fromString(buf, 0, 65);
476 fail("expected IllegalArgumentException for too long AbbreviatedLongObjectId");
477 } catch (IllegalArgumentException e) {
478
479 }
480 }
481
482 @Test
483 public void testFromStringWrongLength() {
484 AnyLongObjectId id = LongObjectIdTestUtils.hash("test");
485 try {
486 AbbreviatedLongObjectId.fromString(id.name() + "c0ffee");
487 fail("expected IllegalArgumentException for too long AbbreviatedLongObjectId");
488 } catch (IllegalArgumentException e) {
489
490 }
491 }
492
493 @Test
494 public void testFromLongObjectId() {
495 AnyLongObjectId id = LongObjectIdTestUtils.hash("test");
496 assertEquals(0,
497 AbbreviatedLongObjectId.fromLongObjectId(id).prefixCompare(id));
498 }
499
500 @Test
501 public void testPrefixCompareByte() {
502 AnyLongObjectId id = LongObjectId.fromString(
503 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
504 byte[] buf = new byte[32];
505 id.copyRawTo(buf, 0);
506
507 AbbreviatedLongObjectId a = id.abbreviate(62);
508 assertEquals(0, a.prefixCompare(buf, 0));
509
510 a = LongObjectId
511 .fromString(
512 "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
513 .abbreviate(16);
514 assertEquals(-1, a.prefixCompare(buf, 0));
515 a = LongObjectId
516 .fromString(
517 "0123456789abcdef0023456789abcdef0123456789abcdef0123456789abcdef")
518 .abbreviate(32);
519 assertEquals(-1, a.prefixCompare(buf, 0));
520 a = LongObjectId
521 .fromString(
522 "0123456789abcdef0123456789abcdef0023456789abcdef0123456789abcdef")
523 .abbreviate(48);
524 assertEquals(-1, a.prefixCompare(buf, 0));
525 a = LongObjectId
526 .fromString(
527 "0123456789abcdef0123456789abcdef0123456789abcdef0023456789abcdef")
528 .abbreviate(64);
529 assertEquals(-1, a.prefixCompare(buf, 0));
530
531 a = LongObjectId
532 .fromString(
533 "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
534 .abbreviate(16);
535 assertEquals(1, a.prefixCompare(buf, 0));
536 a = LongObjectId
537 .fromString(
538 "0123456789abcdef1123456789abcdef0123456789abcdef0123456789abcdef")
539 .abbreviate(32);
540 assertEquals(1, a.prefixCompare(buf, 0));
541 a = LongObjectId
542 .fromString(
543 "0123456789abcdef0123456789abcdef1123456789abcdef0123456789abcdef")
544 .abbreviate(48);
545 assertEquals(1, a.prefixCompare(buf, 0));
546 a = LongObjectId
547 .fromString(
548 "0123456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef")
549 .abbreviate(64);
550 assertEquals(1, a.prefixCompare(buf, 0));
551 }
552
553 @Test
554 public void testPrefixCompareLong() {
555 AnyLongObjectId id = new LongObjectId(1L, 2L, 3L, 4L);
556 long[] buf = new long[4];
557 id.copyRawTo(buf, 0);
558
559 AbbreviatedLongObjectId a = id.abbreviate(62);
560 assertEquals(0, a.prefixCompare(buf, 0));
561
562 a = new LongObjectId(0L, 2L, 3L, 4L).abbreviate(16);
563 assertEquals(-1, a.prefixCompare(buf, 0));
564 a = new LongObjectId(1L, 1L, 3L, 4L).abbreviate(32);
565 assertEquals(-1, a.prefixCompare(buf, 0));
566 a = new LongObjectId(1L, 2L, 2L, 4L).abbreviate(48);
567 assertEquals(-1, a.prefixCompare(buf, 0));
568 a = new LongObjectId(1L, 2L, 3L, 3L).abbreviate(64);
569 assertEquals(-1, a.prefixCompare(buf, 0));
570
571 a = new LongObjectId(2L, 2L, 3L, 4L).abbreviate(16);
572 assertEquals(1, a.prefixCompare(buf, 0));
573 a = new LongObjectId(1L, 3L, 3L, 4L).abbreviate(32);
574 assertEquals(1, a.prefixCompare(buf, 0));
575 a = new LongObjectId(1L, 2L, 4L, 4L).abbreviate(48);
576 assertEquals(1, a.prefixCompare(buf, 0));
577 a = new LongObjectId(1L, 2L, 3L, 5L).abbreviate(64);
578 assertEquals(1, a.prefixCompare(buf, 0));
579 }
580
581 @Test
582 public void testGetFirstByte() {
583 AnyLongObjectId id = LongObjectId.fromString(
584 "f423456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef");
585 AbbreviatedLongObjectId a = id.abbreviate(10);
586 assertEquals(0xf4, a.getFirstByte());
587 assertEquals(id.getFirstByte(), a.getFirstByte());
588 }
589
590 @SuppressWarnings("unlikely-arg-type")
591 @Test
592 public void testNotEquals() {
593 AbbreviatedLongObjectId a = new LongObjectId(1L, 2L, 3L, 4L)
594 .abbreviate(10);
595 assertFalse(a.equals("different"));
596 }
597 }