View Javadoc
1   /*
2    * Copyright (C) 2008, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 		// These are all too short.
396 		assertFalse(AbbreviatedObjectId.isId(""));
397 		assertFalse(AbbreviatedObjectId.isId("a"));
398 
399 		// These are too long.
400 		assertFalse(AbbreviatedObjectId.isId(ObjectId.fromString(
401 				"7b6e8067ec86acef9a4184b43210d583b6d2f99a").name()
402 				+ "0"));
403 		assertFalse(AbbreviatedObjectId.isId(ObjectId.fromString(
404 				"7b6e8067ec86acef9a4184b43210d583b6d2f99a").name()
405 				+ "c0ffee"));
406 
407 		// These contain non-hex characters.
408 		assertFalse(AbbreviatedObjectId.isId("01notahexstring"));
409 
410 		// These should all work.
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 }