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