When getting the format version it matches expected

This commit is contained in:
Paul Campbell 2018-06-15 12:48:55 +01:00
parent 111527f570
commit 00bfa35460
6 changed files with 51 additions and 4 deletions

View file

@ -23,6 +23,7 @@
<assertj.version>3.10.0</assertj.version>
<mon.version>0.4.0</mon.version>
<mockito.version>2.18.3</mockito.version>
<java-semver.version>0.9.0</java-semver.version>
</properties>
<dependencies>
@ -36,7 +37,11 @@
<artifactId>mon</artifactId>
<version>${mon.version}</version>
</dependency>
<dependency>
<groupId>com.github.zafarkhaja</groupId>
<artifactId>java-semver</artifactId>
<version>${java-semver.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View file

@ -21,6 +21,7 @@
package net.kemitix.gitdb;
import com.github.zafarkhaja.semver.Version;
import net.kemitix.gitdb.impl.LocalGitDB;
import java.io.IOException;
@ -34,6 +35,12 @@ import java.util.Optional;
*/
public interface GitDB {
int MAJOR = 1;
int MINOR = 0;
int PATCH = 0;
Version VERSION = Version.forIntegers(MAJOR, MINOR, PATCH);
/**
* Initialise a new local gitdb.
*
@ -71,4 +78,5 @@ public interface GitDB {
* @throws IOException if there is an error accessing the branch name
*/
Optional<GitDBBranch> branch(String name) throws IOException;
}

View file

@ -21,6 +21,8 @@
package net.kemitix.gitdb;
import com.github.zafarkhaja.semver.Version;
import java.io.IOException;
import java.util.Optional;
@ -58,4 +60,14 @@ public interface GitDBBranch {
* @throws IOException if there was an error removing the key/value
*/
GitDBBranch remove(String key) throws IOException;
/**
* Returns the GitDB format for the current branch.
*
* <p>Different branches can have different versions.</p>
*
* @return the format as per semantic versioning, i.e. "x.y.z" within an Optional
* @throws IOException error reading version
*/
Optional<Version> getFormatVersion() throws IOException;
}

View file

@ -21,6 +21,7 @@
package net.kemitix.gitdb.impl;
import com.github.zafarkhaja.semver.Version;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import net.kemitix.gitdb.GitDBBranch;
@ -99,6 +100,12 @@ class GitDBBranchImpl implements GitDBBranch {
return this;
}
@Override
public Optional<Version> getFormatVersion() throws IOException {
return gitDBRepo.readValue(branchRef, "GitDB.Version")
.map(Version::valueOf);
}
private String commitMessageForAdd(final String key, final String value) {
return String.format("Add key [%s] = [%s]", key, value);
}

View file

@ -21,6 +21,7 @@
package net.kemitix.gitdb.impl;
import net.kemitix.gitdb.GitDB;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryCache;
@ -45,7 +46,7 @@ class InitGitDBRepo {
private static final String INIT_USER = "GitDB";
private static final String INIT_EMAIL = "pcampbell@kemitix.net";
private static final String MASTER = "master";
private static final String IS_GIT_DB = "isGitDB";
private static final String GIT_DB_VERSION = "GitDB.Version";
private static final String REFS_HEADS_FORMAT = "refs/heads/%s";
/**
@ -65,8 +66,9 @@ class InitGitDBRepo {
private void createInitialBranchOnMaster(final Repository repository) throws IOException {
final GitDBRepo repo = new GitDBRepo(repository);
final ObjectId objectId = repo.insertBlob(new byte[0]);
final ObjectId treeId = repo.insertNewTree(IS_GIT_DB, objectId);
final ObjectId objectId =
repo.insertBlob(GitDB.VERSION.toString().getBytes(StandardCharsets.UTF_8));
final ObjectId treeId = repo.insertNewTree(GIT_DB_VERSION, objectId);
final ObjectId commitId = repo.initialCommit(treeId, INIT_MESSAGE, INIT_USER, INIT_EMAIL);
createBranch(repository, commitId, MASTER);
}

View file

@ -1,5 +1,6 @@
package net.kemitix.gitdb.test;
import com.github.zafarkhaja.semver.Version;
import net.kemitix.gitdb.GitDB;
import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.InvalidRepositoryException;
@ -213,6 +214,18 @@ class GitDBTest implements WithAssertions {
return branchOptional.get();
}
// When getting the format version it matches expected
@Test
void getVersionFormat_thenFormatIsSet() throws IOException {
//given
final GitDBBranch gitDBBranch = gitDBBranch();
//when
final Optional<Version> formatVersion = gitDBBranch.getFormatVersion();
//then
assertThat(formatVersion).contains(GitDB.VERSION);
assertThat(formatVersion.get()).isNotSameAs(GitDB.VERSION);
}
// When putting a key/value pair then a GitDbBranch is returned
@Test
void putValue_thenReturnUpdatedGitDBBranch() throws IOException {