lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
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;
143fc9 19 import java.util.concurrent.Executors;
7e8873 20 import java.util.concurrent.atomic.AtomicBoolean;
2a7306 21
JM 22 import org.eclipse.jgit.lib.Repository;
23 import org.eclipse.jgit.storage.file.FileRepository;
7e8873 24 import org.junit.AfterClass;
JM 25 import org.junit.BeforeClass;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.Suite;
28 import org.junit.runners.Suite.SuiteClasses;
2a7306 29
28d6b2 30 import com.gitblit.GitBlit;
a125cf 31 import com.gitblit.GitBlitException;
143fc9 32 import com.gitblit.GitBlitServer;
a125cf 33 import com.gitblit.models.RepositoryModel;
168566 34 import com.gitblit.utils.JGitUtils;
28d6b2 35
7e8873 36 /**
JM 37  * The GitBlitSuite uses test-gitblit.properties and test-users.conf. The suite
38  * is fairly comprehensive for all lower-level functionality. Wicket pages are
39  * currently not unit-tested.
40  * 
41  * This suite starts a Gitblit server instance within the same JVM instance as
42  * the unit tests. This allows the unit tests to access the GitBlit static
43  * singleton while also being able to communicate with the instance via tcp/ip
44  * for testing rpc requests, federation requests, and git servlet operations.
45  * 
46  * @author James Moger
47  * 
48  */
49 @RunWith(Suite.class)
c89745 50 @SuiteClasses({ ArrayUtilsTest.class, FileUtilsTest.class, TimeUtilsTest.class,
JM 51         StringUtilsTest.class, Base64Test.class, JsonUtilsTest.class, ByteFormatTest.class,
52         ObjectCacheTest.class, UserServiceTest.class, MarkdownUtilsTest.class, JGitUtilsTest.class,
53         SyndicationUtilsTest.class, DiffUtilsTest.class, MetricUtilsTest.class,
54         TicgitUtilsTest.class, GitBlitTest.class, FederationTests.class, RpcTests.class,
55         GitServletTest.class, GroovyScriptTest.class })
7e8873 56 public class GitBlitSuite {
143fc9 57
2a7306 58     public static final File REPOSITORIES = new File("git");
143fc9 59
JM 60     static int port = 8280;
61     static int shutdownPort = 8281;
62
63     public static String url = "http://localhost:" + port;
64     public static String account = "admin";
65     public static String password = "admin";
2a7306 66
7e8873 67     private static AtomicBoolean started = new AtomicBoolean(false);
1f9dae 68
2a7306 69     public static Repository getHelloworldRepository() throws Exception {
JM 70         return new FileRepository(new File(REPOSITORIES, "helloworld.git"));
71     }
72
73     public static Repository getTicgitRepository() throws Exception {
74         return new FileRepository(new File(REPOSITORIES, "ticgit.git"));
75     }
76
4ab184 77     public static Repository getJGitRepository() throws Exception {
168566 78         return new FileRepository(new File(REPOSITORIES, "test/jgit.git"));
4ab184 79     }
JM 80
81     public static Repository getBluezGnomeRepository() throws Exception {
168566 82         return new FileRepository(new File(REPOSITORIES, "test/bluez-gnome.git"));
143fc9 83     }
JM 84
11924d 85     public static Repository getAmbitionRepository() throws Exception {
JM 86         return new FileRepository(new File(REPOSITORIES, "test/ambition.git"));
87     }
88
89     public static Repository getTheoreticalPhysicsRepository() throws Exception {
90         return new FileRepository(new File(REPOSITORIES, "test/theoretical-physics.git"));
91     }
92
7e8873 93     public static boolean startGitblit() throws Exception {
JM 94         if (started.get()) {
95             // already started
96             return false;
97         }
143fc9 98         // Start a Gitblit instance
JM 99         Executors.newSingleThreadExecutor().execute(new Runnable() {
100             public void run() {
101                 GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
102                         "" + shutdownPort, "--repositoriesFolder",
103                         "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
7e8873 104                         "test-users.conf", "--settings", "test-gitblit.properties");
143fc9 105             }
JM 106         });
107
108         // Wait a few seconds for it to be running
109         Thread.sleep(2500);
7e8873 110
JM 111         started.set(true);
112         return true;
143fc9 113     }
JM 114
115     public static void stopGitblit() throws Exception {
116         // Stop Gitblit
117         GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
118
119         // Wait a few seconds for it to be running
120         Thread.sleep(2500);
4ab184 121     }
JM 122
7e8873 123     @BeforeClass
JM 124     public static void setUp() throws Exception {
125         startGitblit();
a125cf 126
JM 127         if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
168566 128             cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
JM 129             cloneOrFetch("ticgit.git", "https://github.com/jeffWelling/ticgit.git");
130             cloneOrFetch("test/bluez-gnome.git",
131                     "https://git.kernel.org/pub/scm/bluetooth/bluez-gnome.git");
132             cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
133             cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
11924d 134             cloneOrFetch("test/ambition.git", "https://github.com/defunkt/ambition.git");
JM 135             cloneOrFetch("test/theoretical-physics.git", "https://github.com/certik/theoretical-physics.git");
136             
a125cf 137             enableTickets("ticgit.git");
JM 138             enableDocs("ticgit.git");
139             showRemoteBranches("ticgit.git");
168566 140             showRemoteBranches("test/jgit.git");
a125cf 141         }
143fc9 142     }
JM 143
7e8873 144     @AfterClass
JM 145     public static void tearDown() throws Exception {
143fc9 146         stopGitblit();
2a7306 147     }
JM 148
7e8873 149     private static void cloneOrFetch(String name, String fromUrl) throws Exception {
168566 150         System.out.print("Fetching " + name + "... ");
JM 151         JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
152         System.out.println("done.");
4ab184 153     }
a125cf 154
7e8873 155     private static void enableTickets(String repositoryName) {
a125cf 156         try {
JM 157             RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
158             model.useTickets = true;
892570 159             GitBlit.self().updateRepositoryModel(model.name, model, false);
a125cf 160         } catch (GitBlitException g) {
JM 161             g.printStackTrace();
162         }
163     }
168566 164
7e8873 165     private static void enableDocs(String repositoryName) {
a125cf 166         try {
JM 167             RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
168             model.useDocs = true;
892570 169             GitBlit.self().updateRepositoryModel(model.name, model, false);
a125cf 170         } catch (GitBlitException g) {
JM 171             g.printStackTrace();
172         }
173     }
168566 174
7e8873 175     private static void showRemoteBranches(String repositoryName) {
a125cf 176         try {
JM 177             RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
178             model.showRemoteBranches = true;
892570 179             GitBlit.self().updateRepositoryModel(model.name, model, false);
a125cf 180         } catch (GitBlitException g) {
JM 181             g.printStackTrace();
182         }
183     }
2a7306 184 }