View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.mavibot.btree.managed;
21  
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  
26  import java.io.File;
27  import java.lang.reflect.Method;
28  import java.nio.ByteBuffer;
29  
30  import org.junit.Rule;
31  import org.junit.Test;
32  import org.junit.rules.TemporaryFolder;
33  
34  
35  /**
36   * Test the RecordManager.readXXX() methods using reflection
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class ReadTest
41  {
42      @Rule
43      public TemporaryFolder tempFolder = new TemporaryFolder();
44  
45  
46      /**
47       * Test the readInt method
48       */
49      @Test
50      public void testReadInt() throws Exception
51      {
52          File tempFile = tempFolder.newFile( "mavibot.db" );
53          String tempFileName = tempFile.getAbsolutePath();
54  
55          // Create page size of 32 only
56          RecordManager recordManager = new RecordManager( tempFileName, 32 );
57          Method storeMethod = RecordManager.class.getDeclaredMethod( "store", long.class, int.class, PageIO[].class );
58          Method readIntMethod = RecordManager.class.getDeclaredMethod( "readInt", PageIO[].class, long.class );
59          storeMethod.setAccessible( true );
60          readIntMethod.setAccessible( true );
61  
62          // Allocate some Pages
63          PageIO[] pageIos = new PageIO[2];
64          pageIos[0] = new PageIO();
65          pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
66          pageIos[1] = new PageIO();
67          pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
68  
69          // Set the int at the beginning
70          storeMethod.invoke( recordManager, 0, 0x12345678, pageIos );
71  
72          // Read it back
73          int readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 0 );
74  
75          assertEquals( 0x12345678, readValue );
76  
77          // Set the int at the end of the first page
78          storeMethod.invoke( recordManager, 16, 0x12345678, pageIos );
79  
80          // Read it back
81          readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 16 );
82  
83          assertEquals( 0x12345678, readValue );
84  
85          // Set the int at the end of the first page and overlapping on the second page
86          // 1 byte overlapping
87          storeMethod.invoke( recordManager, 17, 0x12345678, pageIos );
88  
89          // Read it back
90          readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 17 );
91  
92          assertEquals( 0x12345678, readValue );
93  
94          // Set the int at the end of the first page and overlapping on the second page
95          // 2 bytes overlapping
96          storeMethod.invoke( recordManager, 18, 0x12345678, pageIos );
97  
98          // Read it back
99          readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 18 );
100 
101         assertEquals( 0x12345678, readValue );
102 
103         // Set the int at the end of the first page and overlapping on the second page
104         // 3 bytes overlapping
105         storeMethod.invoke( recordManager, 19, 0x12345678, pageIos );
106 
107         // Read it back
108         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 19 );
109 
110         assertEquals( 0x12345678, readValue );
111 
112         // Set the int at the beginning of the second page
113         storeMethod.invoke( recordManager, 20, 0x12345678, pageIos );
114 
115         // Read it back
116         readValue = ( Integer ) readIntMethod.invoke( recordManager, pageIos, 20 );
117     }
118 
119 
120     /**
121      * Test the readLong method
122      */
123     @Test
124     public void testReadLong() throws Exception
125     {
126         File tempFile = tempFolder.newFile( "mavibot.db" );
127         String tempFileName = tempFile.getAbsolutePath();
128 
129         // Create page size of 32 only
130         RecordManager recordManager = new RecordManager( tempFileName, 32 );
131         Method storeMethod = RecordManager.class.getDeclaredMethod( "store", long.class, long.class, PageIO[].class );
132         Method readLongMethod = RecordManager.class.getDeclaredMethod( "readLong", PageIO[].class, long.class );
133         storeMethod.setAccessible( true );
134         readLongMethod.setAccessible( true );
135 
136         // Allocate some Pages
137         PageIO[] pageIos = new PageIO[2];
138         pageIos[0] = new PageIO();
139         pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
140         pageIos[1] = new PageIO();
141         pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
142 
143         // Set the int at the beginning
144         storeMethod.invoke( recordManager, 0, 0x0123456789ABCDEFL, pageIos );
145 
146         // Read it back
147         long readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 0 );
148 
149         assertEquals( 0x0123456789ABCDEFL, readValue );
150 
151         // Set the int at the end of the first page
152         storeMethod.invoke( recordManager, 12, 0x0123456789ABCDEFL, pageIos );
153 
154         // Read it back
155         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 12 );
156 
157         assertEquals( 0x0123456789ABCDEFL, readValue );
158 
159         // Set the int at the end of the first page and overlapping on the second page
160         // 1 byte overlapping
161         storeMethod.invoke( recordManager, 13, 0x0123456789ABCDEFL, pageIos );
162 
163         // Read it back
164         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 13 );
165 
166         assertEquals( 0x0123456789ABCDEFL, readValue );
167 
168         // Set the int at the end of the first page and overlapping on the second page
169         // 2 bytes overlapping
170         storeMethod.invoke( recordManager, 14, 0x0123456789ABCDEFL, pageIos );
171 
172         // Read it back
173         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 14 );
174 
175         assertEquals( 0x0123456789ABCDEFL, readValue );
176 
177         // Set the int at the end of the first page and overlapping on the second page
178         // 3 bytes overlapping
179         storeMethod.invoke( recordManager, 15, 0x0123456789ABCDEFL, pageIos );
180 
181         // Read it back
182         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 15 );
183 
184         assertEquals( 0x0123456789ABCDEFL, readValue );
185 
186         // Set the int at the end of the first page and overlapping on the second page
187         // 4 bytes overlapping
188         storeMethod.invoke( recordManager, 16, 0x0123456789ABCDEFL, pageIos );
189 
190         // Read it back
191         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 16 );
192 
193         assertEquals( 0x0123456789ABCDEFL, readValue );
194 
195         // Set the int at the end of the first page and overlapping on the second page
196         // 5 bytes overlapping
197         storeMethod.invoke( recordManager, 17, 0x0123456789ABCDEFL, pageIos );
198 
199         // Read it back
200         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 17 );
201 
202         assertEquals( 0x0123456789ABCDEFL, readValue );
203 
204         // Set the int at the end of the first page and overlapping on the second page
205         // 6 bytes overlapping
206         storeMethod.invoke( recordManager, 18, 0x0123456789ABCDEFL, pageIos );
207 
208         // Read it back
209         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 18 );
210 
211         assertEquals( 0x0123456789ABCDEFL, readValue );
212 
213         // Set the int at the end of the first page and overlapping on the second page
214         // 7 bytes overlapping
215         storeMethod.invoke( recordManager, 19, 0x0123456789ABCDEFL, pageIos );
216 
217         // Read it back
218         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 19 );
219 
220         assertEquals( 0x0123456789ABCDEFL, readValue );
221 
222         // Set the int at the beginning of the second page
223         storeMethod.invoke( recordManager, 20, 0x0123456789ABCDEFL, pageIos );
224 
225         // Read it back
226         readValue = ( Long ) readLongMethod.invoke( recordManager, pageIos, 20 );
227     }
228 
229 
230     /**
231      * Test the readBytes() method
232      */
233     @Test
234     public void testReadBytes() throws Exception
235     {
236         File tempFile = tempFolder.newFile( "mavibot.db" );
237         String tempFileName = tempFile.getAbsolutePath();
238 
239         // We use smaller pages
240         RecordManager recordManager = new RecordManager( tempFileName, 32 );
241         Method storeMethod = RecordManager.class.getDeclaredMethod( "store", long.class, byte[].class, PageIO[].class );
242         Method readBytesMethod = RecordManager.class.getDeclaredMethod( "readBytes", PageIO[].class, long.class );
243         storeMethod.setAccessible( true );
244         readBytesMethod.setAccessible( true );
245 
246         // Allocate some Pages
247         PageIO[] pageIos = new PageIO[4];
248         pageIos[0] = new PageIO();
249         pageIos[0].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
250         pageIos[1] = new PageIO();
251         pageIos[1].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
252         pageIos[2] = new PageIO();
253         pageIos[2].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
254         pageIos[3] = new PageIO();
255         pageIos[3].setData( ByteBuffer.allocate( recordManager.getPageSize() ) );
256 
257         // We start with 4 bytes
258         byte[] bytes = new byte[]
259             { 0x01, 0x23, 0x45, 0x67 };
260 
261         // Set the bytes at the beginning
262         storeMethod.invoke( recordManager, 0L, bytes, pageIos );
263 
264         // Read the bytes back
265         ByteBuffer readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 0L );
266 
267         // The byte length
268         assertNotNull( readBytes );
269         assertEquals( 4, readBytes.limit() );
270         // The data
271         assertEquals( 0x01, readBytes.get() );
272         assertEquals( 0x23, readBytes.get() );
273         assertEquals( 0x45, readBytes.get() );
274         assertEquals( 0x67, readBytes.get() );
275 
276         // Set the bytes at the end of the first page
277         storeMethod.invoke( recordManager, 12L, bytes, pageIos );
278 
279         // Read the bytes back
280         readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 12L );
281 
282         // The byte length
283         assertNotNull( readBytes );
284         assertEquals( 4, readBytes.limit() );
285         // The data
286         assertEquals( 0x01, readBytes.get() );
287         assertEquals( 0x23, readBytes.get() );
288         assertEquals( 0x45, readBytes.get() );
289         assertEquals( 0x67, readBytes.get() );
290 
291         // Set A full page of bytes in the first page 
292         bytes = new byte[16];
293 
294         for ( int i = 0; i < 16; i++ )
295         {
296             bytes[i] = ( byte ) ( i + 1 );
297         }
298 
299         storeMethod.invoke( recordManager, 0L, bytes, pageIos );
300 
301         // Read the bytes back
302         readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 0L );
303 
304         // The byte length
305         assertNotNull( readBytes );
306         assertEquals( 16, readBytes.limit() );
307 
308         // The data
309         for ( int i = 0; i < 16; i++ )
310         {
311             assertEquals( i + 1, readBytes.get() );
312         }
313 
314         // Write the bytes over 2 pages
315         storeMethod.invoke( recordManager, 15L, bytes, pageIos );
316 
317         // Read the bytes back
318         readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 15L );
319 
320         // The byte length
321         assertNotNull( readBytes );
322         assertEquals( 16, readBytes.limit() );
323         // The data
324         for ( int i = 0; i < 16; i++ )
325         {
326             assertEquals( i + 1, readBytes.get() );
327         }
328 
329         // Write the bytes over 4 pages
330         bytes = new byte[80];
331 
332         for ( int i = 0; i < 80; i++ )
333         {
334             bytes[i] = ( byte ) ( i + 1 );
335         }
336 
337         storeMethod.invoke( recordManager, 2L, bytes, pageIos );
338 
339         // Read the bytes back
340         readBytes = ( ByteBuffer ) readBytesMethod.invoke( recordManager, pageIos, 2L );
341 
342         // The byte length
343         assertNotNull( readBytes );
344         assertEquals( 80, readBytes.limit() );
345         
346         // The data
347         for ( int i = 0; i < 80; i++ )
348         {
349             assertEquals( i + 1, readBytes.get() );
350         }
351     }
352 }