Paul Martin
2014-09-29 f9c78c0ccc709509cdf7f83c45c898883d329db2
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;
843c42 28 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
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
a125cf 36 import com.gitblit.GitBlitException;
143fc9 37 import com.gitblit.GitBlitServer;
db4f6b 38 import com.gitblit.manager.IRepositoryManager;
a125cf 39 import com.gitblit.models.RepositoryModel;
7bf6e1 40 import com.gitblit.servlet.GitblitContext;
168566 41 import com.gitblit.utils.JGitUtils;
28d6b2 42
7e8873 43 /**
JM 44  * The GitBlitSuite uses test-gitblit.properties and test-users.conf. The suite
45  * is fairly comprehensive for all lower-level functionality. Wicket pages are
46  * currently not unit-tested.
5dd805 47  *
7e8873 48  * This suite starts a Gitblit server instance within the same JVM instance as
JM 49  * the unit tests. This allows the unit tests to access the GitBlit static
50  * singleton while also being able to communicate with the instance via tcp/ip
51  * for testing rpc requests, federation requests, and git servlet operations.
5dd805 52  *
7e8873 53  * @author James Moger
5dd805 54  *
7e8873 55  */
JM 56 @RunWith(Suite.class)
c89745 57 @SuiteClasses({ ArrayUtilsTest.class, FileUtilsTest.class, TimeUtilsTest.class,
JM 58         StringUtilsTest.class, Base64Test.class, JsonUtilsTest.class, ByteFormatTest.class,
37d5b7 59         UserModelTest.class, UserChoiceTest.class,
04a985 60         ObjectCacheTest.class, PermissionsTest.class, UserServiceTest.class, LdapAuthenticationTest.class,
98b4b9 61         MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class,
5dd805 62         DiffUtilsTest.class, MetricUtilsTest.class, X509UtilsTest.class,
75bca8 63         GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class, GitDaemonTest.class,
245836 64         SshDaemonTest.class, GroovyScriptTest.class, LuceneExecutorTest.class, RepositoryModelTest.class,
04a985 65         FanoutServiceTest.class, Issue0259Test.class, Issue0271Test.class, HtpasswdAuthenticationTest.class,
5e3521 66         ModelUtilsTest.class, JnaUtilsTest.class, LdapSyncServiceTest.class, FileTicketServiceTest.class,
521cb6 67         BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class,
f9c78c 68         SshKeysDispatcherTest.class, UITicketTest.class })
7e8873 69 public class GitBlitSuite {
143fc9 70
f8f6aa 71     public static final File BASEFOLDER = new File("data");
JM 72
93d506 73     public static final File REPOSITORIES = new File("data/git");
5dd805 74
06fa4b 75     public static final File SETTINGS = new File("src/test/config/test-gitblit.properties");
5dd805 76
06fa4b 77     public static final File USERSCONF = new File("src/test/config/test-users.conf");
143fc9 78
JM 79     static int port = 8280;
75bca8 80     static int gitPort = 8300;
143fc9 81     static int shutdownPort = 8281;
245836 82     static int sshPort = 39418;
143fc9 83
JM 84     public static String url = "http://localhost:" + port;
75bca8 85     public static String gitServletUrl = "http://localhost:" + port + "/git";
JM 86     public static String gitDaemonUrl = "git://localhost:" + gitPort;
245836 87     public static String sshDaemonUrl = "ssh://admin@localhost:" + sshPort;
143fc9 88     public static String account = "admin";
JM 89     public static String password = "admin";
2a7306 90
7e8873 91     private static AtomicBoolean started = new AtomicBoolean(false);
1f9dae 92
f8f6aa 93     public static Repository getHelloworldRepository() {
843c42 94         return getRepository("helloworld.git");
2a7306 95     }
JM 96
f8f6aa 97     public static Repository getTicgitRepository() {
843c42 98         return getRepository("ticgit.git");
2a7306 99     }
JM 100
f8f6aa 101     public static Repository getJGitRepository() {
843c42 102         return getRepository("test/jgit.git");
4ab184 103     }
JM 104
f8f6aa 105     public static Repository getAmbitionRepository() {
843c42 106         return getRepository("test/ambition.git");
11924d 107     }
JM 108
f8f6aa 109     public static Repository getGitectiveRepository() {
843c42 110         return getRepository("test/gitective.git");
JM 111     }
5dd805 112
5e3521 113     public static Repository getTicketsTestRepository() {
JM 114         JGitUtils.createRepository(REPOSITORIES, "gb-tickets.git").close();
115         return getRepository("gb-tickets.git");
116     }
117
f8f6aa 118     private static Repository getRepository(String name) {
JM 119         try {
120             File gitDir = FileKey.resolve(new File(REPOSITORIES, name), FS.DETECTED);
121             Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
122             return repository;
123         } catch (Exception e) {
124             e.printStackTrace();
125         }
126         return null;
0f43a5 127     }
JM 128
7e8873 129     public static boolean startGitblit() throws Exception {
JM 130         if (started.get()) {
131             // already started
132             return false;
133         }
5dd805 134
f3ce6e 135         GitServletTest.deleteWorkingFolders();
5dd805 136
143fc9 137         // Start a Gitblit instance
JM 138         Executors.newSingleThreadExecutor().execute(new Runnable() {
5dd805 139             @Override
143fc9 140             public void run() {
521cb6 141                 GitBlitServer.main(
JM 142                         "--httpPort", "" + port,
143                         "--httpsPort", "0",
144                         "--shutdownPort", "" + shutdownPort,
145                         "--gitPort", "" + gitPort,
146                         "--sshPort", "" + sshPort,
147                         "--repositoriesFolder", GitBlitSuite.REPOSITORIES.getAbsolutePath(),
148                         "--userService", GitBlitSuite.USERSCONF.getAbsolutePath(),
149                         "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),
150                         "--baseFolder", "data");
143fc9 151             }
JM 152         });
153
154         // Wait a few seconds for it to be running
d1dc77 155         Thread.sleep(5000);
7e8873 156
JM 157         started.set(true);
158         return true;
143fc9 159     }
JM 160
161     public static void stopGitblit() throws Exception {
162         // Stop Gitblit
163         GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
164
165         // Wait a few seconds for it to be running
f3ce6e 166         Thread.sleep(5000);
4ab184 167     }
JM 168
7e8873 169     @BeforeClass
JM 170     public static void setUp() throws Exception {
171         startGitblit();
a125cf 172
JM 173         if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
168566 174             cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
f3ce6e 175             cloneOrFetch("ticgit.git", "https://github.com/schacon/ticgit.git");
168566 176             cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
JM 177             cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
11924d 178             cloneOrFetch("test/ambition.git", "https://github.com/defunkt/ambition.git");
d3065f 179             cloneOrFetch("test/gitective.git", "https://github.com/kevinsawicki/gitective.git");
5dd805 180
a125cf 181             showRemoteBranches("ticgit.git");
0f47b2 182             automaticallyTagBranchTips("ticgit.git");
168566 183             showRemoteBranches("test/jgit.git");
5dd805 184             automaticallyTagBranchTips("test/jgit.git");
a125cf 185         }
143fc9 186     }
JM 187
7e8873 188     @AfterClass
JM 189     public static void tearDown() throws Exception {
143fc9 190         stopGitblit();
2a7306 191     }
JM 192
7e8873 193     private static void cloneOrFetch(String name, String fromUrl) throws Exception {
168566 194         System.out.print("Fetching " + name + "... ");
75bca8 195         try {
JM 196             JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
197         } catch (Throwable t) {
198             System.out.println("Error: " + t.getMessage());
199         }
168566 200         System.out.println("done.");
a125cf 201     }
168566 202
7e8873 203     private static void showRemoteBranches(String repositoryName) {
a125cf 204         try {
7bf6e1 205             IRepositoryManager repositoryManager = GitblitContext.getManager(IRepositoryManager.class);
db4f6b 206             RepositoryModel model = repositoryManager.getRepositoryModel(repositoryName);
a125cf 207             model.showRemoteBranches = true;
db4f6b 208             repositoryManager.updateRepositoryModel(model.name, model, false);
a125cf 209         } catch (GitBlitException g) {
JM 210             g.printStackTrace();
211         }
212     }
5dd805 213
0f47b2 214     private static void automaticallyTagBranchTips(String repositoryName) {
JM 215         try {
7bf6e1 216             IRepositoryManager repositoryManager = GitblitContext.getManager(IRepositoryManager.class);
db4f6b 217             RepositoryModel model = repositoryManager.getRepositoryModel(repositoryName);
0f47b2 218             model.useIncrementalPushTags = true;
db4f6b 219             repositoryManager.updateRepositoryModel(model.name, model, false);
0f47b2 220         } catch (GitBlitException g) {
JM 221             g.printStackTrace();
222         }
223     }
5dd805 224
8daefa 225     public static void close(File repository) {
JM 226         try {
227             File gitDir = FileKey.resolve(repository, FS.detect());
228             if (gitDir != null && gitDir.exists()) {
229                 close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));
230             }
231         } catch (Exception e) {
232             e.printStackTrace();
233         }
234     }
5dd805 235
8daefa 236     public static void close(Git git) {
JM 237         close(git.getRepository());
238     }
5dd805 239
8daefa 240     public static void close(Repository r) {
JM 241         RepositoryCache.close(r);
242
243         // assume 2 uses in case reflection fails
244         int uses = 2;
245         try {
246             Field useCnt = Repository.class.getDeclaredField("useCnt");
247             useCnt.setAccessible(true);
248             uses = ((AtomicInteger) useCnt.get(r)).get();
249         } catch (Exception e) {
250             e.printStackTrace();
251         }
252         for (int i = 0; i < uses; i++) {
253             r.close();
254         }
255     }
2a7306 256 }