James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
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;
2f1c77 20 import java.text.SimpleDateFormat;
a125cf 21 import java.util.Arrays;
2a7306 22 import java.util.Date;
JM 23 import java.util.List;
a125cf 24 import java.util.Map;
2a7306 25
a125cf 26 import org.eclipse.jgit.diff.DiffEntry.ChangeType;
2a7306 27 import org.eclipse.jgit.lib.Constants;
a125cf 28 import org.eclipse.jgit.lib.FileMode;
JM 29 import org.eclipse.jgit.lib.ObjectId;
30 import org.eclipse.jgit.lib.PersonIdent;
2a7306 31 import org.eclipse.jgit.lib.Repository;
20714a 32 import org.eclipse.jgit.lib.RepositoryCache;
168566 33 import org.eclipse.jgit.lib.RepositoryCache.FileKey;
f084f4 34 import org.eclipse.jgit.revplot.PlotCommit;
JM 35 import org.eclipse.jgit.revplot.PlotCommitList;
36 import org.eclipse.jgit.revplot.PlotLane;
37 import org.eclipse.jgit.revplot.PlotWalk;
2a7306 38 import org.eclipse.jgit.revwalk.RevCommit;
ae9e15 39 import org.eclipse.jgit.revwalk.RevTree;
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;
4ab184 45 import com.gitblit.models.GitNote;
a125cf 46 import com.gitblit.models.PathModel;
1f9dae 47 import com.gitblit.models.PathModel.PathChangeModel;
JM 48 import com.gitblit.models.RefModel;
59b817 49 import com.gitblit.utils.CompressionUtils;
2a7306 50 import com.gitblit.utils.JGitUtils;
e85277 51 import com.gitblit.utils.JnaUtils;
4ab184 52 import com.gitblit.utils.StringUtils;
2a7306 53
db4f6b 54 public class JGitUtilsTest extends GitblitUnitTest {
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() {
0adceb 69         List<String> list = JGitUtils.getRepositoryList(null, false, true, -1, null);
7e8873 70         assertEquals(0, list.size());
0adceb 71         list.addAll(JGitUtils.getRepositoryList(new File("DoesNotExist"), true, true, -1, null));
7e8873 72         assertEquals(0, list.size());
0adceb 73         list.addAll(JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, null));
2a7306 74         assertTrue("No repositories found in " + GitBlitSuite.REPOSITORIES, list.size() > 0);
JM 75     }
76
7e8873 77     @Test
0adceb 78     public void testFindExclusions() {
JM 79         List<String> list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, null);
80         assertTrue("Missing jgit repository?!", list.contains("test/jgit.git"));
81
82         list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("test/jgit\\.git"));
83         assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));
84
85         list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList("test/*"));
86         assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));
87
bb55f5 88         list = JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, false, true, -1, Arrays.asList(".*jgit.*"));
0adceb 89         assertFalse("Repository exclusion failed!", list.contains("test/jgit.git"));
JM 90         assertFalse("Repository exclusion failed!", list.contains("working/jgit"));
91         assertFalse("Repository exclusion failed!", list.contains("working/jgit2"));
92
93     }
94
95     @Test
2a7306 96     public void testOpenRepository() throws Exception {
JM 97         Repository repository = GitBlitSuite.getHelloworldRepository();
98         repository.close();
7e8873 99         assertNotNull("Could not find repository!", repository);
2a7306 100     }
JM 101
7e8873 102     @Test
2a7306 103     public void testFirstCommit() throws Exception {
7e8873 104         assertEquals(new Date(0), JGitUtils.getFirstChange(null, null));
a125cf 105
2a7306 106         Repository repository = GitBlitSuite.getHelloworldRepository();
JM 107         RevCommit commit = JGitUtils.getFirstCommit(repository, null);
db653a 108         Date firstChange = JGitUtils.getFirstChange(repository, null);
2a7306 109         repository.close();
7e8873 110         assertNotNull("Could not get first commit!", commit);
JM 111         assertEquals("Incorrect first commit!", "f554664a346629dc2b839f7292d06bad2db4aece",
112                 commit.getName());
db653a 113         assertTrue(firstChange.equals(new Date(commit.getCommitTime() * 1000L)));
f1720c 114     }
a125cf 115
7e8873 116     @Test
f1720c 117     public void testLastCommit() throws Exception {
5c5b7a 118         assertEquals(new Date(0), JGitUtils.getLastChange(null).when);
a125cf 119
f1720c 120         Repository repository = GitBlitSuite.getHelloworldRepository();
JM 121         assertTrue(JGitUtils.getCommit(repository, null) != null);
5c5b7a 122         Date date = JGitUtils.getLastChange(repository).when;
f1720c 123         repository.close();
7e8873 124         assertNotNull("Could not get last repository change date!", date);
f1720c 125     }
JM 126
7e8873 127     @Test
f1720c 128     public void testCreateRepository() throws Exception {
JM 129         String[] repositories = { "NewTestRepository.git", "NewTestRepository" };
008322 130         for (String repositoryName : repositories) {
f1720c 131             Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
168566 132                     repositoryName);
008322 133             File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName),
JM 134                     FS.DETECTED);
7e8873 135             assertNotNull(repository);
f1720c 136             assertFalse(JGitUtils.hasCommits(repository));
7e8873 137             assertNull(JGitUtils.getFirstCommit(repository, null));
JM 138             assertEquals(folder.lastModified(), JGitUtils.getFirstChange(repository, null)
139                     .getTime());
5c5b7a 140             assertEquals(folder.lastModified(), JGitUtils.getLastChange(repository).when.getTime());
7e8873 141             assertNull(JGitUtils.getCommit(repository, null));
f1720c 142             repository.close();
20714a 143             RepositoryCache.close(repository);
JM 144             FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
f1720c 145         }
2a7306 146     }
JM 147
7e8873 148     @Test
8a67d9 149     public void testCreateRepositoryShared() throws Exception {
e85277 150         String[] repositories = { "NewSharedTestRepository.git" };
8a67d9 151         for (String repositoryName : repositories) {
FZ 152             Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
153                     repositoryName, "group");
154             File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName),
155                     FS.DETECTED);
156             assertNotNull(repository);
157             assertFalse(JGitUtils.hasCommits(repository));
158             assertNull(JGitUtils.getFirstCommit(repository, null));
e85277 159
9d2f66 160             assertEquals("1", repository.getConfig().getString("core", null, "sharedRepository"));
FZ 161
e85277 162             assertTrue(folder.exists());
9d2f66 163             if (! JnaUtils.isWindows()) {
FZ 164                 int mode = JnaUtils.getFilemode(folder);
165                 assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);
166                 assertEquals(JnaUtils.S_IRWXG, mode & JnaUtils.S_IRWXG);
e85277 167
9d2f66 168                 mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD");
FZ 169                 assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
e85277 170
9d2f66 171                 mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
FZ 172                 assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
173             }
e85277 174
8a67d9 175             repository.close();
FZ 176             RepositoryCache.close(repository);
e85277 177             FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
8a67d9 178         }
FZ 179     }
180
181     @Test
8b5730 182     public void testCreateRepositorySharedCustom() throws Exception {
FZ 183         String[] repositories = { "NewSharedTestRepository.git" };
184         for (String repositoryName : repositories) {
185             Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
186                     repositoryName, "660");
187             File folder = FileKey.resolve(new File(GitBlitSuite.REPOSITORIES, repositoryName),
188                     FS.DETECTED);
189             assertNotNull(repository);
190             assertFalse(JGitUtils.hasCommits(repository));
191             assertNull(JGitUtils.getFirstCommit(repository, null));
192
193             assertEquals("0660", repository.getConfig().getString("core", null, "sharedRepository"));
194
195             assertTrue(folder.exists());
196             if (! JnaUtils.isWindows()) {
197                 int mode = JnaUtils.getFilemode(folder);
198                 assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);
199                 assertEquals(JnaUtils.S_IRWXG, mode & JnaUtils.S_IRWXG);
200                 assertEquals(0, mode & JnaUtils.S_IRWXO);
201
202                 mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD");
203                 assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
204                 assertEquals(0, mode & JnaUtils.S_IRWXO);
205
206                 mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
207                 assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
208                 assertEquals(0, mode & JnaUtils.S_IRWXO);
209             }
210
211             repository.close();
212             RepositoryCache.close(repository);
213             FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
214         }
215     }
216
217     @Test
b72444 218     public void testCreateRepositorySharedSgidParent() throws Exception {
FZ 219         if (! JnaUtils.isWindows()) {
220             String repositoryAll = "NewTestRepositoryAll.git";
221             String repositoryUmask = "NewTestRepositoryUmask.git";
222             String sgidParent = "sgid";
c00577 223
b72444 224             File parent = new File(GitBlitSuite.REPOSITORIES, sgidParent);
FZ 225             File folder = null;
226             boolean parentExisted = parent.exists();
227             try {
228                 if (!parentExisted) {
229                     assertTrue("Could not create SGID parent folder.", parent.mkdir());
230                 }
231                 int mode = JnaUtils.getFilemode(parent);
232                 assertTrue(mode > 0);
233                 assertEquals(0, JnaUtils.setFilemode(parent, mode | JnaUtils.S_ISGID | JnaUtils.S_IWGRP));
234
235                 Repository repository = JGitUtils.createRepository(parent, repositoryAll, "all");
236                 folder = FileKey.resolve(new File(parent, repositoryAll), FS.DETECTED);
237                 assertNotNull(repository);
c00577 238
b72444 239                 assertEquals("2", repository.getConfig().getString("core", null, "sharedRepository"));
c00577 240
b72444 241                 assertTrue(folder.exists());
FZ 242                 mode = JnaUtils.getFilemode(folder);
243                 assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);
c00577 244
b72444 245                 mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD");
FZ 246                 assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
247                 assertEquals(JnaUtils.S_IROTH, mode & JnaUtils.S_IRWXO);
c00577 248
b72444 249                 mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");
FZ 250                 assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);
251                 assertEquals(JnaUtils.S_IROTH, mode & JnaUtils.S_IRWXO);
c00577 252
b72444 253                 repository.close();
FZ 254                 RepositoryCache.close(repository);
255
256
257
258                 repository = JGitUtils.createRepository(parent, repositoryUmask, "umask");
259                 folder = FileKey.resolve(new File(parent, repositoryUmask), FS.DETECTED);
260                 assertNotNull(repository);
c00577 261
b72444 262                 assertEquals(null, repository.getConfig().getString("core", null, "sharedRepository"));
c00577 263
b72444 264                 assertTrue(folder.exists());
FZ 265                 mode = JnaUtils.getFilemode(folder);
266                 assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);
c00577 267
b72444 268                 repository.close();
FZ 269                 RepositoryCache.close(repository);
270             }
271             finally {
272                 FileUtils.delete(new File(parent, repositoryAll), FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
273                 FileUtils.delete(new File(parent, repositoryUmask), FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
274                 if (!parentExisted) {
275                     FileUtils.delete(parent, FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
276                 }
277             }
278         }
279     }
280
281     @Test
28d6b2 282     public void testRefs() throws Exception {
4ab184 283         Repository repository = GitBlitSuite.getJGitRepository();
JM 284         Map<ObjectId, List<RefModel>> map = JGitUtils.getAllRefs(repository);
a125cf 285         repository.close();
JM 286         assertTrue(map.size() > 0);
4ab184 287         for (Map.Entry<ObjectId, List<RefModel>> entry : map.entrySet()) {
JM 288             List<RefModel> list = entry.getValue();
289             for (RefModel ref : list) {
290                 if (ref.displayName.equals("refs/tags/spearce-gpg-pub")) {
7e8873 291                     assertEquals("refs/tags/spearce-gpg-pub", ref.toString());
JM 292                     assertEquals("8bbde7aacf771a9afb6992434f1ae413e010c6d8", ref.getObjectId()
293                             .getName());
294                     assertEquals("spearce@spearce.org", ref.getAuthorIdent().getEmailAddress());
4ab184 295                     assertTrue(ref.getShortMessage().startsWith("GPG key"));
008322 296                     assertTrue(ref.getFullMessage().startsWith("GPG key"));
7e8873 297                     assertEquals(Constants.OBJ_BLOB, ref.getReferencedObjectType());
4ab184 298                 } else if (ref.displayName.equals("refs/tags/v0.12.1")) {
JM 299                     assertTrue(ref.isAnnotatedTag());
300                 }
301             }
302         }
a125cf 303     }
JM 304
7e8873 305     @Test
a125cf 306     public void testBranches() throws Exception {
168566 307         Repository repository = GitBlitSuite.getJGitRepository();
85c2e6 308         assertTrue(JGitUtils.getLocalBranches(repository, true, 0).size() == 0);
5cc4f2 309         for (RefModel model : JGitUtils.getLocalBranches(repository, true, -1)) {
28d6b2 310             assertTrue(model.getName().startsWith(Constants.R_HEADS));
JM 311             assertTrue(model.equals(model));
312             assertFalse(model.equals(""));
4ab184 313             assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
28d6b2 314                     + model.getName().hashCode());
4ab184 315             assertTrue(model.getShortMessage().equals(model.getShortMessage()));
28d6b2 316         }
5cc4f2 317         for (RefModel model : JGitUtils.getRemoteBranches(repository, true, -1)) {
28d6b2 318             assertTrue(model.getName().startsWith(Constants.R_REMOTES));
JM 319             assertTrue(model.equals(model));
320             assertFalse(model.equals(""));
4ab184 321             assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
28d6b2 322                     + model.getName().hashCode());
4ab184 323             assertTrue(model.getShortMessage().equals(model.getShortMessage()));
28d6b2 324         }
168566 325         assertTrue(JGitUtils.getRemoteBranches(repository, true, 8).size() == 8);
a125cf 326         repository.close();
JM 327     }
328
7e8873 329     @Test
a125cf 330     public void testTags() throws Exception {
168566 331         Repository repository = GitBlitSuite.getJGitRepository();
85c2e6 332         assertTrue(JGitUtils.getTags(repository, true, 5).size() == 5);
5cc4f2 333         for (RefModel model : JGitUtils.getTags(repository, true, -1)) {
168566 334             if (model.getObjectId().getName().equals("d28091fb2977077471138fe97da1440e0e8ae0da")) {
28d6b2 335                 assertTrue("Not an annotated tag!", model.isAnnotatedTag());
JM 336             }
337             assertTrue(model.getName().startsWith(Constants.R_TAGS));
338             assertTrue(model.equals(model));
339             assertFalse(model.equals(""));
4ab184 340             assertTrue(model.hashCode() == model.getReferencedObjectId().hashCode()
28d6b2 341                     + model.getName().hashCode());
4ab184 342         }
JM 343         repository.close();
008322 344
d3065f 345         repository = GitBlitSuite.getGitectiveRepository();
5cc4f2 346         for (RefModel model : JGitUtils.getTags(repository, true, -1)) {
d3065f 347             if (model.getObjectId().getName().equals("035254295a9bba11f72b1f9d6791a6b957abee7b")) {
4ab184 348                 assertFalse(model.isAnnotatedTag());
d3065f 349                 assertTrue(model.getAuthorIdent().getEmailAddress().equals("kevinsawicki@gmail.com"));
JM 350                 assertEquals("Add scm and issue tracker elements to pom.xml\n", model.getFullMessage());
4ab184 351             }
28d6b2 352         }
JM 353         repository.close();
354     }
355
7e8873 356     @Test
a125cf 357     public void testCommitNotes() throws Exception {
4ab184 358         Repository repository = GitBlitSuite.getJGitRepository();
JM 359         RevCommit commit = JGitUtils.getCommit(repository,
360                 "690c268c793bfc218982130fbfc25870f292295e");
361         List<GitNote> list = JGitUtils.getNotesOnCommit(repository, commit);
362         repository.close();
363         assertTrue(list.size() > 0);
7e8873 364         assertEquals("183474d554e6f68478a02d9d7888b67a9338cdff", list.get(0).notesRef
JM 365                 .getReferencedObjectId().getName());
2a7306 366     }
c00577 367
90b8d7 368     @Test
JM 369     public void testRelinkHEAD() throws Exception {
370         Repository repository = GitBlitSuite.getJGitRepository();
371         // confirm HEAD is master
372         String currentRef = JGitUtils.getHEADRef(repository);
373         assertEquals("refs/heads/master", currentRef);
374         List<String> availableHeads = JGitUtils.getAvailableHeadTargets(repository);
375         assertTrue(availableHeads.size() > 0);
c00577 376
90b8d7 377         // set HEAD to stable-1.2
JM 378         JGitUtils.setHEADtoRef(repository, "refs/heads/stable-1.2");
379         currentRef = JGitUtils.getHEADRef(repository);
380         assertEquals("refs/heads/stable-1.2", currentRef);
381
382         // restore HEAD to master
383         JGitUtils.setHEADtoRef(repository, "refs/heads/master");
384         currentRef = JGitUtils.getHEADRef(repository);
385         assertEquals("refs/heads/master", currentRef);
c00577 386
90b8d7 387         repository.close();
JM 388     }
2a7306 389
7e8873 390     @Test
f96d32 391     public void testRelinkBranch() throws Exception {
JM 392         Repository repository = GitBlitSuite.getJGitRepository();
c00577 393
f96d32 394         // create/set the branch
JM 395         JGitUtils.setBranchRef(repository, "refs/heads/reftest", "3b358ce514ec655d3ff67de1430994d8428cdb04");
396         assertEquals(1, JGitUtils.getAllRefs(repository).get(ObjectId.fromString("3b358ce514ec655d3ff67de1430994d8428cdb04")).size());
397         assertEquals(null, JGitUtils.getAllRefs(repository).get(ObjectId.fromString("755dfdb40948f5c1ec79e06bde3b0a78c352f27f")));
c00577 398
f96d32 399         // reset the branch
JM 400         JGitUtils.setBranchRef(repository, "refs/heads/reftest", "755dfdb40948f5c1ec79e06bde3b0a78c352f27f");
401         assertEquals(null, JGitUtils.getAllRefs(repository).get(ObjectId.fromString("3b358ce514ec655d3ff67de1430994d8428cdb04")));
402         assertEquals(1, JGitUtils.getAllRefs(repository).get(ObjectId.fromString("755dfdb40948f5c1ec79e06bde3b0a78c352f27f")).size());
403
404         // delete the branch
405         assertTrue(JGitUtils.deleteBranchRef(repository, "refs/heads/reftest"));
406         repository.close();
407     }
408
409     @Test
a2709d 410     public void testCreateOrphanedBranch() throws Exception {
671c19 411         Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, "orphantest");
a2709d 412         assertTrue(JGitUtils.createOrphanBranch(repository,
7b0895 413                 "x" + Long.toHexString(System.currentTimeMillis()).toUpperCase(), null));
JM 414          FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
a2709d 415     }
JM 416
7e8873 417     @Test
a125cf 418     public void testStringContent() throws Exception {
2a7306 419         Repository repository = GitBlitSuite.getHelloworldRepository();
ae9e15 420         String contentA = JGitUtils.getStringContent(repository, (RevTree) null, "java.java");
2a7306 421         RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
4ab184 422         String contentB = JGitUtils.getStringContent(repository, commit.getTree(), "java.java");
74344a 423
JM 424         assertTrue("ContentA is null!", contentA != null && contentA.length() > 0);
425         assertTrue("ContentB is null!", contentB != null && contentB.length() > 0);
426         assertTrue(contentA.equals(contentB));
427
4ab184 428         String contentC = JGitUtils.getStringContent(repository, commit.getTree(), "missing.txt");
JM 429
430         // manually construct a blob, calculate the hash, lookup the hash in git
431         StringBuilder sb = new StringBuilder();
432         sb.append("blob ").append(contentA.length()).append('\0');
433         sb.append(contentA);
434         String sha1 = StringUtils.getSHA1(sb.toString());
435         String contentD = JGitUtils.getStringContent(repository, sha1);
2a7306 436         repository.close();
7e8873 437         assertNull(contentC);
4ab184 438         assertTrue(contentA.equals(contentD));
2a7306 439     }
JM 440
7e8873 441     @Test
2a7306 442     public void testFilesInCommit() throws Exception {
JM 443         Repository repository = GitBlitSuite.getHelloworldRepository();
444         RevCommit commit = JGitUtils.getCommit(repository,
445                 "1d0c2933a4ae69c362f76797d42d6bd182d05176");
446         List<PathChangeModel> paths = JGitUtils.getFilesInCommit(repository, commit);
a125cf 447
JM 448         commit = JGitUtils.getCommit(repository, "af0e9b2891fda85afc119f04a69acf7348922830");
449         List<PathChangeModel> deletions = JGitUtils.getFilesInCommit(repository, commit);
450
451         commit = JGitUtils.getFirstCommit(repository, null);
452         List<PathChangeModel> additions = JGitUtils.getFilesInCommit(repository, commit);
453
454         List<PathChangeModel> latestChanges = JGitUtils.getFilesInCommit(repository, null);
455
2a7306 456         repository.close();
JM 457         assertTrue("No changed paths found!", paths.size() == 1);
28d6b2 458         for (PathChangeModel path : paths) {
JM 459             assertTrue("PathChangeModel hashcode incorrect!",
460                     path.hashCode() == (path.commitId.hashCode() + path.path.hashCode()));
461             assertTrue("PathChangeModel equals itself failed!", path.equals(path));
462             assertFalse("PathChangeModel equals string failed!", path.equals(""));
463         }
7e8873 464         assertEquals(ChangeType.DELETE, deletions.get(0).changeType);
JM 465         assertEquals(ChangeType.ADD, additions.get(0).changeType);
a125cf 466         assertTrue(latestChanges.size() > 0);
JM 467     }
468
7e8873 469     @Test
a125cf 470     public void testFilesInPath() throws Exception {
7e8873 471         assertEquals(0, JGitUtils.getFilesInPath(null, null, null).size());
a125cf 472         Repository repository = GitBlitSuite.getHelloworldRepository();
JM 473         List<PathModel> files = JGitUtils.getFilesInPath(repository, null, null);
474         repository.close();
475         assertTrue(files.size() > 10);
476     }
477
7e8873 478     @Test
a9a2ff 479     public void testFilesInPath2() throws Exception {
MC 480         assertEquals(0, JGitUtils.getFilesInPath2(null, null, null).size());
481         Repository repository = GitBlitSuite.getHelloworldRepository();
482         List<PathModel> files = JGitUtils.getFilesInPath2(repository, null, null);
483         repository.close();
484         assertTrue(files.size() > 10);
485     }
486
487     @Test
a125cf 488     public void testDocuments() throws Exception {
JM 489         Repository repository = GitBlitSuite.getTicgitRepository();
1feb04 490         List<String> extensions = Arrays.asList(new String[] { ".mkd", ".md" });
a125cf 491         List<PathModel> markdownDocs = JGitUtils.getDocuments(repository, extensions);
JM 492         List<PathModel> allFiles = JGitUtils.getDocuments(repository, null);
493         repository.close();
494         assertTrue(markdownDocs.size() > 0);
495         assertTrue(allFiles.size() > markdownDocs.size());
496     }
497
7e8873 498     @Test
a125cf 499     public void testFileModes() throws Exception {
7e8873 500         assertEquals("drwxr-xr-x", JGitUtils.getPermissionsFromMode(FileMode.TREE.getBits()));
JM 501         assertEquals("-rw-r--r--",
502                 JGitUtils.getPermissionsFromMode(FileMode.REGULAR_FILE.getBits()));
503         assertEquals("-rwxr-xr-x",
504                 JGitUtils.getPermissionsFromMode(FileMode.EXECUTABLE_FILE.getBits()));
505         assertEquals("symlink", JGitUtils.getPermissionsFromMode(FileMode.SYMLINK.getBits()));
45f5f2 506         assertEquals("submodule", JGitUtils.getPermissionsFromMode(FileMode.GITLINK.getBits()));
7e8873 507         assertEquals("missing", JGitUtils.getPermissionsFromMode(FileMode.MISSING.getBits()));
a125cf 508     }
JM 509
7e8873 510     @Test
a125cf 511     public void testRevlog() throws Exception {
85c2e6 512         assertTrue(JGitUtils.getRevLog(null, 0).size() == 0);
a125cf 513         List<RevCommit> commits = JGitUtils.getRevLog(null, 10);
2f1c77 514         assertEquals(0, commits.size());
a125cf 515
JM 516         Repository repository = GitBlitSuite.getHelloworldRepository();
517         // get most recent 10 commits
518         commits = JGitUtils.getRevLog(repository, 10);
2f1c77 519         assertEquals(10, commits.size());
a125cf 520
JM 521         // test paging and offset by getting the 10th most recent commit
522         RevCommit lastCommit = JGitUtils.getRevLog(repository, null, 9, 1).get(0);
2f1c77 523         assertEquals(lastCommit, commits.get(9));
a125cf 524
JM 525         // grab the two most recent commits to java.java
526         commits = JGitUtils.getRevLog(repository, null, "java.java", 0, 2);
2f1c77 527         assertEquals(2, commits.size());
7e8873 528
2f1c77 529         // grab the commits since 2008-07-15
7e8873 530         commits = JGitUtils.getRevLog(repository, null,
JM 531                 new SimpleDateFormat("yyyy-MM-dd").parse("2008-07-15"));
5da0fd 532         assertEquals(12, commits.size());
a125cf 533         repository.close();
JM 534     }
535
7e8873 536     @Test
f47590 537     public void testRevLogRange() throws Exception {
JM 538         Repository repository = GitBlitSuite.getHelloworldRepository();
539         List<RevCommit> commits = JGitUtils.getRevLog(repository,
540                 "fbd14fa6d1a01d4aefa1fca725792683800fc67e",
541                 "85a0e4087b8439c0aa6b1f4f9e08c26052ab7e87");
542         repository.close();
543         assertEquals(14, commits.size());
544     }
545
546     @Test
a125cf 547     public void testSearchTypes() throws Exception {
7e8873 548         assertEquals(SearchType.COMMIT, SearchType.forName("commit"));
JM 549         assertEquals(SearchType.COMMITTER, SearchType.forName("committer"));
550         assertEquals(SearchType.AUTHOR, SearchType.forName("author"));
551         assertEquals(SearchType.COMMIT, SearchType.forName("unknown"));
a125cf 552
7e8873 553         assertEquals("commit", SearchType.COMMIT.toString());
JM 554         assertEquals("committer", SearchType.COMMITTER.toString());
555         assertEquals("author", SearchType.AUTHOR.toString());
a125cf 556     }
JM 557
7e8873 558     @Test
a125cf 559     public void testSearchRevlogs() throws Exception {
7e8873 560         assertEquals(0, JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0, 0).size());
JM 561         List<RevCommit> results = JGitUtils.searchRevlogs(null, null, "java", SearchType.COMMIT, 0,
a125cf 562                 3);
7e8873 563         assertEquals(0, results.size());
a125cf 564
JM 565         // test commit message search
566         Repository repository = GitBlitSuite.getHelloworldRepository();
7e8873 567         results = JGitUtils.searchRevlogs(repository, null, "java", SearchType.COMMIT, 0, 3);
JM 568         assertEquals(3, results.size());
a125cf 569
JM 570         // test author search
7e8873 571         results = JGitUtils.searchRevlogs(repository, null, "timothy", SearchType.AUTHOR, 0, -1);
JM 572         assertEquals(1, results.size());
a125cf 573
JM 574         // test committer search
7e8873 575         results = JGitUtils.searchRevlogs(repository, null, "mike", SearchType.COMMITTER, 0, 10);
JM 576         assertEquals(10, results.size());
a125cf 577
JM 578         // test paging and offset
7e8873 579         RevCommit commit = JGitUtils.searchRevlogs(repository, null, "mike", SearchType.COMMITTER,
a125cf 580                 9, 1).get(0);
7e8873 581         assertEquals(results.get(9), commit);
a125cf 582
JM 583         repository.close();
2a7306 584     }
JM 585
7e8873 586     @Test
2a7306 587     public void testZip() throws Exception {
59b817 588         assertFalse(CompressionUtils.zip(null, null, null, null));
2a7306 589         Repository repository = GitBlitSuite.getHelloworldRepository();
a125cf 590         File zipFileA = new File(GitBlitSuite.REPOSITORIES, "helloworld.zip");
JM 591         FileOutputStream fosA = new FileOutputStream(zipFileA);
59b817 592         boolean successA = CompressionUtils.zip(repository, null, Constants.HEAD, fosA);
a125cf 593         fosA.close();
JM 594
595         File zipFileB = new File(GitBlitSuite.REPOSITORIES, "helloworld-java.zip");
596         FileOutputStream fosB = new FileOutputStream(zipFileB);
59b817 597         boolean successB = CompressionUtils.zip(repository, "java.java", Constants.HEAD, fosB);
a125cf 598         fosB.close();
JM 599
2a7306 600         repository.close();
a125cf 601         assertTrue("Failed to generate zip file!", successA);
JM 602         assertTrue(zipFileA.length() > 0);
603         zipFileA.delete();
604
605         assertTrue("Failed to generate zip file!", successB);
606         assertTrue(zipFileB.length() > 0);
607         zipFileB.delete();
2a7306 608     }
c00577 609
f084f4 610     @Test
JM 611     public void testPlots() throws Exception {
612         Repository repository = GitBlitSuite.getTicgitRepository();
613         PlotWalk pw = new PlotWalk(repository);
614         PlotCommitList<PlotLane> commits = new PlotCommitList<PlotLane>();
615         commits.source(pw);
616         commits.fillTo(25);
617         for (PlotCommit<PlotLane> commit : commits) {
618             System.out.println(commit);
619         }
620         repository.close();
621     }
2a7306 622 }