2 * Copyright (C) 2008, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
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
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
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.
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
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.
44 package org
.eclipse
.jgit
.lib
;
46 import junit
.framework
.TestCase
;
48 public class AbbreviatedObjectIdTest
extends TestCase
{
49 public void testEmpty_FromByteArray() {
50 final AbbreviatedObjectId i
;
51 i
= AbbreviatedObjectId
.fromString(new byte[] {}, 0, 0);
53 assertEquals(0, i
.length());
54 assertFalse(i
.isComplete());
55 assertEquals("", i
.name());
58 public void testEmpty_FromString() {
59 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString("");
61 assertEquals(0, i
.length());
62 assertFalse(i
.isComplete());
63 assertEquals("", i
.name());
66 public void testFull_FromByteArray() {
67 final String s
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
68 final byte[] b
= Constants
.encodeASCII(s
);
69 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(b
, 0,
72 assertEquals(s
.length(), i
.length());
73 assertTrue(i
.isComplete());
74 assertEquals(s
, i
.name());
76 final ObjectId f
= i
.toObjectId();
78 assertEquals(ObjectId
.fromString(s
), f
);
79 assertEquals(f
.hashCode(), i
.hashCode());
82 public void testFull_FromString() {
83 final String s
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
84 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
86 assertEquals(s
.length(), i
.length());
87 assertTrue(i
.isComplete());
88 assertEquals(s
, i
.name());
90 final ObjectId f
= i
.toObjectId();
92 assertEquals(ObjectId
.fromString(s
), f
);
93 assertEquals(f
.hashCode(), i
.hashCode());
96 public void test1_FromString() {
98 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
100 assertEquals(s
.length(), i
.length());
101 assertFalse(i
.isComplete());
102 assertEquals(s
, i
.name());
103 assertNull(i
.toObjectId());
106 public void test2_FromString() {
107 final String s
= "7b";
108 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
110 assertEquals(s
.length(), i
.length());
111 assertFalse(i
.isComplete());
112 assertEquals(s
, i
.name());
113 assertNull(i
.toObjectId());
116 public void test3_FromString() {
117 final String s
= "7b6";
118 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
120 assertEquals(s
.length(), i
.length());
121 assertFalse(i
.isComplete());
122 assertEquals(s
, i
.name());
123 assertNull(i
.toObjectId());
126 public void test4_FromString() {
127 final String s
= "7b6e";
128 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
130 assertEquals(s
.length(), i
.length());
131 assertFalse(i
.isComplete());
132 assertEquals(s
, i
.name());
133 assertNull(i
.toObjectId());
136 public void test5_FromString() {
137 final String s
= "7b6e8";
138 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
140 assertEquals(s
.length(), i
.length());
141 assertFalse(i
.isComplete());
142 assertEquals(s
, i
.name());
143 assertNull(i
.toObjectId());
146 public void test6_FromString() {
147 final String s
= "7b6e80";
148 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
150 assertEquals(s
.length(), i
.length());
151 assertFalse(i
.isComplete());
152 assertEquals(s
, i
.name());
153 assertNull(i
.toObjectId());
156 public void test7_FromString() {
157 final String s
= "7b6e806";
158 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
160 assertEquals(s
.length(), i
.length());
161 assertFalse(i
.isComplete());
162 assertEquals(s
, i
.name());
163 assertNull(i
.toObjectId());
166 public void test8_FromString() {
167 final String s
= "7b6e8067";
168 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
170 assertEquals(s
.length(), i
.length());
171 assertFalse(i
.isComplete());
172 assertEquals(s
, i
.name());
173 assertNull(i
.toObjectId());
176 public void test9_FromString() {
177 final String s
= "7b6e8067e";
178 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
180 assertEquals(s
.length(), i
.length());
181 assertFalse(i
.isComplete());
182 assertEquals(s
, i
.name());
183 assertNull(i
.toObjectId());
186 public void test17_FromString() {
187 final String s
= "7b6e8067ec96acef9";
188 final AbbreviatedObjectId i
= AbbreviatedObjectId
.fromString(s
);
190 assertEquals(s
.length(), i
.length());
191 assertFalse(i
.isComplete());
192 assertEquals(s
, i
.name());
193 assertNull(i
.toObjectId());
196 public void testEquals_Short() {
197 final String s
= "7b6e8067";
198 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(s
);
199 final AbbreviatedObjectId b
= AbbreviatedObjectId
.fromString(s
);
201 assertTrue(a
.hashCode() == b
.hashCode());
202 assertTrue(a
.equals(b
));
203 assertTrue(b
.equals(a
));
206 public void testEquals_Full() {
207 final String s
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
208 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(s
);
209 final AbbreviatedObjectId b
= AbbreviatedObjectId
.fromString(s
);
211 assertTrue(a
.hashCode() == b
.hashCode());
212 assertTrue(a
.equals(b
));
213 assertTrue(b
.equals(a
));
216 public void testNotEquals_SameLength() {
217 final String sa
= "7b6e8067";
218 final String sb
= "7b6e806e";
219 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(sa
);
220 final AbbreviatedObjectId b
= AbbreviatedObjectId
.fromString(sb
);
221 assertFalse(a
.equals(b
));
222 assertFalse(b
.equals(a
));
225 public void testNotEquals_DiffLength() {
226 final String sa
= "7b6e8067abcd";
227 final String sb
= "7b6e8067";
228 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(sa
);
229 final AbbreviatedObjectId b
= AbbreviatedObjectId
.fromString(sb
);
230 assertFalse(a
.equals(b
));
231 assertFalse(b
.equals(a
));
234 public void testPrefixCompare_Full() {
235 final String s1
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
236 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(s1
);
237 final ObjectId i1
= ObjectId
.fromString(s1
);
238 assertEquals(0, a
.prefixCompare(i1
));
239 assertTrue(i1
.startsWith(a
));
241 final String s2
= "7b6e8067ec96acef9a4184b43210d583b6d2f99b";
242 final ObjectId i2
= ObjectId
.fromString(s2
);
243 assertTrue(a
.prefixCompare(i2
) < 0);
244 assertFalse(i2
.startsWith(a
));
246 final String s3
= "7b6e8067ec96acef9a4184b43210d583b6d2f999";
247 final ObjectId i3
= ObjectId
.fromString(s3
);
248 assertTrue(a
.prefixCompare(i3
) > 0);
249 assertFalse(i3
.startsWith(a
));
252 public void testPrefixCompare_1() {
253 final String sa
= "7";
254 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(sa
);
256 final String s1
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
257 final ObjectId i1
= ObjectId
.fromString(s1
);
258 assertEquals(0, a
.prefixCompare(i1
));
259 assertTrue(i1
.startsWith(a
));
261 final String s2
= "8b6e8067ec96acef9a4184b43210d583b6d2f99a";
262 final ObjectId i2
= ObjectId
.fromString(s2
);
263 assertTrue(a
.prefixCompare(i2
) < 0);
264 assertFalse(i2
.startsWith(a
));
266 final String s3
= "6b6e8067ec96acef9a4184b43210d583b6d2f99a";
267 final ObjectId i3
= ObjectId
.fromString(s3
);
268 assertTrue(a
.prefixCompare(i3
) > 0);
269 assertFalse(i3
.startsWith(a
));
272 public void testPrefixCompare_7() {
273 final String sa
= "7b6e806";
274 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(sa
);
276 final String s1
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
277 final ObjectId i1
= ObjectId
.fromString(s1
);
278 assertEquals(0, a
.prefixCompare(i1
));
279 assertTrue(i1
.startsWith(a
));
281 final String s2
= "7b6e8167ec86acef9a4184b43210d583b6d2f99a";
282 final ObjectId i2
= ObjectId
.fromString(s2
);
283 assertTrue(a
.prefixCompare(i2
) < 0);
284 assertFalse(i2
.startsWith(a
));
286 final String s3
= "7b6e8057eca6acef9a4184b43210d583b6d2f99a";
287 final ObjectId i3
= ObjectId
.fromString(s3
);
288 assertTrue(a
.prefixCompare(i3
) > 0);
289 assertFalse(i3
.startsWith(a
));
292 public void testPrefixCompare_8() {
293 final String sa
= "7b6e8067";
294 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(sa
);
296 final String s1
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
297 final ObjectId i1
= ObjectId
.fromString(s1
);
298 assertEquals(0, a
.prefixCompare(i1
));
299 assertTrue(i1
.startsWith(a
));
301 final String s2
= "7b6e8167ec86acef9a4184b43210d583b6d2f99a";
302 final ObjectId i2
= ObjectId
.fromString(s2
);
303 assertTrue(a
.prefixCompare(i2
) < 0);
304 assertFalse(i2
.startsWith(a
));
306 final String s3
= "7b6e8057eca6acef9a4184b43210d583b6d2f99a";
307 final ObjectId i3
= ObjectId
.fromString(s3
);
308 assertTrue(a
.prefixCompare(i3
) > 0);
309 assertFalse(i3
.startsWith(a
));
312 public void testPrefixCompare_9() {
313 final String sa
= "7b6e8067e";
314 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(sa
);
316 final String s1
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
317 final ObjectId i1
= ObjectId
.fromString(s1
);
318 assertEquals(0, a
.prefixCompare(i1
));
319 assertTrue(i1
.startsWith(a
));
321 final String s2
= "7b6e8167ec86acef9a4184b43210d583b6d2f99a";
322 final ObjectId i2
= ObjectId
.fromString(s2
);
323 assertTrue(a
.prefixCompare(i2
) < 0);
324 assertFalse(i2
.startsWith(a
));
326 final String s3
= "7b6e8057eca6acef9a4184b43210d583b6d2f99a";
327 final ObjectId i3
= ObjectId
.fromString(s3
);
328 assertTrue(a
.prefixCompare(i3
) > 0);
329 assertFalse(i3
.startsWith(a
));
332 public void testPrefixCompare_17() {
333 final String sa
= "7b6e8067ec96acef9";
334 final AbbreviatedObjectId a
= AbbreviatedObjectId
.fromString(sa
);
336 final String s1
= "7b6e8067ec96acef9a4184b43210d583b6d2f99a";
337 final ObjectId i1
= ObjectId
.fromString(s1
);
338 assertEquals(0, a
.prefixCompare(i1
));
339 assertTrue(i1
.startsWith(a
));
341 final String s2
= "7b6e8067eca6acef9a4184b43210d583b6d2f99a";
342 final ObjectId i2
= ObjectId
.fromString(s2
);
343 assertTrue(a
.prefixCompare(i2
) < 0);
344 assertFalse(i2
.startsWith(a
));
346 final String s3
= "7b6e8067ec86acef9a4184b43210d583b6d2f99a";
347 final ObjectId i3
= ObjectId
.fromString(s3
);
348 assertTrue(a
.prefixCompare(i3
) > 0);
349 assertFalse(i3
.startsWith(a
));