OnDiskSnapshotsStore implements SnapshotsStore { public void initialize() throws StorageException { checkState(!initialized, "store already initialized"); try { dbi.withHandle(new HandleCallback<Void>() { @Override public Void withHandle(Handle handle) throws Exception { SnapshotsDAO dao = handle.attach(SnapshotsDAO.class); dao.createSnapshotsTableWithIndex(); return null; } }); initialized = true; } catch (CallbackFailedException e) { throw new StorageException("fail create snapshots table", e.getCause()); } catch (Exception e) { throw new StorageException("fail create snapshots table", e); } } OnDiskSnapshotsStore(DBI dbi, String snapshotsDirectory); void initialize(); void teardown(); @Override ExtendedSnapshotWriter newSnapshotWriter(); @SuppressWarnings("ConstantConditions") @Override void storeSnapshot(ExtendedSnapshotWriter snapshotWriter); @Override @Nullable ExtendedSnapshot getLatestSnapshot(); List<SnapshotMetadata> getAllSnapshotsFromLatestToOldest(); void pruneSnapshots(final int numSnapshotsToKeep); void reconcileSnapshots();  }
@Test public void shouldThrowStorageExceptionIfDatabaseCallFailsInCallToInitialize() { DBI brokenDBI = mock(DBI.class); Handle brokenHandle = mock(Handle.class); OnDiskSnapshotsStore brokenSnapshotStore = new OnDiskSnapshotsStore(brokenDBI, snapshotsDirectory.getAbsolutePath()); IllegalArgumentException failure = new IllegalArgumentException("failed!"); when(brokenDBI.open()).thenReturn(brokenHandle); when(brokenDBI.withHandle(any(HandleCallback.class))).thenCallRealMethod(); when(brokenHandle.attach(any(Class.class))).thenThrow(failure); boolean exceptionThrown = false; try { brokenSnapshotStore.initialize(); } catch (StorageException e) { exceptionThrown = true; assertThat(e.getCause(), Matchers.<Throwable>sameInstance(failure)); } assertThat(exceptionThrown, is(true)); }
@Test public void shouldThrowStorageExceptionIfExceptionThrownInCallToInitialize() { DBI brokenDBI = mock(DBI.class); OnDiskSnapshotsStore brokenSnapshotStore = new OnDiskSnapshotsStore(brokenDBI, snapshotsDirectory.getAbsolutePath()); IllegalArgumentException failure = new IllegalArgumentException("failed!"); when(brokenDBI.open()).thenThrow(failure); when(brokenDBI.withHandle(any(HandleCallback.class))).thenCallRealMethod(); boolean exceptionThrown = false; try { brokenSnapshotStore.initialize(); } catch (StorageException e) { exceptionThrown = true; assertThat(e.getCause(), Matchers.<Throwable>sameInstance(failure)); } assertThat(exceptionThrown, is(true)); }
@Test(expected = IllegalStateException.class) public void shouldThrowIllegalStateExceptionIfSnapshotStoreInitializedMultipleTimes() throws StorageException { snapshotsStore.initialize(); }