Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

GitBlitSuite.java 8.6KB

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