### Question:
ZooKeeperStateHandleStore { public void replace(String pathInZooKeeper, int expectedVersion, T state) throws Exception { checkNotNull(pathInZooKeeper, "Path in ZooKeeper"); checkNotNull(state, "State"); final String path = normalizePath(pathInZooKeeper); RetrievableStateHandle<T> oldStateHandle = get(path, false); RetrievableStateHandle<T> newStateHandle = storage.store(state); boolean success = false; try { byte[] serializedStateHandle = InstantiationUtil.serializeObject(newStateHandle); client.setData() .withVersion(expectedVersion) .forPath(path, serializedStateHandle); success = true; } catch (KeeperException.NoNodeException e) { throw new ConcurrentModificationException("ZooKeeper unexpectedly modified", e); } finally { if (success) { oldStateHandle.discardState(); } else { newStateHandle.discardState(); } } } ZooKeeperStateHandleStore(
		CuratorFramework client,
		RetrievableStateStorageHelper<T> storage); RetrievableStateHandle<T> addAndLock(
			String pathInZooKeeper,
			T state); void replace(String pathInZooKeeper, int expectedVersion, T state); int exists(String pathInZooKeeper); RetrievableStateHandle<T> getAndLock(String pathInZooKeeper); Collection<String> getAllPaths(); @SuppressWarnings("unchecked") List<Tuple2<RetrievableStateHandle<T>, String>> getAllAndLock(); @Nullable boolean releaseAndTryRemove(String pathInZooKeeper); void releaseAndTryRemoveAll(); void release(String pathInZooKeeper); void releaseAll(); void deleteChildren();  }### Answer:
@Test public void testReplace() throws Exception { LongStateStorage stateHandleProvider = new LongStateStorage(); ZooKeeperStateHandleStore<Long> store = new ZooKeeperStateHandleStore<>( ZOOKEEPER.getClient(), stateHandleProvider); final String pathInZooKeeper = "/testReplace"; final Long initialState = 30968470898L; final Long replaceState = 88383776661L; store.addAndLock(pathInZooKeeper, initialState); store.replace(pathInZooKeeper, 0, replaceState); assertEquals(2, stateHandleProvider.getStateHandles().size()); assertEquals(initialState, stateHandleProvider.getStateHandles().get(0).retrieveState()); assertEquals(replaceState, stateHandleProvider.getStateHandles().get(1).retrieveState()); Stat stat = ZOOKEEPER.getClient().checkExists().forPath(pathInZooKeeper); assertNotNull(stat); assertEquals(0, stat.getEphemeralOwner()); @SuppressWarnings("unchecked") Long actual = ((RetrievableStateHandle<Long>) InstantiationUtil.deserializeObject( ZOOKEEPER.getClient().getData().forPath(pathInZooKeeper), ClassLoader.getSystemClassLoader())).retrieveState(); assertEquals(replaceState, actual); }
@Test(expected = Exception.class) public void testReplaceNonExistingPath() throws Exception { RetrievableStateStorageHelper<Long> stateStorage = new LongStateStorage(); ZooKeeperStateHandleStore<Long> store = new ZooKeeperStateHandleStore<>( ZOOKEEPER.getClient(), stateStorage); store.replace("/testReplaceNonExistingPath", 0, 1L); }