1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.notes;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16
17 import java.io.IOException;
18
19 import org.eclipse.jgit.lib.AnyObjectId;
20 import org.eclipse.jgit.lib.MutableObjectId;
21 import org.junit.Test;
22
23 public class LeafBucketTest {
24 @Test
25 public void testEmpty() {
26 LeafBucket b = new LeafBucket(0);
27 assertNull(b.getNote(id(0x00), null));
28 assertNull(b.getNote(id(0x01), null));
29 assertNull(b.getNote(id(0xfe), null));
30 }
31
32 @Test
33 public void testParseFive() {
34 LeafBucket b = new LeafBucket(0);
35
36 b.parseOneEntry(id(0x11), id(0x81));
37 b.parseOneEntry(id(0x22), id(0x82));
38 b.parseOneEntry(id(0x33), id(0x83));
39 b.parseOneEntry(id(0x44), id(0x84));
40 b.parseOneEntry(id(0x55), id(0x85));
41
42 assertNull(b.getNote(id(0x01), null));
43 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
44 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
45 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
46 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
47 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
48 assertNull(b.getNote(id(0x66), null));
49 }
50
51 @Test
52 public void testSetFive_InOrder() throws IOException {
53 LeafBucket b = new LeafBucket(0);
54
55 assertSame(b, b.set(id(0x11), id(0x81), null));
56 assertSame(b, b.set(id(0x22), id(0x82), null));
57 assertSame(b, b.set(id(0x33), id(0x83), null));
58 assertSame(b, b.set(id(0x44), id(0x84), null));
59 assertSame(b, b.set(id(0x55), id(0x85), null));
60
61 assertNull(b.getNote(id(0x01), null));
62 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
63 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
64 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
65 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
66 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
67 assertNull(b.getNote(id(0x66), null));
68 }
69
70 @Test
71 public void testSetFive_ReverseOrder() throws IOException {
72 LeafBucket b = new LeafBucket(0);
73
74 assertSame(b, b.set(id(0x55), id(0x85), null));
75 assertSame(b, b.set(id(0x44), id(0x84), null));
76 assertSame(b, b.set(id(0x33), id(0x83), null));
77 assertSame(b, b.set(id(0x22), id(0x82), null));
78 assertSame(b, b.set(id(0x11), id(0x81), null));
79
80 assertNull(b.getNote(id(0x01), null));
81 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
82 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
83 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
84 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
85 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
86 assertNull(b.getNote(id(0x66), null));
87 }
88
89 @Test
90 public void testSetFive_MixedOrder() throws IOException {
91 LeafBucket b = new LeafBucket(0);
92
93 assertSame(b, b.set(id(0x11), id(0x81), null));
94 assertSame(b, b.set(id(0x33), id(0x83), null));
95 assertSame(b, b.set(id(0x55), id(0x85), null));
96
97 assertSame(b, b.set(id(0x22), id(0x82), null));
98 assertSame(b, b.set(id(0x44), id(0x84), null));
99
100 assertNull(b.getNote(id(0x01), null));
101 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
102 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
103 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
104 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
105 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
106 assertNull(b.getNote(id(0x66), null));
107 }
108
109 @Test
110 public void testSet_Replace() throws IOException {
111 LeafBucket b = new LeafBucket(0);
112
113 assertSame(b, b.set(id(0x11), id(0x81), null));
114 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
115
116 assertSame(b, b.set(id(0x11), id(0x01), null));
117 assertEquals(id(0x01), b.getNote(id(0x11), null).getData());
118 }
119
120 @Test
121 public void testRemoveMissingNote() throws IOException {
122 LeafBucket b = new LeafBucket(0);
123 assertNull(b.getNote(id(0x11), null));
124 assertSame(b, b.set(id(0x11), null, null));
125 assertNull(b.getNote(id(0x11), null));
126 }
127
128 @Test
129 public void testRemoveFirst() throws IOException {
130 LeafBucket b = new LeafBucket(0);
131
132 assertSame(b, b.set(id(0x11), id(0x81), null));
133 assertSame(b, b.set(id(0x22), id(0x82), null));
134 assertSame(b, b.set(id(0x33), id(0x83), null));
135 assertSame(b, b.set(id(0x44), id(0x84), null));
136 assertSame(b, b.set(id(0x55), id(0x85), null));
137
138 assertSame(b, b.set(id(0x11), null, null));
139
140 assertNull(b.getNote(id(0x01), null));
141 assertNull(b.getNote(id(0x11), null));
142 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
143 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
144 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
145 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
146 assertNull(b.getNote(id(0x66), null));
147 }
148
149 @Test
150 public void testRemoveMiddle() throws IOException {
151 LeafBucket b = new LeafBucket(0);
152
153 assertSame(b, b.set(id(0x11), id(0x81), null));
154 assertSame(b, b.set(id(0x22), id(0x82), null));
155 assertSame(b, b.set(id(0x33), id(0x83), null));
156 assertSame(b, b.set(id(0x44), id(0x84), null));
157 assertSame(b, b.set(id(0x55), id(0x85), null));
158
159 assertSame(b, b.set(id(0x33), null, null));
160
161 assertNull(b.getNote(id(0x01), null));
162 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
163 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
164 assertNull(b.getNote(id(0x33), null));
165 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
166 assertEquals(id(0x85), b.getNote(id(0x55), null).getData());
167 assertNull(b.getNote(id(0x66), null));
168 }
169
170 @Test
171 public void testRemoveLast() throws IOException {
172 LeafBucket b = new LeafBucket(0);
173
174 assertSame(b, b.set(id(0x11), id(0x81), null));
175 assertSame(b, b.set(id(0x22), id(0x82), null));
176 assertSame(b, b.set(id(0x33), id(0x83), null));
177 assertSame(b, b.set(id(0x44), id(0x84), null));
178 assertSame(b, b.set(id(0x55), id(0x85), null));
179
180 assertSame(b, b.set(id(0x55), null, null));
181
182 assertNull(b.getNote(id(0x01), null));
183 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
184 assertEquals(id(0x82), b.getNote(id(0x22), null).getData());
185 assertEquals(id(0x83), b.getNote(id(0x33), null).getData());
186 assertEquals(id(0x84), b.getNote(id(0x44), null).getData());
187 assertNull(b.getNote(id(0x55), null));
188 assertNull(b.getNote(id(0x66), null));
189 }
190
191 @Test
192 public void testRemoveMakesEmpty() throws IOException {
193 LeafBucket b = new LeafBucket(0);
194
195 assertSame(b, b.set(id(0x11), id(0x81), null));
196 assertEquals(id(0x81), b.getNote(id(0x11), null).getData());
197
198 assertNull(b.set(id(0x11), null, null));
199 assertNull(b.getNote(id(0x11), null));
200 }
201
202 private static AnyObjectId id(int first) {
203 MutableObjectId id = new MutableObjectId();
204 id.setByte(1, first);
205 return id;
206 }
207 }