James Moger
2012-10-11 fa89b73e63911f5b216ac8c06e39cff8e987e3ba
commit | author | age
ec97f7 1 /*
JM 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  */
2a7306 16 package com.gitblit.tests;
JM 17
18 import java.io.File;
8daefa 19 import java.lang.reflect.Field;
143fc9 20 import java.util.concurrent.Executors;
7e8873 21 import java.util.concurrent.atomic.AtomicBoolean;
8daefa 22 import java.util.concurrent.atomic.AtomicInteger;
2a7306 23
8daefa 24 import org.eclipse.jgit.api.Git;
2a7306 25 import org.eclipse.jgit.lib.Repository;
8daefa 26 import org.eclipse.jgit.lib.RepositoryCache;
JM 27 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
2a7306 28 import org.eclipse.jgit.storage.file.FileRepository;
8daefa 29 import org.eclipse.jgit.util.FS;
7e8873 30 import org.junit.AfterClass;
JM 31 import org.junit.BeforeClass;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.Suite;
34 import org.junit.runners.Suite.SuiteClasses;
2a7306 35
28d6b2 36 import com.gitblit.GitBlit;
a125cf 37 import com.gitblit.GitBlitException;
143fc9 38 import com.gitblit.GitBlitServer;
a125cf 39 import com.gitblit.models.RepositoryModel;
168566 40 import com.gitblit.utils.JGitUtils;
28d6b2 41
7e8873 42 /**
JM 43  * The GitBlitSuite uses test-gitblit.properties and test-users.conf. The suite
44  * is fairly comprehensive for all lower-level functionality. Wicket pages are
45  * currently not unit-tested.
46  * 
47  * This suite starts a Gitblit server instance within the same JVM instance as
48  * the unit tests. This allows the unit tests to access the GitBlit static
49  * singleton while also being able to communicate with the instance via tcp/ip
50  * for testing rpc requests, federation requests, and git servlet operations.
51  * 
52  * @author James Moger
53  * 
54  */
55 @RunWith(Suite.class)
c89745 56 @SuiteClasses({ ArrayUtilsTest.class, FileUtilsTest.class, TimeUtilsTest.class,
JM 57         StringUtilsTest.class, Base64Test.class, JsonUtilsTest.class, ByteFormatTest.class,
20714a 58         ObjectCacheTest.class, PermissionsTest.class, UserServiceTest.class, LdapUserServiceTest.class,
98b4b9 59         MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class,
JM 60         DiffUtilsTest.class, MetricUtilsTest.class, TicgitUtilsTest.class,
61         GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class,
cca55e 62         GroovyScriptTest.class, LuceneExecutorTest.class, IssuesTest.class, RepositoryModelTest.class })
7e8873 63 public class GitBlitSuite {
143fc9 64
2a7306 65     public static final File REPOSITORIES = new File("git");
143fc9 66
JM 67     static int port = 8280;
68     static int shutdownPort = 8281;
69
70     public static String url = "http://localhost:" + port;
71     public static String account = "admin";
72     public static String password = "admin";
2a7306 73
7e8873 74     private static AtomicBoolean started = new AtomicBoolean(false);
1f9dae 75
2a7306 76     public static Repository getHelloworldRepository() throws Exception {
JM 77         return new FileRepository(new File(REPOSITORIES, "helloworld.git"));
78     }
79
80     public static Repository getTicgitRepository() throws Exception {
81         return new FileRepository(new File(REPOSITORIES, "ticgit.git"));
82     }
83
4ab184 84     public static Repository getJGitRepository() throws Exception {
168566 85         return new FileRepository(new File(REPOSITORIES, "test/jgit.git"));
4ab184 86     }
JM 87
11924d 88     public static Repository getAmbitionRepository() throws Exception {
JM 89         return new FileRepository(new File(REPOSITORIES, "test/ambition.git"));
90     }
91
92     public static Repository getTheoreticalPhysicsRepository() throws Exception {
93         return new FileRepository(new File(REPOSITORIES, "test/theoretical-physics.git"));
94     }
95
0f43a5 96     public static Repository getIssuesTestRepository() throws Exception {
d896e6 97         JGitUtils.createRepository(REPOSITORIES, "gb-issues.git").close();
0f43a5 98         return new FileRepository(new File(REPOSITORIES, "gb-issues.git"));
d3065f 99     }
JM 100     
101     public static Repository getGitectiveRepository() throws Exception {
9e2f9d 102         return new FileRepository(new File(REPOSITORIES, "test/gitective.git"));
0f43a5 103     }
JM 104
7e8873 105     public static boolean startGitblit() throws Exception {
JM 106         if (started.get()) {
107             // already started
108             return false;
109         }
f3ce6e 110         
JM 111         GitServletTest.deleteWorkingFolders();
112         
143fc9 113         // Start a Gitblit instance
JM 114         Executors.newSingleThreadExecutor().execute(new Runnable() {
115             public void run() {
116                 GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
117                         "" + shutdownPort, "--repositoriesFolder",
118                         "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
7e8873 119                         "test-users.conf", "--settings", "test-gitblit.properties");
143fc9 120             }
JM 121         });
122
123         // Wait a few seconds for it to be running
124         Thread.sleep(2500);
7e8873 125
JM 126         started.set(true);
127         return true;
143fc9 128     }
JM 129
130     public static void stopGitblit() throws Exception {
131         // Stop Gitblit
132         GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
133
134         // Wait a few seconds for it to be running
f3ce6e 135         Thread.sleep(5000);
4ab184 136     }
JM 137
7e8873 138     @BeforeClass
JM 139     public static void setUp() throws Exception {
140         startGitblit();
a125cf 141
JM 142         if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
168566 143             cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
f3ce6e 144             cloneOrFetch("ticgit.git", "https://github.com/schacon/ticgit.git");
168566 145             cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
JM 146             cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
11924d 147             cloneOrFetch("test/ambition.git", "https://github.com/defunkt/ambition.git");
JM 148             cloneOrFetch("test/theoretical-physics.git", "https://github.com/certik/theoretical-physics.git");
d3065f 149             cloneOrFetch("test/gitective.git", "https://github.com/kevinsawicki/gitective.git");
11924d 150             
a125cf 151             enableTickets("ticgit.git");
JM 152             enableDocs("ticgit.git");
153             showRemoteBranches("ticgit.git");
168566 154             showRemoteBranches("test/jgit.git");
a125cf 155         }
143fc9 156     }
JM 157
7e8873 158     @AfterClass
JM 159     public static void tearDown() throws Exception {
143fc9 160         stopGitblit();
2a7306 161     }
JM 162
7e8873 163     private static void cloneOrFetch(String name, String fromUrl) throws Exception {
168566 164         System.out.print("Fetching " + name + "... ");
JM 165         JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
166         System.out.println("done.");
4ab184 167     }
a125cf 168
7e8873 169     private static void enableTickets(String repositoryName) {
a125cf 170         try {
JM 171             RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
172             model.useTickets = true;
892570 173             GitBlit.self().updateRepositoryModel(model.name, model, false);
a125cf 174         } catch (GitBlitException g) {
JM 175             g.printStackTrace();
176         }
177     }
168566 178
7e8873 179     private static void enableDocs(String repositoryName) {
a125cf 180         try {
JM 181             RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
182             model.useDocs = true;
892570 183             GitBlit.self().updateRepositoryModel(model.name, model, false);
a125cf 184         } catch (GitBlitException g) {
JM 185             g.printStackTrace();
186         }
187     }
168566 188
7e8873 189     private static void showRemoteBranches(String repositoryName) {
a125cf 190         try {
JM 191             RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
192             model.showRemoteBranches = true;
892570 193             GitBlit.self().updateRepositoryModel(model.name, model, false);
a125cf 194         } catch (GitBlitException g) {
JM 195             g.printStackTrace();
196         }
197     }
8daefa 198     
JM 199     public static void close(File repository) {
200         try {
201             File gitDir = FileKey.resolve(repository, FS.detect());
202             if (gitDir != null && gitDir.exists()) {
203                 close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));
204             }
205         } catch (Exception e) {
206             e.printStackTrace();
207         }
208     }
209     
210     public static void close(Git git) {
211         close(git.getRepository());
212     }
213     
214     public static void close(Repository r) {
215         RepositoryCache.close(r);
216
217         // assume 2 uses in case reflection fails
218         int uses = 2;
219         try {
220             Field useCnt = Repository.class.getDeclaredField("useCnt");
221             useCnt.setAccessible(true);
222             uses = ((AtomicInteger) useCnt.get(r)).get();
223         } catch (Exception e) {
224             e.printStackTrace();
225         }
226         for (int i = 0; i < uses; i++) {
227             r.close();
228         }
229     }
2a7306 230 }