James Moger
2011-07-20 330247866e931d5e5f93999db9947bc2fc1fc9ee
commit | author | age
2a7306 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  */
16 package com.gitblit.tests;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
a125cf 20 import java.util.Arrays;
2a7306 21 import java.util.Date;
JM 22 import java.util.List;
a125cf 23 import java.util.Map;
2a7306 24
JM 25 import junit.framework.TestCase;
26
a125cf 27 import org.eclipse.jgit.diff.DiffEntry.ChangeType;
2a7306 28 import org.eclipse.jgit.lib.Constants;
a125cf 29 import org.eclipse.jgit.lib.FileMode;
JM 30 import org.eclipse.jgit.lib.ObjectId;
31 import org.eclipse.jgit.lib.PersonIdent;
2a7306 32 import org.eclipse.jgit.lib.Repository;
168566 33 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
2a7306 34 import org.eclipse.jgit.revwalk.RevCommit;
168566 35 import org.eclipse.jgit.util.FS;
2a7306 36
f1720c 37 import com.gitblit.GitBlit;
a125cf 38 import com.gitblit.Keys;
4ab184 39 import com.gitblit.models.GitNote;
a125cf 40 import com.gitblit.models.PathModel;
1f9dae 41 import com.gitblit.models.PathModel.PathChangeModel;
JM 42 import com.gitblit.models.RefModel;
2a7306 43 import com.gitblit.utils.JGitUtils;
a125cf 44 import com.gitblit.utils.JGitUtils.SearchType;
4ab184 45 import com.gitblit.utils.StringUtils;
2a7306 46
JM 47 public class JGitUtilsTest extends TestCase {
a125cf 48
JM 49     public void testDisplayName() throws Exception {
50         assertTrue(JGitUtils.getDisplayName(new PersonIdent("Napoleon Bonaparte", "")).equals(
51                 "Napoleon Bonaparte"));
52         assertTrue(JGitUtils.getDisplayName(new PersonIdent("", "someone@somewhere.com")).equals(
53                 "<someone@somewhere.com>"));
54         assertTrue(JGitUtils.getDisplayName(
55                 new PersonIdent("Napoleon Bonaparte", "someone@somewhere.com")).equals(
56                 "Napoleon Bonaparte <someone@somewhere.com>"));
57     }
2a7306 58
JM 59     public void testFindRepositories() {
f1720c 60         List<String> list = JGitUtils.getRepositoryList(null, true, true);
JM 61         assertTrue(list.size() == 0);
62         list.addAll(JGitUtils.getRepositoryList(new File("DoesNotExist"), true, true));
63         assertTrue(list.size() == 0);
64         list.addAll(JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, true, true));
2a7306 65         assertTrue("No repositories found in " + GitBlitSuite.REPOSITORIES, list.size() > 0);
JM 66     }
67
68     public void testOpenRepository() throws Exception {
69         Repository repository = GitBlitSuite.getHelloworldRepository();
70         repository.close();
71         assertTrue("Could not find repository!", repository != null);
72     }
73
74     public void testFirstCommit() throws Exception {
f1720c 75         assertTrue(JGitUtils.getFirstChange(null, null).equals(new Date(0)));
a125cf 76
2a7306 77         Repository repository = GitBlitSuite.getHelloworldRepository();
JM 78         RevCommit commit = JGitUtils.getFirstCommit(repository, null);
db653a 79         Date firstChange = JGitUtils.getFirstChange(repository, null);
2a7306 80         repository.close();
JM 81         assertTrue("Could not get first commit!", commit != null);
82         assertTrue("Incorrect first commit!",
83                 commit.getName().equals("f554664a346629dc2b839f7292d06bad2db4aece"));
db653a 84         assertTrue(firstChange.equals(new Date(commit.getCommitTime() * 1000L)));
f1720c 85     }
a125cf 86
f1720c 87     public void testLastCommit() throws Exception {
ed21d2 88         assertTrue(JGitUtils.getLastChange(null, null).equals(new Date(0)));
a125cf 89
f1720c 90         Repository repository = GitBlitSuite.getHelloworldRepository();
JM 91         assertTrue(JGitUtils.getCommit(repository, null) != null);
ed21d2 92         Date date = JGitUtils.getLastChange(repository, null);
f1720c 93         repository.close();
JM 94         assertTrue("Could not get last repository change date!", date != null);
95     }
96
97     public void testCreateRepository() throws Exception {
98         String[] repositories = { "NewTestRepository.git", "NewTestRepository" };
008322 99         for (String repositoryName : repositories) {
f1720c 100             Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
168566 101                     repositoryName);
008322 102             File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName),
JM 103                     FS.DETECTED);
f1720c 104             assertTrue(repository != null);
JM 105             assertFalse(JGitUtils.hasCommits(repository));
106             assertTrue(JGitUtils.getFirstCommit(repository, null) == null);
107             assertTrue(JGitUtils.getFirstChange(repository, null).getTime() == folder
108                     .lastModified());
ed21d2 109             assertTrue(JGitUtils.getLastChange(repository, null).getTime() == folder.lastModified());
f1720c 110             assertTrue(JGitUtils.getCommit(repository, null) == null);
JM 111             repository.close();
112             assertTrue(GitBlit.self().deleteRepository(repositoryName));
113         }
2a7306 114     }
JM 115
28d6b2 116     public void testRefs() throws Exception {
4ab184 117         Repository repository = GitBlitSuite.getJGitRepository();
JM 118         Map<ObjectId, List<RefModel>> map = JGitUtils.getAllRefs(repository);
a125cf 119         repository.close();
JM 120         assertTrue(map.size() > 0);
4ab184 121         for (Map.Entry<ObjectId, List<RefModel>> entry : map.entrySet()) {
JM 122             List<RefModel> list = entry.getValue();
123             for (RefModel ref : list) {
124                 if (ref.displayName.equals("refs/tags/spearce-gpg-pub")) {
f339f5 125                     assertTrue(ref.toString().equals("refs/tags/spearce-gpg-pub"));
008322 126                     assertTrue(ref.getObjectId().getName()
JM 127                             .equals("8bbde7aacf771a9afb6992434f1ae413e010c6d8"));
4ab184 128                     assertTrue(ref.getAuthorIdent().getEmailAddress().equals("spearce@spearce.org"));
JM 129                     assertTrue(ref.getShortMessage().startsWith("GPG key"));
008322 130                     assertTrue(ref.getFullMessage().startsWith("GPG key"));
4ab184 131                     assertTrue(ref.getReferencedObjectType() == Constants.OBJ_BLOB);
JM 132                 } else if (ref.displayName.equals("refs/tags/v0.12.1")) {
133                     assertTrue(ref.isAnnotatedTag());
134                 }
135             }
136         }
a125cf 137     }
JM 138
139     public void testBranches() throws Exception {
168566 140         Repository repository = GitBlitSuite.getJGitRepository();
85c2e6 141         assertTrue(JGitUtils.getLocalBranches(repository, true, 0).size() == 0);
5cc4f2 142         for (RefModel model : JGitUtils.getLocalBranches(repository, true, -1)) {
28d6b2 143             assertTrue(model.getName().startsWith(Constants.R_HEADS));
JM 144             assertTrue(model.equals(model));
145             assertFalse(model.equals(""));
4ab184 146             assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
28d6b2 147                     + model.getName().hashCode());
4ab184 148             assertTrue(model.getShortMessage().equals(model.getShortMessage()));
28d6b2 149         }
5cc4f2 150         for (RefModel model : JGitUtils.getRemoteBranches(repository, true, -1)) {
28d6b2 151             assertTrue(model.getName().startsWith(Constants.R_REMOTES));
JM 152             assertTrue(model.equals(model));
153             assertFalse(model.equals(""));
4ab184 154             assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
28d6b2 155                     + model.getName().hashCode());
4ab184 156             assertTrue(model.getShortMessage().equals(model.getShortMessage()));
28d6b2 157         }
168566 158         assertTrue(JGitUtils.getRemoteBranches(repository, true, 8).size() == 8);
a125cf 159         repository.close();
JM 160     }
161
162     public void testTags() throws Exception {
168566 163         Repository repository = GitBlitSuite.getJGitRepository();
85c2e6 164         assertTrue(JGitUtils.getTags(repository, true, 5).size() == 5);
5cc4f2 165         for (RefModel model : JGitUtils.getTags(repository, true, -1)) {
168566 166             if (model.getObjectId().getName().equals("d28091fb2977077471138fe97da1440e0e8ae0da")) {
28d6b2 167                 assertTrue("Not an annotated tag!", model.isAnnotatedTag());
JM 168             }
169             assertTrue(model.getName().startsWith(Constants.R_TAGS));
170             assertTrue(model.equals(model));
171             assertFalse(model.equals(""));
4ab184 172             assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
28d6b2 173                     + model.getName().hashCode());
4ab184 174         }
JM 175         repository.close();
008322 176
4ab184 177         repository = GitBlitSuite.getBluezGnomeRepository();
5cc4f2 178         for (RefModel model : JGitUtils.getTags(repository, true, -1)) {
4ab184 179             if (model.getObjectId().getName().equals("728643ec0c438c77e182898c2f2967dbfdc231c8")) {
JM 180                 assertFalse(model.isAnnotatedTag());
181                 assertTrue(model.getAuthorIdent().getEmailAddress().equals("marcel@holtmann.org"));
008322 182                 assertTrue(model.getFullMessage().equals(
JM 183                         "Update changelog and bump version number\n"));
4ab184 184             }
28d6b2 185         }
JM 186         repository.close();
187     }
188
a125cf 189     public void testCommitNotes() throws Exception {
4ab184 190         Repository repository = GitBlitSuite.getJGitRepository();
JM 191         RevCommit commit = JGitUtils.getCommit(repository,
192                 "690c268c793bfc218982130fbfc25870f292295e");
193         List<GitNote> list = JGitUtils.getNotesOnCommit(repository, commit);
194         repository.close();
195         assertTrue(list.size() > 0);
196         assertTrue(list.get(0).notesRef.getReferencedObjectId().getName()
197                 .equals("183474d554e6f68478a02d9d7888b67a9338cdff"));
2a7306 198     }
JM 199
a125cf 200     public void testStringContent() throws Exception {
2a7306 201         Repository repository = GitBlitSuite.getHelloworldRepository();
4ab184 202         String contentA = JGitUtils.getStringContent(repository, null, "java.java");
2a7306 203         RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
4ab184 204         String contentB = JGitUtils.getStringContent(repository, commit.getTree(), "java.java");
JM 205         String contentC = JGitUtils.getStringContent(repository, commit.getTree(), "missing.txt");
206
207         // manually construct a blob, calculate the hash, lookup the hash in git
208         StringBuilder sb = new StringBuilder();
209         sb.append("blob ").append(contentA.length()).append('\0');
210         sb.append(contentA);
211         String sha1 = StringUtils.getSHA1(sb.toString());
212         String contentD = JGitUtils.getStringContent(repository, sha1);
2a7306 213         repository.close();
a125cf 214         assertTrue("ContentA is null!", contentA != null && contentA.length() > 0);
JM 215         assertTrue("ContentB is null!", contentB != null && contentB.length() > 0);
216         assertTrue(contentA.equals(contentB));
217         assertTrue(contentC == null);
4ab184 218         assertTrue(contentA.equals(contentD));
2a7306 219     }
JM 220
221     public void testFilesInCommit() throws Exception {
222         Repository repository = GitBlitSuite.getHelloworldRepository();
223         RevCommit commit = JGitUtils.getCommit(repository,
224                 "1d0c2933a4ae69c362f76797d42d6bd182d05176");
225         List<PathChangeModel> paths = JGitUtils.getFilesInCommit(repository, commit);
a125cf 226
JM 227         commit = JGitUtils.getCommit(repository, "af0e9b2891fda85afc119f04a69acf7348922830");
228         List<PathChangeModel> deletions = JGitUtils.getFilesInCommit(repository, commit);
229
230         commit = JGitUtils.getFirstCommit(repository, null);
231         List<PathChangeModel> additions = JGitUtils.getFilesInCommit(repository, commit);
232
233         List<PathChangeModel> latestChanges = JGitUtils.getFilesInCommit(repository, null);
234
2a7306 235         repository.close();
JM 236         assertTrue("No changed paths found!", paths.size() == 1);
28d6b2 237         for (PathChangeModel path : paths) {
JM 238             assertTrue("PathChangeModel hashcode incorrect!",
239                     path.hashCode() == (path.commitId.hashCode() + path.path.hashCode()));
240             assertTrue("PathChangeModel equals itself failed!", path.equals(path));
241             assertFalse("PathChangeModel equals string failed!", path.equals(""));
242         }
a125cf 243         assertTrue(deletions.get(0).changeType.equals(ChangeType.DELETE));
JM 244         assertTrue(additions.get(0).changeType.equals(ChangeType.ADD));
245         assertTrue(latestChanges.size() > 0);
246     }
247
248     public void testFilesInPath() throws Exception {
249         assertTrue(JGitUtils.getFilesInPath(null, null, null).size() == 0);
250         Repository repository = GitBlitSuite.getHelloworldRepository();
251         List<PathModel> files = JGitUtils.getFilesInPath(repository, null, null);
252         repository.close();
253         assertTrue(files.size() > 10);
254     }
255
256     public void testDocuments() throws Exception {
257         Repository repository = GitBlitSuite.getTicgitRepository();
258         List<String> extensions = GitBlit.getStrings(Keys.web.markdownExtensions);
259         List<PathModel> markdownDocs = JGitUtils.getDocuments(repository, extensions);
260         List<PathModel> markdownDocs2 = JGitUtils.getDocuments(repository,
261                 Arrays.asList(new String[] { ".mkd", ".md" }));
262         List<PathModel> allFiles = JGitUtils.getDocuments(repository, null);
263         repository.close();
264         assertTrue(markdownDocs.size() > 0);
265         assertTrue(markdownDocs2.size() > 0);
266         assertTrue(allFiles.size() > markdownDocs.size());
267     }
268
269     public void testFileModes() throws Exception {
270         assertTrue(JGitUtils.getPermissionsFromMode(FileMode.TREE.getBits()).equals("drwxr-xr-x"));
271         assertTrue(JGitUtils.getPermissionsFromMode(FileMode.REGULAR_FILE.getBits()).equals(
272                 "-rw-r--r--"));
273         assertTrue(JGitUtils.getPermissionsFromMode(FileMode.EXECUTABLE_FILE.getBits()).equals(
274                 "-rwxr-xr-x"));
275         assertTrue(JGitUtils.getPermissionsFromMode(FileMode.SYMLINK.getBits()).equals("symlink"));
276         assertTrue(JGitUtils.getPermissionsFromMode(FileMode.GITLINK.getBits()).equals("gitlink"));
277         assertTrue(JGitUtils.getPermissionsFromMode(FileMode.MISSING.getBits()).equals("missing"));
278     }
279
280     public void testRevlog() throws Exception {
85c2e6 281         assertTrue(JGitUtils.getRevLog(null, 0).size() == 0);
a125cf 282         List<RevCommit> commits = JGitUtils.getRevLog(null, 10);
JM 283         assertTrue(commits.size() == 0);
284
285         Repository repository = GitBlitSuite.getHelloworldRepository();
286         // get most recent 10 commits
287         commits = JGitUtils.getRevLog(repository, 10);
288         assertTrue(commits.size() == 10);
289
290         // test paging and offset by getting the 10th most recent commit
291         RevCommit lastCommit = JGitUtils.getRevLog(repository, null, 9, 1).get(0);
292         assertTrue(commits.get(9).equals(lastCommit));
293
294         // grab the two most recent commits to java.java
295         commits = JGitUtils.getRevLog(repository, null, "java.java", 0, 2);
296         assertTrue(commits.size() == 2);
297         repository.close();
298     }
299
300     public void testSearchTypes() throws Exception {
301         assertTrue(SearchType.forName("commit").equals(SearchType.COMMIT));
302         assertTrue(SearchType.forName("committer").equals(SearchType.COMMITTER));
303         assertTrue(SearchType.forName("author").equals(SearchType.AUTHOR));
304         assertTrue(SearchType.forName("unknown").equals(SearchType.COMMIT));
305
306         assertTrue(SearchType.COMMIT.toString().equals("commit"));
307         assertTrue(SearchType.COMMITTER.toString().equals("committer"));
308         assertTrue(SearchType.AUTHOR.toString().equals("author"));
309     }
310
311     public void testSearchRevlogs() throws Exception {
85c2e6 312         assertTrue(JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0, 0).size() == 0);
a125cf 313         List<RevCommit> results = JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0,
JM 314                 3);
315         assertTrue(results.size() == 0);
316
317         // test commit message search
318         Repository repository = GitBlitSuite.getHelloworldRepository();
319         results = JGitUtils.searchRevlogs(repository, null, "java", SearchType.COMMIT, 0, 3);
320         assertTrue(results.size() == 3);
321
322         // test author search
323         results = JGitUtils.searchRevlogs(repository, null, "timothy", SearchType.AUTHOR, 0, -1);
324         assertTrue(results.size() == 1);
325
326         // test committer search
327         results = JGitUtils.searchRevlogs(repository, null, "mike", SearchType.COMMITTER, 0, 10);
328         assertTrue(results.size() == 10);
329
330         // test paging and offset
331         RevCommit commit = JGitUtils.searchRevlogs(repository, null, "mike", SearchType.COMMITTER,
332                 9, 1).get(0);
333         assertTrue(results.get(9).equals(commit));
334
335         repository.close();
2a7306 336     }
JM 337
338     public void testZip() throws Exception {
a125cf 339         assertFalse(JGitUtils.zip(null, null, null, null));
2a7306 340         Repository repository = GitBlitSuite.getHelloworldRepository();
a125cf 341         File zipFileA = new File(GitBlitSuite.REPOSITORIES, "helloworld.zip");
JM 342         FileOutputStream fosA = new FileOutputStream(zipFileA);
343         boolean successA = JGitUtils.zip(repository, null, Constants.HEAD, fosA);
344         fosA.close();
345
346         File zipFileB = new File(GitBlitSuite.REPOSITORIES, "helloworld-java.zip");
347         FileOutputStream fosB = new FileOutputStream(zipFileB);
348         boolean successB = JGitUtils.zip(repository, "java.java", Constants.HEAD, fosB);
349         fosB.close();
350
2a7306 351         repository.close();
a125cf 352         assertTrue("Failed to generate zip file!", successA);
JM 353         assertTrue(zipFileA.length() > 0);
354         zipFileA.delete();
355
356         assertTrue("Failed to generate zip file!", successB);
357         assertTrue(zipFileB.length() > 0);
358         zipFileB.delete();
2a7306 359     }
JM 360 }