public void testJCS156() throws Exception { // SETUP String fileName = "testJCS156"; File file = new File( rafDir, fileName + ".data" ); file.delete(); int blockSizeBytes = 4096; BlockDisk disk = new BlockDisk( file, blockSizeBytes, new StandardSerializer() ); long offset = disk.calculateByteOffsetForBlockAsLong(Integer.MAX_VALUE); assertTrue("Must not wrap round", offset > 0); assertEquals(Integer.MAX_VALUE*4096L,offset); file.delete(); } }
BlockDisk blockDisk = new BlockDisk(file, 200, elementSerializer);
/** * Verify that it says we need two blocks if the total size will fit. * <p> * @throws Exception */ public void testCalculateBlocksNeededDouble() throws Exception { // SETUP String fileName = "testCalculateBlocksNeededDouble"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int result = disk.calculateTheNumberOfBlocksNeeded( new byte[disk.getBlockSizeBytes() * 2 - ( 2 * BlockDisk.HEADER_SIZE_BYTES )] ); // Verify assertEquals( "Wrong number of blocks", 2, result ); }
/** * Test writing and reading an element within a single block size. * <p> * @throws Exception */ public void testWriteAndRead_SingleBlockElement() throws Exception { // SETUP String fileName = "testWriteAndRead_SingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int bytes = 1 * 1024; int[] blocks = disk.write( new byte[bytes] ); byte[] result = (byte[]) disk.read( blocks ); // VERIFY assertEquals( "Wrong item retured.", new byte[bytes].length, result.length ); }
/** * Verify that it says we need two blocks if the total size will fit. * <p> * @throws Exception */ public void testCalculateBlocksNeededDouble() throws Exception { // SETUP String fileName = "testCalculateBlocksNeededDouble"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int result = disk.calculateTheNumberOfBlocksNeeded( new byte[disk.getBlockSizeBytes() * 2 - ( 2 * BlockDisk.HEADER_SIZE_BYTES )] ); // Verify assertEquals( "Wrong number of blocks", 2, result ); }
/** * Test writing and reading an element within a single block size. * <p> * @throws Exception */ public void testWriteAndRead_SingleBlockElement() throws Exception { // SETUP String fileName = "testWriteAndRead_SingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int bytes = 1 * 1024; int[] blocks = disk.write( new byte[bytes] ); byte[] result = (byte[]) disk.read( blocks ); // VERIFY assertEquals( "Wrong item retured.", new byte[bytes].length, result.length ); }
/** * Verify that the block disk can handle a big string. * <p> * @throws Exception */ public void testWriteAndRead_BigString2() throws Exception { // SETUP String fileName = "testWriteAndRead_BigString"; File file = new File( rafDir, fileName + ".data" ); file.delete(); int blockSizeBytes = 47;//4096;//1024; BlockDisk disk = new BlockDisk( file, blockSizeBytes, new StandardSerializer() ); String string = "abcdefghijklmnopqrstuvwxyz1234567890"; string += string; string += string; // DO WORK int[] blocks = disk.write( string ); String result = (String) disk.read( blocks ); // VERIFY assertEquals( "Wrong item retured.", string, result ); }
/** * Verify that the block disk can handle a big string. * <p> * @throws Exception */ public void testWriteAndRead_BigString2() throws Exception { // SETUP String fileName = "testWriteAndRead_BigString"; File file = new File( rafDir, fileName + ".data" ); file.delete(); int blockSizeBytes = 47;//4096;//1024; BlockDisk disk = new BlockDisk( file, blockSizeBytes, new StandardSerializer() ); String string = "abcdefghijklmnopqrstuvwxyz1234567890"; string += string; string += string; // DO WORK int[] blocks = disk.write( string ); String result = (String) disk.read( blocks ); // VERIFY assertEquals( "Wrong item retured.", string, result ); } }
/** * Test writing a null object within a single block size. * <p> * @throws Exception */ public void testWrite_NullBlockElement() throws Exception { // SETUP String fileName = "testWrite_NullBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int[] blocks = disk.write( null ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 1, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** * Test writing an element within a single block size. * <p> * @throws Exception */ public void testWrite_SingleBlockElement() throws Exception { // SETUP String fileName = "testWrite_SingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int bytes = 1 * 1024; int[] blocks = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 1, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** * Test writing an element within a single block size. * <p> * @throws Exception */ public void testWrite_SingleBlockElement() throws Exception { // SETUP String fileName = "testWrite_SingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int bytes = 1 * 1024; int[] blocks = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 1, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** * Test writing a null object within a single block size. * <p> * @throws Exception */ public void testWrite_NullBlockElement() throws Exception { // SETUP String fileName = "testWrite_NullBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int[] blocks = disk.write( null ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 1, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 1, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** * Test writing and reading elements that do not fit within a single block. * <p> * @throws Exception */ public void testWriteAndReadMultipleMultiBlockElement() throws Exception { // SETUP String fileName = "testWriteAndReadSingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int numBlocksPerElement = 4; int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement ); int numElements = 100; for ( int i = 0; i < numElements; i++ ) { int[] blocks = disk.write( new byte[bytes] ); byte[] result = (byte[]) disk.read( blocks ); // VERIFY assertEquals( "Wrong item retured.", new byte[bytes].length, result.length ); assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length ); } }
/** * Test writing and reading elements that do not fit within a single block. * <p> * @throws Exception */ public void testWriteAndReadMultipleMultiBlockElement() throws Exception { // SETUP String fileName = "testWriteAndReadSingleBlockElement"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int numBlocksPerElement = 4; int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocksPerElement ); int numElements = 100; for ( int i = 0; i < numElements; i++ ) { int[] blocks = disk.write( new byte[bytes] ); byte[] result = (byte[]) disk.read( blocks ); // VERIFY assertEquals( "Wrong item retured.", new byte[bytes].length, result.length ); assertEquals( "Wrong number of blocks returned.", numBlocksPerElement, blocks.length ); } }
/** * Test writing an element that takes two blocks. * <p> * @throws Exception */ public void testWrite_DoubleBlockElement() throws Exception { // SETUP String fileName = "testWriteDoubleBlockElement"; File file = new File( rafDir, fileName + ".data" ); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK // byte arrays encur 27 bytes of serialization overhead. int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), 2 ); int[] blocks = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 2, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** * Test writing two elements that each fit within a single block size. * <p> * @throws Exception */ public void testWrite_TwoSingleBlockElements() throws Exception { // SETUP String fileName = "testWrite_TwoSingleBlockElements"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int bytes = 1 * 1024; int[] blocks1 = disk.write( new byte[bytes] ); int[] blocks2 = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 1, blocks1.length ); assertEquals( "Wrong block returned.", 0, blocks1[0] ); assertEquals( "Wrong number of blocks returned.", 1, blocks2.length ); assertEquals( "Wrong block returned.", 1, blocks2[0] ); }
/** * Test writing an element that takes 128 blocks. There was a byte in a for loop that limited the number to 127. I fixed this. * <p> * @throws Exception */ public void testWrite_128BlockElement() throws Exception { // SETUP int numBlocks = 128; String fileName = "testWrite_128BlockElement"; File file = new File( rafDir, fileName + ".data" ); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK // byte arrays encur 27 bytes of serialization overhead. int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocks ); int[] blocks = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", numBlocks, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", numBlocks, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** * Test writing an element that takes 128 blocks. There was a byte in a for loop that limited the number to 127. I fixed this. * <p> * @throws Exception */ public void testWrite_128BlockElement() throws Exception { // SETUP int numBlocks = 128; String fileName = "testWrite_128BlockElement"; File file = new File( rafDir, fileName + ".data" ); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK // byte arrays encur 27 bytes of serialization overhead. int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), numBlocks ); int[] blocks = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", numBlocks, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", numBlocks, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }
/** * Test writing two elements that each fit within a single block size. * <p> * @throws Exception */ public void testWrite_TwoSingleBlockElements() throws Exception { // SETUP String fileName = "testWrite_TwoSingleBlockElements"; File file = new File( rafDir, fileName + ".data" ); file.delete(); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK int bytes = 1 * 1024; int[] blocks1 = disk.write( new byte[bytes] ); int[] blocks2 = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 1, blocks1.length ); assertEquals( "Wrong block returned.", 0, blocks1[0] ); assertEquals( "Wrong number of blocks returned.", 1, blocks2.length ); assertEquals( "Wrong block returned.", 1, blocks2[0] ); }
/** * Test writing an element that takes two blocks. * <p> * @throws Exception */ public void testWrite_DoubleBlockElement() throws Exception { // SETUP String fileName = "testWriteDoubleBlockElement"; File file = new File( rafDir, fileName + ".data" ); BlockDisk disk = new BlockDisk( file, new StandardSerializer() ); // DO WORK // byte arrays encur 27 bytes of serialization overhead. int bytes = getBytesForBlocksOfByteArrays( disk.getBlockSizeBytes(), 2 ); int[] blocks = disk.write( new byte[bytes] ); // VERIFY assertEquals( "Wrong number of blocks recorded.", 2, disk.getNumberOfBlocks() ); assertEquals( "Wrong number of blocks returned.", 2, blocks.length ); assertEquals( "Wrong block returned.", 0, blocks[0] ); }