James Moger
2012-02-03 fe7c01a8bd76dff240e74bb770212911e227ba59
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     }
90b8d7 215     
JM 216     @Test
217     public void testRelinkHEAD() throws Exception {
218         Repository repository = GitBlitSuite.getJGitRepository();
219         // confirm HEAD is master
220         String currentRef = JGitUtils.getHEADRef(repository);
221         assertEquals("refs/heads/master", currentRef);
222         List<String> availableHeads = JGitUtils.getAvailableHeadTargets(repository);
223         assertTrue(availableHeads.size() > 0);
224         
225         // set HEAD to stable-1.2
226         JGitUtils.setHEADtoRef(repository, "refs/heads/stable-1.2");
227         currentRef = JGitUtils.getHEADRef(repository);
228         assertEquals("refs/heads/stable-1.2", currentRef);
229
230         // restore HEAD to master
231         JGitUtils.setHEADtoRef(repository, "refs/heads/master");
232         currentRef = JGitUtils.getHEADRef(repository);
233         assertEquals("refs/heads/master", currentRef);
234         
235         repository.close();
236     }
2a7306 237
7e8873 238     @Test
a2709d 239     public void testCreateOrphanedBranch() throws Exception {
671c19 240         Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, "orphantest");
a2709d 241         assertTrue(JGitUtils.createOrphanBranch(repository,
7b0895 242                 "x" + Long.toHexString(System.currentTimeMillis()).toUpperCase(), null));
JM 243          FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
a2709d 244     }
JM 245
7e8873 246     @Test
a125cf 247     public void testStringContent() throws Exception {
2a7306 248         Repository repository = GitBlitSuite.getHelloworldRepository();
4ab184 249         String contentA = JGitUtils.getStringContent(repository, null, "java.java");
2a7306 250         RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
4ab184 251         String contentB = JGitUtils.getStringContent(repository, commit.getTree(), "java.java");
JM 252         String contentC = JGitUtils.getStringContent(repository, commit.getTree(), "missing.txt");
253
254         // manually construct a blob, calculate the hash, lookup the hash in git
255         StringBuilder sb = new StringBuilder();
256         sb.append("blob ").append(contentA.length()).append('\0');
257         sb.append(contentA);
258         String sha1 = StringUtils.getSHA1(sb.toString());
259         String contentD = JGitUtils.getStringContent(repository, sha1);
2a7306 260         repository.close();
a125cf 261         assertTrue("ContentA is null!", contentA != null && contentA.length() > 0);
JM 262         assertTrue("ContentB is null!", contentB != null && contentB.length() > 0);
263         assertTrue(contentA.equals(contentB));
7e8873 264         assertNull(contentC);
4ab184 265         assertTrue(contentA.equals(contentD));
2a7306 266     }
JM 267
7e8873 268     @Test
2a7306 269     public void testFilesInCommit() throws Exception {
JM 270         Repository repository = GitBlitSuite.getHelloworldRepository();
271         RevCommit commit = JGitUtils.getCommit(repository,
272                 "1d0c2933a4ae69c362f76797d42d6bd182d05176");
273         List<PathChangeModel> paths = JGitUtils.getFilesInCommit(repository, commit);
a125cf 274
JM 275         commit = JGitUtils.getCommit(repository, "af0e9b2891fda85afc119f04a69acf7348922830");
276         List<PathChangeModel> deletions = JGitUtils.getFilesInCommit(repository, commit);
277
278         commit = JGitUtils.getFirstCommit(repository, null);
279         List<PathChangeModel> additions = JGitUtils.getFilesInCommit(repository, commit);
280
281         List<PathChangeModel> latestChanges = JGitUtils.getFilesInCommit(repository, null);
282
2a7306 283         repository.close();
JM 284         assertTrue("No changed paths found!", paths.size() == 1);
28d6b2 285         for (PathChangeModel path : paths) {
JM 286             assertTrue("PathChangeModel hashcode incorrect!",
287                     path.hashCode() == (path.commitId.hashCode() + path.path.hashCode()));
288             assertTrue("PathChangeModel equals itself failed!", path.equals(path));
289             assertFalse("PathChangeModel equals string failed!", path.equals(""));
290         }
7e8873 291         assertEquals(ChangeType.DELETE, deletions.get(0).changeType);
JM 292         assertEquals(ChangeType.ADD, additions.get(0).changeType);
a125cf 293         assertTrue(latestChanges.size() > 0);
JM 294     }
295
7e8873 296     @Test
a125cf 297     public void testFilesInPath() throws Exception {
7e8873 298         assertEquals(0, JGitUtils.getFilesInPath(null, null, null).size());
a125cf 299         Repository repository = GitBlitSuite.getHelloworldRepository();
JM 300         List<PathModel> files = JGitUtils.getFilesInPath(repository, null, null);
301         repository.close();
302         assertTrue(files.size() > 10);
303     }
304
7e8873 305     @Test
a125cf 306     public void testDocuments() throws Exception {
JM 307         Repository repository = GitBlitSuite.getTicgitRepository();
308         List<String> extensions = GitBlit.getStrings(Keys.web.markdownExtensions);
309         List<PathModel> markdownDocs = JGitUtils.getDocuments(repository, extensions);
310         List<PathModel> markdownDocs2 = JGitUtils.getDocuments(repository,
311                 Arrays.asList(new String[] { ".mkd", ".md" }));
312         List<PathModel> allFiles = JGitUtils.getDocuments(repository, null);
313         repository.close();
314         assertTrue(markdownDocs.size() > 0);
315         assertTrue(markdownDocs2.size() > 0);
316         assertTrue(allFiles.size() > markdownDocs.size());
317     }
318
7e8873 319     @Test
a125cf 320     public void testFileModes() throws Exception {
7e8873 321         assertEquals("drwxr-xr-x", JGitUtils.getPermissionsFromMode(FileMode.TREE.getBits()));
JM 322         assertEquals("-rw-r--r--",
323                 JGitUtils.getPermissionsFromMode(FileMode.REGULAR_FILE.getBits()));
324         assertEquals("-rwxr-xr-x",
325                 JGitUtils.getPermissionsFromMode(FileMode.EXECUTABLE_FILE.getBits()));
326         assertEquals("symlink", JGitUtils.getPermissionsFromMode(FileMode.SYMLINK.getBits()));
327         assertEquals("gitlink", JGitUtils.getPermissionsFromMode(FileMode.GITLINK.getBits()));
328         assertEquals("missing", JGitUtils.getPermissionsFromMode(FileMode.MISSING.getBits()));
a125cf 329     }
JM 330
7e8873 331     @Test
a125cf 332     public void testRevlog() throws Exception {
85c2e6 333         assertTrue(JGitUtils.getRevLog(null, 0).size() == 0);
a125cf 334         List<RevCommit> commits = JGitUtils.getRevLog(null, 10);
2f1c77 335         assertEquals(0, commits.size());
a125cf 336
JM 337         Repository repository = GitBlitSuite.getHelloworldRepository();
338         // get most recent 10 commits
339         commits = JGitUtils.getRevLog(repository, 10);
2f1c77 340         assertEquals(10, commits.size());
a125cf 341
JM 342         // test paging and offset by getting the 10th most recent commit
343         RevCommit lastCommit = JGitUtils.getRevLog(repository, null, 9, 1).get(0);
2f1c77 344         assertEquals(lastCommit, commits.get(9));
a125cf 345
JM 346         // grab the two most recent commits to java.java
347         commits = JGitUtils.getRevLog(repository, null, "java.java", 0, 2);
2f1c77 348         assertEquals(2, commits.size());
7e8873 349
2f1c77 350         // grab the commits since 2008-07-15
7e8873 351         commits = JGitUtils.getRevLog(repository, null,
JM 352                 new SimpleDateFormat("yyyy-MM-dd").parse("2008-07-15"));
2f1c77 353         assertEquals(12, commits.size());
a125cf 354         repository.close();
JM 355     }
356
7e8873 357     @Test
f47590 358     public void testRevLogRange() throws Exception {
JM 359         Repository repository = GitBlitSuite.getHelloworldRepository();
360         List<RevCommit> commits = JGitUtils.getRevLog(repository,
361                 "fbd14fa6d1a01d4aefa1fca725792683800fc67e",
362                 "85a0e4087b8439c0aa6b1f4f9e08c26052ab7e87");
363         repository.close();
364         assertEquals(14, commits.size());
365     }
366
367     @Test
a125cf 368     public void testSearchTypes() throws Exception {
7e8873 369         assertEquals(SearchType.COMMIT, SearchType.forName("commit"));
JM 370         assertEquals(SearchType.COMMITTER, SearchType.forName("committer"));
371         assertEquals(SearchType.AUTHOR, SearchType.forName("author"));
372         assertEquals(SearchType.COMMIT, SearchType.forName("unknown"));
a125cf 373
7e8873 374         assertEquals("commit", SearchType.COMMIT.toString());
JM 375         assertEquals("committer", SearchType.COMMITTER.toString());
376         assertEquals("author", SearchType.AUTHOR.toString());
a125cf 377     }
JM 378
7e8873 379     @Test
a125cf 380     public void testSearchRevlogs() throws Exception {
7e8873 381         assertEquals(0, JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0, 0).size());
JM 382         List<RevCommit> results = JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0,
a125cf 383                 3);
7e8873 384         assertEquals(0, results.size());
a125cf 385
JM 386         // test commit message search
387         Repository repository = GitBlitSuite.getHelloworldRepository();
7e8873 388         results = JGitUtils.searchRevlogs(repository, null, "java", SearchType.COMMIT, 0, 3);
JM 389         assertEquals(3, results.size());
a125cf 390
JM 391         // test author search
7e8873 392         results = JGitUtils.searchRevlogs(repository, null, "timothy", SearchType.AUTHOR, 0, -1);
JM 393         assertEquals(1, results.size());
a125cf 394
JM 395         // test committer search
7e8873 396         results = JGitUtils.searchRevlogs(repository, null, "mike", SearchType.COMMITTER, 0, 10);
JM 397         assertEquals(10, results.size());
a125cf 398
JM 399         // test paging and offset
7e8873 400         RevCommit commit = JGitUtils.searchRevlogs(repository, null, "mike", SearchType.COMMITTER,
a125cf 401                 9, 1).get(0);
7e8873 402         assertEquals(results.get(9), commit);
a125cf 403
JM 404         repository.close();
2a7306 405     }
JM 406
7e8873 407     @Test
2a7306 408     public void testZip() throws Exception {
a125cf 409         assertFalse(JGitUtils.zip(null, null, null, null));
2a7306 410         Repository repository = GitBlitSuite.getHelloworldRepository();
a125cf 411         File zipFileA = new File(GitBlitSuite.REPOSITORIES, "helloworld.zip");
JM 412         FileOutputStream fosA = new FileOutputStream(zipFileA);
413         boolean successA = JGitUtils.zip(repository, null, Constants.HEAD, fosA);
414         fosA.close();
415
416         File zipFileB = new File(GitBlitSuite.REPOSITORIES, "helloworld-java.zip");
417         FileOutputStream fosB = new FileOutputStream(zipFileB);
418         boolean successB = JGitUtils.zip(repository, "java.java", Constants.HEAD, fosB);
419         fosB.close();
420
2a7306 421         repository.close();
a125cf 422         assertTrue("Failed to generate zip file!", successA);
JM 423         assertTrue(zipFileA.length() > 0);
424         zipFileA.delete();
425
426         assertTrue("Failed to generate zip file!", successB);
427         assertTrue(zipFileB.length() > 0);
428         zipFileB.delete();
2a7306 429     }
a2709d 430
2a7306 431 }