blob: 2540c567a4d9e70591991c92b0bcd0eee2057de8 [file] [log] [blame]
Dave Borowitz14ce8282012-12-20 14:08:25 -08001// 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
15package com.google.gitiles;
16
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Dave Borowitz14ce8282012-12-20 14:08:25 -080018
Dave Borowitz3b744b12016-08-19 16:11:10 -040019import java.io.IOException;
Shawn Pearceb43b2d52013-03-18 10:55:15 -070020import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
21import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
22import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
Dave Borowitz14ce8282012-12-20 14:08:25 -080023import org.eclipse.jgit.junit.TestRepository;
24import org.eclipse.jgit.lib.ObjectId;
25import org.eclipse.jgit.lib.ObjectInserter;
26import org.eclipse.jgit.lib.TagBuilder;
27import org.eclipse.jgit.revwalk.RevBlob;
28import org.eclipse.jgit.revwalk.RevCommit;
29import org.eclipse.jgit.revwalk.RevTag;
30import org.eclipse.jgit.revwalk.RevTree;
31import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070032import org.junit.Before;
33import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080034import org.junit.runner.RunWith;
35import org.junit.runners.JUnit4;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070036
Dave Borowitz14ce8282012-12-20 14:08:25 -080037/** Unit tests for {@link TimeCache}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080038@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070039public class TimeCacheTest {
Dave Borowitz14ce8282012-12-20 14:08:25 -080040 private TestRepository<DfsRepository> repo;
41 private RevWalk walk;
42 private TimeCache cache;
43
44 /**
45 * Start time of {@link #repo}.
Dave Borowitz40255d52016-08-19 16:16:22 -040046 *
47 * <p>Note that commits auto-increment the repo's ticker, but tags do not.
Dave Borowitz14ce8282012-12-20 14:08:25 -080048 */
49 private long start;
50
Dave Borowitzd40bdf12014-04-19 19:33:56 -070051 @Before
52 public void setUp() throws Exception {
David Pursehouseccaa85d2017-05-30 10:47:27 +090053 repo = new TestRepository<>(new InMemoryRepository(new DfsRepositoryDescription("test")));
Dave Borowitz14ce8282012-12-20 14:08:25 -080054 walk = new RevWalk(repo.getRepository());
55 cache = new TimeCache();
Matthias Sohn12102302023-09-30 23:23:56 +020056 start = repo.getInstant().toEpochMilli() / 1000;
Dave Borowitz14ce8282012-12-20 14:08:25 -080057 }
58
59 private long getTime(ObjectId id) throws IOException {
60 return cache.getTime(walk, id);
61 }
62
Dave Borowitzd40bdf12014-04-19 19:33:56 -070063 @Test
64 public void commitTime() throws Exception {
Dave Borowitz14ce8282012-12-20 14:08:25 -080065 RevCommit root = repo.commit().create();
66 RevCommit master = repo.commit().parent(root).create();
Dave Borowitzfde41fd2015-09-16 15:14:38 -040067 assertThat(getTime(root)).isEqualTo(start + 1);
68 assertThat(getTime(master)).isEqualTo(start + 2);
Dave Borowitz14ce8282012-12-20 14:08:25 -080069 }
70
Dave Borowitzd40bdf12014-04-19 19:33:56 -070071 @Test
72 public void taggedCommitTime() throws Exception {
Dave Borowitz14ce8282012-12-20 14:08:25 -080073 RevCommit commit = repo.commit().create();
74 repo.tick(1);
75 RevTag tag = repo.tag("tag", commit);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040076 assertThat(getTime(commit)).isEqualTo(start + 1);
77 assertThat(getTime(tag)).isEqualTo(start + 2);
Dave Borowitz14ce8282012-12-20 14:08:25 -080078 }
79
Dave Borowitzd40bdf12014-04-19 19:33:56 -070080 @Test
81 public void taggedTreeAndBlobTime() throws Exception {
Dave Borowitz14ce8282012-12-20 14:08:25 -080082 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 Borowitzfde41fd2015-09-16 15:14:38 -040088 assertThat(getTime(blobTag)).isEqualTo(start + 1);
89 assertThat(getTime(treeTag)).isEqualTo(start + 2);
Dave Borowitz14ce8282012-12-20 14:08:25 -080090 }
91
Dave Borowitzd40bdf12014-04-19 19:33:56 -070092 @Test
93 public void taggedTagTime() throws Exception {
Dave Borowitz14ce8282012-12-20 14:08:25 -080094 repo.tick(2);
95 RevTag tag = repo.tag("tag", repo.commit().create());
96 repo.tick(-1);
97 RevTag tagTag = repo.tag("tagtag", tag);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040098 assertThat(getTime(tag)).isEqualTo(start + 3);
99 assertThat(getTime(tagTag)).isEqualTo(start + 2);
Dave Borowitz14ce8282012-12-20 14:08:25 -0800100 }
101
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700102 @Test
103 public void treeAndBlobTime() throws Exception {
Dave Borowitz14ce8282012-12-20 14:08:25 -0800104 RevBlob blob = repo.blob("contents");
105 RevTree tree = repo.tree(repo.file("foo", blob));
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400106 assertThat(getTime(blob)).isEqualTo(Long.MIN_VALUE);
107 assertThat(getTime(tree)).isEqualTo(Long.MIN_VALUE);
Dave Borowitz14ce8282012-12-20 14:08:25 -0800108 }
109
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700110 @Test
111 public void tagMissingTime() throws Exception {
Dave Borowitz14ce8282012-12-20 14:08:25 -0800112 RevCommit commit = repo.commit().create();
113 TagBuilder builder = new TagBuilder();
114 builder.setObjectId(commit);
115 builder.setTag("tag");
116 builder.setMessage("");
Dave Borowitz14ce8282012-12-20 14:08:25 -0800117 ObjectId id;
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700118 try (ObjectInserter ins = repo.getRepository().newObjectInserter()) {
Dave Borowitz14ce8282012-12-20 14:08:25 -0800119 id = ins.insert(builder);
120 ins.flush();
Dave Borowitz14ce8282012-12-20 14:08:25 -0800121 }
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400122 assertThat(getTime(commit)).isEqualTo(start + 1);
123 assertThat(getTime(id)).isEqualTo(start + 1);
Dave Borowitz14ce8282012-12-20 14:08:25 -0800124 }
125
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700126 @Test
127 public void firstTagMissingTime() throws Exception {
Dave Borowitz14ce8282012-12-20 14:08:25 -0800128 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 Borowitz14ce8282012-12-20 14:08:25 -0800137 ObjectId tagTagId;
Shawn Pearceb5ad0a02015-05-24 20:33:17 -0700138 try (ObjectInserter ins = repo.getRepository().newObjectInserter()) {
Dave Borowitz14ce8282012-12-20 14:08:25 -0800139 tagTagId = ins.insert(builder);
140 ins.flush();
Dave Borowitz14ce8282012-12-20 14:08:25 -0800141 }
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400142 assertThat(getTime(commit)).isEqualTo(start + 1);
143 assertThat(getTime(tag)).isEqualTo(start + 2);
144 assertThat(getTime(tagTagId)).isEqualTo(start + 2);
Dave Borowitz14ce8282012-12-20 14:08:25 -0800145 }
146}