Initialise DB

This commit is contained in:
Paul Campbell 2018-05-11 19:51:58 +01:00
parent f93a819ba9
commit c8cf933a33
3 changed files with 62 additions and 3 deletions

View file

@ -14,6 +14,7 @@
<junit.version>5.2.0</junit.version>
<assertj.version>3.9.1</assertj.version>
<jgit.version>4.11.0.201803080745-r</jgit.version>
<java.version>8</java.version>
</properties>
<dependencies>
@ -65,6 +66,14 @@
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -1,5 +1,27 @@
package net.kemitix.gitdb
class GitDB {
import java.nio.file.Path
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import scala.beans.BeanProperty
class GitDB (val dir: Path) {
@BeanProperty
val repository: Repository = {
val fileRepositoryBuilder = new FileRepositoryBuilder
fileRepositoryBuilder.setGitDir(dir.toFile)
fileRepositoryBuilder.build()
}
def close: Unit = repository.close()
}
object GitDB {
def init(dir: Path): GitDB = {
new GitDB(dir)
}
}

View file

@ -1,13 +1,41 @@
package net.kemitix.gitdb;
import org.eclipse.jgit.lib.Repository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import static org.assertj.core.api.Assertions.assertThat;
class GitDBTest {
private final Path dbDir = Files.createTempDirectory("gitdb");
private final GitDB gitDB = GitDB.init(dbDir);
GitDBTest() throws IOException {
}
@Test
void dummy() {
assertThat(true).isTrue();
void shouldInitialiseGitDB() {
//then
assertThat(gitDB).isNotNull();
assertThat(gitDB.dir()).isDirectory()
.isEqualTo(dbDir);
final Repository repository = gitDB.getRepository();
assertThat(repository.isBare()).isTrue();
}
@AfterEach
void tearDown() throws IOException {
gitDB.close();
Files.walk(dbDir)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}