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 java.util.Locale;
53
54 import org.eclipse.jgit.errors.InvalidObjectIdException;
55 import org.junit.Test;
56
57 public class ObjectIdTest {
58 @Test
59 public void test001_toString() {
60 final String x = "def4c620bc3713bb1bb26b808ec9312548e73946";
61 final ObjectId oid = ObjectId.fromString(x);
62 assertEquals(x, oid.name());
63 }
64
65 @Test
66 public void test002_toString() {
67 final String x = "ff00eedd003713bb1bb26b808ec9312548e73946";
68 final ObjectId oid = ObjectId.fromString(x);
69 assertEquals(x, oid.name());
70 }
71
72 @Test
73 public void test003_equals() {
74 final String x = "def4c620bc3713bb1bb26b808ec9312548e73946";
75 final ObjectId a = ObjectId.fromString(x);
76 final ObjectId b = ObjectId.fromString(x);
77 assertEquals(a.hashCode(), b.hashCode());
78 assertEquals("a and b are same", b, a);
79 }
80
81 @Test
82 public void test004_isId() {
83 assertTrue("valid id", ObjectId
84 .isId("def4c620bc3713bb1bb26b808ec9312548e73946"));
85 }
86
87 @Test
88 public void test005_notIsId() {
89 assertFalse("bob is not an id", ObjectId.isId("bob"));
90 }
91
92 @Test
93 public void test006_notIsId() {
94 assertFalse("39 digits is not an id", ObjectId
95 .isId("def4c620bc3713bb1bb26b808ec9312548e7394"));
96 }
97
98 @Test
99 public void test007_isId() {
100 assertTrue("uppercase is accepted", ObjectId
101 .isId("Def4c620bc3713bb1bb26b808ec9312548e73946"));
102 }
103
104 @Test
105 public void test008_notIsId() {
106 assertFalse("g is not a valid hex digit", ObjectId
107 .isId("gef4c620bc3713bb1bb26b808ec9312548e73946"));
108 }
109
110 @Test
111 public void test009_toString() {
112 final String x = "ff00eedd003713bb1bb26b808ec9312548e73946";
113 final ObjectId oid = ObjectId.fromString(x);
114 assertEquals(x, ObjectId.toString(oid));
115 }
116
117 @Test
118 public void test010_toString() {
119 final String x = "0000000000000000000000000000000000000000";
120 assertEquals(x, ObjectId.toString(null));
121 }
122
123 @Test
124 public void test011_toString() {
125 final String x = "0123456789ABCDEFabcdef1234567890abcdefAB";
126 final ObjectId oid = ObjectId.fromString(x);
127 assertEquals(x.toLowerCase(Locale.ROOT), oid.name());
128 }
129
130 @Test(expected = InvalidObjectIdException.class)
131 public void testFromString_short() {
132 ObjectId.fromString("cafe1234");
133 }
134
135 @Test(expected = InvalidObjectIdException.class)
136 public void testFromString_nonHex() {
137 ObjectId.fromString("0123456789abcdefghij0123456789abcdefghij");
138 }
139
140 @Test(expected = InvalidObjectIdException.class)
141 public void testFromString_shortNonHex() {
142 ObjectId.fromString("6789ghij");
143 }
144
145 @Test
146 public void testGetByte() {
147 byte[] raw = new byte[20];
148 for (int i = 0; i < 20; i++)
149 raw[i] = (byte) (0xa0 + i);
150 ObjectId id = ObjectId.fromRaw(raw);
151
152 assertEquals(raw[0] & 0xff, id.getFirstByte());
153 assertEquals(raw[0] & 0xff, id.getByte(0));
154 assertEquals(raw[1] & 0xff, id.getByte(1));
155
156 for (int i = 2; i < 20; i++)
157 assertEquals("index " + i, raw[i] & 0xff, id.getByte(i));
158 }
159
160 @Test
161 public void testSetByte() {
162 byte[] exp = new byte[20];
163 for (int i = 0; i < 20; i++)
164 exp[i] = (byte) (0xa0 + i);
165
166 MutableObjectId id = new MutableObjectId();
167 id.fromRaw(exp);
168 assertEquals(ObjectId.fromRaw(exp).name(), id.name());
169
170 id.setByte(0, 0x10);
171 assertEquals(0x10, id.getByte(0));
172 exp[0] = 0x10;
173 assertEquals(ObjectId.fromRaw(exp).name(), id.name());
174
175 for (int p = 1; p < 20; p++) {
176 id.setByte(p, 0x10 + p);
177 assertEquals(0x10 + p, id.getByte(p));
178 exp[p] = (byte) (0x10 + p);
179 assertEquals(ObjectId.fromRaw(exp).name(), id.name());
180 }
181
182 for (int p = 0; p < 20; p++) {
183 id.setByte(p, 0x80 + p);
184 assertEquals(0x80 + p, id.getByte(p));
185 exp[p] = (byte) (0x80 + p);
186 assertEquals(ObjectId.fromRaw(exp).name(), id.name());
187 }
188 }
189 }