]> source.dussan.org Git - gitblit.git/commitdiff
Simplified git servlet unit tests and cleanup working clones afterwards
authorJames Moger <james.moger@gitblit.com>
Sat, 7 Jan 2012 20:53:17 +0000 (15:53 -0500)
committerJames Moger <james.moger@gitblit.com>
Sat, 7 Jan 2012 20:53:17 +0000 (15:53 -0500)
tests/com/gitblit/tests/GitBlitTest.java
tests/com/gitblit/tests/GitServletTest.java

index 00b5854667213185ab0c984b263c22f32310d9d2..28603f87b92050ccc2b17d0cc049497a0c0bbf9f 100644 (file)
@@ -43,7 +43,7 @@ public class GitBlitTest {
                RepositoryModel model = GitBlit.self().getRepositoryModel(\r
                                GitBlitSuite.getHelloworldRepository().getDirectory().getName());\r
                assertTrue("Helloworld model is null!", model != null);\r
-               assertEquals(GitBlitSuite.getHelloworldRepository().getDirectory().getName(), model.toString());\r
+               assertEquals(GitBlitSuite.getHelloworldRepository().getDirectory().getName(), model.name);\r
                assertTrue(GitBlit.self().calculateSize(model) > 22000L);\r
        }\r
 \r
index a4c5e882cb4541c739990a3467ec1fa70f367cff..88bbe91741b0a286719aec577d8f9f02bb6c4d7e 100644 (file)
@@ -25,9 +25,11 @@ import com.gitblit.models.RepositoryModel;
 \r
 public class GitServletTest {\r
 \r
-       File folder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit");\r
+       static File ticgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit");\r
        \r
-       File jgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/jgit");\r
+       static File ticgit2Folder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit2");\r
+\r
+       static File jgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/jgit");\r
 \r
        String url = GitBlitSuite.url;\r
        String account = GitBlitSuite.account;\r
@@ -37,6 +39,7 @@ public class GitServletTest {
 \r
        @BeforeClass\r
        public static void startGitblit() throws Exception {\r
+               deleteWorkingFolders();\r
                started.set(GitBlitSuite.startGitblit());\r
        }\r
 \r
@@ -45,20 +48,30 @@ public class GitServletTest {
                if (started.get()) {\r
                        GitBlitSuite.stopGitblit();\r
                }\r
+               deleteWorkingFolders();\r
+       }\r
+       \r
+       private static void deleteWorkingFolders() throws Exception {\r
+               if (ticgitFolder.exists()) {\r
+                       FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE);\r
+               }\r
+               if (ticgit2Folder.exists()) {\r
+                       FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);\r
+               }\r
+               if (jgitFolder.exists()) {\r
+                       FileUtils.delete(jgitFolder, FileUtils.RECURSIVE);\r
+               }\r
        }\r
 \r
        @Test\r
        public void testClone() throws Exception {\r
-               if (folder.exists()) {\r
-                       FileUtils.delete(folder, FileUtils.RECURSIVE);\r
-               }\r
                CloneCommand clone = Git.cloneRepository();\r
                clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));\r
-               clone.setDirectory(folder);\r
+               clone.setDirectory(ticgitFolder);\r
                clone.setBare(false);\r
                clone.setCloneAllBranches(true);\r
                clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));\r
-               clone.call();\r
+               close(clone.call());            \r
                assertTrue(true);\r
        }\r
 \r
@@ -69,20 +82,16 @@ public class GitServletTest {
                model.accessRestriction = AccessRestrictionType.CLONE;\r
                GitBlit.self().updateRepositoryModel(model.name, model, false);\r
 \r
-               // delete any existing working folder\r
-               File folder = new File(GitBlitSuite.REPOSITORIES, "working/gitblit");\r
-               if (folder.exists()) {\r
-                       FileUtils.delete(folder, FileUtils.RECURSIVE);\r
-               }\r
+               // delete any existing working folder           \r
                boolean cloned = false;\r
                try {\r
                        CloneCommand clone = Git.cloneRepository();\r
                        clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));\r
-                       clone.setDirectory(folder);\r
+                       clone.setDirectory(ticgit2Folder);\r
                        clone.setBare(false);\r
                        clone.setCloneAllBranches(true);\r
                        clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("bogus", "bogus"));\r
-                       clone.call();\r
+                       close(clone.call());\r
                        cloned = true;\r
                } catch (Exception e) {\r
                        // swallow the exception which we expect\r
@@ -97,45 +106,46 @@ public class GitServletTest {
 \r
        @Test\r
        public void testAnonymousPush() throws Exception {\r
-               Git git = Git.open(folder);\r
-               File file = new File(folder, "TODO");\r
-               for (int i = 0; i < 3; i++) {\r
-                       OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
-                       BufferedWriter w = new BufferedWriter(os);\r
-                       w.write("// " + new Date().toString() + "\n");\r
-                       w.close();\r
-                       git.add().addFilepattern(file.getName()).call();\r
-                       git.commit().setMessage("test commit #" + (i + 1)).call();\r
-               }\r
+               Git git = Git.open(ticgitFolder);\r
+               File file = new File(ticgitFolder, "TODO");\r
+               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
+               BufferedWriter w = new BufferedWriter(os);\r
+               w.write("// " + new Date().toString() + "\n");\r
+               w.close();\r
+               git.add().addFilepattern(file.getName()).call();\r
+               git.commit().setMessage("test commit").call();\r
                git.push().setPushAll().call();\r
-               git.getRepository().close();\r
+               close(git);\r
        }\r
-       \r
+\r
        @Test\r
        public void testSubfolderPush() throws Exception {\r
-               if (jgitFolder.exists()) {\r
-                       FileUtils.delete(jgitFolder, FileUtils.RECURSIVE);\r
-               }\r
                CloneCommand clone = Git.cloneRepository();\r
                clone.setURI(MessageFormat.format("{0}/git/test/jgit.git", url));\r
                clone.setDirectory(jgitFolder);\r
                clone.setBare(false);\r
                clone.setCloneAllBranches(true);\r
                clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));\r
-               clone.call();\r
+               close(clone.call());\r
                assertTrue(true);\r
-               \r
+\r
                Git git = Git.open(jgitFolder);\r
                File file = new File(jgitFolder, "TODO");\r
-               for (int i = 0; i < 3; i++) {\r
-                       OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
-                       BufferedWriter w = new BufferedWriter(os);\r
-                       w.write("// " + new Date().toString() + "\n");\r
-                       w.close();\r
-                       git.add().addFilepattern(file.getName()).call();\r
-                       git.commit().setMessage("test commit #" + (i + 1)).call();\r
-               }\r
+               OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));\r
+               BufferedWriter w = new BufferedWriter(os);\r
+               w.write("// " + new Date().toString() + "\n");\r
+               w.close();\r
+               git.add().addFilepattern(file.getName()).call();\r
+               git.commit().setMessage("test commit").call();\r
                git.push().setPushAll().call();\r
-               git.getRepository().close();\r
+               close(git);\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
        }\r
 }\r