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 package org.eclipse.jgit.notes;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNull;
48 import static org.junit.Assert.assertSame;
49
50 import java.io.IOException;
51
52 import org.eclipse.jgit.lib.AnyObjectId;
53 import org.eclipse.jgit.lib.MutableObjectId;
54 import org.junit.Test;
55
56 public class LeafBucketTest {
57 @Test
58 public void testEmpty() {
59 LeafBucket b = new LeafBucket(0);
60 assertNull(b.getNote(id(0x00), null));
61 assertNull(b.getNote(id(0x01), null));
62 assertNull(b.getNote(id(0xfe), null));
63 }
64
65 @Test
66 public void testParseFive() {
67 LeafBucket b = new LeafBucket(0);
68
69 b.parseOneEntry(id(0x11), id(0x81));
70 b.parseOneEntry(id(0x22), id(0x82));
71 b.parseOneEntry(id(0x33), id(0x83));
72 b.parseOneEntry(id(0x44), id(0x84));
73 b.parseOneEntry(id(0x55), id(0x85));
74
75 assertNull(b.getNote(id(0x01), null));
76 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
77 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
78 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
79 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
80 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
81 assertNull(b.getNote(id(0x66), null));
82 }
83
84 @Test
85 public void testSetFive_InOrder() throws IOException {
86 LeafBucket b = new LeafBucket(0);
87
88 assertSame(b, b.set(id(0x11), id(0x81), null));
89 assertSame(b, b.set(id(0x22), id(0x82), null));
90 assertSame(b, b.set(id(0x33), id(0x83), null));
91 assertSame(b, b.set(id(0x44), id(0x84), null));
92 assertSame(b, b.set(id(0x55), id(0x85), null));
93
94 assertNull(b.getNote(id(0x01), null));
95 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
96 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
97 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
98 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
99 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
100 assertNull(b.getNote(id(0x66), null));
101 }
102
103 @Test
104 public void testSetFive_ReverseOrder() throws IOException {
105 LeafBucket b = new LeafBucket(0);
106
107 assertSame(b, b.set(id(0x55), id(0x85), null));
108 assertSame(b, b.set(id(0x44), id(0x84), null));
109 assertSame(b, b.set(id(0x33), id(0x83), null));
110 assertSame(b, b.set(id(0x22), id(0x82), null));
111 assertSame(b, b.set(id(0x11), id(0x81), null));
112
113 assertNull(b.getNote(id(0x01), null));
114 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
115 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
116 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
117 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
118 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
119 assertNull(b.getNote(id(0x66), null));
120 }
121
122 @Test
123 public void testSetFive_MixedOrder() throws IOException {
124 LeafBucket b = new LeafBucket(0);
125
126 assertSame(b, b.set(id(0x11), id(0x81), null));
127 assertSame(b, b.set(id(0x33), id(0x83), null));
128 assertSame(b, b.set(id(0x55), id(0x85), null));
129
130 assertSame(b, b.set(id(0x22), id(0x82), null));
131 assertSame(b, b.set(id(0x44), id(0x84), null));
132
133 assertNull(b.getNote(id(0x01), null));
134 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
135 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
136 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
137 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
138 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
139 assertNull(b.getNote(id(0x66), null));
140 }
141
142 @Test
143 public void testSet_Replace() throws IOException {
144 LeafBucket b = new LeafBucket(0);
145
146 assertSame(b, b.set(id(0x11), id(0x81), null));
147 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
148
149 assertSame(b, b.set(id(0x11), id(0x01), null));
150 assertEquals(id(0x01), b.getNote(id(0x11), null).getData());
151 }
152
153 @Test
154 public void testRemoveMissingNote() throws IOException {
155 LeafBucket b = new LeafBucket(0);
156 assertNull(b.getNote(id(0x11), null));
157 assertSame(b, b.set(id(0x11), null, null));
158 assertNull(b.getNote(id(0x11), null));
159 }
160
161 @Test
162 public void testRemoveFirst() throws IOException {
163 LeafBucket b = new LeafBucket(0);
164
165 assertSame(b, b.set(id(0x11), id(0x81), null));
166 assertSame(b, b.set(id(0x22), id(0x82), null));
167 assertSame(b, b.set(id(0x33), id(0x83), null));
168 assertSame(b, b.set(id(0x44), id(0x84), null));
169 assertSame(b, b.set(id(0x55), id(0x85), null));
170
171 assertSame(b, b.set(id(0x11), null, null));
172
173 assertNull(b.getNote(id(0x01), null));
174 assertNull(b.getNote(id(0x11), null));
175 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
176 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
177 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
178 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
179 assertNull(b.getNote(id(0x66), null));
180 }
181
182 @Test
183 public void testRemoveMiddle() throws IOException {
184 LeafBucket b = new LeafBucket(0);
185
186 assertSame(b, b.set(id(0x11), id(0x81), null));
187 assertSame(b, b.set(id(0x22), id(0x82), null));
188 assertSame(b, b.set(id(0x33), id(0x83), null));
189 assertSame(b, b.set(id(0x44), id(0x84), null));
190 assertSame(b, b.set(id(0x55), id(0x85), null));
191
192 assertSame(b, b.set(id(0x33), null, null));
193
194 assertNull(b.getNote(id(0x01), null));
195 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
196 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
197 assertNull(b.getNote(id(0x33), null));
198 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
199 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
200 assertNull(b.getNote(id(0x66), null));
201 }
202
203 @Test
204 public void testRemoveLast() throws IOException {
205 LeafBucket b = new LeafBucket(0);
206
207 assertSame(b, b.set(id(0x11), id(0x81), null));
208 assertSame(b, b.set(id(0x22), id(0x82), null));
209 assertSame(b, b.set(id(0x33), id(0x83), null));
210 assertSame(b, b.set(id(0x44), id(0x84), null));
211 assertSame(b, b.set(id(0x55), id(0x85), null));
212
213 assertSame(b, b.set(id(0x55), null, null));
214
215 assertNull(b.getNote(id(0x01), null));
216 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
217 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
218 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
219 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
220 assertNull(b.getNote(id(0x55), null));
221 assertNull(b.getNote(id(0x66), null));
222 }
223
224 @Test
225 public void testRemoveMakesEmpty() throws IOException {
226 LeafBucket b = new LeafBucket(0);
227
228 assertSame(b, b.set(id(0x11), id(0x81), null));
229 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
230
231 assertNull(b.set(id(0x11), null, null));
232 assertNull(b.getNote(id(0x11), null));
233 }
234
235 private static AnyObjectId id(int first) {
236 MutableObjectId id = new MutableObjectId();
237 id.setByte(1, first);
238 return id;
239 }
240 }