Source:
public void testStoreDescriptor() throws IOException { String storeId = "demoId"; UUID incarnationId = UUID.randomUUID(); StoreDescriptor storeDescriptor = new StoreDescriptor(storeId, incarnationId); byte[] toBytes = storeDescriptor.toBytes(); storeDescriptor = StoreDescriptor.fromBytes(new DataInputStream(new ByteBufferInputStream(ByteBuffer.wrap(toBytes)))); Assert.assertEquals("StoreId mismatch ", storeId, storeDescriptor.storeId); Assert.assertEquals("IncarnationId mismatch ", incarnationId, storeDescriptor.incarnationId); incarnationId = null; storeDescriptor = new StoreDescriptor(storeId, incarnationId); Assert.assertEquals("StoreId mismatch ", storeId, storeDescriptor.storeId); Assert.assertEquals("IncarnationId mismatch ", incarnationId, storeDescriptor.incarnationId); toBytes = storeDescriptor.toBytes(); storeDescriptor = StoreDescriptor.fromBytes(new DataInputStream(new ByteBufferInputStream(ByteBuffer.wrap(toBytes)))); Assert.assertEquals("StoreId mismatch ", storeId, storeDescriptor.storeId); Assert.assertEquals("IncarnationId mismatch ", incarnationId, storeDescriptor.incarnationId); incarnationId = UUID.randomUUID(); int size = StoreDescriptor.VERSION_SIZE + StoreDescriptor.STORE_ID_SIZE + storeId.toString().getBytes().length + StoreDescriptor.INCARNATION_ID_SIZE + incarnationId.toString().getBytes().length; toBytes = new byte[size]; ByteBuffer byteBuffer = ByteBuffer.wrap(toBytes); byteBuffer.putShort((short) 2); byteBuffer.putInt(storeId.toString().getBytes().length); byteBuffer.put(storeId.toString().getBytes()); byteBuffer.putInt(incarnationId.toString().getBytes().length); byteBuffer.put(incarnationId.toString().getBytes()); byteBuffer.flip(); try { StoreDescriptor.fromBytes(new DataInputStream(new ByteBufferInputStream(byteBuffer))); } catch (IllegalArgumentException e) { } }

Target:
public void testStoreDescriptor() throws IOException { File tempDir = StoreTestUtils.createTempDirectory("storeDir"); File storeDescriptorFile = new File(tempDir.getAbsolutePath(), StoreDescriptor.STORE_DESCRIPTOR_FILENAME); StoreDescriptor storeDescriptor = new StoreDescriptor(tempDir.getAbsolutePath()); StoreDescriptor newStoreDescriptor = new StoreDescriptor(tempDir.getAbsolutePath()); assertEquals("IncarnationId mismatch ", storeDescriptor.getIncarnationId(), newStoreDescriptor.getIncarnationId()); assertTrue("Store descriptor file could not be deleted", storeDescriptorFile.delete()); UUID incarnationIdUUID = UUID.randomUUID(); byte[] toBytes = getBytesForStoreDescriptor(StoreDescriptor.VERSION_0, incarnationIdUUID); storeDescriptorFile = new File(tempDir.getAbsolutePath(), StoreDescriptor.STORE_DESCRIPTOR_FILENAME); assertTrue("Store descriptor file could not be created", storeDescriptorFile.createNewFile()); createStoreFile(storeDescriptorFile, toBytes); storeDescriptor = new StoreDescriptor(tempDir.getAbsolutePath()); assertEquals("IncarnationId mismatch ", incarnationIdUUID, storeDescriptor.getIncarnationId()); assertTrue("Store descriptor file could not be deleted", storeDescriptorFile.delete()); toBytes = getBytesForStoreDescriptor((short) 1, incarnationIdUUID); assertTrue("Store descriptor file could not be created", storeDescriptorFile.createNewFile()); createStoreFile(storeDescriptorFile, toBytes); try { new StoreDescriptor(tempDir.getAbsolutePath()); fail("Wrong version should have thrown IllegalArgumentException "); } catch (IllegalArgumentException e) { } assertTrue("Store descriptor file could not be deleted", storeDescriptorFile.delete()); assertTrue("Store descriptor file could not be created", storeDescriptorFile.createNewFile()); toBytes = getBytesForStoreDescriptor(StoreDescriptor.VERSION_0, incarnationIdUUID); CrcOutputStream crcOutputStream = new CrcOutputStream(new FileOutputStream(storeDescriptorFile)); DataOutputStream dataOutputStream = new DataOutputStream(crcOutputStream); dataOutputStream.write(toBytes); dataOutputStream.writeLong(crcOutputStream.getValue() + 1); dataOutputStream.close(); try { new StoreDescriptor(tempDir.getAbsolutePath()); fail("Wrong CRC should have thrown IllegalStateException "); } catch (IllegalStateException e) { } }