James Moger
2011-11-10 c7a721aad7c23640d59ad682854e066f4538436c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.tests;
 
import java.io.File;
import java.util.concurrent.Executors;
 
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
 
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepository;
 
import com.gitblit.FileSettings;
import com.gitblit.FileUserService;
import com.gitblit.GitBlit;
import com.gitblit.GitBlitException;
import com.gitblit.GitBlitServer;
import com.gitblit.models.RepositoryModel;
import com.gitblit.utils.JGitUtils;
 
public class GitBlitSuite extends TestSetup {
 
    public static final File REPOSITORIES = new File("git");
 
    static int port = 8280;
    static int shutdownPort = 8281;
 
    public static String url = "http://localhost:" + port;
    public static String account = "admin";
    public static String password = "admin";
 
    private GitBlitSuite(TestSuite suite) {
        super(suite);
    }
 
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(FileUtilsTest.class);
        suite.addTestSuite(TimeUtilsTest.class);
        suite.addTestSuite(StringUtilsTest.class);
        suite.addTestSuite(Base64Test.class);
        suite.addTestSuite(JsonUtilsTest.class);
        suite.addTestSuite(ByteFormatTest.class);
        suite.addTestSuite(ObjectCacheTest.class);
        suite.addTestSuite(MarkdownUtilsTest.class);
        suite.addTestSuite(JGitUtilsTest.class);
        suite.addTestSuite(SyndicationUtilsTest.class);
        suite.addTestSuite(DiffUtilsTest.class);
        suite.addTestSuite(MetricUtilsTest.class);
        suite.addTestSuite(TicgitUtilsTest.class);
        suite.addTestSuite(GitBlitTest.class);
        suite.addTestSuite(RpcTests.class);
        return new GitBlitSuite(suite);
    }
 
    public static Repository getHelloworldRepository() throws Exception {
        return new FileRepository(new File(REPOSITORIES, "helloworld.git"));
    }
 
    public static Repository getTicgitRepository() throws Exception {
        return new FileRepository(new File(REPOSITORIES, "ticgit.git"));
    }
 
    public static Repository getJGitRepository() throws Exception {
        return new FileRepository(new File(REPOSITORIES, "test/jgit.git"));
    }
 
    public static Repository getBluezGnomeRepository() throws Exception {
        return new FileRepository(new File(REPOSITORIES, "test/bluez-gnome.git"));
    }
 
    public static void startGitblit() throws Exception {
        // Start a Gitblit instance
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            public void run() {
                GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
                        "" + shutdownPort, "--repositoriesFolder",
                        "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
                        "distrib/users.properties");
            }
        });
 
        // Wait a few seconds for it to be running
        Thread.sleep(2500);
    }
 
    public static void stopGitblit() throws Exception {
        // Stop Gitblit
        GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
 
        // Wait a few seconds for it to be running
        Thread.sleep(2500);
    }
 
    @Override
    protected void setUp() throws Exception {
        FileSettings settings = new FileSettings("distrib/gitblit.properties");
        GitBlit.self().configureContext(settings, true);
        FileUserService loginService = new FileUserService(new File("distrib/users.properties"));
        GitBlit.self().setUserService(loginService);
 
        if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
            cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
            cloneOrFetch("ticgit.git", "https://github.com/jeffWelling/ticgit.git");
            cloneOrFetch("test/bluez-gnome.git",
                    "https://git.kernel.org/pub/scm/bluetooth/bluez-gnome.git");
            cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
            cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
 
            enableTickets("ticgit.git");
            enableDocs("ticgit.git");
            showRemoteBranches("ticgit.git");
            showRemoteBranches("test/jgit.git");
        }
 
        startGitblit();
    }
 
    @Override
    protected void tearDown() throws Exception {
        stopGitblit();
    }
 
    private void cloneOrFetch(String name, String fromUrl) throws Exception {
        System.out.print("Fetching " + name + "... ");
        JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
        System.out.println("done.");
    }
 
    private void enableTickets(String repositoryName) {
        try {
            RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
            model.useTickets = true;
            GitBlit.self().updateRepositoryModel(model.name, model, false);
        } catch (GitBlitException g) {
            g.printStackTrace();
        }
    }
 
    private void enableDocs(String repositoryName) {
        try {
            RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
            model.useDocs = true;
            GitBlit.self().updateRepositoryModel(model.name, model, false);
        } catch (GitBlitException g) {
            g.printStackTrace();
        }
    }
 
    private void showRemoteBranches(String repositoryName) {
        try {
            RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
            model.showRemoteBranches = true;
            GitBlit.self().updateRepositoryModel(model.name, model, false);
        } catch (GitBlitException g) {
            g.printStackTrace();
        }
    }
}