| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 18 | |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 19 | import java.io.IOException; |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 20 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 21 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 22 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 23 | import org.eclipse.jgit.junit.TestRepository; |
| 24 | import org.eclipse.jgit.lib.ObjectId; |
| 25 | import org.eclipse.jgit.lib.ObjectInserter; |
| 26 | import org.eclipse.jgit.lib.TagBuilder; |
| 27 | import org.eclipse.jgit.revwalk.RevBlob; |
| 28 | import org.eclipse.jgit.revwalk.RevCommit; |
| 29 | import org.eclipse.jgit.revwalk.RevTag; |
| 30 | import org.eclipse.jgit.revwalk.RevTree; |
| 31 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 34 | import org.junit.runner.RunWith; |
| 35 | import org.junit.runners.JUnit4; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 36 | |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 37 | /** Unit tests for {@link TimeCache}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 38 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 39 | public class TimeCacheTest { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 40 | private TestRepository<DfsRepository> repo; |
| 41 | private RevWalk walk; |
| 42 | private TimeCache cache; |
| 43 | |
| 44 | /** |
| 45 | * Start time of {@link #repo}. |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 46 | * |
| 47 | * <p>Note that commits auto-increment the repo's ticker, but tags do not. |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 48 | */ |
| 49 | private long start; |
| 50 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 51 | @Before |
| 52 | public void setUp() throws Exception { |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 53 | repo = new TestRepository<>(new InMemoryRepository(new DfsRepositoryDescription("test"))); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 54 | walk = new RevWalk(repo.getRepository()); |
| 55 | cache = new TimeCache(); |
| Terry Parker | f10481d | 2015-10-22 13:46:33 -0700 | [diff] [blame] | 56 | start = repo.getDate().getTime() / 1000; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | private long getTime(ObjectId id) throws IOException { |
| 60 | return cache.getTime(walk, id); |
| 61 | } |
| 62 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 63 | @Test |
| 64 | public void commitTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 65 | RevCommit root = repo.commit().create(); |
| 66 | RevCommit master = repo.commit().parent(root).create(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 67 | assertThat(getTime(root)).isEqualTo(start + 1); |
| 68 | assertThat(getTime(master)).isEqualTo(start + 2); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 71 | @Test |
| 72 | public void taggedCommitTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 73 | RevCommit commit = repo.commit().create(); |
| 74 | repo.tick(1); |
| 75 | RevTag tag = repo.tag("tag", commit); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 76 | assertThat(getTime(commit)).isEqualTo(start + 1); |
| 77 | assertThat(getTime(tag)).isEqualTo(start + 2); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 80 | @Test |
| 81 | public void taggedTreeAndBlobTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 82 | RevBlob blob = repo.blob("contents"); |
| 83 | RevTree tree = repo.tree(repo.file("foo", blob)); |
| 84 | repo.tick(1); |
| 85 | RevTag blobTag = repo.tag("blob", blob); |
| 86 | repo.tick(1); |
| 87 | RevTag treeTag = repo.tag("tree", tree); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 88 | assertThat(getTime(blobTag)).isEqualTo(start + 1); |
| 89 | assertThat(getTime(treeTag)).isEqualTo(start + 2); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 92 | @Test |
| 93 | public void taggedTagTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 94 | repo.tick(2); |
| 95 | RevTag tag = repo.tag("tag", repo.commit().create()); |
| 96 | repo.tick(-1); |
| 97 | RevTag tagTag = repo.tag("tagtag", tag); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 98 | assertThat(getTime(tag)).isEqualTo(start + 3); |
| 99 | assertThat(getTime(tagTag)).isEqualTo(start + 2); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 102 | @Test |
| 103 | public void treeAndBlobTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 104 | RevBlob blob = repo.blob("contents"); |
| 105 | RevTree tree = repo.tree(repo.file("foo", blob)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 106 | assertThat(getTime(blob)).isEqualTo(Long.MIN_VALUE); |
| 107 | assertThat(getTime(tree)).isEqualTo(Long.MIN_VALUE); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 110 | @Test |
| 111 | public void tagMissingTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 112 | RevCommit commit = repo.commit().create(); |
| 113 | TagBuilder builder = new TagBuilder(); |
| 114 | builder.setObjectId(commit); |
| 115 | builder.setTag("tag"); |
| 116 | builder.setMessage(""); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 117 | ObjectId id; |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 118 | try (ObjectInserter ins = repo.getRepository().newObjectInserter()) { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 119 | id = ins.insert(builder); |
| 120 | ins.flush(); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 121 | } |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 122 | assertThat(getTime(commit)).isEqualTo(start + 1); |
| 123 | assertThat(getTime(id)).isEqualTo(start + 1); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 126 | @Test |
| 127 | public void firstTagMissingTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 128 | RevCommit commit = repo.commit().create(); |
| 129 | repo.tick(1); |
| 130 | RevTag tag = repo.tag("tag", commit); |
| 131 | repo.tick(1); |
| 132 | |
| 133 | TagBuilder builder = new TagBuilder(); |
| 134 | builder.setObjectId(tag); |
| 135 | builder.setTag("tagtag"); |
| 136 | builder.setMessage(""); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 137 | ObjectId tagTagId; |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 138 | try (ObjectInserter ins = repo.getRepository().newObjectInserter()) { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 139 | tagTagId = ins.insert(builder); |
| 140 | ins.flush(); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 141 | } |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 142 | assertThat(getTime(commit)).isEqualTo(start + 1); |
| 143 | assertThat(getTime(tag)).isEqualTo(start + 2); |
| 144 | assertThat(getTime(tagTagId)).isEqualTo(start + 2); |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 145 | } |
| 146 | } |