package com.gitblit.tests;\r
\r
import java.io.File;\r
+import java.lang.reflect.Field;\r
import java.util.concurrent.Executors;\r
import java.util.concurrent.atomic.AtomicBoolean;\r
+import java.util.concurrent.atomic.AtomicInteger;\r
\r
+import org.eclipse.jgit.api.Git;\r
import org.eclipse.jgit.lib.Repository;\r
+import org.eclipse.jgit.lib.RepositoryCache;\r
+import org.eclipse.jgit.lib.RepositoryCache.FileKey;\r
import org.eclipse.jgit.storage.file.FileRepository;\r
+import org.eclipse.jgit.util.FS;\r
import org.junit.AfterClass;\r
import org.junit.BeforeClass;\r
import org.junit.runner.RunWith;\r
g.printStackTrace();\r
}\r
}\r
+ \r
+ public static void close(File repository) {\r
+ try {\r
+ File gitDir = FileKey.resolve(repository, FS.detect());\r
+ if (gitDir != null && gitDir.exists()) {\r
+ close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));\r
+ }\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ public static void close(Git git) {\r
+ close(git.getRepository());\r
+ }\r
+ \r
+ public static void close(Repository r) {\r
+ RepositoryCache.close(r);\r
+\r
+ // assume 2 uses in case reflection fails\r
+ int uses = 2;\r
+ try {\r
+ Field useCnt = Repository.class.getDeclaredField("useCnt");\r
+ useCnt.setAccessible(true);\r
+ uses = ((AtomicInteger) useCnt.get(r)).get();\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ }\r
+ for (int i = 0; i < uses; i++) {\r
+ r.close();\r
+ }\r
+ }\r
}\r
\r
public static void deleteWorkingFolders() throws Exception {\r
if (ticgitFolder.exists()) {\r
+ GitBlitSuite.close(ticgitFolder);\r
FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE);\r
}\r
if (ticgit2Folder.exists()) {\r
+ GitBlitSuite.close(ticgit2Folder);\r
FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);\r
}\r
if (jgitFolder.exists()) {\r
+ GitBlitSuite.close(jgitFolder);\r
FileUtils.delete(jgitFolder, FileUtils.RECURSIVE);\r
}\r
if (jgit2Folder.exists()) {\r
+ GitBlitSuite.close(jgit2Folder);\r
FileUtils.delete(jgit2Folder, FileUtils.RECURSIVE);\r
}\r
}\r
clone.setBare(false);\r
clone.setCloneAllBranches(true);\r
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));\r
- close(clone.call()); \r
+ GitBlitSuite.close(clone.call()); \r
assertTrue(true);\r
}\r
\r
clone.setBare(false);\r
clone.setCloneAllBranches(true);\r
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("bogus", "bogus"));\r
- close(clone.call());\r
+ GitBlitSuite.close(clone.call());\r
cloned = true;\r
} catch (Exception e) {\r
// swallow the exception which we expect\r
clone.setBare(false);\r
clone.setCloneAllBranches(true);\r
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(user.username, user.password));\r
- close(clone.call());\r
+ GitBlitSuite.close(clone.call());\r
cloned = true;\r
} catch (Exception e) {\r
// swallow the exception which we expect\r
clone.setBare(false);\r
clone.setCloneAllBranches(true);\r
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(user.username, user.password));\r
- close(clone.call());\r
+ GitBlitSuite.close(clone.call());\r
cloned = true;\r
\r
assertTrue("Authenticated login could not clone!", cloned);\r
git.add().addFilepattern(file.getName()).call();\r
git.commit().setMessage("test commit").call();\r
git.push().setPushAll().call();\r
- close(git);\r
+ GitBlitSuite.close(git);\r
}\r
\r
@Test\r
clone.setBare(false);\r
clone.setCloneAllBranches(true);\r
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));\r
- close(clone.call());\r
+ GitBlitSuite.close(clone.call());\r
assertTrue(true);\r
\r
Git git = Git.open(jgitFolder);\r
git.add().addFilepattern(file.getName()).call();\r
git.commit().setMessage("test commit").call();\r
git.push().setPushAll().call();\r
- close(git);\r
+ GitBlitSuite.close(git);\r
}\r
\r
@Test\r
clone.setBare(false);\r
clone.setCloneAllBranches(true);\r
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));\r
- close(clone.call());\r
+ GitBlitSuite.close(clone.call());\r
assertTrue(true);\r
\r
Git git = Git.open(jgit2Folder);\r
} catch (Exception e) {\r
assertTrue(e.getCause().getMessage().contains("git-receive-pack not permitted"));\r
}\r
- close(git);\r
+ GitBlitSuite.close(git);\r
}\r
\r
@Test\r
clone.setBare(true);\r
clone.setCloneAllBranches(true);\r
clone.setCredentialsProvider(cp);\r
- close(clone.call());\r
+ GitBlitSuite.close(clone.call());\r
\r
// elevate repository to clone permission\r
RepositoryModel model = GitBlit.self().getRepositoryModel("refchecks/ticgit.git");\r
clone.setCredentialsProvider(cp);\r
\r
try {\r
- close(clone.call());\r
+ GitBlitSuite.close(clone.call());\r
} catch (GitAPIException e) {\r
if (permission.atLeast(AccessPermission.CLONE)) {\r
throw e;\r
} else {\r
+ // close serving repository\r
+ GitBlitSuite.close(refChecks);\r
+ \r
// user does not have clone permission\r
- assertTrue(e.getMessage(), e.getMessage().contains("not permitted")); \r
+ assertTrue(e.getMessage(), e.getMessage().contains("not permitted")); \r
return;\r
}\r
}\r
if (permission.atLeast(AccessPermission.PUSH)) {\r
throw e;\r
} else {\r
+ // close serving repository\r
+ GitBlitSuite.close(refChecks);\r
+ \r
// user does not have push permission\r
assertTrue(e.getMessage(), e.getMessage().contains("not permitted"));\r
- close(git);\r
+ GitBlitSuite.close(git);\r
return;\r
}\r
}\r
if (permission.atLeast(AccessPermission.PUSH)) {\r
assertTrue("User failed to push commit?! " + status.name(), Status.OK.equals(status));\r
} else {\r
+ // close serving repository\r
+ GitBlitSuite.close(refChecks);\r
+\r
assertTrue("User was able to push commit! " + status.name(), Status.REJECTED_OTHER_REASON.equals(status));\r
- close(git);\r
+ GitBlitSuite.close(git);\r
// skip delete test\r
return;\r
}\r
if (Status.OK.equals(expectedCreate)) {\r
assertTrue("User failed to push creation?! " + status.name(), status.equals(expectedCreate));\r
} else {\r
+ // close serving repository\r
+ GitBlitSuite.close(refChecks);\r
+\r
assertTrue("User was able to push ref creation! " + status.name(), status.equals(expectedCreate));\r
- close(git);\r
+ GitBlitSuite.close(git);\r
// skip delete test\r
return;\r
}\r
if (Status.OK.equals(expectedDelete)) {\r
assertTrue("User failed to push ref deletion?! " + status.name(), status.equals(Status.OK));\r
} else {\r
+ // close serving repository\r
+ GitBlitSuite.close(refChecks);\r
+\r
assertTrue("User was able to push ref deletion?! " + status.name(), status.equals(expectedDelete));\r
- close(git);\r
+ GitBlitSuite.close(git);\r
// skip rewind test\r
return;\r
}\r
assertTrue("User was able to rewind master?! " + status.name(), status.equals(expectedRewind));\r
}\r
}\r
- close(git);\r
+ GitBlitSuite.close(git);\r
\r
- GitBlit.self().deleteUser(user.username);\r
- }\r
+ // close serving repository\r
+ GitBlitSuite.close(refChecks);\r
\r
- \r
- private void close(Git git) {\r
- // really close the repository\r
- // decrement the use counter to 0\r
- for (int i = 0; i < 2; i++) {\r
- git.getRepository().close();\r
- }\r
+ GitBlit.self().deleteUser(user.username);\r
}\r
}\r