You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitBlitSuite.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.tests;
  17. import java.io.File;
  18. import java.lang.reflect.Field;
  19. import java.util.concurrent.Executors;
  20. import java.util.concurrent.atomic.AtomicBoolean;
  21. import java.util.concurrent.atomic.AtomicInteger;
  22. import org.eclipse.jgit.api.Git;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.eclipse.jgit.lib.RepositoryCache;
  25. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  26. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  27. import org.eclipse.jgit.util.FS;
  28. import org.junit.AfterClass;
  29. import org.junit.BeforeClass;
  30. import org.junit.runner.RunWith;
  31. import org.junit.runners.Suite;
  32. import org.junit.runners.Suite.SuiteClasses;
  33. import com.gitblit.GitBlit;
  34. import com.gitblit.GitBlitException;
  35. import com.gitblit.GitBlitServer;
  36. import com.gitblit.models.RepositoryModel;
  37. import com.gitblit.utils.JGitUtils;
  38. /**
  39. * The GitBlitSuite uses test-gitblit.properties and test-users.conf. The suite
  40. * is fairly comprehensive for all lower-level functionality. Wicket pages are
  41. * currently not unit-tested.
  42. *
  43. * This suite starts a Gitblit server instance within the same JVM instance as
  44. * the unit tests. This allows the unit tests to access the GitBlit static
  45. * singleton while also being able to communicate with the instance via tcp/ip
  46. * for testing rpc requests, federation requests, and git servlet operations.
  47. *
  48. * @author James Moger
  49. *
  50. */
  51. @RunWith(Suite.class)
  52. @SuiteClasses({ ArrayUtilsTest.class, FileUtilsTest.class, TimeUtilsTest.class,
  53. StringUtilsTest.class, Base64Test.class, JsonUtilsTest.class, ByteFormatTest.class,
  54. ObjectCacheTest.class, PermissionsTest.class, UserServiceTest.class, LdapUserServiceTest.class,
  55. MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class,
  56. DiffUtilsTest.class, MetricUtilsTest.class, X509UtilsTest.class,
  57. GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class, GitDaemonTest.class,
  58. GroovyScriptTest.class, LuceneExecutorTest.class, RepositoryModelTest.class,
  59. FanoutServiceTest.class, Issue0259Test.class, Issue0271Test.class, HtpasswdUserServiceTest.class,
  60. ModelUtilsTest.class, JnaUtilsTest.class })
  61. public class GitBlitSuite {
  62. public static final File REPOSITORIES = new File("data/git");
  63. public static final File SETTINGS = new File("src/test/config/test-gitblit.properties");
  64. public static final File USERSCONF = new File("src/test/config/test-users.conf");
  65. static int port = 8280;
  66. static int gitPort = 8300;
  67. static int shutdownPort = 8281;
  68. public static String url = "http://localhost:" + port;
  69. public static String gitServletUrl = "http://localhost:" + port + "/git";
  70. public static String gitDaemonUrl = "git://localhost:" + gitPort;
  71. public static String account = "admin";
  72. public static String password = "admin";
  73. private static AtomicBoolean started = new AtomicBoolean(false);
  74. public static Repository getHelloworldRepository() throws Exception {
  75. return getRepository("helloworld.git");
  76. }
  77. public static Repository getTicgitRepository() throws Exception {
  78. return getRepository("ticgit.git");
  79. }
  80. public static Repository getJGitRepository() throws Exception {
  81. return getRepository("test/jgit.git");
  82. }
  83. public static Repository getAmbitionRepository() throws Exception {
  84. return getRepository("test/ambition.git");
  85. }
  86. public static Repository getGitectiveRepository() throws Exception {
  87. return getRepository("test/gitective.git");
  88. }
  89. private static Repository getRepository(String name) throws Exception {
  90. File gitDir = FileKey.resolve(new File(REPOSITORIES, name), FS.DETECTED);
  91. Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
  92. return repository;
  93. }
  94. public static boolean startGitblit() throws Exception {
  95. if (started.get()) {
  96. // already started
  97. return false;
  98. }
  99. GitServletTest.deleteWorkingFolders();
  100. // Start a Gitblit instance
  101. Executors.newSingleThreadExecutor().execute(new Runnable() {
  102. @Override
  103. public void run() {
  104. GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
  105. "" + shutdownPort, "--gitPort", "" + gitPort, "--repositoriesFolder",
  106. "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
  107. GitBlitSuite.USERSCONF.getAbsolutePath(), "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),
  108. "--baseFolder", "data");
  109. }
  110. });
  111. // Wait a few seconds for it to be running
  112. Thread.sleep(5000);
  113. started.set(true);
  114. return true;
  115. }
  116. public static void stopGitblit() throws Exception {
  117. // Stop Gitblit
  118. GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
  119. // Wait a few seconds for it to be running
  120. Thread.sleep(5000);
  121. }
  122. @BeforeClass
  123. public static void setUp() throws Exception {
  124. startGitblit();
  125. if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
  126. cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
  127. cloneOrFetch("ticgit.git", "https://github.com/schacon/ticgit.git");
  128. cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
  129. cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
  130. cloneOrFetch("test/ambition.git", "https://github.com/defunkt/ambition.git");
  131. cloneOrFetch("test/gitective.git", "https://github.com/kevinsawicki/gitective.git");
  132. showRemoteBranches("ticgit.git");
  133. automaticallyTagBranchTips("ticgit.git");
  134. showRemoteBranches("test/jgit.git");
  135. automaticallyTagBranchTips("test/jgit.git");
  136. }
  137. }
  138. @AfterClass
  139. public static void tearDown() throws Exception {
  140. stopGitblit();
  141. }
  142. private static void cloneOrFetch(String name, String fromUrl) throws Exception {
  143. System.out.print("Fetching " + name + "... ");
  144. try {
  145. JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
  146. } catch (Throwable t) {
  147. System.out.println("Error: " + t.getMessage());
  148. }
  149. System.out.println("done.");
  150. }
  151. private static void showRemoteBranches(String repositoryName) {
  152. try {
  153. RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
  154. model.showRemoteBranches = true;
  155. GitBlit.self().updateRepositoryModel(model.name, model, false);
  156. } catch (GitBlitException g) {
  157. g.printStackTrace();
  158. }
  159. }
  160. private static void automaticallyTagBranchTips(String repositoryName) {
  161. try {
  162. RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
  163. model.useIncrementalPushTags = true;
  164. GitBlit.self().updateRepositoryModel(model.name, model, false);
  165. } catch (GitBlitException g) {
  166. g.printStackTrace();
  167. }
  168. }
  169. public static void close(File repository) {
  170. try {
  171. File gitDir = FileKey.resolve(repository, FS.detect());
  172. if (gitDir != null && gitDir.exists()) {
  173. close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));
  174. }
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. }
  178. }
  179. public static void close(Git git) {
  180. close(git.getRepository());
  181. }
  182. public static void close(Repository r) {
  183. RepositoryCache.close(r);
  184. // assume 2 uses in case reflection fails
  185. int uses = 2;
  186. try {
  187. Field useCnt = Repository.class.getDeclaredField("useCnt");
  188. useCnt.setAccessible(true);
  189. uses = ((AtomicInteger) useCnt.get(r)).get();
  190. } catch (Exception e) {
  191. e.printStackTrace();
  192. }
  193. for (int i = 0; i < uses; i++) {
  194. r.close();
  195. }
  196. }
  197. }