Create explicit method to create new repository locations

This commit is contained in:
René Pfeuffer
2019-06-05 14:27:35 +02:00
parent c7875e7f78
commit b274952fa9
10 changed files with 57 additions and 14 deletions

View File

@@ -14,8 +14,26 @@ public abstract class RepositoryLocationResolver {
}
public interface RepositoryLocationResolverInstance<T> {
/**
* Get the existing location for the repository.
* @param repositoryId The id of the repository.
* @throws IllegalStateException when there is no known location for the given repository.
*/
T getLocation(String repositoryId);
/**
* Create a new location for the new repository.
* @param repositoryId The id of the new repository.
* @throws IllegalStateException when there already is a location for the given repository registered.
*/
T createLocation(String repositoryId);
/**
* Set the location of a new repository.
* @param repositoryId The id of the new repository.
* @throws IllegalStateException when there already is a location for the given repository registered.
*/
void setLocation(String repositoryId, T location);
}
}

View File

@@ -71,6 +71,15 @@ public class PathBasedRepositoryLocationResolver extends BasicRepositoryLocation
public T getLocation(String repositoryId) {
if (pathById.containsKey(repositoryId)) {
return (T) contextProvider.resolve(pathById.get(repositoryId));
} else {
throw new IllegalStateException("location for repository " + repositoryId + " does not exist");
}
}
@Override
public T createLocation(String repositoryId) {
if (pathById.containsKey(repositoryId)) {
throw new IllegalStateException("location for repository " + repositoryId + " already exists");
} else {
return (T) create(repositoryId);
}
@@ -78,7 +87,11 @@ public class PathBasedRepositoryLocationResolver extends BasicRepositoryLocation
@Override
public void setLocation(String repositoryId, T location) {
PathBasedRepositoryLocationResolver.this.setLocation(repositoryId, ((Path) location).toAbsolutePath());
if (pathById.containsKey(repositoryId)) {
throw new IllegalStateException("location for repository " + repositoryId + " already exists");
} else {
PathBasedRepositoryLocationResolver.this.setLocation(repositoryId, ((Path) location).toAbsolutePath());
}
}
};
}

View File

