| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [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 | |
| 17 | import static com.google.common.base.Preconditions.checkArgument; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 18 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 19 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 21 | import java.util.List; |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 22 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 23 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 24 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 25 | import org.eclipse.jgit.junit.TestRepository; |
| 26 | import org.eclipse.jgit.revwalk.RevCommit; |
| 27 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 31 | import org.junit.runner.RunWith; |
| 32 | import org.junit.runners.JUnit4; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 33 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 34 | /** Unit tests for {@link LogServlet}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 35 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 36 | public class PaginatorTest { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 37 | private TestRepository<DfsRepository> repo; |
| 38 | private RevWalk walk; |
| 39 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 40 | @Before |
| 41 | public void setUp() throws Exception { |
| David Pursehouse | ccaa85d | 2017-05-30 10:47:27 +0900 | [diff] [blame] | 42 | repo = new TestRepository<>(new InMemoryRepository(new DfsRepositoryDescription("test"))); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 43 | walk = new RevWalk(repo.getRepository()); |
| 44 | } |
| 45 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 46 | @After |
| 47 | public void tearDown() throws Exception { |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 48 | walk.close(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 51 | @Test |
| Dave Borowitz | 61d12e8 | 2014-07-30 08:45:10 -0700 | [diff] [blame] | 52 | public void oneResult() throws Exception { |
| 53 | List<RevCommit> commits = linearCommits(1); |
| 54 | walk.markStart(commits.get(0)); |
| 55 | Paginator p = new Paginator(walk, 10, null); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 56 | assertThat(p).containsExactly(commits.get(0)); |
| 57 | assertThat(p.getPreviousStart()).isNull(); |
| 58 | assertThat(p.getNextStart()).isNull(); |
| Dave Borowitz | 61d12e8 | 2014-07-30 08:45:10 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void lessThanOnePage() throws Exception { |
| 63 | List<RevCommit> commits = linearCommits(3); |
| 64 | walk.markStart(commits.get(2)); |
| 65 | Paginator p = new Paginator(walk, 10, null); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 66 | assertThat(p).containsExactly(commits.get(2), commits.get(1), commits.get(0)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 67 | assertThat(p.getPreviousStart()).isNull(); |
| 68 | assertThat(p.getNextStart()).isNull(); |
| Dave Borowitz | 61d12e8 | 2014-07-30 08:45:10 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void exactlyOnePage() throws Exception { |
| 73 | List<RevCommit> commits = linearCommits(3); |
| 74 | walk.markStart(commits.get(2)); |
| 75 | Paginator p = new Paginator(walk, 3, null); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 76 | assertThat(p).containsExactly(commits.get(2), commits.get(1), commits.get(0)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 77 | assertThat(p.getPreviousStart()).isNull(); |
| 78 | assertThat(p.getNextStart()).isNull(); |
| Dave Borowitz | 61d12e8 | 2014-07-30 08:45:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void moreThanOnePage() throws Exception { |
| 83 | List<RevCommit> commits = linearCommits(5); |
| 84 | walk.markStart(commits.get(4)); |
| 85 | Paginator p = new Paginator(walk, 3, null); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 86 | assertThat(p).containsExactly(commits.get(4), commits.get(3), commits.get(2)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 87 | assertThat(p.getPreviousStart()).isNull(); |
| 88 | assertThat(p.getNextStart()).isEqualTo(commits.get(1)); |
| Dave Borowitz | 61d12e8 | 2014-07-30 08:45:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Test |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 92 | public void start() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 93 | List<RevCommit> commits = linearCommits(10); |
| 94 | walk.markStart(commits.get(9)); |
| 95 | Paginator p = new Paginator(walk, 3, commits.get(9)); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 96 | assertThat(p).containsExactly(commits.get(9), commits.get(8), commits.get(7)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 97 | assertThat(p.getPreviousStart()).isNull(); |
| 98 | assertThat(p.getNextStart()).isEqualTo(commits.get(6)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 101 | @Test |
| 102 | public void noStartCommit() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 103 | List<RevCommit> commits = linearCommits(10); |
| 104 | walk.markStart(commits.get(9)); |
| 105 | Paginator p = new Paginator(walk, 3, null); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 106 | assertThat(p).containsExactly(commits.get(9), commits.get(8), commits.get(7)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 107 | assertThat(p.getPreviousStart()).isNull(); |
| 108 | assertThat(p.getNextStart()).isEqualTo(commits.get(6)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 111 | @Test |
| 112 | public void lessThanOnePageIn() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 113 | List<RevCommit> commits = linearCommits(10); |
| 114 | walk.markStart(commits.get(9)); |
| 115 | Paginator p = new Paginator(walk, 3, commits.get(8)); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 116 | assertThat(p).containsExactly(commits.get(8), commits.get(7), commits.get(6)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 117 | assertThat(p.getPreviousStart()).isEqualTo(commits.get(9)); |
| 118 | assertThat(p.getNextStart()).isEqualTo(commits.get(5)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 121 | @Test |
| 122 | public void atLeastOnePageIn() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 123 | List<RevCommit> commits = linearCommits(10); |
| 124 | walk.markStart(commits.get(9)); |
| 125 | Paginator p = new Paginator(walk, 3, commits.get(7)); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 126 | assertThat(p).containsExactly(commits.get(7), commits.get(6), commits.get(5)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 127 | assertThat(p.getPreviousStart()).isEqualTo(commits.get(9)); |
| 128 | assertThat(p.getNextStart()).isEqualTo(commits.get(4)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 131 | @Test |
| 132 | public void end() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 133 | List<RevCommit> commits = linearCommits(10); |
| 134 | walk.markStart(commits.get(9)); |
| 135 | Paginator p = new Paginator(walk, 3, commits.get(2)); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 136 | assertThat(p).containsExactly(commits.get(2), commits.get(1), commits.get(0)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 137 | assertThat(p.getPreviousStart()).isEqualTo(commits.get(5)); |
| 138 | assertThat(p.getNextStart()).isNull(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 141 | @Test |
| 142 | public void onePastEnd() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 143 | List<RevCommit> commits = linearCommits(10); |
| 144 | walk.markStart(commits.get(9)); |
| 145 | Paginator p = new Paginator(walk, 3, commits.get(1)); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 146 | assertThat(p).containsExactly(commits.get(1), commits.get(0)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 147 | assertThat(p.getPreviousStart()).isEqualTo(commits.get(4)); |
| 148 | assertThat(p.getNextStart()).isNull(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 151 | @Test |
| 152 | public void manyPastEnd() throws Exception { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 153 | List<RevCommit> commits = linearCommits(10); |
| 154 | walk.markStart(commits.get(9)); |
| 155 | Paginator p = new Paginator(walk, 5, commits.get(1)); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 156 | assertThat(p).containsExactly(commits.get(1), commits.get(0)).inOrder(); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 157 | assertThat(p.getPreviousStart()).isEqualTo(commits.get(6)); |
| 158 | assertThat(p.getNextStart()).isNull(); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | private List<RevCommit> linearCommits(int n) throws Exception { |
| 162 | checkArgument(n > 0); |
| 163 | List<RevCommit> commits = Lists.newArrayList(); |
| 164 | commits.add(repo.commit().create()); |
| Dave Borowitz | 61d12e8 | 2014-07-30 08:45:10 -0700 | [diff] [blame] | 165 | for (int i = 1; i < n; i++) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 166 | commits.add(repo.commit().parent(commits.get(commits.size() - 1)).create()); |
| 167 | } |
| 168 | return commits; |
| 169 | } |
| 170 | } |