public void testWriteChunks() throws Exception {
final int BUFFER_SIZE = 64;
Cache cache = cacheManager.getCache();
Directory dir = DirectoryBuilder.newDirectoryInstance(cache, cache, cache, INDEXNAME).chunkSize(BUFFER_SIZE).create();
IndexOutput io = dir.createOutput("MyNewFile.txt", IOContext.DEFAULT);
io.writeByte((byte) 66);
io.writeByte((byte) 69);
io.close();
assertTrue(Arrays.asList(dir.listAll()).contains("MyNewFile.txt"));
assertNotNull(cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 0, BUFFER_SIZE, -1)));
byte[] buf = new byte[9];
IndexInput ii = dir.openInput("MyNewFile.txt", IOContext.DEFAULT);
ii.readBytes(buf, 0, (int) ii.length());
ii.close();
assertEquals(new String(new byte[] { 66, 69 }), new String(buf).trim());
String testText = "This is some rubbish again that will span more than one chunk - one hopes. Who knows, maybe even three or four chunks.";
io = dir.createOutput("MyNewFile.txt", IOContext.DEFAULT);
((InfinispanIndexOutput)io).seek(0);
io.writeBytes(testText.getBytes(), 0, testText.length());
io.close();
byte[] chunk1 = (byte[]) cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 0, BUFFER_SIZE, -1));
byte[] chunk2 = (byte[]) cache.get(new ChunkCacheKey(INDEXNAME, "MyNewFile.txt", 1, BUFFER_SIZE, -1));
assert null != chunk1;
assert null != chunk2;
assert testText.equals(new String(chunk1) + new String(chunk2).trim());
dir.close();
DirectoryIntegrityCheck.verifyDirectoryStructure(cache, INDEXNAME);
}