Paul Martin
2016-04-27 c2188a840bc4153ae92112b04b2e06a90d3944aa
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,
a9a2ff 66         ModelUtilsTest.class, JnaUtilsTest.class, LdapSyncServiceTest.class, FileTicketServiceTest.class,
521cb6 67         BranchTicketServiceTest.class, RedisTicketServiceTest.class, AuthenticationManagerTest.class,
f790d5 68         SshKeysDispatcherTest.class, UITicketTest.class, PathUtilsTest.class, SshKerberosAuthenticationTest.class,
c2188a 69         GravatarTest.class, FilestoreManagerTest.class, FilestoreServletTest.class, TicketReferenceTest.class })
7e8873 70 public class GitBlitSuite {
143fc9 71
f8f6aa 72     public static final File BASEFOLDER = new File("data");
JM 73
93d506 74     public static final File REPOSITORIES = new File("data/git");
5dd805 75
06fa4b 76     public static final File SETTINGS = new File("src/test/config/test-gitblit.properties");
5dd805 77
06fa4b 78     public static final File USERSCONF = new File("src/test/config/test-users.conf");
143fc9 79
JM 80     static int port = 8280;
75bca8 81     static int gitPort = 8300;
143fc9 82     static int shutdownPort = 8281;
245836 83     static int sshPort = 39418;
143fc9 84
JM 85     public static String url = "http://localhost:" + port;
75bca8 86     public static String gitServletUrl = "http://localhost:" + port + "/git";
JM 87     public static String gitDaemonUrl = "git://localhost:" + gitPort;
245836 88     public static String sshDaemonUrl = "ssh://admin@localhost:" + sshPort;
143fc9 89     public static String account = "admin";
JM 90     public static String password = "admin";
2a7306 91
7e8873 92     private static AtomicBoolean started = new AtomicBoolean(false);
1f9dae 93
f8f6aa 94     public static Repository getHelloworldRepository() {
843c42 95         return getRepository("helloworld.git");
2a7306 96     }
JM 97
f8f6aa 98     public static Repository getTicgitRepository() {
843c42 99         return getRepository("ticgit.git");
2a7306 100     }
JM 101
f8f6aa 102     public static Repository getJGitRepository() {
843c42 103         return getRepository("test/jgit.git");
4ab184 104     }
JM 105
f8f6aa 106     public static Repository getAmbitionRepository() {
843c42 107         return getRepository("test/ambition.git");
11924d 108     }
JM 109
f8f6aa 110     public static Repository getGitectiveRepository() {
843c42 111         return getRepository("test/gitective.git");
JM 112     }
5dd805 113
a9a2ff 114     public static Repository getTicketsTestRepository() {
MC 115         JGitUtils.createRepository(REPOSITORIES, "gb-tickets.git").close();
116         return getRepository("gb-tickets.git");
117     }
118
f8f6aa 119     private static Repository getRepository(String name) {
JM 120         try {
121             File gitDir = FileKey.resolve(new File(REPOSITORIES, name), FS.DETECTED);
122             Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
123             return repository;
124         } catch (Exception e) {
125             e.printStackTrace();
126         }
127         return null;
0f43a5 128     }
JM 129
7e8873 130     public static boolean startGitblit() throws Exception {
JM 131         if (started.get()) {
132             // already started
133             return false;
134         }
5dd805 135
f3ce6e 136         GitServletTest.deleteWorkingFolders();
5dd805 137
143fc9 138         // Start a Gitblit instance
JM 139         Executors.newSingleThreadExecutor().execute(new Runnable() {
5dd805 140             @Override
143fc9 141             public void run() {
521cb6 142                 GitBlitServer.main(
JM 143                         "--httpPort", "" + port,
144                         "--httpsPort", "0",
145                         "--shutdownPort", "" + shutdownPort,
146                         "--gitPort", "" + gitPort,
147                         "--sshPort", "" + sshPort,
148                         "--repositoriesFolder", GitBlitSuite.REPOSITORIES.getAbsolutePath(),
149                         "--userService", GitBlitSuite.USERSCONF.getAbsolutePath(),
150                         "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),
151                         "--baseFolder", "data");
143fc9 152             }
JM 153         });
154
155         // Wait a few seconds for it to be running
d1dc77 156         Thread.sleep(5000);
7e8873 157
JM 158         started.set(true);
159         return true;
143fc9 160     }
JM 161
162     public static void stopGitblit() throws Exception {
163         // Stop Gitblit
164         GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
165
166         // Wait a few seconds for it to be running
f3ce6e 167         Thread.sleep(5000);
4ab184 168     }
JM 169
7e8873 170     @BeforeClass
JM 171     public static void setUp() throws Exception {
172         startGitblit();
a125cf 173
JM 174         if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
168566 175             cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
f3ce6e 176             cloneOrFetch("ticgit.git", "https://github.com/schacon/ticgit.git");
168566 177             cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
JM 178             cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
11924d 179             cloneOrFetch("test/ambition.git", "https://github.com/defunkt/ambition.git");
d3065f 180             cloneOrFetch("test/gitective.git", "https://github.com/kevinsawicki/gitective.git");
5dd805 181
a125cf 182             showRemoteBranches("ticgit.git");
0f47b2 183             automaticallyTagBranchTips("ticgit.git");
168566 184             showRemoteBranches("test/jgit.git");
5dd805 185             automaticallyTagBranchTips("test/jgit.git");
a125cf 186         }
143fc9 187     }
JM 188
7e8873 189     @AfterClass
JM 190     public static void tearDown() throws Exception {
143fc9 191         stopGitblit();
2a7306 192     }
JM 193
7e8873 194     private static void cloneOrFetch(String name, String fromUrl) throws Exception {
168566 195         System.out.print("Fetching " + name + "... ");
75bca8 196         try {
JM 197             JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
198         } catch (Throwable t) {
199             System.out.println("Error: " + t.getMessage());
200         }
168566 201         System.out.println("done.");
a125cf 202     }
168566 203
7e8873 204     private static void showRemoteBranches(String repositoryName) {
a125cf 205         try {
7bf6e1 206             IRepositoryManager repositoryManager = GitblitContext.getManager(IRepositoryManager.class);
db4f6b 207             RepositoryModel model = repositoryManager.getRepositoryModel(repositoryName);
a125cf 208             model.showRemoteBranches = true;
db4f6b 209             repositoryManager.updateRepositoryModel(model.name, model, false);
a125cf 210         } catch (GitBlitException g) {
JM 211             g.printStackTrace();
212         }
213     }
5dd805 214
0f47b2 215     private static void automaticallyTagBranchTips(String repositoryName) {
JM 216         try {
7bf6e1 217             IRepositoryManager repositoryManager = GitblitContext.getManager(IRepositoryManager.class);
db4f6b 218             RepositoryModel model = repositoryManager.getRepositoryModel(repositoryName);
0f47b2 219             model.useIncrementalPushTags = true;
db4f6b 220             repositoryManager.updateRepositoryModel(model.name, model, false);
0f47b2 221         } catch (GitBlitException g) {
JM 222             g.printStackTrace();
223         }
224     }
5dd805 225
8daefa 226     public static void close(File repository) {
JM 227         try {
228             File gitDir = FileKey.resolve(repository, FS.detect());
229             if (gitDir != null && gitDir.exists()) {
230                 close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));
231             }
232         } catch (Exception e) {
233             e.printStackTrace();
234         }
235     }
5dd805 236
8daefa 237     public static void close(Git git) {
JM 238         close(git.getRepository());
239     }
5dd805 240
8daefa 241     public static void close(Repository r) {
JM 242         RepositoryCache.close(r);
243
244         // assume 2 uses in case reflection fails
245         int uses = 2;
246         try {
247             Field useCnt = Repository.class.getDeclaredField("useCnt");
248             useCnt.setAccessible(true);
249             uses = ((AtomicInteger) useCnt.get(r)).get();
250         } catch (Exception e) {
251             e.printStackTrace();
252         }
253         for (int i = 0; i < uses; i++) {
254             r.close();
255         }
256     }
2a7306 257 }