Remove PathBasedRepositoryDAO

The computation of the file path for the repository is done by
RepositoryLocationResolver.
This commit is contained in:
René Pfeuffer
2019-05-10 08:06:36 +02:00
parent c44d38cc61
commit 7f4792ec49
10 changed files with 450 additions and 456 deletions

View File

@@ -1,18 +0,0 @@
package sonia.scm.repository;
import java.nio.file.Path;
/**
* A DAO used for Repositories accessible by a path
*
* @author Mohamed Karray
* @since 2.0.0
*/
public interface PathBasedRepositoryDAO extends RepositoryDAO {
/**
* Get the current path of the repository for the given id.
* This works for existing repositories only, not for repositories that should be created.
*/
Path getPath(String repositoryId) ;
}

View File

@@ -6,7 +6,8 @@ public abstract class RepositoryLocationResolver {
protected abstract <T> RepositoryLocationResolverInstance<T> create(Class<T> type);
public final <T> RepositoryLocationResolverInstance<T> forClass(Class<T> type) {
// TODO make final, but fix mockito mocking
public <T> RepositoryLocationResolverInstance<T> forClass(Class<T> type) {
if (!supportsLocationType(type)) {
throw new IllegalStateException("no support for location of class " + type);
}

View File

@@ -1,14 +1,17 @@
package sonia.scm.repository.xml;
import com.google.common.annotations.VisibleForTesting;
import sonia.scm.SCMContextProvider;
import sonia.scm.repository.BasicRepositoryLocationResolver;
import sonia.scm.repository.InitialRepositoryLocationResolver;
import sonia.scm.repository.PathBasedRepositoryDAO;
import sonia.scm.repository.RepositoryDAO;
import sonia.scm.repository.RepositoryLocationResolver;
import sonia.scm.repository.Repository;
import sonia.scm.store.StoreConstants;
import javax.inject.Inject;
import java.nio.file.Path;
import java.time.Clock;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* A Location Resolver for File based Repository Storage.
@@ -24,30 +27,115 @@ import java.nio.file.Path;
*/
public class PathBasedRepositoryLocationResolver extends BasicRepositoryLocationResolver<Path> {
private static final String STORE_NAME = "repositories";
private final SCMContextProvider contextProvider;
private final InitialRepositoryLocationResolver initialRepositoryLocationResolver;
private final RepositoryDAO repositoryDAO;
private final SCMContextProvider context;
private final PathDatabase pathDatabase;
private final Map<String, Path> pathById;
private final Clock clock;
private Long creationTime;
private Long lastModified;
@Inject
public PathBasedRepositoryLocationResolver(SCMContextProvider contextProvider, RepositoryDAO repositoryDAO, InitialRepositoryLocationResolver initialRepositoryLocationResolver) {
public PathBasedRepositoryLocationResolver(SCMContextProvider contextProvider, InitialRepositoryLocationResolver initialRepositoryLocationResolver, SCMContextProvider context) {
this(contextProvider, initialRepositoryLocationResolver, context, Clock.systemUTC());
}
public PathBasedRepositoryLocationResolver(SCMContextProvider contextProvider, InitialRepositoryLocationResolver initialRepositoryLocationResolver, SCMContextProvider context, Clock clock) {
super(Path.class);
this.contextProvider = contextProvider;
this.initialRepositoryLocationResolver = initialRepositoryLocationResolver;
this.repositoryDAO = repositoryDAO;
this.context = context;
this.pathById = new ConcurrentHashMap<>();
this.clock = clock;
this.creationTime = clock.millis();
pathDatabase = new PathDatabase(resolveStorePath());
read();
}
@Override
protected <T> RepositoryLocationResolverInstance<T> create(Class<T> type) {
return repositoryId -> {
Path path;
if (repositoryDAO instanceof PathBasedRepositoryDAO) {
path = ((PathBasedRepositoryDAO) repositoryDAO).getPath(repositoryId);
if (pathById.containsKey(repositoryId)) {
path = pathById.get(repositoryId);
} else {
path = initialRepositoryLocationResolver.getPath(repositoryId);
path = create(repositoryId);
}
return (T) contextProvider.resolve(path);
};
}
Path create(String repositoryId) {
Path path = initialRepositoryLocationResolver.getPath(repositoryId);
pathById.put(repositoryId, path);
writePathDatabase();
return path;
}
Path remove(String repositoryId) {
Path removedPath = pathById.remove(repositoryId);
writePathDatabase();
return removedPath;
}
private void writePathDatabase() {
lastModified = clock.millis();
pathDatabase.write(creationTime, lastModified, pathById);
}
private void read() {
Path storePath = resolveStorePath();
// Files.exists is slow on java 8
if (storePath.toFile().exists()) {
pathDatabase.read(this::onLoadDates, this::onLoadRepository);
}
}
private void onLoadDates(Long creationTime, Long lastModified) {
this.creationTime = creationTime;
this.lastModified = lastModified;
}
public Long getCreationTime() {
return creationTime;
}
public Long getLastModified() {
return lastModified;
}
private void onLoadRepository(String id, Path repositoryPath) {
// Path metadataPath = resolveMetadataPath(context.resolve(repositoryPath));
//
// Repository repository = metadataStore.read(metadataPath);
//
// byId.put(id, repository);
// byNamespaceAndName.put(repository.getNamespaceAndName(), repository);
pathById.put(id, repositoryPath);
}
@VisibleForTesting
Path resolveMetadataPath(Path repositoryPath) {
return repositoryPath.resolve(StoreConstants.REPOSITORY_METADATA.concat(StoreConstants.FILE_EXTENSION));
}
@VisibleForTesting
Path resolveStorePath() {
return context.getBaseDirectory()
.toPath()
.resolve(StoreConstants.CONFIG_DIRECTORY_NAME)
.resolve(STORE_NAME.concat(StoreConstants.FILE_EXTENSION));
}
}

View File

@@ -41,8 +41,8 @@ import sonia.scm.io.FileSystem;
import sonia.scm.repository.InitialRepositoryLocationResolver;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.PathBasedRepositoryDAO;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryDAO;
import sonia.scm.store.StoreConstants;
import javax.inject.Inject;
@@ -57,82 +57,38 @@ import java.util.concurrent.ConcurrentHashMap;
* @author Sebastian Sdorra
*/
@Singleton
public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
public class XmlRepositoryDAO implements RepositoryDAO {
private static final String STORE_NAME = "repositories";
private final PathDatabase pathDatabase;
private final MetadataStore metadataStore = new MetadataStore();
private final SCMContextProvider context;
private final InitialRepositoryLocationResolver locationResolver;
private final PathBasedRepositoryLocationResolver repositoryLocationResolver;
private final FileSystem fileSystem;
private final Map<String, Path> pathById;
private final Map<String, Repository> byId;
private final Map<NamespaceAndName, Repository> byNamespaceAndName;
private final Clock clock;
private Long creationTime;
private Long lastModified;
@Inject
public XmlRepositoryDAO(SCMContextProvider context, InitialRepositoryLocationResolver locationResolver, FileSystem fileSystem) {
this(context, locationResolver, fileSystem, Clock.systemUTC());
public XmlRepositoryDAO(SCMContextProvider context, PathBasedRepositoryLocationResolver repositoryLocationResolver, InitialRepositoryLocationResolver initialLocationResolver, FileSystem fileSystem) {
this(context, repositoryLocationResolver, initialLocationResolver, fileSystem, Clock.systemUTC());
}
XmlRepositoryDAO(SCMContextProvider context, InitialRepositoryLocationResolver locationResolver, FileSystem fileSystem, Clock clock) {
XmlRepositoryDAO(SCMContextProvider context, PathBasedRepositoryLocationResolver repositoryLocationResolver, InitialRepositoryLocationResolver initialLocationResolver, FileSystem fileSystem, Clock clock) {
this.context = context;
this.locationResolver = locationResolver;
this.repositoryLocationResolver = repositoryLocationResolver;
this.fileSystem = fileSystem;
this.clock = clock;
this.creationTime = clock.millis();
this.pathById = new ConcurrentHashMap<>();
this.byId = new ConcurrentHashMap<>();
this.byNamespaceAndName = new ConcurrentHashMap<>();
pathDatabase = new PathDatabase(resolveStorePath());
read();
}
private void read() {
Path storePath = resolveStorePath();
// Files.exists is slow on java 8
if (storePath.toFile().exists()) {
pathDatabase.read(this::onLoadDates, this::onLoadRepository);
}
}
private void onLoadDates(Long creationTime, Long lastModified) {
this.creationTime = creationTime;
this.lastModified = lastModified;
}
private void onLoadRepository(String id, Path repositoryPath) {
Path metadataPath = resolveMetadataPath(context.resolve(repositoryPath));
Repository repository = metadataStore.read(metadataPath);
byId.put(id, repository);
byNamespaceAndName.put(repository.getNamespaceAndName(), repository);
pathById.put(id, repositoryPath);
}
@VisibleForTesting
Path resolveStorePath() {
return context.getBaseDirectory()
.toPath()
.resolve(StoreConstants.CONFIG_DIRECTORY_NAME)
.resolve(STORE_NAME.concat(StoreConstants.FILE_EXTENSION));
}
@VisibleForTesting
Path resolveMetadataPath(Path repositoryPath) {
Path resolveDataPath(Path repositoryPath) {
return repositoryPath.resolve(StoreConstants.REPOSITORY_METADATA.concat(StoreConstants.FILE_EXTENSION));
}
@@ -141,47 +97,30 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
return "xml";
}
@Override
public Long getCreationTime() {
return creationTime;
}
@Override
public Long getLastModified() {
return lastModified;
}
@Override
public void add(Repository repository) {
Repository clone = repository.clone();
Path repositoryPath = locationResolver.getPath(repository.getId());
Path resolvedPath = context.resolve(repositoryPath);
try {
synchronized (this) {
Path repositoryPath = repositoryLocationResolver.create(repository.getId());
Path resolvedPath = context.resolve(repositoryPath);
fileSystem.create(resolvedPath.toFile());
Path metadataPath = resolveMetadataPath(resolvedPath);
Path metadataPath = resolveDataPath(resolvedPath);
metadataStore.write(metadataPath, repository);
synchronized (this) {
pathById.put(repository.getId(), repositoryPath);
byId.put(repository.getId(), clone);
byNamespaceAndName.put(repository.getNamespaceAndName(), clone);
writePathDatabase();
}
} catch (IOException e) {
} catch (Exception e) {
repositoryLocationResolver.remove(repository.getId());
throw new InternalRepositoryException(repository, "failed to create filesystem", e);
}
}
private void writePathDatabase() {
lastModified = clock.millis();
pathDatabase.write(creationTime, lastModified, pathById);
}
@Override
public boolean contains(Repository repository) {
@@ -224,12 +163,10 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
byNamespaceAndName.remove(prev.getNamespaceAndName());
}
byNamespaceAndName.put(clone.getNamespaceAndName(), clone);
writePathDatabase();
}
Path repositoryPath = context.resolve(getPath(repository.getId()));
Path metadataPath = resolveMetadataPath(repositoryPath);
Path repositoryPath = context.resolve(repositoryLocationResolver.create(Path.class).getLocation(repository.getId()));
Path metadataPath = resolveDataPath(repositoryPath);
metadataStore.write(metadataPath, clone);
}
@@ -241,10 +178,7 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
if (prev != null) {
byNamespaceAndName.remove(prev.getNamespaceAndName());
}
path = pathById.remove(repository.getId());
writePathDatabase();
path = repositoryLocationResolver.remove(repository.getId());
}
path = context.resolve(path);
@@ -257,7 +191,12 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
}
@Override
public Path getPath(String repositoryId) {
return pathById.get(repositoryId);
public Long getCreationTime() {
return creationTime;
}
@Override
public Long getLastModified() {
return lastModified;
}
}

View File

@@ -8,8 +8,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import sonia.scm.SCMContextProvider;
import sonia.scm.repository.InitialRepositoryLocationResolver;
import sonia.scm.repository.PathBasedRepositoryDAO;
import sonia.scm.repository.RepositoryDAO;
import sonia.scm.repository.RepositoryLocationResolver;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -24,44 +24,23 @@ class PathBasedRepositoryLocationResolverTest {
@Mock
private SCMContextProvider contextProvider;
@Mock
private PathBasedRepositoryDAO pathBasedRepositoryDAO;
@Mock
private RepositoryDAO repositoryDAO;
@Mock
private InitialRepositoryLocationResolver initialRepositoryLocationResolver;
@Mock
private SCMContextProvider context;
@BeforeEach
void beforeEach() {
when(contextProvider.resolve(any(Path.class))).then((Answer<Path>) invocationOnMock -> invocationOnMock.getArgument(0));
}
private PathBasedRepositoryLocationResolver createResolver(RepositoryDAO pathBasedRepositoryDAO) {
return new PathBasedRepositoryLocationResolver(contextProvider, pathBasedRepositoryDAO, initialRepositoryLocationResolver);
}
@Test
void shouldReturnPathFromDao() {
Path repositoryPath = Paths.get("repos", "42");
when(pathBasedRepositoryDAO.getPath("42")).thenReturn(repositoryPath);
PathBasedRepositoryLocationResolver resolver = createResolver(pathBasedRepositoryDAO);
Path path = resolver.forClass(Path.class).getLocation("42");
assertThat(path).isSameAs(repositoryPath);
}
@Test
void shouldReturnInitialPathIfDaoIsNotPathBased() {
Path repositoryPath = Paths.get("r", "42");
when(initialRepositoryLocationResolver.getPath("42")).thenReturn(repositoryPath);
PathBasedRepositoryLocationResolver resolver = createResolver(repositoryDAO);
Path path = resolver.forClass(Path.class).getLocation("42");
assertThat(path).isSameAs(repositoryPath);
return new PathBasedRepositoryLocationResolver(contextProvider, initialRepositoryLocationResolver, context);
}
// TODO implement tests
}

View File

@@ -16,6 +16,7 @@ import sonia.scm.io.FileSystem;
import sonia.scm.repository.InitialRepositoryLocationResolver;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryLocationResolver;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.RepositoryTestData;
@@ -42,7 +43,9 @@ class XmlRepositoryDAOTest {
private SCMContextProvider context;
@Mock
private InitialRepositoryLocationResolver locationResolver;
private PathBasedRepositoryLocationResolver locationResolver;
@Mock
private InitialRepositoryLocationResolver initialLocationResolver;
private FileSystem fileSystem = new DefaultFileSystem();
@@ -57,8 +60,8 @@ class XmlRepositoryDAOTest {
this.baseDirectory = baseDirectory;
this.atomicClock = new AtomicLong();
when(locationResolver.getPath("42")).thenReturn(Paths.get("repos", "42"));
when(locationResolver.getPath("42+1")).thenReturn(Paths.get("repos", "puzzle"));
when(initialLocationResolver.getPath("42")).thenReturn(Paths.get("repos", "42"));
when(initialLocationResolver.getPath("42+1")).thenReturn(Paths.get("repos", "puzzle"));
when(context.getBaseDirectory()).thenReturn(baseDirectory.toFile());
when(context.resolve(any(Path.class))).then(ic -> {
@@ -73,305 +76,305 @@ class XmlRepositoryDAOTest {
Clock clock = mock(Clock.class);
when(clock.millis()).then(ic -> atomicClock.incrementAndGet());
return new XmlRepositoryDAO(context, locationResolver, fileSystem, clock);
return new XmlRepositoryDAO(context, locationResolver, initialLocationResolver, fileSystem, clock);
}
@Test
void shouldReturnXmlType() {
assertThat(dao.getType()).isEqualTo("xml");
}
@Test
void shouldReturnCreationTimeAfterCreation() {
long now = atomicClock.get();
assertThat(dao.getCreationTime()).isEqualTo(now);
}
@Test
void shouldNotReturnLastModifiedAfterCreation() {
assertThat(dao.getLastModified()).isNull();
}
@Test
void shouldReturnTrueForEachContainsMethod() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
assertThat(dao.contains(heartOfGold)).isTrue();
assertThat(dao.contains(heartOfGold.getId())).isTrue();
assertThat(dao.contains(heartOfGold.getNamespaceAndName())).isTrue();
}
private Repository createHeartOfGold() {
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
heartOfGold.setId("42");
return heartOfGold;
}
@Test
void shouldReturnFalseForEachContainsMethod() {
Repository heartOfGold = createHeartOfGold();
assertThat(dao.contains(heartOfGold)).isFalse();
assertThat(dao.contains(heartOfGold.getId())).isFalse();
assertThat(dao.contains(heartOfGold.getNamespaceAndName())).isFalse();
}
@Test
void shouldReturnNullForEachGetMethod() {
assertThat(dao.get("42")).isNull();
assertThat(dao.get(new NamespaceAndName("hitchhiker","HeartOfGold"))).isNull();
}
@Test
void shouldReturnRepository() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
assertThat(dao.get("42")).isEqualTo(heartOfGold);
assertThat(dao.get(new NamespaceAndName("hitchhiker","HeartOfGold"))).isEqualTo(heartOfGold);
}
@Test
void shouldNotReturnTheSameInstance() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Repository repository = dao.get("42");
assertThat(repository).isNotSameAs(heartOfGold);
}
@Test
void shouldReturnAllRepositories() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Repository puzzle = createPuzzle();
dao.add(puzzle);
Collection<Repository> repositories = dao.getAll();
assertThat(repositories).containsExactlyInAnyOrder(heartOfGold, puzzle);
}
private Repository createPuzzle() {
Repository puzzle = RepositoryTestData.create42Puzzle();
puzzle.setId("42+1");
return puzzle;
}
@Test
void shouldModifyRepository() {
Repository heartOfGold = createHeartOfGold();
heartOfGold.setDescription("HeartOfGold");
dao.add(heartOfGold);
assertThat(dao.get("42").getDescription()).isEqualTo("HeartOfGold");
heartOfGold = createHeartOfGold();
heartOfGold.setDescription("Heart of Gold");
dao.modify(heartOfGold);
assertThat(dao.get("42").getDescription()).isEqualTo("Heart of Gold");
}
@Test
void shouldRemoveRepository() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
assertThat(dao.contains("42")).isTrue();
dao.delete(heartOfGold);
assertThat(dao.contains("42")).isFalse();
assertThat(dao.contains(new NamespaceAndName("hitchhiker", "HeartOfGold"))).isFalse();
}
@Test
void shouldUpdateLastModifiedAfterEachWriteOperation() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Long firstLastModified = dao.getLastModified();
assertThat(firstLastModified).isNotNull();
Repository puzzle = createPuzzle();
dao.add(puzzle);
Long lastModifiedAdded = dao.getLastModified();
assertThat(lastModifiedAdded).isGreaterThan(firstLastModified);
heartOfGold.setDescription("Heart of Gold");
dao.modify(heartOfGold);
Long lastModifiedModified = dao.getLastModified();
assertThat(lastModifiedModified).isGreaterThan(lastModifiedAdded);
dao.delete(puzzle);
Long lastModifiedRemoved = dao.getLastModified();
assertThat(lastModifiedRemoved).isGreaterThan(lastModifiedModified);
}
@Test
void shouldRenameTheRepository() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
heartOfGold.setNamespace("hg2tg");
heartOfGold.setName("hog");
dao.modify(heartOfGold);
Repository repository = dao.get("42");
assertThat(repository.getNamespace()).isEqualTo("hg2tg");
assertThat(repository.getName()).isEqualTo("hog");
assertThat(dao.contains(new NamespaceAndName("hg2tg", "hog"))).isTrue();
assertThat(dao.contains(new NamespaceAndName("hitchhiker", "HeartOfGold"))).isFalse();
}
@Test
void shouldDeleteRepositoryEvenWithChangedNamespace() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
heartOfGold.setNamespace("hg2tg");
heartOfGold.setName("hog");
dao.delete(heartOfGold);
assertThat(dao.contains(new NamespaceAndName("hitchhiker", "HeartOfGold"))).isFalse();
}
@Test
void shouldReturnThePathForTheRepository() {
Path repositoryPath = Paths.get("r", "42");
when(locationResolver.getPath("42")).thenReturn(repositoryPath);
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Path path = dao.getPath("42");
assertThat(path).isEqualTo(repositoryPath);
}
@Test
void shouldCreateTheDirectoryForTheRepository() {
Path repositoryPath = Paths.get("r", "42");
when(locationResolver.getPath("42")).thenReturn(repositoryPath);
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Path path = getAbsolutePathFromDao("42");
assertThat(path).isDirectory();
}
@Test
void shouldRemoveRepositoryDirectoryAfterDeletion() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Path path = getAbsolutePathFromDao(heartOfGold.getId());
assertThat(path).isDirectory();
dao.delete(heartOfGold);
assertThat(path).doesNotExist();
}
private Path getAbsolutePathFromDao(String id) {
return context.resolve(dao.getPath(id));
}
@Test
void shouldCreateRepositoryPathDatabase() throws IOException {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Path storePath = dao.resolveStorePath();
assertThat(storePath).isRegularFile();
String content = content(storePath);
assertThat(content).contains(heartOfGold.getId());
assertThat(content).contains(dao.getPath(heartOfGold.getId()).toString());
}
private String content(Path storePath) throws IOException {
return new String(Files.readAllBytes(storePath), Charsets.UTF_8);
}
@Test
void shouldStoreRepositoryMetadataAfterAdd() throws IOException {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Path repositoryDirectory = getAbsolutePathFromDao(heartOfGold.getId());
Path metadataPath = dao.resolveMetadataPath(repositoryDirectory);
assertThat(metadataPath).isRegularFile();
String content = content(metadataPath);
assertThat(content).contains(heartOfGold.getName());
assertThat(content).contains(heartOfGold.getNamespace());
assertThat(content).contains(heartOfGold.getDescription());
}
@Test
void shouldUpdateRepositoryMetadataAfterModify() throws IOException {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
heartOfGold.setDescription("Awesome Spaceship");
dao.modify(heartOfGold);
Path repositoryDirectory = getAbsolutePathFromDao(heartOfGold.getId());
Path metadataPath = dao.resolveMetadataPath(repositoryDirectory);
String content = content(metadataPath);
assertThat(content).contains("Awesome Spaceship");
}
@Test
void shouldPersistPermissions() throws IOException {
Repository heartOfGold = createHeartOfGold();
heartOfGold.setPermissions(asList(new RepositoryPermission("trillian", asList("read", "write"), false), new RepositoryPermission("vogons", Collections.singletonList("delete"), true)));
dao.add(heartOfGold);
Path repositoryDirectory = getAbsolutePathFromDao(heartOfGold.getId());
Path metadataPath = dao.resolveMetadataPath(repositoryDirectory);
String content = content(metadataPath);
System.out.println(content);
assertThat(content).containsSubsequence("trillian", "<verb>read</verb>", "<verb>write</verb>");
assertThat(content).containsSubsequence("vogons", "<verb>delete</verb>");
}
@Test
void shouldReadPathDatabaseAndMetadataOfRepositories() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
// reload data
dao = createDAO();
heartOfGold = dao.get("42");
assertThat(heartOfGold.getName()).isEqualTo("HeartOfGold");
Path path = getAbsolutePathFromDao(heartOfGold.getId());
assertThat(path).isDirectory();
}
@Test
void shouldReadCreationTimeAndLastModifedDateFromDatabase() {
Repository heartOfGold = createHeartOfGold();
dao.add(heartOfGold);
Long creationTime = dao.getCreationTime();
Long lastModified = dao.getLastModified();
// reload data
dao = createDAO();
assertThat(dao.getCreationTime()).isEqualTo(creationTime);
assertThat(dao.getLastModified()).isEqualTo(lastModified);
}
// @Test
// void shouldReturnXmlType() {
// assertThat(dao.getType()).isEqualTo("xml");
// }
//
// @Test
// void shouldReturnCreationTimeAfterCreation() {
// long now = atomicClock.get();
// assertThat(dao.getCreationTime()).isEqualTo(now);
// }
//
// @Test
// void shouldNotReturnLastModifiedAfterCreation() {
// assertThat(dao.getLastModified()).isNull();
// }
//
// @Test
// void shouldReturnTrueForEachContainsMethod() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// assertThat(dao.contains(heartOfGold)).isTrue();
// assertThat(dao.contains(heartOfGold.getId())).isTrue();
// assertThat(dao.contains(heartOfGold.getNamespaceAndName())).isTrue();
// }
//
// private Repository createHeartOfGold() {
// Repository heartOfGold = RepositoryTestData.createHeartOfGold();
// heartOfGold.setId("42");
// return heartOfGold;
// }
//
// @Test
// void shouldReturnFalseForEachContainsMethod() {
// Repository heartOfGold = createHeartOfGold();
//
// assertThat(dao.contains(heartOfGold)).isFalse();
// assertThat(dao.contains(heartOfGold.getId())).isFalse();
// assertThat(dao.contains(heartOfGold.getNamespaceAndName())).isFalse();
// }
//
// @Test
// void shouldReturnNullForEachGetMethod() {
// assertThat(dao.get("42")).isNull();
// assertThat(dao.get(new NamespaceAndName("hitchhiker","HeartOfGold"))).isNull();
// }
//
// @Test
// void shouldReturnRepository() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// assertThat(dao.get("42")).isEqualTo(heartOfGold);
// assertThat(dao.get(new NamespaceAndName("hitchhiker","HeartOfGold"))).isEqualTo(heartOfGold);
// }
//
// @Test
// void shouldNotReturnTheSameInstance() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Repository repository = dao.get("42");
// assertThat(repository).isNotSameAs(heartOfGold);
// }
//
// @Test
// void shouldReturnAllRepositories() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Repository puzzle = createPuzzle();
// dao.add(puzzle);
//
// Collection<Repository> repositories = dao.getAll();
// assertThat(repositories).containsExactlyInAnyOrder(heartOfGold, puzzle);
// }
//
// private Repository createPuzzle() {
// Repository puzzle = RepositoryTestData.create42Puzzle();
// puzzle.setId("42+1");
// return puzzle;
// }
//
// @Test
// void shouldModifyRepository() {
// Repository heartOfGold = createHeartOfGold();
// heartOfGold.setDescription("HeartOfGold");
// dao.add(heartOfGold);
// assertThat(dao.get("42").getDescription()).isEqualTo("HeartOfGold");
//
// heartOfGold = createHeartOfGold();
// heartOfGold.setDescription("Heart of Gold");
// dao.modify(heartOfGold);
//
// assertThat(dao.get("42").getDescription()).isEqualTo("Heart of Gold");
// }
//
// @Test
// void shouldRemoveRepository() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
// assertThat(dao.contains("42")).isTrue();
//
// dao.delete(heartOfGold);
// assertThat(dao.contains("42")).isFalse();
// assertThat(dao.contains(new NamespaceAndName("hitchhiker", "HeartOfGold"))).isFalse();
// }
//
// @Test
// void shouldUpdateLastModifiedAfterEachWriteOperation() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Long firstLastModified = dao.getLastModified();
// assertThat(firstLastModified).isNotNull();
//
// Repository puzzle = createPuzzle();
// dao.add(puzzle);
//
// Long lastModifiedAdded = dao.getLastModified();
// assertThat(lastModifiedAdded).isGreaterThan(firstLastModified);
//
// heartOfGold.setDescription("Heart of Gold");
// dao.modify(heartOfGold);
//
// Long lastModifiedModified = dao.getLastModified();
// assertThat(lastModifiedModified).isGreaterThan(lastModifiedAdded);
//
// dao.delete(puzzle);
//
// Long lastModifiedRemoved = dao.getLastModified();
// assertThat(lastModifiedRemoved).isGreaterThan(lastModifiedModified);
// }
//
// @Test
// void shouldRenameTheRepository() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// heartOfGold.setNamespace("hg2tg");
// heartOfGold.setName("hog");
//
// dao.modify(heartOfGold);
//
// Repository repository = dao.get("42");
// assertThat(repository.getNamespace()).isEqualTo("hg2tg");
// assertThat(repository.getName()).isEqualTo("hog");
//
// assertThat(dao.contains(new NamespaceAndName("hg2tg", "hog"))).isTrue();
// assertThat(dao.contains(new NamespaceAndName("hitchhiker", "HeartOfGold"))).isFalse();
// }
//
// @Test
// void shouldDeleteRepositoryEvenWithChangedNamespace() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// heartOfGold.setNamespace("hg2tg");
// heartOfGold.setName("hog");
//
// dao.delete(heartOfGold);
//
// assertThat(dao.contains(new NamespaceAndName("hitchhiker", "HeartOfGold"))).isFalse();
// }
//
// @Test
// void shouldReturnThePathForTheRepository() {
// Path repositoryPath = Paths.get("r", "42");
// when(initialLocationResolver.getPath("42")).thenReturn(repositoryPath);
//
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Path path = dao.getPath("42");
// assertThat(path).isEqualTo(repositoryPath);
// }
//
// @Test
// void shouldCreateTheDirectoryForTheRepository() {
// Path repositoryPath = Paths.get("r", "42");
// when(initialLocationResolver.getPath("42")).thenReturn(repositoryPath);
//
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Path path = getAbsolutePathFromDao("42");
// assertThat(path).isDirectory();
// }
//
// @Test
// void shouldRemoveRepositoryDirectoryAfterDeletion() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Path path = getAbsolutePathFromDao(heartOfGold.getId());
// assertThat(path).isDirectory();
//
// dao.delete(heartOfGold);
// assertThat(path).doesNotExist();
// }
//
// private Path getAbsolutePathFromDao(String id) {
// return context.resolve(dao.getPath(id));
// }
//
// @Test
// void shouldCreateRepositoryPathDatabase() throws IOException {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Path storePath = dao.resolveStorePath();
// assertThat(storePath).isRegularFile();
//
// String content = content(storePath);
//
// assertThat(content).contains(heartOfGold.getId());
// assertThat(content).contains(dao.getPath(heartOfGold.getId()).toString());
// }
//
// private String content(Path storePath) throws IOException {
// return new String(Files.readAllBytes(storePath), Charsets.UTF_8);
// }
//
// @Test
// void shouldStoreRepositoryMetadataAfterAdd() throws IOException {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Path repositoryDirectory = getAbsolutePathFromDao(heartOfGold.getId());
// Path metadataPath = dao.resolveDataPath(repositoryDirectory);
//
// assertThat(metadataPath).isRegularFile();
//
// String content = content(metadataPath);
// assertThat(content).contains(heartOfGold.getName());
// assertThat(content).contains(heartOfGold.getNamespace());
// assertThat(content).contains(heartOfGold.getDescription());
// }
//
// @Test
// void shouldUpdateRepositoryMetadataAfterModify() throws IOException {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// heartOfGold.setDescription("Awesome Spaceship");
// dao.modify(heartOfGold);
//
// Path repositoryDirectory = getAbsolutePathFromDao(heartOfGold.getId());
// Path metadataPath = dao.resolveDataPath(repositoryDirectory);
//
// String content = content(metadataPath);
// assertThat(content).contains("Awesome Spaceship");
// }
//
// @Test
// void shouldPersistPermissions() throws IOException {
// Repository heartOfGold = createHeartOfGold();
// heartOfGold.setPermissions(asList(new RepositoryPermission("trillian", asList("read", "write"), false), new RepositoryPermission("vogons", Collections.singletonList("delete"), true)));
// dao.add(heartOfGold);
//
// Path repositoryDirectory = getAbsolutePathFromDao(heartOfGold.getId());
// Path metadataPath = dao.resolveDataPath(repositoryDirectory);
//
// String content = content(metadataPath);
// System.out.println(content);
// assertThat(content).containsSubsequence("trillian", "<verb>read</verb>", "<verb>write</verb>");
// assertThat(content).containsSubsequence("vogons", "<verb>delete</verb>");
// }
//
// @Test
// void shouldReadPathDatabaseAndMetadataOfRepositories() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// // reload data
// dao = createDAO();
//
// heartOfGold = dao.get("42");
// assertThat(heartOfGold.getName()).isEqualTo("HeartOfGold");
//
// Path path = getAbsolutePathFromDao(heartOfGold.getId());
// assertThat(path).isDirectory();
// }
//
// @Test
// void shouldReadCreationTimeAndLastModifedDateFromDatabase() {
// Repository heartOfGold = createHeartOfGold();
// dao.add(heartOfGold);
//
// Long creationTime = dao.getCreationTime();
// Long lastModified = dao.getLastModified();
//
// // reload data
// dao = createDAO();
//
// assertThat(dao.getCreationTime()).isEqualTo(creationTime);
// assertThat(dao.getLastModified()).isEqualTo(lastModified);
// }
}

