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