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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.GitBlitException;
  34. import com.gitblit.GitBlitServer;
  35. import com.gitblit.manager.IRepositoryManager;
  36. import com.gitblit.models.RepositoryModel;
  37. import com.gitblit.servlet.GitblitContext;
  38. import com.gitblit.utils.JGitUtils;
  39. /**
  40. * The GitBlitSuite uses test-gitblit.properties and test-users.conf. The suite
  41. * is fairly comprehensive for all lower-level functionality. Wicket pages are
  42. * currently not unit-tested.
  43. *
  44. * This suite starts a Gitblit server instance within the same JVM instance as
  45. * the unit tests. This allows the unit tests to access the GitBlit static
  46. * singleton while also being able to communicate with the instance via tcp/ip
  47. * for testing rpc requests, federation requests, and git servlet operations.
  48. *
  49. * @author James Moger
  50. *
  51. */
  52. @RunWith(Suite.class)
  53. @SuiteClasses({ ArrayUtilsTest.class, FileUtilsTest.class, TimeUtilsTest.class,
  54. StringUtilsTest.class, Base64Test.class, JsonUtilsTest.class, ByteFormatTest.class,
  55. ObjectCacheTest.class, PermissionsTest.class, UserServiceTest.class, LdapAuthenticationTest.class,
  56. MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class,
  57. DiffUtilsTest.class, MetricUtilsTest.class, X509UtilsTest.class,
  58. GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class, GitDaemonTest.class,
  59. GroovyScriptTest.class, LuceneExecutorTest.class, RepositoryModelTest.class,
  60. FanoutServiceTest.class, Issue0259Test.class, Issue0271Test.class, HtpasswdAuthenticationTest.class,
  61. ModelUtilsTest.class, JnaUtilsTest.class })
  62. public class GitBlitSuite {
  63. public static final File BASEFOLDER = new File("data");
  64. public static final File REPOSITORIES = new File("data/git");
  65. public static final File SETTINGS = new File("src/test/config/test-gitblit.properties");
  66. public static final File USERSCONF = new File("src/test/config/test-users.conf");
  67. static int port = 8280;
  68. static int gitPort = 8300;
  69. static int shutdownPort = 8281;
  70. public static String url = "http://localhost:" + port;
  71. public static String gitServletUrl = "http://localhost:" + port + "/git";
  72. public static String gitDaemonUrl = "git://localhost:" + gitPort;
  73. public static String account = "admin";
  74. public static String password = "admin";
  75. private static AtomicBoolean started = new AtomicBoolean(false);
  76. public static Repository getHelloworldRepository() {
  77. return getRepository("helloworld.git");
  78. }
  79. public static Repository getTicgitRepository() {
  80. return getRepository("ticgit.git");
  81. }
  82. public static Repository getJGitRepository() {
  83. return getRepository("test/jgit.git");
  84. }
  85. public static Repository getAmbitionRepository() {
  86. return getRepository("test/ambition.git");
  87. }
  88. public static Repository getGitectiveRepository() {
  89. return getRepository("test/gitective.git");
  90. }
  91. private static Repository getRepository(String name) {
  92. try {
  93. File gitDir = FileKey.resolve(new File(REPOSITORIES, name), FS.DETECTED);
  94. Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
  95. return repository;
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. return null;
  100. }
  101. public static boolean startGitblit() throws Exception {
  102. if (started.get()) {
  103. // already started
  104. return false;
  105. }
  106. GitServletTest.deleteWorkingFolders();
  107. // Start a Gitblit instance
  108. Executors.newSingleThreadExecutor().execute(new Runnable() {
  109. @Override
  110. public void run() {
  111. GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
  112. "" + shutdownPort, "--gitPort", "" + gitPort, "--repositoriesFolder",
  113. "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
  114. GitBlitSuite.USERSCONF.getAbsolutePath(), "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),
  115. "--baseFolder", "data");
  116. }
  117. });
  118. // Wait a few seconds for it to be running
  119. Thread.sleep(5000);
  120. started.set(true);
  121. return true;
  122. }
  123. public static void stopGitblit() throws Exception {
  124. // Stop Gitblit
  125. GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
  126. // Wait a few seconds for it to be running
  127. Thread.sleep(5000);
  128. }
  129. @BeforeClass
  130. public static void setUp() throws Exception {
  131. startGitblit();
  132. if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
  133. cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
  134. cloneOrFetch("ticgit.git", "https://github.com/schacon/ticgit.git");
  135. cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
  136. cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
  137. cloneOrFetch("test/ambition.git", "https://github.com/defunkt/ambition.git");
  138. cloneOrFetch("test/gitective.git", "https://github.com/kevinsawicki/gitective.git");
  139. showRemoteBranches("ticgit.git");
  140. automaticallyTagBranchTips("ticgit.git");
  141. showRemoteBranches("test/jgit.git");
  142. automaticallyTagBranchTips("test/jgit.git");
  143. }
  144. }
  145. @AfterClass
  146. public static void tearDown() throws Exception {
  147. stopGitblit();
  148. }
  149. private static void cloneOrFetch(String name, String fromUrl) throws Exception {
  150. System.out.print("Fetching " + name + "... ");
  151. try {
  152. JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
  153. } catch (Throwable t) {
  154. System.out.println("Error: " + t.getMessage());
  155. }
  156. System.out.println("done.");
  157. }
  158. private static void showRemoteBranches(String repositoryName) {
  159. try {
  160. IRepositoryManager repositoryManager = GitblitContext.getManager(IRepositoryManager.class);
  161. RepositoryModel model = repositoryManager.getRepositoryModel(repositoryName);
  162. model.showRemoteBranches = true;
  163. repositoryManager.updateRepositoryModel(model.name, model, false);
  164. } catch (GitBlitException g) {
  165. g.printStackTrace();
  166. }
  167. }
  168. private static void automaticallyTagBranchTips(String repositoryName) {
  169. try {
  170. IRepositoryManager repositoryManager = GitblitContext.getManager(IRepositoryManager.class);
  171. RepositoryModel model = repositoryManager.getRepositoryModel(repositoryName);
  172. model.useIncrementalPushTags = true;
  173. repositoryManager.updateRepositoryModel(model.name, model, false);
  174. } catch (GitBlitException g) {
  175. g.printStackTrace();
  176. }
  177. }
  178. public static void close(File repository) {
  179. try {
  180. File gitDir = FileKey.resolve(repository, FS.detect());
  181. if (gitDir != null && gitDir.exists()) {
  182. close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));
  183. }
  184. } catch (Exception e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. public static void close(Git git) {
  189. close(git.getRepository());
  190. }
  191. public static void close(Repository r) {
  192. RepositoryCache.close(r);
  193. // assume 2 uses in case reflection fails
  194. int uses = 2;
  195. try {
  196. Field useCnt = Repository.class.getDeclaredField("useCnt");
  197. useCnt.setAccessible(true);
  198. uses = ((AtomicInteger) useCnt.get(r)).get();
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. }
  202. for (int i = 0; i < uses; i++) {
  203. r.close();
  204. }
  205. }
  206. }