From 2de83ba3bcfdb5d35c1e3a39bf8863777cc07408 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 25 Jun 2018 21:51:08 +0100 Subject: [PATCH] Refactoring GitDBTest --- .../net/kemitix/gitdb/test/GitDBTest.java | 54 +++++++------------ 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java index 23428e8..7302324 100644 --- a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java @@ -13,38 +13,23 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Repository; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintStream; import java.nio.file.DirectoryNotEmptyException; import java.nio.file.Files; import java.nio.file.NotDirectoryException; import java.nio.file.Path; import java.util.UUID; import java.util.function.Consumer; +import java.util.function.Function; import java.util.function.Supplier; -@ExtendWith(MockitoExtension.class) class GitDBTest implements WithAssertions { private final Supplier stringSupplier = UUID.randomUUID()::toString; private final String userName = stringSupplier.get(); private final String userEmailAddress = stringSupplier.get(); - private static void tree(final Path dbDir, final PrintStream out) throws IOException { - final Process treeProcess = new ProcessBuilder("tree", dbDir.toString()).start(); - try (final BufferedReader reader = new BufferedReader(new InputStreamReader(treeProcess.getInputStream()))) { - String line; - while (null != (line = reader.readLine())) { - out.println("line = " + line); - } - } - } - // When initialising a repo in a dir that doesn't exist then a bare repo is created @Test void initRepo_whenDirNotExist_thenCreateBareRepo() throws IOException { @@ -53,7 +38,7 @@ class GitDBTest implements WithAssertions { //when final Result gitDB = GitDB.initLocal(dir, userName, userEmailAddress); //then - assertThat(gitDB.isOkay()).isTrue(); + assertThatResultIsOkay(gitDB); assertThatIsBareRepo(dir); } @@ -63,6 +48,10 @@ class GitDBTest implements WithAssertions { return directory; } + private void assertThatResultIsOkay(final Result result) { + assertThat(result.isOkay()).isTrue(); + } + private void assertThatIsBareRepo(final Path dbDir) throws IOException { final Git git = Git.open(dbDir.toFile()); final Repository repository = git.getRepository(); @@ -128,7 +117,7 @@ class GitDBTest implements WithAssertions { //when final Result gitDB = GitDB.initLocal(dir, userName, userEmailAddress); //then - assertThat(gitDB.isOkay()).isTrue(); + assertThatResultIsOkay(gitDB); assertThatIsBareRepo(dir); } @@ -234,7 +223,7 @@ class GitDBTest implements WithAssertions { //given final Result gitDb = gitDB(dirDoesNotExist()); //when - final Result> branch = gitDb.flatMap(db -> db.branch("unknown")); + final Result> branch = gitDb.flatMap(selectBranch("unknown")); //then assertThat(branch.orElseThrow().toOptional()).isEmpty(); } @@ -243,6 +232,12 @@ class GitDBTest implements WithAssertions { return GitDB.initLocal(dbDir, userName, userEmailAddress); } + private Function>> selectBranch(final String branchName) { + return db -> db.branch(branchName); + } + + // Given a valid GitDbBranch handle + // When select a valid branch then a GitDbBranch is returned @Test void selectBranch_branchExists_thenReturnBranch() throws Throwable { @@ -250,13 +245,11 @@ class GitDBTest implements WithAssertions { final Path dbDir = dirDoesNotExist(); final Result gitDb = gitDB(dbDir); //when - final Result> branch = gitDb.flatMap(db -> db.branch("master")); + final Result> branch = gitDb.flatMap(selectBranch("master")); //then assertThat(branch.orElseThrow().toOptional()).as("Branch master exists").isNotEmpty(); } - // Given a valid GitDbBranch handle - // When getting a key that does not exist then return an empty Optional @Test void getKey_whenKeyNotExist_thenReturnEmptyOptional() throws IOException { @@ -273,18 +266,9 @@ class GitDBTest implements WithAssertions { private GitDBBranch gitDBBranch() { try { - final Result gitDBResult = gitDB(dirDoesNotExist()); - System.out.println("gitDBResult = " + gitDBResult); - final Result> master = gitDBResult.flatMap(db -> { - final Result> maybeResult = db.branch("master"); - System.out.println("maybeResult = " + maybeResult); - return maybeResult; - }); - assert master != null; - System.out.println("master = " + master); - final Maybe gitDBBranchMaybe = master.orElseThrow(); - final GitDBBranch gitDBBranch = gitDBBranchMaybe.orElse(null); - return gitDBBranch; + return gitDB(dirDoesNotExist()) + .flatMap(selectBranch("master")) + .orElseThrow().orElse(null); } catch (Throwable throwable) { throw new RuntimeException("Couldn't create master branch", throwable); } @@ -321,8 +305,6 @@ class GitDBTest implements WithAssertions { success -> assertThat(success).isNotNull().isNotSameAs(originalBranch), failOnError() ); - assertThat(updatedBranch).isNotNull() - .isNotSameAs(originalBranch); } // When getting a key that does exist then the value is returned inside an Optional