From 11575f53e78f7d1715b2654d9e0cadc20d5cd583 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 2 Jun 2018 12:35:14 +0100 Subject: [PATCH] When select a branch that doesn't exist then an empty Optional is returned --- src/main/java/net/kemitix/gitdb/GitDB.java | 9 ++++++++ .../gitdb/{Branch.java => GitDBBranch.java} | 18 +++++++--------- .../java/net/kemitix/gitdb/GitDBLocal.java | 6 ++++++ .../java/net/kemitix/gitdb/GitDBTest.java | 21 ++++++++++++++++--- 4 files changed, 40 insertions(+), 14 deletions(-) rename src/main/java/net/kemitix/gitdb/{Branch.java => GitDBBranch.java} (81%) diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 296e2d5..fc4f292 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -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 branch(String name); } diff --git a/src/main/java/net/kemitix/gitdb/Branch.java b/src/main/java/net/kemitix/gitdb/GitDBBranch.java similarity index 81% rename from src/main/java/net/kemitix/gitdb/Branch.java rename to src/main/java/net/kemitix/gitdb/GitDBBranch.java index 788aba5..3223947 100644 --- a/src/main/java/net/kemitix/gitdb/Branch.java +++ b/src/main/java/net/kemitix/gitdb/GitDBBranch.java @@ -21,14 +21,10 @@ package net.kemitix.gitdb; -//import net.kemitix.mon.TypeAlias; - -//public class Branch extends TypeAlias { -// 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 { +} diff --git a/src/main/java/net/kemitix/gitdb/GitDBLocal.java b/src/main/java/net/kemitix/gitdb/GitDBLocal.java index f0e1faa..8c8f92e 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBLocal.java +++ b/src/main/java/net/kemitix/gitdb/GitDBLocal.java @@ -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 branch(final String name) { + return Optional.empty(); + } + // @Override // @SneakyThrows // public T get(Branch branch, Key key, Class type) { diff --git a/src/test/java/net/kemitix/gitdb/GitDBTest.java b/src/test/java/net/kemitix/gitdb/GitDBTest.java index 679074c..4018182 100644 --- a/src/test/java/net/kemitix/gitdb/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/GitDBTest.java @@ -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 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