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