1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.lib;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18
19 import java.util.Locale;
20
21 import org.eclipse.jgit.errors.InvalidObjectIdException;
22 import org.junit.Test;
23
24 public class ObjectIdTest {
25 @Test
26 public void test001_toString() {
27 final String x = "def4c620bc3713bb1bb26b808ec9312548e73946";
28 final ObjectId oid = ObjectId.fromString(x);
29 assertEquals(x, oid.name());
30 }
31
32 @Test
33 public void test002_toString() {
34 final String x = "ff00eedd003713bb1bb26b808ec9312548e73946";
35 final ObjectId oid = ObjectId.fromString(x);
36 assertEquals(x, oid.name());
37 }
38
39 @Test
40 public void test003_equals() {
41 final String x = "def4c620bc3713bb1bb26b808ec9312548e73946";
42 final ObjectId a = ObjectId.fromString(x);
43 final ObjectId b = ObjectId.fromString(x);
44 assertEquals(a.hashCode(), b.hashCode());
45 assertEquals("a and b are same", b, a);
46 }
47
48 @Test
49 public void test004_isId() {
50 assertTrue("valid id", ObjectId
51 .isId("def4c620bc3713bb1bb26b808ec9312548e73946"));
52 }
53
54 @Test
55 public void test005_notIsId() {
56 assertFalse("bob is not an id", ObjectId.isId("bob"));
57 }
58
59 @Test
60 public void test006_notIsId() {
61 assertFalse("39 digits is not an id", ObjectId
62 .isId("def4c620bc3713bb1bb26b808ec9312548e7394"));
63 }
64
65 @Test
66 public void test007_isId() {
67 assertTrue("uppercase is accepted", ObjectId
68 .isId("Def4c620bc3713bb1bb26b808ec9312548e73946"));
69 }
70
71 @Test
72 public void test008_notIsId() {
73 assertFalse("g is not a valid hex digit", ObjectId
74 .isId("gef4c620bc3713bb1bb26b808ec9312548e73946"));
75 }
76
77 @Test
78 public void test009_toString() {
79 final String x = "ff00eedd003713bb1bb26b808ec9312548e73946";
80 final ObjectId oid = ObjectId.fromString(x);
81 assertEquals(x, ObjectId.toString(oid));
82 }
83
84 @Test
85 public void test010_toString() {
86 final String x = "0000000000000000000000000000000000000000";
87 assertEquals(x, ObjectId.toString(null));
88 }
89
90 @Test
91 public void test011_toString() {
92 final String x = "0123456789ABCDEFabcdef1234567890abcdefAB";
93 final ObjectId oid = ObjectId.fromString(x);
94 assertEquals(x.toLowerCase(Locale.ROOT), oid.name());
95 }
96
97 @Test(expected = InvalidObjectIdException.class)
98 public void testFromString_short() {
99 ObjectId.fromString("cafe1234");
100 }
101
102 @Test(expected = InvalidObjectIdException.class)
103 public void testFromString_nonHex() {
104 ObjectId.fromString("0123456789abcdefghij0123456789abcdefghij");
105 }
106
107 @Test(expected = InvalidObjectIdException.class)
108 public void testFromString_shortNonHex() {
109 ObjectId.fromString("6789ghij");
110 }
111
112 @Test
113 public void testGetByte() {
114 byte[] raw = new byte[20];
115 for (int i = 0; i < 20; i++)
116 raw[i] = (byte) (0xa0 + i);
117 ObjectId id = ObjectId.fromRaw(raw);
118
119 assertEquals(raw[0] & 0xff, id.getFirstByte());
120 assertEquals(raw[0] & 0xff, id.getByte(0));
121 assertEquals(raw[1] & 0xff, id.getByte(1));
122
123 for (int i = 2; i < 20; i++)
124 assertEquals("index " + i, raw[i] & 0xff, id.getByte(i));
125 }
126
127 @Test
128 public void testSetByte() {
129 byte[] exp = new byte[20];
130 for (int i = 0; i < 20; i++)
131 exp[i] = (byte) (0xa0 + i);
132
133 MutableObjectId id = new MutableObjectId();
134 id.fromRaw(exp);
135 assertEquals(ObjectId.fromRaw(exp).name(), id.name());
136
137 id.setByte(0, 0x10);
138 assertEquals(0x10, id.getByte(0));
139 exp[0] = 0x10;
140 assertEquals(ObjectId.fromRaw(exp).name(), id.name());
141
142 for (int p = 1; p < 20; p++) {
143 id.setByte(p, 0x10 + p);
144 assertEquals(0x10 + p, id.getByte(p));
145 exp[p] = (byte) (0x10 + p);
146 assertEquals(ObjectId.fromRaw(exp).name(), id.name());
147 }
148
149 for (int p = 0; p < 20; p++) {
150 id.setByte(p, 0x80 + p);
151 assertEquals(0x80 + p, id.getByte(p));
152 exp[p] = (byte) (0x80 + p);
153 assertEquals(ObjectId.fromRaw(exp).name(), id.name());
154 }
155 }
156 }