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  		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 		// These are all too short.
386 		assertFalse(AbbreviatedObjectId.isId(""));
387 		assertFalse(AbbreviatedObjectId.isId("a"));
388 
389 		// These are too long.
390 		assertFalse(AbbreviatedObjectId.isId(ObjectId.fromString(
391 				"7b6e8067ec86acef9a4184b43210d583b6d2f99a").name()
392 				+ "0"));
393 		assertFalse(AbbreviatedObjectId.isId(ObjectId.fromString(
394 				"7b6e8067ec86acef9a4184b43210d583b6d2f99a").name()
395 				+ "c0ffee"));
396 
397 		// These contain non-hex characters.
398 		assertFalse(AbbreviatedObjectId.isId("01notahexstring"));
399 
400 		// These should all work.
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 }