| 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 | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 17 | import static org.junit.Assert.assertEquals; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 18 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 19 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 20 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 21 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 22 | import org.eclipse.jgit.junit.TestRepository; |
| 23 | import org.eclipse.jgit.lib.ObjectId; |
| 24 | import org.eclipse.jgit.lib.ObjectInserter; |
| 25 | import org.eclipse.jgit.lib.TagBuilder; |
| 26 | import org.eclipse.jgit.revwalk.RevBlob; |
| 27 | import org.eclipse.jgit.revwalk.RevCommit; |
| 28 | import org.eclipse.jgit.revwalk.RevTag; |
| 29 | import org.eclipse.jgit.revwalk.RevTree; |
| 30 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | |
| 34 | import java.io.IOException; |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 35 | |
| 36 | /** Unit tests for {@link TimeCache}. */ |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 37 | public class TimeCacheTest { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 38 | private TestRepository<DfsRepository> repo; |
| 39 | private RevWalk walk; |
| 40 | private TimeCache cache; |
| 41 | |
| 42 | /** |
| 43 | * Start time of {@link #repo}. |
| 44 | * <p> |
| 45 | * Note that commits auto-increment the repo's ticker, but tags do not. |
| 46 | */ |
| 47 | private long start; |
| 48 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 49 | @Before |
| 50 | public void setUp() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 51 | repo = new TestRepository<DfsRepository>( |
| 52 | new InMemoryRepository(new DfsRepositoryDescription("test"))); |
| 53 | walk = new RevWalk(repo.getRepository()); |
| 54 | cache = new TimeCache(); |
| 55 | start = repo.getClock().getTime() / 1000; |
| 56 | } |
| 57 | |
| 58 | private long getTime(ObjectId id) throws IOException { |
| 59 | return cache.getTime(walk, id); |
| 60 | } |
| 61 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 62 | @Test |
| 63 | public void commitTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 64 | RevCommit root = repo.commit().create(); |
| 65 | RevCommit master = repo.commit().parent(root).create(); |
| 66 | assertEquals(start + 1, getTime(root)); |
| 67 | assertEquals(start + 2, getTime(master)); |
| 68 | } |
| 69 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 70 | @Test |
| 71 | public void taggedCommitTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 72 | RevCommit commit = repo.commit().create(); |
| 73 | repo.tick(1); |
| 74 | RevTag tag = repo.tag("tag", commit); |
| 75 | assertEquals(start + 1, getTime(commit)); |
| 76 | assertEquals(start + 2, getTime(tag)); |
| 77 | } |
| 78 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 79 | @Test |
| 80 | public void taggedTreeAndBlobTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 81 | RevBlob blob = repo.blob("contents"); |
| 82 | RevTree tree = repo.tree(repo.file("foo", blob)); |
| 83 | repo.tick(1); |
| 84 | RevTag blobTag = repo.tag("blob", blob); |
| 85 | repo.tick(1); |
| 86 | RevTag treeTag = repo.tag("tree", tree); |
| 87 | assertEquals(start + 1, getTime(blobTag)); |
| 88 | assertEquals(start + 2, getTime(treeTag)); |
| 89 | } |
| 90 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 91 | @Test |
| 92 | public void taggedTagTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 93 | repo.tick(2); |
| 94 | RevTag tag = repo.tag("tag", repo.commit().create()); |
| 95 | repo.tick(-1); |
| 96 | RevTag tagTag = repo.tag("tagtag", tag); |
| 97 | assertEquals(start + 3, getTime(tag)); |
| 98 | assertEquals(start + 2, getTime(tagTag)); |
| 99 | } |
| 100 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 101 | @Test |
| 102 | public void treeAndBlobTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 103 | RevBlob blob = repo.blob("contents"); |
| 104 | RevTree tree = repo.tree(repo.file("foo", blob)); |
| 105 | assertEquals(Long.MIN_VALUE, getTime(blob)); |
| 106 | assertEquals(Long.MIN_VALUE, getTime(tree)); |
| 107 | } |
| 108 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 109 | @Test |
| 110 | public void tagMissingTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 111 | RevCommit commit = repo.commit().create(); |
| 112 | TagBuilder builder = new TagBuilder(); |
| 113 | builder.setObjectId(commit); |
| 114 | builder.setTag("tag"); |
| 115 | builder.setMessage(""); |
| 116 | ObjectInserter ins = repo.getRepository().newObjectInserter(); |
| 117 | ObjectId id; |
| 118 | try { |
| 119 | id = ins.insert(builder); |
| 120 | ins.flush(); |
| 121 | } finally { |
| 122 | ins.release(); |
| 123 | } |
| 124 | assertEquals(start + 1, getTime(commit)); |
| 125 | assertEquals(start + 1, getTime(id)); |
| 126 | } |
| 127 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 128 | @Test |
| 129 | public void firstTagMissingTime() throws Exception { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 130 | RevCommit commit = repo.commit().create(); |
| 131 | repo.tick(1); |
| 132 | RevTag tag = repo.tag("tag", commit); |
| 133 | repo.tick(1); |
| 134 | |
| 135 | TagBuilder builder = new TagBuilder(); |
| 136 | builder.setObjectId(tag); |
| 137 | builder.setTag("tagtag"); |
| 138 | builder.setMessage(""); |
| 139 | ObjectInserter ins = repo.getRepository().newObjectInserter(); |
| 140 | ObjectId tagTagId; |
| 141 | try { |
| 142 | tagTagId = ins.insert(builder); |
| 143 | ins.flush(); |
| 144 | } finally { |
| 145 | ins.release(); |
| 146 | } |
| 147 | assertEquals(start + 1, getTime(commit)); |
| 148 | assertEquals(start + 2, getTime(tag)); |
| 149 | assertEquals(start + 2, getTime(tagTagId)); |
| 150 | } |
| 151 | } |