From fbab2f27ccd1b9bacc0c8513795d846c3f1b9c88 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 1 Jun 2018 07:31:53 +0100 Subject: [PATCH] Add initRepo_whenDirIsFile test --- pom.xml | 7 +++ src/main/java/net/kemitix/gitdb/GitDB.java | 11 ++-- .../java/net/kemitix/gitdb/GitDBLocal.java | 27 ++++++++- .../gitdb/UnexpectedGitDbException.java | 45 ++++++++++++++ .../java/net/kemitix/gitdb/GitDBTest.java | 59 ++++++++++++++----- 5 files changed, 126 insertions(+), 23 deletions(-) create mode 100644 src/main/java/net/kemitix/gitdb/UnexpectedGitDbException.java diff --git a/pom.xml b/pom.xml index 929f227..8f3a377 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ 5.2.0 3.10.0 0.4.0 + 2.18.3 @@ -59,6 +60,12 @@ junit-jupiter-engine runtime + + org.mockito + mockito-junit-jupiter + ${mockito.version} + test + diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 201f326..25d5302 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -22,9 +22,8 @@ package net.kemitix.gitdb; import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.api.InitCommand; -import org.eclipse.jgit.api.errors.GitAPIException; +import java.nio.file.NotDirectoryException; import java.nio.file.Path; /** @@ -39,12 +38,10 @@ public interface GitDB { * * @param dbDir the path to initialise the local repo in * @return a GitDB instance for the created local gitdb - * @throws GitAPIException if there is an error initialising the Git repo + * @throws NotDirectoryException if {@code dbDir} it is not a directory */ - static GitDB initLocal(final Path dbDir) throws GitAPIException { - final InitCommand initCommand = Git.init().setGitDir(dbDir.toFile()).setBare(true); - final Git git = initCommand.call(); - return new GitDBLocal(git); + static GitDB initLocal(final Path dbDir) throws NotDirectoryException { + return new GitDBLocal(Git.init(), dbDir.toFile()); } } diff --git a/src/main/java/net/kemitix/gitdb/GitDBLocal.java b/src/main/java/net/kemitix/gitdb/GitDBLocal.java index df29913..290bccd 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBLocal.java +++ b/src/main/java/net/kemitix/gitdb/GitDBLocal.java @@ -21,19 +21,42 @@ package net.kemitix.gitdb; -import lombok.RequiredArgsConstructor; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.InitCommand; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.api.errors.JGitInternalException; + +import java.io.File; +import java.nio.file.NotDirectoryException; /** * Implementation of GitDB for working with a local Repo. * * @author Paul Campbell (pcampbell@kemitix.net) */ -@RequiredArgsConstructor + class GitDBLocal implements GitDB { private final Git git; + /** + * Constructors a new instance of this class. + * + * @param initCommand a JGit InitCommand + * @param dbDir the path to instantiate the git repo in + * @throws NotDirectoryException if {@code dbDir} it is not a directory + */ + @SuppressWarnings("avoidhidingcauseexception") + GitDBLocal(final InitCommand initCommand, final File dbDir) throws NotDirectoryException { + try { + this.git = initCommand.setGitDir(dbDir).setBare(true).call(); + } catch (JGitInternalException e) { + throw new NotDirectoryException(dbDir.toString()); + } catch (GitAPIException e) { + throw new UnexpectedGitDbException("Unhandled Git API Exception", e); + } + } + // @Override // @SneakyThrows // public T get(Branch branch, Key key, Class type) { diff --git a/src/main/java/net/kemitix/gitdb/UnexpectedGitDbException.java b/src/main/java/net/kemitix/gitdb/UnexpectedGitDbException.java new file mode 100644 index 0000000..a6a32d8 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/UnexpectedGitDbException.java @@ -0,0 +1,45 @@ +/* + The MIT License (MIT) + + Copyright (c) 2018 Paul Campbell + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software + and associated documentation files (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies + or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.kemitix.gitdb; + +/** + * Unchecked exception thrown when JGit throws a an unexpected exception. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +public class UnexpectedGitDbException extends RuntimeException { + + /** + * Constructs an instance of this class. + * + * @param message the detail message. The detail message is saved for + * later retrieval by the {@link #getMessage()} method. + * @param cause the cause (which is saved for later retrieval by the + * {@link #getCause()} method). (A null value is + * permitted, and indicates that the cause is nonexistent or + * unknown.) + */ + public UnexpectedGitDbException(final String message, final Throwable cause) { + super(message, cause); + } + +} diff --git a/src/test/java/net/kemitix/gitdb/GitDBTest.java b/src/test/java/net/kemitix/gitdb/GitDBTest.java index f391bf7..325c812 100644 --- a/src/test/java/net/kemitix/gitdb/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/GitDBTest.java @@ -1,17 +1,24 @@ package net.kemitix.gitdb; +import org.assertj.core.api.WithAssertions; +import org.eclipse.jgit.api.InitCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; import java.io.IOException; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; +import java.nio.file.*; import java.util.List; -import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; -class GitDBTest { +@ExtendWith(MockitoExtension.class) +class GitDBTest implements WithAssertions { // private final Branch master = Branch.name("master"); // private final Message message = Message.message(UUID.randomUUID().toString()); @@ -20,13 +27,13 @@ class GitDBTest { // When initialising a repo in a dir that doesn't exist then a bare repo is created @Test - void initRepo_whenDirNotExist_thenCreateBareRepo() throws IOException, GitAPIException { + void initRepo_whenDirNotExist_thenCreateBareRepo() throws IOException { //given final Path dir = dirDoesNotExist(); //when - final GitDB gitdb = GitDB.initLocal(dir); + final GitDB gitDB = GitDB.initLocal(dir); //then - assertThat(gitdb).isNotNull(); + assertThat(gitDB).isNotNull(); assertThatIsBareRepo(dir); } @@ -52,19 +59,43 @@ class GitDBTest { return directory; } + // When initialising a repo and an unexpected error occurs then an exception is thrown + @Test + void initRepo_whenUnexpectedError_thenThrowException() throws IOException, GitAPIException { + //given + final Path dbDir = dirDoesNotExist(); + final InitCommand initCommand = spy(InitCommand.class); + given(initCommand.call()).willThrow(mock(GitAPIException.class)); + //then + assertThatExceptionOfType(UnexpectedGitDbException.class) + .isThrownBy(() -> new GitDBLocal(initCommand, dbDir.toFile())) + .withMessageContaining("Unhandled Git API Exception"); + } + // When initialising a repo in a dir that is a file then an exception is thrown @Test - void initRepo_whenDirIsFile_thenThrowException() { + void initRepo_whenDirIsFile_thenThrowException() throws IOException { + //given + final Path dir = fileExists(); + //then + assertThatExceptionOfType(NotDirectoryException.class) + .isThrownBy(() -> GitDB.initLocal(dir)) + .withMessageContaining(dir.toString()); + } + + private Path fileExists() throws IOException { + return Files.createTempFile("gitdb", "file"); } // When initialising a repo in a dir is exists then an exception is thrown - @Test - void initRepo_whenDirExists_thenThrowException() { - } + //@Test + //void initRepo_whenDirExists_thenThrowException() { + //} - // When opening a repo in a dir that doesn't exist then an exception is thrown - // When opening a repo in a dir that is a file then an exception is thrown // When opening a repo in a dir that is not a bare repo then an exception is thrown + // When opening a repo in a dir that is a file then an exception is thrown + // When opening a repo in a dir that doesn't exist then an exception is thrown + // When opening a repo in a dir that is a bare repo then GitDb is returned // Given a valid GitDb handle