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