When select a branch that doesn't exist then an empty Optional is returned

This commit is contained in:
Paul Campbell 2018-06-02 12:35:14 +01:00
parent 79a5818242
commit 11575f53e7
4 changed files with 40 additions and 14 deletions

View file

@ -26,6 +26,7 @@ import org.eclipse.jgit.errors.RepositoryNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
/**
* Main API for connecting to a Git repo as a database.
@ -63,4 +64,12 @@ public interface GitDB {
throw new GitDBRepoNotFoundException(dbDir, e);
}
}
/**
* Select the named branch.
*
* @param name the branch to select
* @return an Optional containing the branch if it exists
*/
Optional<GitDBBranch> branch(String name);
}

View file

@ -21,14 +21,10 @@
package net.kemitix.gitdb;
//import net.kemitix.mon.TypeAlias;
//public class Branch extends TypeAlias<String> {
// protected Branch(final String value) {
// super(value);
// }
//
// static Branch name(final String name) {
// return new Branch(name);
// }
//}
/**
* API for interacting with a branch in a GirDB.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public interface GitDBBranch {
}

View file

@ -31,6 +31,7 @@ import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
import java.util.Optional;
/**
* Implementation of GitDB for working with a local Repo.
@ -99,6 +100,11 @@ class GitDBLocal implements GitDB {
return initCommand.setGitDir(dbDir).setBare(true).call();
}
@Override
public Optional<GitDBBranch> branch(final String name) {
return Optional.empty();
}
// @Override
// @SneakyThrows
// public <T> T get(Branch branch, Key key, Class<T> type) {

View file

@ -11,6 +11,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
import java.nio.file.*;
import java.util.List;
import java.util.Optional;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
@ -173,21 +174,35 @@ class GitDBTest implements WithAssertions {
@Test
void openRepo_whenGitDB_thenReturnGitDB() throws IOException {
//given
final Path dir = gitDBRepo();
final Path dir = gitDBRepoPath();
//when
final GitDBLocal gitDB = GitDB.openLocal(dir);
//then
assertThat(gitDB).isNotNull();
}
private Path gitDBRepo() throws IOException {
private Path gitDBRepoPath() throws IOException {
final Path dbDir = dirDoesNotExist();
GitDB.initLocal(dbDir);
return dbDir;
}
// Given a valid GitDb handle
// When select a branch that doesn't exist then an exception is thrown
// When select a branch that doesn't exist then an empty Optional is returned
@Test
void selectBranch_branchNotExist_thenEmptyOptional() throws IOException {
//given
final GitDB gitDb = newGitDBRepo();
//when
final Optional<GitDBBranch> branch = gitDb.branch("unknown");
//then
assertThat(branch).isEmpty();
}
private GitDB newGitDBRepo() throws IOException {
return GitDB.initLocal(dirDoesNotExist());
}
// When select a valid branch then a GitDbBranch is returned
// Given a valid GitDbBranch handle