Add initRepo_whenDirIsFile test

This commit is contained in:
Paul Campbell 2018-06-01 07:31:53 +01:00
parent 51dcf05c5d
commit fbab2f27cc
5 changed files with 126 additions and 23 deletions

View file

@ -22,6 +22,7 @@
<junit.version>5.2.0</junit.version>
<assertj.version>3.10.0</assertj.version>
<mon.version>0.4.0</mon.version>
<mockito.version>2.18.3</mockito.version>
</properties>
<dependencies>
@ -59,6 +60,12 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>

View file

@ -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());
}
}

View file

@ -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> T get(Branch branch, Key key, Class<T> type) {

View file

@ -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 <tt>null</tt> value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public UnexpectedGitDbException(final String message, final Throwable cause) {
super(message, cause);
}
}

View file

@ -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