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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.FileRepository;
  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, TicgitUtilsTest.class, X509UtilsTest.class,
  57. GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class,
  58. GroovyScriptTest.class, LuceneExecutorTest.class, IssuesTest.class, RepositoryModelTest.class,
  59. FanoutServiceTest.class })
  60. public class GitBlitSuite {
  61. public static final File REPOSITORIES = new File("data/git");
  62. static int port = 8280;
  63. static int shutdownPort = 8281;
  64. public static String url = "http://localhost:" + port;
  65. public static String account = "admin";
  66. public static String password = "admin";
  67. private static AtomicBoolean started = new AtomicBoolean(false);
  68. public static Repository getHelloworldRepository() throws Exception {
  69. return new FileRepository(new File(REPOSITORIES, "helloworld.git"));
  70. }
  71. public static Repository getTicgitRepository() throws Exception {
  72. return new FileRepository(new File(REPOSITORIES, "ticgit.git"));
  73. }
  74. public static Repository getJGitRepository() throws Exception {
  75. return new FileRepository(new File(REPOSITORIES, "test/jgit.git"));
  76. }
  77. public static Repository getAmbitionRepository() throws Exception {
  78. return new FileRepository(new File(REPOSITORIES, "test/ambition.git"));
  79. }
  80. public static Repository getTheoreticalPhysicsRepository() throws Exception {
  81. return new FileRepository(new File(REPOSITORIES, "test/theoretical-physics.git"));
  82. }
  83. public static Repository getIssuesTestRepository() throws Exception {
  84. JGitUtils.createRepository(REPOSITORIES, "gb-issues.git").close();
  85. return new FileRepository(new File(REPOSITORIES, "gb-issues.git"));
  86. }
  87. public static Repository getGitectiveRepository() throws Exception {
  88. return new FileRepository(new File(REPOSITORIES, "test/gitective.git"));
  89. }
  90. public static boolean startGitblit() throws Exception {
  91. if (started.get()) {
  92. // already started
  93. return false;
  94. }
  95. GitServletTest.deleteWorkingFolders();
  96. // Start a Gitblit instance
  97. Executors.newSingleThreadExecutor().execute(new Runnable() {
  98. public void run() {
  99. GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
  100. "" + shutdownPort, "--repositoriesFolder",
  101. "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
  102. "test-users.conf", "--settings", "test-gitblit.properties",
  103. "--baseFolder", "data");
  104. }
  105. });
  106. // Wait a few seconds for it to be running
  107. Thread.sleep(2500);
  108. started.set(true);
  109. return true;
  110. }
  111. public static void stopGitblit() throws Exception {
  112. // Stop Gitblit
  113. GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
  114. // Wait a few seconds for it to be running
  115. Thread.sleep(5000);
  116. }
  117. @BeforeClass
  118. public static void setUp() throws Exception {
  119. startGitblit();
  120. if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
  121. cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
  122. cloneOrFetch("ticgit.git", "https://github.com/schacon/ticgit.git");
  123. cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
  124. cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
  125. cloneOrFetch("test/ambition.git", "https://github.com/defunkt/ambition.git");
  126. cloneOrFetch("test/theoretical-physics.git", "https://github.com/certik/theoretical-physics.git");
  127. cloneOrFetch("test/gitective.git", "https://github.com/kevinsawicki/gitective.git");
  128. enableTickets("ticgit.git");
  129. enableDocs("ticgit.git");
  130. showRemoteBranches("ticgit.git");
  131. showRemoteBranches("test/jgit.git");
  132. }
  133. }
  134. @AfterClass
  135. public static void tearDown() throws Exception {
  136. stopGitblit();
  137. }
  138. private static void cloneOrFetch(String name, String fromUrl) throws Exception {
  139. System.out.print("Fetching " + name + "... ");
  140. JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
  141. System.out.println("done.");
  142. }
  143. private static void enableTickets(String repositoryName) {
  144. try {
  145. RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
  146. model.useTickets = true;
  147. GitBlit.self().updateRepositoryModel(model.name, model, false);
  148. } catch (GitBlitException g) {
  149. g.printStackTrace();
  150. }
  151. }
  152. private static void enableDocs(String repositoryName) {
  153. try {
  154. RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
  155. model.useDocs = true;
  156. GitBlit.self().updateRepositoryModel(model.name, model, false);
  157. } catch (GitBlitException g) {
  158. g.printStackTrace();
  159. }
  160. }
  161. private static void showRemoteBranches(String repositoryName) {
  162. try {
  163. RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
  164. model.showRemoteBranches = true;
  165. GitBlit.self().updateRepositoryModel(model.name, model, false);
  166. } catch (GitBlitException g) {
  167. g.printStackTrace();
  168. }
  169. }
  170. public static void close(File repository) {
  171. try {
  172. File gitDir = FileKey.resolve(repository, FS.detect());
  173. if (gitDir != null && gitDir.exists()) {
  174. close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));
  175. }
  176. } catch (Exception e) {
  177. e.printStackTrace();
  178. }
  179. }
  180. public static void close(Git git) {
  181. close(git.getRepository());
  182. }
  183. public static void close(Repository r) {
  184. RepositoryCache.close(r);
  185. // assume 2 uses in case reflection fails
  186. int uses = 2;
  187. try {
  188. Field useCnt = Repository.class.getDeclaredField("useCnt");
  189. useCnt.setAccessible(true);
  190. uses = ((AtomicInteger) useCnt.get(r)).get();
  191. } catch (Exception e) {
  192. e.printStackTrace();
  193. }
  194. for (int i = 0; i < uses; i++) {
  195. r.close();
  196. }
  197. }
  198. }