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