1 package org.apache.commons.jcs3.auxiliary.disk.block;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Random;
6
7 import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import junit.framework.TestCase;
29
30
31
32
33 public class BlockDiskUnitTest
34 extends TestCase
35 {
36
37 private File rafDir;
38 private BlockDisk disk;
39
40
41
42
43
44 @Override
45 protected void setUp() throws Exception
46 {
47 super.setUp();
48 final String rootDirName = "target/test-sandbox/block";
49 this.rafDir = new File( rootDirName );
50 this.rafDir.mkdirs();
51 }
52
53 private void setUpBlockDisk(final String fileName) throws IOException
54 {
55 final File file = new File(rafDir, fileName + ".data");
56 file.delete();
57 this.disk = new BlockDisk(file, new StandardSerializer());
58 }
59
60 private void setUpBlockDisk(final String fileName, final int blockSize) throws IOException
61 {
62 final File file = new File(rafDir, fileName + ".data");
63 file.delete();
64 this.disk = new BlockDisk(file, blockSize, new StandardSerializer());
65 }
66
67
68
69
70 @Override
71 protected void tearDown() throws Exception
72 {
73 disk.close();
74 super.tearDown();
75 }
76
77
78
79
80
81
82 public void testWrite_NullBlockElement()
83 throws Exception
84 {
85
86 setUpBlockDisk("testWrite_NullBlockElement");
87
88
89 final int[] blocks = disk.write( null );
90
91
92 assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() );
93 assertEquals( "Wrong number of blocks returned.", 1, blocks.length );
94 assertEquals( "Wrong block returned.", 0, blocks[0] );
95 }
96
97
98
99
100
101
102 public void testWrite_SingleBlockElement()
103 throws Exception
104 {
105
106 setUpBlockDisk("testWrite_SingleBlockElement");
107
108
109 final int bytes = 1 * 1024;
110 final int[] blocks = disk.write( new byte[bytes] );
111
112
113 assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() );
114 assertEquals( "Wrong number of blocks returned.", 1, blocks.length );
115 assertEquals( "Wrong block returned.", 0, blocks[0] );
116 }
117
118
119
120
121
122
123 public void testWriteAndRead_SingleBlockElement()
124 throws Exception
125 {
126
127 setUpBlockDisk("testWriteAndRead_SingleBlockElement");
128
129
130 final int bytes = 1 * 1024;
131 final int[] blocks = disk.write( new byte[bytes] );
132
133 final byte[] result = (byte[]) disk.read( blocks );
134
135
136 assertEquals( "Wrong item retured.", new byte[bytes].length, result.length );
137 }
138
139
140
141
142
143
144 public void testWrite_TwoSingleBlockElements()
145 throws Exception
146 {
147
148 setUpBlockDisk("testWrite_TwoSingleBlockElements");
149
150
151 final int bytes = 1 * 1024;
152 final int[] blocks1 = disk.write( new byte[bytes] );
153 final int[] blocks2 = disk.write( new byte[bytes] );
154
155
156 assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() );
157 assertEquals( "Wrong number of blocks returned.", 1, blocks1.length );
158 assertEquals( "Wrong block returned.", 0, blocks1[0] );
159 assertEquals( "Wrong number of blocks returned.", 1, blocks2.length );
160 assertEquals( "Wrong block returned.", 1, blocks2[0] );
161 }
162
163
164
165
166
167
168 public void testCalculateBlocksNeededDouble()
169 throws Exception
170 {
171
172 setUpBlockDisk("testCalculateBlocksNeededDouble");
173
174
175 final int result = disk.calculateTheNumberOfBlocksNeeded( new byte[disk.getBlockSizeBytes() * 2
176 - ( 2 * BlockDisk.HEADER_SIZE_BYTES )] );
177
178
179 assertEquals( "Wrong number of blocks", 2, result );
180 }
181
182
183
184
185
186
187 public void testWrite_DoubleBlockElement()
188 throws Exception
189 {
190
191 setUpBlockDisk("testWriteDoubleBlockElement");
192
193
194
195 final int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), 2 );
196 final int[] blocks = disk.write( new byte[bytes] );
197
198
199 assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() );
200 assertEquals( "Wrong number of blocks returned.", 2, blocks.length );
201 assertEquals( "Wrong block returned.", 0, blocks[0] );
202 }
203
204
205
206
207
208
209 public void testWrite_128BlockElement()
210 throws Exception
211 {
212
213 final int numBlocks = 128;
214
215 setUpBlockDisk("testWrite_128BlockElement");
216
217
218
219 final int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocks );
220 final int[] blocks = disk.write( new byte[bytes] );
221
222
223 assertEquals( "Wrong number of blocks recorded.", numBlocks, disk.getNumberOfBlocks() );
224 assertEquals( "Wrong number of blocks returned.", numBlocks, blocks.length );
225 assertEquals( "Wrong block returned.", 0, blocks[0] );
226 }
227
228
229
230
231
232
233 public void testWriteAndReadMultipleMultiBlockElement()
234 throws Exception
235 {
236
237 setUpBlockDisk("testWriteAndReadSingleBlockElement");
238
239
240 final int numBlocksPerElement = 4;
241 final int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement );
242
243 final int numElements = 100;
244 for ( int i = 0; i < numElements; i++ )
245 {
246 final int[] blocks = disk.write( new byte[bytes] );
247 final byte[] result = (byte[]) disk.read( blocks );
248
249
250 assertEquals( "Wrong item retured.", new byte[bytes].length, result.length );
251 assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length );
252 }
253 }
254
255
256
257
258
259
260 public void testWriteAndReadMultipleMultiBlockElement_setSize()
261 throws Exception
262 {
263
264 setUpBlockDisk("testWriteAndReadSingleBlockElement", 1024);
265
266
267 final int numBlocksPerElement = 4;
268 final int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement );
269
270 final int numElements = 100;
271 final Random r = new Random(System.currentTimeMillis());
272 final byte[] src = new byte[bytes];
273 for ( int i = 0; i < numElements; i++ )
274 {
275 r.nextBytes(src);
276 final int[] blocks = disk.write( src );
277 final byte[] result = (byte[]) disk.read( blocks );
278
279
280 assertEquals( "Wrong item length retured.", src.length, result.length );
281 assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length );
282
283
284 for (int j = 0 ; j < src.length ; j++) {
285 assertEquals( "Mismatch at offset " + j + " in attempt # " + (i + 1), src[j], result[j] );
286 }
287 }
288 assertEquals( "Wrong number of elements. "+disk, numBlocksPerElement * numElements, disk.getNumberOfBlocks() );
289 }
290
291
292
293
294
295
296
297
298 private int getBytesForBlocksOfByteArrays( final int blockSize, final int numBlocks )
299 {
300
301 return blockSize * numBlocks - ( numBlocks * BlockDisk.HEADER_SIZE_BYTES ) - ( numBlocks * 14 );
302 }
303
304
305
306
307
308
309 public void testWriteAndRead_BigString()
310 throws Exception
311 {
312
313 setUpBlockDisk("testWriteAndRead_BigString", 4096);
314
315 String string = "This is my big string ABCDEFGH";
316 final StringBuilder sb = new StringBuilder();
317 sb.append( string );
318 for ( int i = 0; i < 8; i++ )
319 {
320 sb.append( " " + i + sb.toString() );
321 }
322 string = sb.toString();
323
324
325 final int[] blocks = disk.write( string );
326 final String result = (String) disk.read( blocks );
327
328
329
330
331
332 assertEquals( "Wrong item retured.", string, result );
333 }
334
335
336
337
338
339
340 public void testWriteAndRead_BigString2()
341 throws Exception
342 {
343
344 setUpBlockDisk("testWriteAndRead_BigString", 47);
345
346 String string = "abcdefghijklmnopqrstuvwxyz1234567890";
347 string += string;
348 string += string;
349
350
351 final int[] blocks = disk.write( string );
352 final String result = (String) disk.read( blocks );
353
354
355 assertEquals( "Wrong item retured.", string, result );
356 }
357
358 public void testJCS156() throws Exception
359 {
360
361 setUpBlockDisk("testJCS156", 4096);
362 final long offset = disk.calculateByteOffsetForBlockAsLong(Integer.MAX_VALUE);
363 assertTrue("Must not wrap round", offset > 0);
364 assertEquals(Integer.MAX_VALUE*4096L,offset);
365 }
366 }