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.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
53 import org.junit.Test;
54
55 public class AbbreviatedObjectIdTest {
56 @Test
57 public void testEmpty_FromByteArray() {
58 final AbbreviatedObjectId i;
59 i = AbbreviatedObjectId.fromString(new byte[] {}, 0, 0);
60 assertNotNull(i);
61 assertEquals(0, i.length());
62 assertFalse(i.isComplete());
63 assertEquals("", i.name());
64 }
65
66 @Test
67 public void testEmpty_FromString() {
68 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString("");
69 assertNotNull(i);
70 assertEquals(0, i.length());
71 assertFalse(i.isComplete());
72 assertEquals("", i.name());
73 }
74
75 @Test
76 public void testFull_FromByteArray() {
77 final String s = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
78 final byte[] b = Constants.encodeASCII(s);
79 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(b, 0,
80 b.length);
81 assertNotNull(i);
82 assertEquals(s.length(), i.length());
83 assertTrue(i.isComplete());
84 assertEquals(s, i.name());
85
86 final ObjectId f = i.toObjectId();
87 assertNotNull(f);
88 assertEquals(ObjectId.fromString(s), f);
89 }
90
91 @Test
92 public void testFull_FromString() {
93 final String s = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
94 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
95 assertNotNull(i);
96 assertEquals(s.length(), i.length());
97 assertTrue(i.isComplete());
98 assertEquals(s, i.name());
99
100 final ObjectId f = i.toObjectId();
101 assertNotNull(f);
102 assertEquals(ObjectId.fromString(s), f);
103 }
104
105 @Test
106 public void test1_FromString() {
107 final String s = "7";
108 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
109 assertNotNull(i);
110 assertEquals(s.length(), i.length());
111 assertFalse(i.isComplete());
112 assertEquals(s, i.name());
113 assertNull(i.toObjectId());
114 }
115
116 @Test
117 public void test2_FromString() {
118 final String s = "7b";
119 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
120 assertNotNull(i);
121 assertEquals(s.length(), i.length());
122 assertFalse(i.isComplete());
123 assertEquals(s, i.name());
124 assertNull(i.toObjectId());
125 }
126
127 @Test
128 public void test3_FromString() {
129 final String s = "7b6";
130 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
131 assertNotNull(i);
132 assertEquals(s.length(), i.length());
133 assertFalse(i.isComplete());
134 assertEquals(s, i.name());
135 assertNull(i.toObjectId());
136 }
137
138 @Test
139 public void test4_FromString() {
140 final String s = "7b6e";
141 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
142 assertNotNull(i);
143 assertEquals(s.length(), i.length());
144 assertFalse(i.isComplete());
145 assertEquals(s, i.name());
146 assertNull(i.toObjectId());
147 }
148
149 @Test
150 public void test5_FromString() {
151 final String s = "7b6e8";
152 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
153 assertNotNull(i);
154 assertEquals(s.length(), i.length());
155 assertFalse(i.isComplete());
156 assertEquals(s, i.name());
157 assertNull(i.toObjectId());
158 }
159
160 @Test
161 public void test6_FromString() {
162 final String s = "7b6e80";
163 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
164 assertNotNull(i);
165 assertEquals(s.length(), i.length());
166 assertFalse(i.isComplete());
167 assertEquals(s, i.name());
168 assertNull(i.toObjectId());
169 }
170
171 @Test
172 public void test7_FromString() {
173 final String s = "7b6e806";
174 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
175 assertNotNull(i);
176 assertEquals(s.length(), i.length());
177 assertFalse(i.isComplete());
178 assertEquals(s, i.name());
179 assertNull(i.toObjectId());
180 }
181
182 @Test
183 public void test8_FromString() {
184 final String s = "7b6e8067";
185 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
186 assertNotNull(i);
187 assertEquals(s.length(), i.length());
188 assertFalse(i.isComplete());
189 assertEquals(s, i.name());
190 assertNull(i.toObjectId());
191 }
192
193 @Test
194 public void test9_FromString() {
195 final String s = "7b6e8067e";
196 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
197 assertNotNull(i);
198 assertEquals(s.length(), i.length());
199 assertFalse(i.isComplete());
200 assertEquals(s, i.name());
201 assertNull(i.toObjectId());
202 }
203
204 @Test
205 public void test17_FromString() {
206 final String s = "7b6e8067ec96acef9";
207 final AbbreviatedObjectId i = AbbreviatedObjectId.fromString(s);
208 assertNotNull(i);
209 assertEquals(s.length(), i.length());
210 assertFalse(i.isComplete());
211 assertEquals(s, i.name());
212 assertNull(i.toObjectId());
213 }
214
215 @Test
216 public void testEquals_Short8() {
217 final String s = "7b6e8067";
218 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(s);
219 final AbbreviatedObjectId b = AbbreviatedObjectId.fromString(s);
220 assertNotSame(a, b);
221 assertTrue(a.hashCode() == b.hashCode());
222 assertEquals(b, a);
223 assertEquals(a, b);
224 }
225
226 @Test
227 public void testEquals_Short4() {
228 final String s = "7b6e";
229 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(s);
230 final AbbreviatedObjectId b = AbbreviatedObjectId.fromString(s);
231 assertNotSame(a, b);
232 assertTrue(a.hashCode() != 0);
233 assertTrue(a.hashCode() == b.hashCode());
234 assertEquals(b, a);
235 assertEquals(a, b);
236 }
237
238 @Test
239 public void testEquals_Full() {
240 final String s = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
241 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(s);
242 final AbbreviatedObjectId b = AbbreviatedObjectId.fromString(s);
243 assertNotSame(a, b);
244 assertTrue(a.hashCode() == b.hashCode());
245 assertEquals(b, a);
246 assertEquals(a, b);
247 }
248
249 @Test
250 public void testNotEquals_SameLength() {
251 final String sa = "7b6e8067";
252 final String sb = "7b6e806e";
253 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(sa);
254 final AbbreviatedObjectId b = AbbreviatedObjectId.fromString(sb);
255 assertFalse(a.equals(b));
256 assertFalse(b.equals(a));
257 }
258
259 @Test
260 public void testNotEquals_DiffLength() {
261 final String sa = "7b6e8067abcd";
262 final String sb = "7b6e8067";
263 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(sa);
264 final AbbreviatedObjectId b = AbbreviatedObjectId.fromString(sb);
265 assertFalse(a.equals(b));
266 assertFalse(b.equals(a));
267 }
268
269 @Test
270 public void testPrefixCompare_Full() {
271 final String s1 = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
272 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(s1);
273 final ObjectId i1 = ObjectId.fromString(s1);
274 assertEquals(0, a.prefixCompare(i1));
275 assertTrue(i1.startsWith(a));
276
277 final String s2 = "7b6e8067ec96acef9a4184b43210d583b6d2f99b";
278 final ObjectId i2 = ObjectId.fromString(s2);
279 assertTrue(a.prefixCompare(i2) < 0);
280 assertFalse(i2.startsWith(a));
281
282 final String s3 = "7b6e8067ec96acef9a4184b43210d583b6d2f999";
283 final ObjectId i3 = ObjectId.fromString(s3);
284 assertTrue(a.prefixCompare(i3) > 0);
285 assertFalse(i3.startsWith(a));
286 }
287
288 @Test
289 public void testPrefixCompare_1() {
290 final String sa = "7";
291 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(sa);
292
293 final String s1 = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
294 final ObjectId i1 = ObjectId.fromString(s1);
295 assertEquals(0, a.prefixCompare(i1));
296 assertTrue(i1.startsWith(a));
297
298 final String s2 = "8b6e8067ec96acef9a4184b43210d583b6d2f99a";
299 final ObjectId i2 = ObjectId.fromString(s2);
300 assertTrue(a.prefixCompare(i2) < 0);
301 assertFalse(i2.startsWith(a));
302
303 final String s3 = "6b6e8067ec96acef9a4184b43210d583b6d2f99a";
304 final ObjectId i3 = ObjectId.fromString(s3);
305 assertTrue(a.prefixCompare(i3) > 0);
306 assertFalse(i3.startsWith(a));
307 }
308
309 @Test
310 public void testPrefixCompare_7() {
311 final String sa = "7b6e806";
312 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(sa);
313
314 final String s1 = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
315 final ObjectId i1 = ObjectId.fromString(s1);
316 assertEquals(0, a.prefixCompare(i1));
317 assertTrue(i1.startsWith(a));
318
319 final String s2 = "7b6e8167ec86acef9a4184b43210d583b6d2f99a";
320 final ObjectId i2 = ObjectId.fromString(s2);
321 assertTrue(a.prefixCompare(i2) < 0);
322 assertFalse(i2.startsWith(a));
323
324 final String s3 = "7b6e8057eca6acef9a4184b43210d583b6d2f99a";
325 final ObjectId i3 = ObjectId.fromString(s3);
326 assertTrue(a.prefixCompare(i3) > 0);
327 assertFalse(i3.startsWith(a));
328 }
329
330 @Test
331 public void testPrefixCompare_8() {
332 final String sa = "7b6e8067";
333 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(sa);
334
335 final String s1 = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
336 final ObjectId i1 = ObjectId.fromString(s1);
337 assertEquals(0, a.prefixCompare(i1));
338 assertTrue(i1.startsWith(a));
339
340 final String s2 = "7b6e8167ec86acef9a4184b43210d583b6d2f99a";
341 final ObjectId i2 = ObjectId.fromString(s2);
342 assertTrue(a.prefixCompare(i2) < 0);
343 assertFalse(i2.startsWith(a));
344
345 final String s3 = "7b6e8057eca6acef9a4184b43210d583b6d2f99a";
346 final ObjectId i3 = ObjectId.fromString(s3);
347 assertTrue(a.prefixCompare(i3) > 0);
348 assertFalse(i3.startsWith(a));
349 }
350
351 @Test
352 public void testPrefixCompare_9() {
353 final String sa = "7b6e8067e";
354 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(sa);
355
356 final String s1 = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
357 final ObjectId i1 = ObjectId.fromString(s1);
358 assertEquals(0, a.prefixCompare(i1));
359 assertTrue(i1.startsWith(a));
360
361 final String s2 = "7b6e8167ec86acef9a4184b43210d583b6d2f99a";
362 final ObjectId i2 = ObjectId.fromString(s2);
363 assertTrue(a.prefixCompare(i2) < 0);
364 assertFalse(i2.startsWith(a));
365
366 final String s3 = "7b6e8057eca6acef9a4184b43210d583b6d2f99a";
367 final ObjectId i3 = ObjectId.fromString(s3);
368 assertTrue(a.prefixCompare(i3) > 0);
369 assertFalse(i3.startsWith(a));
370 }
371
372 @Test
373 public void testPrefixCompare_17() {
374 final String sa = "7b6e8067ec96acef9";
375 final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(sa);
376
377 final String s1 = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
378 final ObjectId i1 = ObjectId.fromString(s1);
379 assertEquals(0, a.prefixCompare(i1));
380 assertTrue(i1.startsWith(a));
381
382 final String s2 = "7b6e8067eca6acef9a4184b43210d583b6d2f99a";
383 final ObjectId i2 = ObjectId.fromString(s2);
384 assertTrue(a.prefixCompare(i2) < 0);
385 assertFalse(i2.startsWith(a));
386
387 final String s3 = "7b6e8067ec86acef9a4184b43210d583b6d2f99a";
388 final ObjectId i3 = ObjectId.fromString(s3);
389 assertTrue(a.prefixCompare(i3) > 0);
390 assertFalse(i3.startsWith(a));
391 }
392
393 @Test
394 public void testIsId() {
395
396 assertFalse(AbbreviatedObjectId.isId(""));
397 assertFalse(AbbreviatedObjectId.isId("a"));
398
399
400 assertFalse(AbbreviatedObjectId.isId(ObjectId.fromString(
401 "7b6e8067ec86acef9a4184b43210d583b6d2f99a").name()
402 + "0"));
403 assertFalse(AbbreviatedObjectId.isId(ObjectId.fromString(
404 "7b6e8067ec86acef9a4184b43210d583b6d2f99a").name()
405 + "c0ffee"));
406
407
408 assertFalse(AbbreviatedObjectId.isId("01notahexstring"));
409
410
411 assertTrue(AbbreviatedObjectId.isId("ab"));
412 assertTrue(AbbreviatedObjectId.isId("abc"));
413 assertTrue(AbbreviatedObjectId.isId("abcd"));
414 assertTrue(AbbreviatedObjectId.isId("abcd0"));
415 assertTrue(AbbreviatedObjectId.isId("abcd09"));
416 assertTrue(AbbreviatedObjectId.isId(ObjectId.fromString(
417 "7b6e8067ec86acef9a4184b43210d583b6d2f99a").name()));
418 }
419 }