mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-15 09:46:16 +01:00
Add delete method for configuration store (#1814)
Add method to delete configuration stores completely.
This commit is contained in:
@@ -26,8 +26,10 @@ package sonia.scm.store;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
/**
|
||||
@@ -64,7 +66,6 @@ public class JAXBConfigurationStore<T> extends AbstractStore<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected T readObject() {
|
||||
LOG.debug("load {} from store {}", type, configFile);
|
||||
|
||||
@@ -82,4 +83,14 @@ public class JAXBConfigurationStore<T> extends AbstractStore<T> {
|
||||
configFile.toPath()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deleteObject() {
|
||||
LOG.debug("deletes {}", configFile.getPath());
|
||||
try {
|
||||
IOUtil.delete(configFile);
|
||||
} catch (IOException e) {
|
||||
throw new StoreException("Failed to delete store object " + configFile.getPath(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.junit.Test;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryReadOnlyChecker;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
@@ -44,15 +45,13 @@ public class JAXBConfigurationStoreTest extends StoreTestBase {
|
||||
private final RepositoryReadOnlyChecker readOnlyChecker = mock(RepositoryReadOnlyChecker.class);
|
||||
|
||||
@Override
|
||||
protected ConfigurationStoreFactory createStoreFactory()
|
||||
{
|
||||
protected JAXBConfigurationStoreFactory createStoreFactory() {
|
||||
return new JAXBConfigurationStoreFactory(contextProvider, repositoryLocationResolver, readOnlyChecker);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldStoreAndLoadInRepository()
|
||||
{
|
||||
public void shouldStoreAndLoadInRepository() {
|
||||
Repository repository = new Repository("id", "git", "ns", "n");
|
||||
ConfigurationStore<StoreObject> store = createStoreFactory()
|
||||
.withType(StoreObject.class)
|
||||
@@ -69,8 +68,7 @@ public class JAXBConfigurationStoreTest extends StoreTestBase {
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldNotWriteArchivedRepository()
|
||||
{
|
||||
public void shouldNotWriteArchivedRepository() {
|
||||
Repository repository = new Repository("id", "git", "ns", "n");
|
||||
when(readOnlyChecker.isReadOnly("id")).thenReturn(true);
|
||||
ConfigurationStore<StoreObject> store = createStoreFactory()
|
||||
@@ -82,4 +80,38 @@ public class JAXBConfigurationStoreTest extends StoreTestBase {
|
||||
StoreObject storeObject = new StoreObject("value");
|
||||
assertThrows(RuntimeException.class, () -> store.set(storeObject));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteConfigStore() {
|
||||
Repository repository = new Repository("id", "git", "ns", "n");
|
||||
ConfigurationStore<StoreObject> store = createStoreFactory()
|
||||
.withType(StoreObject.class)
|
||||
.withName("test")
|
||||
.forRepository(repository)
|
||||
.build();
|
||||
|
||||
store.set(new StoreObject("value"));
|
||||
|
||||
store.delete();
|
||||
StoreObject storeObject = store.get();
|
||||
|
||||
assertThat(storeObject).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotDeleteStoreForArchivedRepository() {
|
||||
Repository repository = new Repository("id", "git", "ns", "n");
|
||||
when(readOnlyChecker.isReadOnly("id")).thenReturn(false);
|
||||
ConfigurationStore<StoreObject> store = createStoreFactory()
|
||||
.withType(StoreObject.class)
|
||||
.withName("test")
|
||||
.forRepository(repository)
|
||||
.build();
|
||||
|
||||
store.set(new StoreObject());
|
||||
when(readOnlyChecker.isReadOnly("id")).thenReturn(true);
|
||||
|
||||
assertThrows(StoreReadOnlyException.class, store::delete);
|
||||
assertThat(store.getOptional()).isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user