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