initial checkin
This commit is contained in:
51
src/test/java/me/brianlong/git/CommandUnitTest.java
Normal file
51
src/test/java/me/brianlong/git/CommandUnitTest.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package me.brianlong.git;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jgit.api.CloneCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.ListBranchCommand.ListMode;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CommandUnitTest {
|
||||
|
||||
private final static GitHubTestCredentials githubCreds = new GitHubTestCredentials();
|
||||
|
||||
private static File tmpdir;
|
||||
private static Git git;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws GitAPIException, IOException {
|
||||
tmpdir = new File(System.getProperty("java.io.tmpdir"), "git.tmp");
|
||||
tmpdir.mkdirs();
|
||||
|
||||
git = new CloneCommand()
|
||||
.setURI("git@github.com:bmlong137/env-docker-adbp.git")
|
||||
.setCredentialsProvider(githubCreds.toJGitCredentialsProvider())
|
||||
.setDirectory(tmpdir)
|
||||
.call();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
git.getRepository().close();
|
||||
git.close();
|
||||
|
||||
tmpdir.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lotsOfBranches() throws GitAPIException {
|
||||
List<Ref> remoteBranches = git.branchList().setListMode(ListMode.REMOTE).call();
|
||||
Assert.assertNotNull(remoteBranches);
|
||||
Assert.assertTrue(remoteBranches.size() > 5);
|
||||
}
|
||||
|
||||
}
|
11
src/test/java/me/brianlong/git/GitHubTestCredentials.java
Normal file
11
src/test/java/me/brianlong/git/GitHubTestCredentials.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package me.brianlong.git;
|
||||
|
||||
public class GitHubTestCredentials extends GitCredentials {
|
||||
|
||||
private static final long serialVersionUID = -4460599306349661667L;
|
||||
|
||||
public GitHubTestCredentials() {
|
||||
super(System.getProperty("github.username"), System.getProperty("github.token"));
|
||||
}
|
||||
|
||||
}
|
124
src/test/java/me/brianlong/git/LocalRepositoryCacheUnitTest.java
Normal file
124
src/test/java/me/brianlong/git/LocalRepositoryCacheUnitTest.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package me.brianlong.git;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.api.errors.InvalidRemoteException;
|
||||
import org.eclipse.jgit.api.errors.TransportException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class LocalRepositoryCacheUnitTest {
|
||||
|
||||
private final static GitHubTestCredentials githubCreds = new GitHubTestCredentials();
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
LocalRepositoryCache.getInstance().clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Since "host-does-not-exist.com" does not exist, an UnknownHostException
|
||||
* is thrown. It is wrapped inside a JGit TransportException.
|
||||
*/
|
||||
@Test(expected = TransportException.class)
|
||||
public void cacheBadHostRepo() throws GitAPIException {
|
||||
LocalRepositoryCache.getInstance().acquire("https://host-does-not-exist.com/bmlong137/does-not-exist.git");
|
||||
}
|
||||
|
||||
/**
|
||||
* Since "does-not-exist" isn't a public repo, it requires authentication
|
||||
* to even check to see if it is private. This causes a
|
||||
* NoRemoteRepositoryException which is wrapped inside a JGit
|
||||
* InvalidRemoteException. This is because SSH authentication makes this
|
||||
* more like cacheNonExistentRepoAuth() than cacheNonExistentRepo().
|
||||
*/
|
||||
@Test(expected = InvalidRemoteException.class)
|
||||
public void cacheNonExistentRepoViaSsh() throws GitAPIException {
|
||||
LocalRepositoryCache.getInstance().acquire("git@github.com:bmlong137/does-not-exist.git");
|
||||
}
|
||||
|
||||
/**
|
||||
* Since "does-not-exist" isn't a public repo, it requires authentication
|
||||
* to even check to see if it is private. This causes a JSchException
|
||||
* which is wrapped inside a JGit TransportException.
|
||||
*/
|
||||
@Test(expected = TransportException.class)
|
||||
public void cacheNonExistentRepo() throws GitAPIException {
|
||||
LocalRepositoryCache.getInstance().acquire("https://github.com/bmlong137/does-not-exist.git");
|
||||
}
|
||||
|
||||
/**
|
||||
* Since "does-not-exist" isn't a repo, a NoRemoteRepositoryException is
|
||||
* thrown. It is wrapped inside a JGit InvalidRemoteException.
|
||||
*/
|
||||
@Test(expected = InvalidRemoteException.class)
|
||||
public void cacheNonExistentRepoAuth() throws GitAPIException {
|
||||
LocalRepositoryCache.getInstance().acquire("https://github.com/bmlong137/does-not-exist.git", githubCreds);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cachePublicRepo() throws GitAPIException {
|
||||
Git git = LocalRepositoryCache.getInstance().acquire("https://github.com/bmlong137/maven-file-management.git");
|
||||
try {
|
||||
this.validateGenericGitRepo(git);
|
||||
} finally {
|
||||
git.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cachePublicRepoViaSsh() throws GitAPIException {
|
||||
Git git = LocalRepositoryCache.getInstance().acquire("git@github.com:bmlong137/maven-file-management.git");
|
||||
try {
|
||||
this.validateGenericGitRepo(git);
|
||||
} finally {
|
||||
git.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Since "github-api" isn't a public repo, it requires authentication
|
||||
* to even check to see if it is private. This causes a JSchException
|
||||
* which is wrapped inside a JGit TransportException.
|
||||
*/
|
||||
@Test(expected = TransportException.class)
|
||||
public void cachePrivateRepoUnauth() throws GitAPIException {
|
||||
LocalRepositoryCache.getInstance().acquire("https://github.com/bmlong137/github-api.git");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cachePrivateRepo() throws GitAPIException {
|
||||
Git git = LocalRepositoryCache.getInstance().acquire("https://github.com/bmlong137/github-api.git", githubCreds);
|
||||
try {
|
||||
this.validateGenericGitRepo(git);
|
||||
} finally {
|
||||
git.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cachePublicRepoBranch() throws GitAPIException, IOException {
|
||||
Git git = LocalRepositoryCache.getInstance().acquire("https://github.com/bmlong137/maven-file-management.git", "master");
|
||||
try {
|
||||
this.validateGenericGitRepo(git);
|
||||
|
||||
Assert.assertEquals("master", git.getRepository().getBranch());
|
||||
} finally {
|
||||
git.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void validateGenericGitRepo(Git git) throws GitAPIException {
|
||||
Assert.assertNotNull(git);
|
||||
|
||||
Repository repo = git.getRepository();
|
||||
Assert.assertTrue(repo.getDirectory().exists());
|
||||
Assert.assertTrue(repo.getWorkTree().exists());
|
||||
Assert.assertTrue(repo.getWorkTree().listFiles().length > 0);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user