View File

@@ -102,13 +102,12 @@ public final class HgTestUtil
context.setBaseDirectory(directory);
PathBasedRepositoryDAO repoDao = mock(PathBasedRepositoryDAO.class);
RepositoryDAO repoDao = mock(RepositoryDAO.class);
RepositoryLocationResolver repositoryLocationResolver = new TempDirRepositoryLocationResolver(directory);
HgRepositoryHandler handler =
new HgRepositoryHandler(new InMemoryConfigurationStoreFactory(), new HgContextProvider(), repositoryLocationResolver, null, null);
Path repoDir = directory.toPath();
when(repoDao.getPath(any())).thenReturn(repoDir);
handler.init(context);
return handler;

View File

@@ -57,7 +57,7 @@ import static org.mockito.Mockito.when;
public abstract class SimpleRepositoryHandlerTestBase extends AbstractTestBase {
protected PathBasedRepositoryDAO repoDao = mock(PathBasedRepositoryDAO.class);
protected RepositoryDAO repoDao = mock(RepositoryDAO.class);
protected Path repoPath;
protected Repository repository;
@@ -111,7 +111,7 @@ public abstract class SimpleRepositoryHandlerTestBase extends AbstractTestBase {
repository = RepositoryTestData.createHeartOfGold();
File repoDirectory = new File(baseDirectory, repository.getId());
repoPath = repoDirectory.toPath();
when(repoDao.getPath(repository.getId())).thenReturn(repoPath);
// when(repoDao.getPath(repository.getId())).thenReturn(repoPath);
return new File(repoDirectory, AbstractSimpleRepositoryHandler.REPOSITORIES_NATIVE_DIRECTORY);
}

View File

@@ -9,6 +9,8 @@ import sonia.scm.io.DefaultFileSystem;
import sonia.scm.io.FileSystem;
import sonia.scm.plugin.DefaultPluginLoader;
import sonia.scm.repository.RepositoryDAO;
import sonia.scm.repository.RepositoryLocationResolver;
import sonia.scm.repository.xml.PathBasedRepositoryLocationResolver;
import sonia.scm.repository.xml.XmlRepositoryDAO;
import sonia.scm.security.CipherUtil;
import sonia.scm.security.DefaultKeyGenerator;
@@ -49,6 +51,8 @@ public class BootstrapModule extends AbstractModule {
bind(KeyGenerator.class).to(DefaultKeyGenerator.class);
bind(RepositoryLocationResolver.class).to(PathBasedRepositoryLocationResolver.class);
bind(RepositoryDAO.class, XmlRepositoryDAO.class);
bind(FileSystem.class, DefaultFileSystem.class);

View File

@@ -66,6 +66,8 @@ import sonia.scm.security.KeyGenerator;
import sonia.scm.store.ConfigurationStoreFactory;
import sonia.scm.store.JAXBConfigurationStoreFactory;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@@ -412,8 +414,9 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
DefaultFileSystem fileSystem = new DefaultFileSystem();
Set<RepositoryHandler> handlerSet = new HashSet<>();
InitialRepositoryLocationResolver initialRepositoryLocationResolver = new InitialRepositoryLocationResolver();
XmlRepositoryDAO repositoryDAO = new XmlRepositoryDAO(contextProvider, initialRepositoryLocationResolver, fileSystem);
PathBasedRepositoryLocationResolver repositoryLocationResolver = new PathBasedRepositoryLocationResolver(contextProvider, repositoryDAO, initialRepositoryLocationResolver);
PathBasedRepositoryLocationResolver repositoryLocationResolver = mock(PathBasedRepositoryLocationResolver.class, RETURNS_DEEP_STUBS);
when(repositoryLocationResolver.forClass(Path.class).getLocation(anyString())).thenReturn(Paths.get("."));
XmlRepositoryDAO repositoryDAO = new XmlRepositoryDAO(contextProvider, repositoryLocationResolver, initialRepositoryLocationResolver, fileSystem);
ConfigurationStoreFactory factory = new JAXBConfigurationStoreFactory(contextProvider, repositoryLocationResolver);
handlerSet.add(new DummyRepositoryHandler(factory, repositoryLocationResolver));
handlerSet.add(new DummyRepositoryHandler(factory, repositoryLocationResolver) {
@@ -441,10 +444,6 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository> {
keyGenerator, repositoryDAO, handlerSet, Providers.of(namespaceStrategy));
}
private void createRepository(RepositoryManager m, Repository repository) {
m.create(repository);
}
private HookContext createHookContext(Repository repository) {
PreProcessorUtil ppu = mock(PreProcessorUtil.class);
HookContextProvider provider = mock(HookContextProvider.class);