H2BackupTask extends TaskSupport { @Override protected Object execute() throws Exception { String dataStoreName = checkNotNull(getConfiguration().getString(H2BackupTaskDescriptor.DATASTORE), "DataStore name not configured"); String location = checkNotNull(getConfiguration().getString(H2BackupTaskDescriptor.LOCATION), "Backup location not configured"); Optional<DataStore<?>> dataStore = dataStoreManager.get(dataStoreName); if (!dataStore.isPresent()) { throw new RuntimeException("Unable to locate datastore with name " + dataStoreName); } location = handleRelative(location); location = interpolate(location); log.info("Starting backup of {} to {}", dataStoreName, location); long start = System.currentTimeMillis(); dataStore.get().backup(location); log.info("Completed backup of {} in {} ms", dataStoreName, System.currentTimeMillis() - start); return null; } @Inject  H2BackupTask(final DataStoreManager dataStoreManager, final ApplicationDirectories applicationDirectories); @Override String getMessage();  }
@Test public void testExecute_dateTime() throws Exception { String folder = "/foo/bar/{datetime}.zip"; Date before = date(); H2BackupTask task = createTask(testName, folder); task.execute(); Date after = date(); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(dataStore).backup(captor.capture()); String backupPath = captor.getValue(); Date serialized = new SimpleDateFormat("yyyy-MM-dd_HHmmss").parse(backupPath.substring(9, backupPath.length() - 4)); assertThat(before.compareTo(serialized), lessThanOrEqualTo(0)); assertThat(serialized.compareTo(after), lessThanOrEqualTo(0)); }
@Test public void testExecute_relativePath() throws Exception { String folder = "foo/bar/backup.zip"; H2BackupTask task = createTask(testName, folder); when(applicationDirectories.getWorkDirectory()).thenReturn(temporaryFolder.getRoot()); task.execute(); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(dataStore).backup(captor.capture()); String backupPath = captor.getValue(); assertThat(backupPath, equalTo(new File(temporaryFolder.getRoot(), "foo/bar/backup.zip").getPath())); }
@Test public void testExecute_missingLocation() throws Exception { H2BackupTask task = createTask(testName, null); thrown.expect(NullPointerException.class); thrown.expectMessage("Backup location not configured"); task.execute(); }
@Test public void testExecute_missingDataStoreName() throws Exception { H2BackupTask task = createTask(null, "/foo/bar.zip"); thrown.expect(NullPointerException.class); thrown.expectMessage("DataStore name not configured"); task.execute(); }
@Test public void testExecute_missingDataStore() throws Exception { when(dataStoreManager.get("config2")).thenReturn(Optional.empty()); H2BackupTask task = createTask("config2", "/foo/bar.zip"); thrown.expect(RuntimeException.class); thrown.expectMessage("Unable to locate datastore with name config2"); task.execute(); }