@@ -57,7 +57,7 @@ class PathBasedRepositoryLocationResolverTest {
@Test
void shouldCreateInitialDirectory() {
Path path = resolver.forClass(Path.class).getLocation("newId");
Path path = resolver.forClass(Path.class).createLocation("newId");
assertThat(path).isEqualTo(basePath.resolve("newId"));
assertThat(path).isDirectory();
@@ -65,7 +65,7 @@ class PathBasedRepositoryLocationResolverTest {
@Test
void shouldPersistInitialDirectory() {
resolver.forClass(Path.class).getLocation("newId");
resolver.forClass(Path.class).createLocation("newId");
String content = getXmlFileContent();
@@ -78,7 +78,7 @@ class PathBasedRepositoryLocationResolverTest {
long now = CREATION_TIME + 100;
when(clock.millis()).thenReturn(now);
resolver.forClass(Path.class).getLocation("newId");
resolver.forClass(Path.class).createLocation("newId");
assertThat(resolver.getCreationTime()).isEqualTo(CREATION_TIME);
@@ -91,7 +91,7 @@ class PathBasedRepositoryLocationResolverTest {
long now = CREATION_TIME + 100;
when(clock.millis()).thenReturn(now);
resolver.forClass(Path.class).getLocation("newId");
resolver.forClass(Path.class).createLocation("newId");
assertThat(resolver.getCreationTime()).isEqualTo(CREATION_TIME);
assertThat(resolver.getLastModified()).isEqualTo(now);
@@ -108,8 +108,8 @@ class PathBasedRepositoryLocationResolverTest {
@BeforeEach
void createExistingDatabase() {
resolver.forClass(Path.class).getLocation("existingId_1");
resolver.forClass(Path.class).getLocation("existingId_2");
resolver.forClass(Path.class).createLocation("existingId_1");
resolver.forClass(Path.class).createLocation("existingId_2");
resolverWithExistingData = createResolver();
}

View File

@@ -64,6 +64,11 @@ class XmlRepositoryDAOTest {
return locationResolver.create(repositoryId);
}
@Override
public Path createLocation(String repositoryId) {
return locationResolver.create(repositoryId);
}
@Override
public void setLocation(String repositoryId, Path location) {
}

View File

@@ -21,6 +21,11 @@ public class TempDirRepositoryLocationResolver extends BasicRepositoryLocationRe
return (T) tempDirectory.toPath();
}
@Override
public T createLocation(String repositoryId) {
return (T) tempDirectory.toPath();
}
@Override
public void setLocation(String repositoryId, T location) {
throw new UnsupportedOperationException("not implemented for tests");

View File

@@ -34,6 +34,7 @@ package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import org.junit.Test;
import org.mockito.stubbing.Answer;
import sonia.scm.AbstractTestBase;
import sonia.scm.store.ConfigurationStoreFactory;
import sonia.scm.store.InMemoryConfigurationStoreFactory;
@@ -82,11 +83,12 @@ public abstract class SimpleRepositoryHandlerTestBase extends AbstractTestBase {
RepositoryLocationResolver.RepositoryLocationResolverInstance instanceMock = mock(RepositoryLocationResolver.RepositoryLocationResolverInstance.class);
when(locationResolver.create(any())).thenReturn(instanceMock);
when(locationResolver.supportsLocationType(any())).thenReturn(true);
when(instanceMock.getLocation(anyString())).then(ic -> {
Answer<Object> pathAnswer = ic -> {
String id = ic.getArgument(0);
return baseDirectory.toPath().resolve(id);
});
};
when(instanceMock.getLocation(anyString())).then(pathAnswer);
when(instanceMock.createLocation(anyString())).then(pathAnswer);
handler = createRepositoryHandler(storeFactory, locationResolver, baseDirectory);
}

View File

@@ -24,7 +24,7 @@ class CopyMigrationStrategy extends BaseMigrationStrategy {
@Override
public Path migrate(String id, String name, String type) {
Path repositoryBasePath = locationResolver.forClass(Path.class).getLocation(id);
Path repositoryBasePath = locationResolver.forClass(Path.class).createLocation(id);
Path targetDataPath = repositoryBasePath
.resolve(RepositoryDirectoryHandler.REPOSITORIES_NATIVE_DIRECTORY);
Path sourceDataPath = getSourceDataPath(name, type);

View File

@@ -28,7 +28,7 @@ class MoveMigrationStrategy extends BaseMigrationStrategy {
@Override
public Path migrate(String id, String name, String type) {
Path repositoryBasePath = locationResolver.forClass(Path.class).getLocation(id);
Path repositoryBasePath = locationResolver.forClass(Path.class).createLocation(id);
Path targetDataPath = repositoryBasePath
.resolve(RepositoryDirectoryHandler.REPOSITORIES_NATIVE_DIRECTORY);
Path sourceDataPath = getSourceDataPath(name, type);

View File

@@ -43,7 +43,7 @@ class CopyMigrationStrategyTest {
void mockLocationResolver(@TempDirectory.TempDir Path tempDir) {
RepositoryLocationResolver.RepositoryLocationResolverInstance instanceMock = mock(RepositoryLocationResolver.RepositoryLocationResolverInstance.class);
when(locationResolver.forClass(Path.class)).thenReturn(instanceMock);
when(instanceMock.getLocation(anyString())).thenAnswer(invocation -> tempDir.resolve((String) invocation.getArgument(0)));
when(instanceMock.createLocation(anyString())).thenAnswer(invocation -> tempDir.resolve((String) invocation.getArgument(0)));
}
@Test

View File

@@ -40,7 +40,7 @@ class MoveMigrationStrategyTest {
void mockLocationResolver(@TempDirectory.TempDir Path tempDir) {
RepositoryLocationResolver.RepositoryLocationResolverInstance instanceMock = mock(RepositoryLocationResolver.RepositoryLocationResolverInstance.class);
when(locationResolver.forClass(Path.class)).thenReturn(instanceMock);
when(instanceMock.getLocation(anyString())).thenAnswer(invocation -> tempDir.resolve((String) invocation.getArgument(0)));
when(instanceMock.createLocation(anyString())).thenAnswer(invocation -> tempDir.resolve((String) invocation.getArgument(0)));
}
@Test