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