1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.lfs.lib;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14 import static java.nio.charset.StandardCharsets.US_ASCII;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotEquals;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.nio.ByteBuffer;
25 import java.nio.charset.Charset;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.util.Locale;
29
30 import org.eclipse.jgit.junit.JGitTestUtil;
31 import org.eclipse.jgit.lfs.errors.InvalidLongObjectIdException;
32 import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
33 import org.eclipse.jgit.util.FileUtils;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37
38
39
40
41 public class LongObjectIdTest {
42 private static Path tmp;
43
44 @BeforeClass
45 public static void setup() throws IOException {
46 tmp = Files.createTempDirectory("jgit_test_");
47 }
48
49 @AfterClass
50 public static void tearDown() throws IOException {
51 FileUtils.delete(tmp.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
52 }
53
54 @Test
55 public void test001_toString() {
56 final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a";
57 final LongObjectId oid = LongObjectId.fromString(x);
58 assertEquals(x, oid.name());
59 }
60
61 @Test
62 public void test002_toString() {
63 final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf";
64 final LongObjectId oid = LongObjectId.fromString(x);
65 assertEquals(x, oid.name());
66 }
67
68 @Test
69 public void test003_equals() {
70 final String x = "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a";
71 final LongObjectId a = LongObjectId.fromString(x);
72 final LongObjectId b = LongObjectId.fromString(x);
73 assertEquals(a.hashCode(), b.hashCode());
74 assertEquals("a and b should be equal", b, a);
75 }
76
77 @Test
78 public void test004_isId() {
79 assertTrue("valid id", LongObjectId.isId(
80 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"));
81 }
82
83 @Test
84 public void test005_notIsId() {
85 assertFalse("bob is not an id", LongObjectId.isId("bob"));
86 }
87
88 @Test
89 public void test006_notIsId() {
90 assertFalse("63 digits is not an id", LongObjectId.isId(
91 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0"));
92 }
93
94 @Test
95 public void test007_isId() {
96 assertTrue("uppercase is accepted", LongObjectId.isId(
97 "8367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2dEb7ab0A"));
98 }
99
100 @Test
101 public void test008_notIsId() {
102 assertFalse("g is not a valid hex digit", LongObjectId.isId(
103 "g367b0edc81df80e6b42eb1b71f783111224e058cb3da37894d065d2deb7ab0a"));
104 }
105
106 @Test
107 public void test009_toString() {
108 final String x = "140ce71d628cceb78e3709940ba52a651a0c4a9c1400f2e15e998a1a43887edf";
109 final LongObjectId oid = LongObjectId.fromString(x);
110 assertEquals(x, LongObjectId.toString(oid));
111 }
112
113 @Test
114 public void test010_toString() {
115 final String x = "0000000000000000000000000000000000000000000000000000000000000000";
116 assertEquals(x, LongObjectId.toString(null));
117 }
118
119 @Test
120 public void test011_toString() {
121 final String x = "0123456789ABCDEFabcdef01234567890123456789ABCDEFabcdef0123456789";
122 final LongObjectId oid = LongObjectId.fromString(x);
123 assertEquals(x.toLowerCase(Locale.ROOT), oid.name());
124 }
125
126 @Test
127 public void testGetByte() {
128 byte[] raw = new byte[32];
129 for (int i = 0; i < 32; i++)
130 raw[i] = (byte) (0xa0 + i);
131 LongObjectId id = LongObjectId.fromRaw(raw);
132
133 assertEquals(raw[0] & 0xff, id.getFirstByte());
134 assertEquals(raw[0] & 0xff, id.getByte(0));
135 assertEquals(raw[1] & 0xff, id.getByte(1));
136 assertEquals(raw[1] & 0xff, id.getSecondByte());
137
138 for (int i = 2; i < 32; i++) {
139 assertEquals("index " + i, raw[i] & 0xff, id.getByte(i));
140 }
141 try {
142 id.getByte(32);
143 fail("LongObjectId has 32 byte only");
144 } catch (ArrayIndexOutOfBoundsException e) {
145
146 }
147 }
148
149 @Test
150 public void testSetByte() {
151 byte[] exp = new byte[32];
152 for (int i = 0; i < 32; i++) {
153 exp[i] = (byte) (0xa0 + i);
154 }
155
156 MutableLongObjectId id = new MutableLongObjectId();
157 id.fromRaw(exp);
158 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
159
160 id.setByte(0, 0x10);
161 assertEquals(0x10, id.getByte(0));
162 exp[0] = 0x10;
163 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
164
165 for (int p = 1; p < 32; p++) {
166 id.setByte(p, 0x10 + p);
167 assertEquals(0x10 + p, id.getByte(p));
168 exp[p] = (byte) (0x10 + p);
169 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
170 }
171
172 for (int p = 0; p < 32; p++) {
173 id.setByte(p, 0x80 + p);
174 assertEquals(0x80 + p, id.getByte(p));
175 exp[p] = (byte) (0x80 + p);
176 assertEquals(LongObjectId.fromRaw(exp).name(), id.name());
177 }
178 }
179
180 @Test
181 public void testZeroId() {
182 AnyLongObjectId zero = new LongObjectId(0L, 0L, 0L, 0L);
183 assertEquals(zero, LongObjectId.zeroId());
184 assertEquals(
185 "0000000000000000000000000000000000000000000000000000000000000000",
186 LongObjectId.zeroId().name());
187 }
188
189 @Test
190 public void testEquals() {
191 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
192 assertTrue("id should equal itself", id1.equals(id1));
193 AnyLongObjectId id2 = new LongObjectId(id1);
194 assertEquals("objects should be equals", id1, id2);
195
196 id2 = LongObjectIdTestUtils.hash("other");
197 assertNotEquals("objects should be not equal", id1, id2);
198 }
199
200 @Test
201 public void testCopyRawBytes() {
202 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
203 AnyLongObjectId id2 = new LongObjectId(id1);
204
205 byte[] buf = new byte[64];
206 id1.copyRawTo(buf, 0);
207 id2.copyRawTo(buf, 32);
208 assertTrue("objects should be equals",
209 LongObjectId.equals(buf, 0, buf, 32));
210 }
211
212 @Test
213 public void testCopyRawLongs() {
214 long[] a = new long[4];
215 a[0] = 1L;
216 a[1] = 2L;
217 a[2] = 3L;
218 a[3] = 4L;
219 AnyLongObjectId id1 = new LongObjectId(a[0], a[1], a[2], a[3]);
220 AnyLongObjectId id2 = LongObjectId.fromRaw(a);
221 assertEquals("objects should be equals", id1, id2);
222 }
223
224 @Test
225 public void testCopyFromStringInvalid() {
226 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
227 try {
228 LongObjectId.fromString(id1.name() + "01234");
229 fail("expected InvalidLongObjectIdException");
230 } catch (InvalidLongObjectIdException e) {
231 assertEquals("Invalid id: " + id1.name() + "01234",
232 e.getMessage());
233 }
234 }
235
236 @Test
237 public void testCopyFromStringByte() {
238 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
239 byte[] buf = new byte[64];
240 Charset cs = US_ASCII;
241 cs.encode(id1.name()).get(buf);
242 AnyLongObjectId id2 = LongObjectId.fromString(buf, 0);
243 assertEquals("objects should be equals", id1, id2);
244 }
245
246 @Test
247 public void testHashFile() throws IOException {
248 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
249 Path f = tmp.resolve("test");
250 JGitTestUtil.write(f.toFile(), "test");
251 AnyLongObjectId id2 = LongObjectIdTestUtils.hash(f);
252 assertEquals("objects should be equals", id1, id2);
253 }
254
255 @Test
256 public void testCompareTo() {
257 AnyLongObjectId id1 = LongObjectId.fromString(
258 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
259 assertEquals(0, id1.compareTo(LongObjectId.fromString(
260 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
261 AnyLongObjectId self = id1;
262 assertEquals(0, id1.compareTo(self));
263
264 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
265 "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
266 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
267 "0123456789abcdef1123456789abcdef0123456789abcdef0123456789abcdef")));
268 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
269 "0123456789abcdef0123456789abcdef1123456789abcdef0123456789abcdef")));
270 assertEquals(-1, id1.compareTo(LongObjectId.fromString(
271 "0123456789abcdef0123456789abcdef0123456789abcdef1123456789abcdef")));
272
273 assertEquals(1, id1.compareTo(LongObjectId.fromString(
274 "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")));
275 assertEquals(1, id1.compareTo(LongObjectId.fromString(
276 "0123456789abcdef0023456789abcdef0123456789abcdef0123456789abcdef")));
277 assertEquals(1, id1.compareTo(LongObjectId.fromString(
278 "0123456789abcdef0123456789abcdef0023456789abcdef0123456789abcdef")));
279 assertEquals(1, id1.compareTo(LongObjectId.fromString(
280 "0123456789abcdef0123456789abcdef0123456789abcdef0023456789abcdef")));
281 }
282
283 @Test
284 public void testCompareToByte() {
285 AnyLongObjectId id1 = LongObjectId.fromString(
286 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
287 byte[] buf = new byte[32];
288 id1.copyRawTo(buf, 0);
289 assertEquals(0, id1.compareTo(buf, 0));
290
291 LongObjectId
292 .fromString(
293 "1123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
294 .copyRawTo(buf, 0);
295 assertEquals(-1, id1.compareTo(buf, 0));
296
297 LongObjectId
298 .fromString(
299 "0023456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
300 .copyRawTo(buf, 0);
301 assertEquals(1, id1.compareTo(buf, 0));
302 }
303
304 @Test
305 public void testCompareToLong() {
306 AnyLongObjectId id1 = new LongObjectId(1L, 2L, 3L, 4L);
307 long[] buf = new long[4];
308 id1.copyRawTo(buf, 0);
309 assertEquals(0, id1.compareTo(buf, 0));
310
311 new LongObjectId(2L, 2L, 3L, 4L).copyRawTo(buf, 0);
312 assertEquals(-1, id1.compareTo(buf, 0));
313
314 new LongObjectId(0L, 2L, 3L, 4L).copyRawTo(buf, 0);
315 assertEquals(1, id1.compareTo(buf, 0));
316 }
317
318 @Test
319 public void testCopyToByte() {
320 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
321 byte[] buf = new byte[64];
322 id1.copyTo(buf, 0);
323 assertEquals(id1, LongObjectId.fromString(buf, 0));
324 }
325
326 @Test
327 public void testCopyRawToByteBuffer() {
328 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
329 ByteBuffer buf = ByteBuffer.allocate(32);
330 id1.copyRawTo(buf);
331 assertEquals(id1, LongObjectId.fromRaw(buf.array(), 0));
332 }
333
334 @Test
335 public void testCopyToByteBuffer() {
336 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
337 ByteBuffer buf = ByteBuffer.allocate(64);
338 id1.copyTo(buf);
339 assertEquals(id1, LongObjectId.fromString(buf.array(), 0));
340 }
341
342 @Test
343 public void testCopyRawToOutputStream() throws IOException {
344 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
345 ByteArrayOutputStream os = new ByteArrayOutputStream(32);
346 id1.copyRawTo(os);
347 assertEquals(id1, LongObjectId.fromRaw(os.toByteArray(), 0));
348 }
349
350 @Test
351 public void testCopyToOutputStream() throws IOException {
352 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
353 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
354 id1.copyTo(os);
355 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
356 }
357
358 @Test
359 public void testCopyToWriter() throws IOException {
360 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
361 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
362 try (OutputStreamWriter w = new OutputStreamWriter(os,
363 UTF_8)) {
364 id1.copyTo(w);
365 }
366 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
367 }
368
369 @Test
370 public void testCopyToWriterWithBuf() throws IOException {
371 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
372 ByteArrayOutputStream os = new ByteArrayOutputStream(64);
373 try (OutputStreamWriter w = new OutputStreamWriter(os,
374 UTF_8)) {
375 char[] buf = new char[64];
376 id1.copyTo(buf, w);
377 }
378 assertEquals(id1, LongObjectId.fromString(os.toByteArray(), 0));
379 }
380
381 @Test
382 public void testCopyToStringBuilder() {
383 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
384 StringBuilder sb = new StringBuilder();
385 char[] buf = new char[64];
386 id1.copyTo(buf, sb);
387 assertEquals(id1, LongObjectId.fromString(sb.toString()));
388 }
389
390 @Test
391 public void testCopy() {
392 AnyLongObjectId id1 = LongObjectIdTestUtils.hash("test");
393 assertEquals(id1.copy(), id1);
394 MutableLongObjectId id2 = new MutableLongObjectId();
395 id2.fromObjectId(id1);
396 assertEquals(id1, id2.copy());
397 }
398 }