|
| 1 | +/* |
| 2 | + * Copyright 2012 GitHub Inc. |
| 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.github.mobile.tests.commit; |
| 17 | + |
| 18 | +import android.test.AndroidTestCase; |
| 19 | + |
| 20 | +import com.github.mobile.core.commit.CommitUtils; |
| 21 | + |
| 22 | +import java.util.Date; |
| 23 | + |
| 24 | +import org.eclipse.egit.github.core.Commit; |
| 25 | +import org.eclipse.egit.github.core.CommitFile; |
| 26 | +import org.eclipse.egit.github.core.CommitUser; |
| 27 | +import org.eclipse.egit.github.core.RepositoryCommit; |
| 28 | +import org.eclipse.egit.github.core.User; |
| 29 | + |
| 30 | +/** |
| 31 | + * Test of {@link CommitUtils} |
| 32 | + */ |
| 33 | +public class CommitUtilsTest extends AndroidTestCase { |
| 34 | + |
| 35 | + /** |
| 36 | + * Test commit SHA-1 abbreviation |
| 37 | + */ |
| 38 | + public void testAbbreviate() { |
| 39 | + assertNull(CommitUtils.abbreviate((Commit) null)); |
| 40 | + assertNull(CommitUtils.abbreviate((RepositoryCommit) null)); |
| 41 | + assertNull(CommitUtils.abbreviate((String) null)); |
| 42 | + assertEquals("", CommitUtils.abbreviate("")); |
| 43 | + assertEquals("a", CommitUtils.abbreviate("a")); |
| 44 | + assertEquals("abcdefghij", CommitUtils.abbreviate("abcdefghijk")); |
| 45 | + assertEquals("abc", CommitUtils.abbreviate(new Commit().setSha("abc"))); |
| 46 | + assertEquals("abcd", |
| 47 | + CommitUtils.abbreviate(new RepositoryCommit().setSha("abcd"))); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test commit name parsing from path |
| 52 | + */ |
| 53 | + public void testGetName() { |
| 54 | + assertNull(CommitUtils.getName((String) null)); |
| 55 | + assertNull(CommitUtils.getName((CommitFile) null)); |
| 56 | + assertEquals("", CommitUtils.getName("")); |
| 57 | + assertEquals("/", CommitUtils.getName("/")); |
| 58 | + assertEquals("b", CommitUtils.getName("a/b")); |
| 59 | + assertEquals("c", |
| 60 | + CommitUtils.getName(new CommitFile().setFilename("a/b/c"))); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test commit SHA-1 evaluation |
| 65 | + */ |
| 66 | + public void testIsValidCommit() { |
| 67 | + assertFalse(CommitUtils.isValidCommit(null)); |
| 68 | + assertFalse(CommitUtils.isValidCommit("")); |
| 69 | + assertTrue(CommitUtils.isValidCommit("a")); |
| 70 | + assertTrue(CommitUtils.isValidCommit("bbbbb")); |
| 71 | + assertFalse(CommitUtils.isValidCommit("am")); |
| 72 | + assertFalse(CommitUtils.isValidCommit("xyz")); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test parsing author from commit |
| 77 | + */ |
| 78 | + public void testGetAuthor() { |
| 79 | + RepositoryCommit commit = new RepositoryCommit(); |
| 80 | + assertNull(CommitUtils.getAuthor(commit)); |
| 81 | + Commit rawCommit = new Commit(); |
| 82 | + commit.setCommit(rawCommit); |
| 83 | + assertNull(CommitUtils.getAuthor(commit)); |
| 84 | + CommitUser user = new CommitUser(); |
| 85 | + rawCommit.setAuthor(user); |
| 86 | + assertNull(CommitUtils.getAuthor(commit)); |
| 87 | + user.setName("u1"); |
| 88 | + assertEquals("u1", CommitUtils.getAuthor(commit)); |
| 89 | + commit.setAuthor(new User().setLogin("u2")); |
| 90 | + assertEquals("u2", CommitUtils.getAuthor(commit)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Test parsing committer from commit |
| 95 | + */ |
| 96 | + public void testGetCommitter() { |
| 97 | + RepositoryCommit commit = new RepositoryCommit(); |
| 98 | + assertNull(CommitUtils.getCommitter(commit)); |
| 99 | + Commit rawCommit = new Commit(); |
| 100 | + commit.setCommit(rawCommit); |
| 101 | + assertNull(CommitUtils.getCommitter(commit)); |
| 102 | + CommitUser user = new CommitUser(); |
| 103 | + rawCommit.setCommitter(user); |
| 104 | + assertNull(CommitUtils.getCommitter(commit)); |
| 105 | + user.setName("u1"); |
| 106 | + assertEquals("u1", CommitUtils.getCommitter(commit)); |
| 107 | + commit.setCommitter(new User().setLogin("u2")); |
| 108 | + assertEquals("u2", CommitUtils.getCommitter(commit)); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test parsing author date from commit |
| 113 | + */ |
| 114 | + public void testGetAuthorDate() { |
| 115 | + RepositoryCommit commit = new RepositoryCommit(); |
| 116 | + assertNull(CommitUtils.getAuthorDate(commit)); |
| 117 | + Commit rawCommit = new Commit(); |
| 118 | + commit.setCommit(rawCommit); |
| 119 | + assertNull(CommitUtils.getAuthorDate(commit)); |
| 120 | + CommitUser user = new CommitUser(); |
| 121 | + rawCommit.setAuthor(user); |
| 122 | + assertNull(CommitUtils.getAuthorDate(commit)); |
| 123 | + user.setDate(new Date(12345)); |
| 124 | + assertEquals(new Date(12345), CommitUtils.getAuthorDate(commit)); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Test parsing committer date from commit |
| 129 | + */ |
| 130 | + public void testGetCommitterDate() { |
| 131 | + RepositoryCommit commit = new RepositoryCommit(); |
| 132 | + assertNull(CommitUtils.getCommiterDate(commit)); |
| 133 | + Commit rawCommit = new Commit(); |
| 134 | + commit.setCommit(rawCommit); |
| 135 | + assertNull(CommitUtils.getCommiterDate(commit)); |
| 136 | + CommitUser user = new CommitUser(); |
| 137 | + rawCommit.setCommitter(user); |
| 138 | + assertNull(CommitUtils.getCommiterDate(commit)); |
| 139 | + user.setDate(new Date(12345)); |
| 140 | + assertEquals(new Date(12345), CommitUtils.getCommiterDate(commit)); |
| 141 | + } |
| 142 | +} |
0 commit comments