/** Test CassandraOutputStream.write(int); */ private void testWriteWith( int blockSize, int subblockSize, int bufferSize, int totalBytesToWrite, int storedSubBlockesExpectation) throws Exception { StoreMock storeMock = new StoreMock(); out = new CassandraOutputStream( null, storeMock, null, null, blockSize, subblockSize, null, bufferSize); Assert.assertEquals(0, out.getPos()); for (int i = 0; i < totalBytesToWrite; i++) { out.write(i); } Assert.assertEquals(totalBytesToWrite, out.getPos()); out.close(); // Validate the expectations. Assert.assertEquals(storedSubBlockesExpectation, storeMock.storeSubBlockCount); // This is always one. Assert.assertEquals(1, storeMock.storeINodeCount); int totalBlocks = calculateTotalBlocks(totalBytesToWrite, blockSize); // Assert the total blocks per file Assert.assertEquals(totalBlocks, storeMock.inodesStored.get(0).getBlocks().length); // Assert SubBlocks per Block int totalSubBlocksPerBlock = blockSize % subblockSize == 0 ? blockSize / subblockSize : (blockSize / subblockSize) + 1; assertSubBlocksInBlocks( storeMock.inodesStored.get(0).getBlocks(), totalSubBlocksPerBlock, storedSubBlockesExpectation); // Assert and print for debug. for (Block block : storeMock.inodesStored.get(0).getBlocks()) { logger.info(block); } }
/** Test CassandraOutputStream.write(buffer, off, len); */ private void testWriteBufferWith( int blockSize, int subblockSize, int bufferSize, int totalBytesToWrite, int storedSubBlockesExpectation) throws Exception { // Null object here are not needed or irrelevant for this test case. // buffer size different from bytes to write is intentional. StoreMock storeMock = new StoreMock(); out = new CassandraOutputStream( null, storeMock, null, null, blockSize, subblockSize, null, bufferSize); Assert.assertEquals(0, out.getPos()); // Fill up the buffer byte[] buffer = new byte[totalBytesToWrite]; for (int i = 0; i < totalBytesToWrite; i++) { buffer[i] = (byte) i; } // Invoke the method being tested. out.write(buffer, 0, totalBytesToWrite); Assert.assertEquals(totalBytesToWrite, out.getPos()); out.close(); // Validate the expectations. Assert.assertEquals(storedSubBlockesExpectation, storeMock.storeSubBlockCount); // This is always one. Assert.assertEquals(1, storeMock.storeINodeCount); int totalBlocks = calculateTotalBlocks(totalBytesToWrite, blockSize); // Assert the total blocks per file Assert.assertEquals(totalBlocks, storeMock.inodesStored.get(0).getBlocks().length); }