1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mavibot.btree.managed;
21
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.File;
26 import java.lang.reflect.Method;
27 import java.nio.ByteBuffer;
28
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.TemporaryFolder;
32
33
34
35
36
37
38
39 public class StoreTest
40 {
41 @Rule
42 public TemporaryFolder tempFolder = new TemporaryFolder();
43
44
45
46
47 @Test
48 public void testInjectInt() throws Exception
49 {
50 File tempFile = tempFolder.newFile( "mavibot.db" );
51 String tempFileName = tempFile.getAbsolutePath();
52
53 RecordManager recordManager = new RecordManager( tempFileName, 4 * 1024 );
54 Method method = RecordManager.class.getDeclaredMethod( "store", long.class, int.class, PageIO[].class );
55 method.setAccessible( true );
56
57
58 PageIO[] pageIos = new PageIO[2];
59 pageIos[0] = new PageIO();
60 pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
61 pageIos[1] = new PageIO();
62 pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
63
64
65 long position = ( Long ) method.invoke( recordManager, 0, 0x12345678, pageIos );
66
67 assertEquals( 4, position );
68 int pos = 12;
69 assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
70 assertEquals( 0x34, pageIos[0].getData().get( pos++ ) );
71 assertEquals( 0x56, pageIos[0].getData().get( pos++ ) );
72 assertEquals( 0x78, pageIos[0].getData().get( pos++ ) );
73
74
75 position = ( Long ) method.invoke( recordManager, 4080, 0x12345678, pageIos );
76
77 assertEquals( 4084, position );
78 pos = 4092;
79 assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
80 assertEquals( 0x34, pageIos[0].getData().get( pos++ ) );
81 assertEquals( 0x56, pageIos[0].getData().get( pos++ ) );
82 assertEquals( 0x78, pageIos[0].getData().get( pos++ ) );
83
84
85
86 position = ( Long ) method.invoke( recordManager, 4081, 0x12345678, pageIos );
87
88 assertEquals( 4085, position );
89 pos = 4093;
90 assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
91 assertEquals( 0x34, pageIos[0].getData().get( pos++ ) );
92 assertEquals( 0x56, pageIos[0].getData().get( pos++ ) );
93 pos = 8;
94 assertEquals( 0x78, pageIos[1].getData().get( pos++ ) );
95
96
97
98 position = ( Long ) method.invoke( recordManager, 4082, 0x12345678, pageIos );
99
100 assertEquals( 4086, position );
101 pos = 4094;
102 assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
103 assertEquals( 0x34, pageIos[0].getData().get( pos++ ) );
104 pos = 8;
105 assertEquals( 0x56, pageIos[1].getData().get( pos++ ) );
106 assertEquals( 0x78, pageIos[1].getData().get( pos++ ) );
107
108
109
110 position = ( Long ) method.invoke( recordManager, 4083, 0x12345678, pageIos );
111
112 assertEquals( 4087, position );
113 pos = 4095;
114 assertEquals( 0x12, pageIos[0].getData().get( pos++ ) );
115 pos = 8;
116 assertEquals( 0x34, pageIos[1].getData().get( pos++ ) );
117 assertEquals( 0x56, pageIos[1].getData().get( pos++ ) );
118 assertEquals( 0x78, pageIos[1].getData().get( pos++ ) );
119
120
121 position = ( Long ) method.invoke( recordManager, 4084, 0x12345678, pageIos );
122
123 assertEquals( 4088, position );
124 pos = 8;
125 assertEquals( 0x12, pageIos[1].getData().get( pos++ ) );
126 assertEquals( 0x34, pageIos[1].getData().get( pos++ ) );
127 assertEquals( 0x56, pageIos[1].getData().get( pos++ ) );
128 assertEquals( 0x78, pageIos[1].getData().get( pos++ ) );
129 }
130
131
132
133
134
135 @Test
136 public void testInjectLong() throws Exception
137 {
138 File tempFile = tempFolder.newFile( "mavibot.db" );
139 String tempFileName = tempFile.getAbsolutePath();
140
141 RecordManager recordManager = new RecordManager( tempFileName, 4 * 1024 );
142 Method method = RecordManager.class.getDeclaredMethod( "store", long.class, long.class, PageIO[].class );
143 method.setAccessible( true );
144
145
146 PageIO[] pageIos = new PageIO[2];
147 pageIos[0] = new PageIO();
148 pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
149 pageIos[1] = new PageIO();
150 pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
151
152
153 long position = ( Long ) method.invoke( recordManager, 0, 0x0123456789ABCDEFL, pageIos );
154
155 assertEquals( 8, position );
156 int pos = 12;
157 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
158 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
159 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
160 assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
161 assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
162 assertEquals( ( byte ) 0xAB, pageIos[0].getData().get( pos++ ) );
163 assertEquals( ( byte ) 0xCD, pageIos[0].getData().get( pos++ ) );
164 assertEquals( ( byte ) 0xEF, pageIos[0].getData().get( pos++ ) );
165
166
167 position = ( Long ) method.invoke( recordManager, 4076, 0x0123456789ABCDEFL, pageIos );
168
169 assertEquals( 4084, position );
170 pos = 4088;
171 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
172 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
173 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
174 assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
175 assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
176 assertEquals( ( byte ) 0xAB, pageIos[0].getData().get( pos++ ) );
177 assertEquals( ( byte ) 0xCD, pageIos[0].getData().get( pos++ ) );
178 assertEquals( ( byte ) 0xEF, pageIos[0].getData().get( pos++ ) );
179
180
181
182 position = ( Long ) method.invoke( recordManager, 4077, 0x0123456789ABCDEFL, pageIos );
183
184 assertEquals( 4085, position );
185 pos = 4089;
186 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
187 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
188 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
189 assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
190 assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
191 assertEquals( ( byte ) 0xAB, pageIos[0].getData().get( pos++ ) );
192 assertEquals( ( byte ) 0xCD, pageIos[0].getData().get( pos++ ) );
193 pos = 8;
194 assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
195
196
197
198 position = ( Long ) method.invoke( recordManager, 4078, 0x0123456789ABCDEFL, pageIos );
199
200 assertEquals( 4086, position );
201 pos = 4090;
202 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
203 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
204 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
205 assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
206 assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
207 assertEquals( ( byte ) 0xAB, pageIos[0].getData().get( pos++ ) );
208 pos = 8;
209 assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
210 assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
211
212
213
214 position = ( Long ) method.invoke( recordManager, 4079, 0x0123456789ABCDEFL, pageIos );
215
216 assertEquals( 4087, position );
217 pos = 4091;
218 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
219 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
220 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
221 assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
222 assertEquals( ( byte ) 0x89, pageIos[0].getData().get( pos++ ) );
223 pos = 8;
224 assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
225 assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
226 assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
227
228
229
230 position = ( Long ) method.invoke( recordManager, 4080, 0x0123456789ABCDEFL, pageIos );
231
232 assertEquals( 4088, position );
233 pos = 4092;
234 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
235 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
236 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
237 assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
238 pos = 8;
239 assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
240 assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
241 assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
242 assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
243
244
245
246 position = ( Long ) method.invoke( recordManager, 4081, 0x0123456789ABCDEFL, pageIos );
247
248 assertEquals( 4089, position );
249 pos = 4093;
250 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
251 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
252 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
253 pos = 8;
254 assertEquals( 0x67, pageIos[1].getData().get( pos++ ) );
255 assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
256 assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
257 assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
258 assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
259
260
261
262 position = ( Long ) method.invoke( recordManager, 4082, 0x0123456789ABCDEFL, pageIos );
263
264 assertEquals( 4090, position );
265 pos = 4094;
266 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
267 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
268 pos = 8;
269 assertEquals( 0x45, pageIos[1].getData().get( pos++ ) );
270 assertEquals( 0x67, pageIos[1].getData().get( pos++ ) );
271 assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
272 assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
273 assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
274 assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
275
276
277
278 position = ( Long ) method.invoke( recordManager, 4083, 0x0123456789ABCDEFL, pageIos );
279
280 assertEquals( 4091, position );
281 pos = 4095;
282 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
283 pos = 8;
284 assertEquals( 0x23, pageIos[1].getData().get( pos++ ) );
285 assertEquals( 0x45, pageIos[1].getData().get( pos++ ) );
286 assertEquals( 0x67, pageIos[1].getData().get( pos++ ) );
287 assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
288 assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
289 assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
290 assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
291
292
293 position = ( Long ) method.invoke( recordManager, 4084, 0x0123456789ABCDEFL, pageIos );
294
295 assertEquals( 4092, position );
296 pos = 8;
297 assertEquals( 0x01, pageIos[1].getData().get( pos++ ) );
298 assertEquals( 0x23, pageIos[1].getData().get( pos++ ) );
299 assertEquals( 0x45, pageIos[1].getData().get( pos++ ) );
300 assertEquals( 0x67, pageIos[1].getData().get( pos++ ) );
301 assertEquals( ( byte ) 0x89, pageIos[1].getData().get( pos++ ) );
302 assertEquals( ( byte ) 0xAB, pageIos[1].getData().get( pos++ ) );
303 assertEquals( ( byte ) 0xCD, pageIos[1].getData().get( pos++ ) );
304 assertEquals( ( byte ) 0xEF, pageIos[1].getData().get( pos++ ) );
305 }
306
307
308
309
310
311 @Test
312 public void testInjectBytes() throws Exception
313 {
314 File tempFile = tempFolder.newFile( "mavibot.db" );
315 String tempFileName = tempFile.getAbsolutePath();
316
317
318 RecordManager recordManager = new RecordManager( tempFileName, 32 );
319 Method storeMethod = RecordManager.class.getDeclaredMethod( "store", long.class, byte[].class, PageIO[].class );
320 storeMethod.setAccessible( true );
321
322
323 PageIO[] pageIos = new PageIO[4];
324 pageIos[0] = new PageIO();
325 pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
326 pageIos[1] = new PageIO();
327 pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
328 pageIos[2] = new PageIO();
329 pageIos[2].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
330 pageIos[3] = new PageIO();
331 pageIos[3].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
332
333
334 byte[] bytes = new byte[]
335 { 0x01, 0x23, 0x45, 0x67 };
336
337
338 long position = ( Long ) storeMethod.invoke( recordManager, 0L, bytes, pageIos );
339
340 assertEquals( 8, position );
341 int pos = 12;
342
343 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
344 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
345 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
346 assertEquals( 0x04, pageIos[0].getData().get( pos++ ) );
347
348 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
349 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
350 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
351 assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
352
353
354 position = ( Long ) storeMethod.invoke( recordManager, 12L, bytes, pageIos );
355
356 assertEquals( 20, position );
357 pos = 24;
358
359 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
360 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
361 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
362 assertEquals( 0x04, pageIos[0].getData().get( pos++ ) );
363
364 assertEquals( 0x01, pageIos[0].getData().get( pos++ ) );
365 assertEquals( 0x23, pageIos[0].getData().get( pos++ ) );
366 assertEquals( 0x45, pageIos[0].getData().get( pos++ ) );
367 assertEquals( 0x67, pageIos[0].getData().get( pos++ ) );
368
369
370 bytes = new byte[16];
371
372 for ( int i = 0; i < 16; i++ )
373 {
374 bytes[i] = ( byte ) ( i + 1 );
375 }
376
377 position = ( Long ) storeMethod.invoke( recordManager, 0L, bytes, pageIos );
378
379 assertEquals( 20, position );
380 pos = 12;
381
382 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
383 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
384 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
385 assertEquals( 0x10, pageIos[0].getData().get( pos++ ) );
386
387
388 for ( int i = 0; i < 16; i++ )
389 {
390 assertEquals( ( byte ) ( i + 1 ), pageIos[0].getData().get( pos++ ) );
391 }
392
393
394 position = ( Long ) storeMethod.invoke( recordManager, 15L, bytes, pageIos );
395
396 assertEquals( 35, position );
397 pos = 27;
398
399 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
400 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
401 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
402 assertEquals( 0x10, pageIos[0].getData().get( pos++ ) );
403
404
405 assertEquals( 1, pageIos[0].getData().get( pos++ ) );
406
407
408 pos = 8;
409
410 for ( int i = 0; i < 15; i++ )
411 {
412 assertEquals( ( byte ) ( i + 2 ), pageIos[1].getData().get( pos++ ) );
413 }
414
415
416 bytes = new byte[80];
417
418 for ( int i = 0; i < 80; i++ )
419 {
420 bytes[i] = ( byte ) ( i + 1 );
421 }
422
423 position = ( Long ) storeMethod.invoke( recordManager, 2L, bytes, pageIos );
424
425 assertEquals( 86, position );
426 pos = 14;
427
428 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
429 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
430 assertEquals( 0x00, pageIos[0].getData().get( pos++ ) );
431 assertEquals( 0x50, pageIos[0].getData().get( pos++ ) );
432
433
434 for ( int i = 0; i < 14; i++ )
435 {
436 assertEquals( ( byte ) ( i + 1 ), pageIos[0].getData().get( pos++ ) );
437 }
438
439
440 pos = 8;
441 for ( int i = 14; i < 38; i++ )
442 {
443 assertEquals( ( byte ) ( i + 1 ), pageIos[1].getData().get( pos++ ) );
444 }
445
446
447 pos = 8;
448 for ( int i = 38; i < 62; i++ )
449 {
450 assertEquals( ( byte ) ( i + 1 ), pageIos[2].getData().get( pos++ ) );
451 }
452
453
454 pos = 8;
455 for ( int i = 62; i < 80; i++ )
456 {
457 assertEquals( ( byte ) ( i + 1 ), pageIos[3].getData().get( pos++ ) );
458 }
459 }
460 }