James Moger
2011-05-26 2a7306a1d92522569a8bb6e5a7c0bcdd5cf4cfaa
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
/*
 * 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.io.FileOutputStream;
import java.util.Date;
import java.util.List;
 
import junit.framework.TestCase;
 
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTree;
 
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.JGitUtils.DiffOutputType;
import com.gitblit.wicket.models.PathModel.PathChangeModel;
import com.gitblit.wicket.models.RefModel;
import com.gitblit.wicket.models.TicketModel;
 
public class JGitUtilsTest extends TestCase {
 
    private List<String> getRepositories() {
        return JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, true, true);
    }
 
    public void testFindRepositories() {
        List<String> list = getRepositories();
        assertTrue("No repositories found in " + GitBlitSuite.REPOSITORIES, list.size() > 0);
    }
 
    public void testOpenRepository() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        repository.close();
        assertTrue("Could not find repository!", repository != null);
    }
 
    public void testLastChangeRepository() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        Date date = JGitUtils.getLastChange(repository);
        repository.close();
        assertTrue("Could not get last repository change date!", date != null);
    }
 
    public void testFirstCommit() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        RevCommit commit = JGitUtils.getFirstCommit(repository, null);
        repository.close();
        assertTrue("Could not get first commit!", commit != null);
        assertTrue("Incorrect first commit!",
                commit.getName().equals("f554664a346629dc2b839f7292d06bad2db4aece"));
    }
 
    public void testRetrieveRevObject() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
        RevTree tree = commit.getTree();
        RevObject object = JGitUtils.getRevObject(repository, tree, "java.java");
        repository.close();
        assertTrue("Object is null!", object != null);
    }
 
    public void testRetrieveStringContent() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
        RevTree tree = commit.getTree();
        RevBlob blob = (RevBlob) JGitUtils.getRevObject(repository, tree, "java.java");
        String content = JGitUtils.getRawContentAsString(repository, blob);
        repository.close();
        assertTrue("Content is null!", content != null && content.length() > 0);
    }
 
    public void testFilesInCommit() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        RevCommit commit = JGitUtils.getCommit(repository,
                "1d0c2933a4ae69c362f76797d42d6bd182d05176");
        List<PathChangeModel> paths = JGitUtils.getFilesInCommit(repository, commit);
        repository.close();
        assertTrue("No changed paths found!", paths.size() == 1);
    }
 
    public void testCommitDiff() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        RevCommit commit = JGitUtils.getCommit(repository,
                "1d0c2933a4ae69c362f76797d42d6bd182d05176");
        String diff = JGitUtils.getCommitDiff(repository, commit, DiffOutputType.PLAIN);
        repository.close();
        assertTrue("Failed to generate diff!", diff != null && diff.length() > 0);
        String expected = "-        system.out.println(\"Hello World\");\n+        System.out.println(\"Hello World\"";
        assertTrue("Diff content mismatch!", diff.indexOf(expected) > -1);
    }
 
    public void testZip() throws Exception {
        Repository repository = GitBlitSuite.getHelloworldRepository();
        File zipFile = new File(GitBlitSuite.REPOSITORIES, "helloworld.zip");
        FileOutputStream fos = new FileOutputStream(zipFile);
        boolean success = JGitUtils.zip(repository, null, Constants.HEAD, fos);
        assertTrue("Failed to generate zip file!", success);
        assertTrue(zipFile.length() > 0);
        fos.close();
        zipFile.delete();
        repository.close();
    }
 
    public void testTicGit() throws Exception {
        Repository repository = GitBlitSuite.getTicgitRepository();
        RefModel branch = JGitUtils.getTicketsBranch(repository);
        assertTrue("Ticgit branch does not exist!", branch != null);
        List<TicketModel> tickets = JGitUtils.getTickets(repository);
        repository.close();
        assertTrue("No tickets found!", tickets.size() > 0);
    }
}