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