]> source.dussan.org Git - gitblit.git/commitdiff
Created static repository close functions for unit testing and fixed Windows sharing...
authorJames Moger <james.moger@gitblit.com>
Thu, 11 Oct 2012 21:11:18 +0000 (17:11 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 11 Oct 2012 21:11:18 +0000 (17:11 -0400)
tests/com/gitblit/tests/GitBlitSuite.java
tests/com/gitblit/tests/GitServletTest.java

index 22bcf137c24ba3d282e0313e9f38b8999aa9d2a6..3527d5838da51f4a9f23d3ac8bbf02b841f4072d 100644 (file)
 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
@@ -189,4 +195,36 @@ public class GitBlitSuite {
                        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
index 09e0e5ad28961faf3a2445c5a3381e1aaf1b2efe..52dddc40815d1e19991d991f03517532f1521839 100644 (file)
@@ -67,15 +67,19 @@ public class GitServletTest {
        \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
@@ -88,7 +92,7 @@ public class GitServletTest {
                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
@@ -108,7 +112,7 @@ public class GitServletTest {
                        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
@@ -143,7 +147,7 @@ public class GitServletTest {
                        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
@@ -165,7 +169,7 @@ public class GitServletTest {
                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
@@ -190,7 +194,7 @@ public class GitServletTest {
                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
@@ -201,7 +205,7 @@ public class GitServletTest {
                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
@@ -213,7 +217,7 @@ public class GitServletTest {
                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
@@ -224,7 +228,7 @@ public class GitServletTest {
                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
@@ -241,7 +245,7 @@ public class GitServletTest {
                } 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
@@ -296,7 +300,7 @@ public class GitServletTest {
                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
@@ -331,13 +335,16 @@ public class GitServletTest {
                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
@@ -359,9 +366,12 @@ public class GitServletTest {
                        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
@@ -372,8 +382,11 @@ public class GitServletTest {
                        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
@@ -389,8 +402,11 @@ public class GitServletTest {
                        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
@@ -408,8 +424,11 @@ public class GitServletTest {
                        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
@@ -445,17 +464,11 @@ public class GitServletTest {
                                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