| 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; |
| 18 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 19 | import java.util.List; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 20 | |
| 21 | import junit.framework.TestCase; |
| 22 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 23 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 24 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 25 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | import org.eclipse.jgit.junit.TestRepository; |
| 27 | import org.eclipse.jgit.revwalk.RevCommit; |
| 28 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 29 | |
| Shawn Pearce | b43b2d5 | 2013-03-18 10:55:15 -0700 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableList; |
| 31 | import com.google.common.collect.Lists; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | |
| 33 | /** Unit tests for {@link LogServlet}. */ |
| 34 | public class PaginatorTest extends TestCase { |
| 35 | private TestRepository<DfsRepository> repo; |
| 36 | private RevWalk walk; |
| 37 | |
| 38 | @Override |
| 39 | protected void setUp() throws Exception { |
| 40 | repo = new TestRepository<DfsRepository>( |
| 41 | new InMemoryRepository(new DfsRepositoryDescription("test"))); |
| 42 | walk = new RevWalk(repo.getRepository()); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected void tearDown() throws Exception { |
| 47 | walk.release(); |
| 48 | } |
| 49 | |
| 50 | public void testStart() throws Exception { |
| 51 | List<RevCommit> commits = linearCommits(10); |
| 52 | walk.markStart(commits.get(9)); |
| 53 | Paginator p = new Paginator(walk, 3, commits.get(9)); |
| 54 | assertEquals( |
| 55 | ImmutableList.of( |
| 56 | commits.get(9), |
| 57 | commits.get(8), |
| 58 | commits.get(7)), |
| 59 | ImmutableList.copyOf(p)); |
| 60 | assertNull(p.getPreviousStart()); |
| 61 | assertEquals(commits.get(6), p.getNextStart()); |
| 62 | } |
| 63 | |
| 64 | public void testNoStartCommit() throws Exception { |
| 65 | List<RevCommit> commits = linearCommits(10); |
| 66 | walk.markStart(commits.get(9)); |
| 67 | Paginator p = new Paginator(walk, 3, null); |
| 68 | assertEquals( |
| 69 | ImmutableList.of( |
| 70 | commits.get(9), |
| 71 | commits.get(8), |
| 72 | commits.get(7)), |
| 73 | ImmutableList.copyOf(p)); |
| 74 | assertNull(p.getPreviousStart()); |
| 75 | assertEquals(commits.get(6), p.getNextStart()); |
| 76 | } |
| 77 | |
| 78 | public void testLessThanOnePageIn() throws Exception { |
| 79 | List<RevCommit> commits = linearCommits(10); |
| 80 | walk.markStart(commits.get(9)); |
| 81 | Paginator p = new Paginator(walk, 3, commits.get(8)); |
| 82 | assertEquals( |
| 83 | ImmutableList.of( |
| 84 | commits.get(8), |
| 85 | commits.get(7), |
| 86 | commits.get(6)), |
| 87 | ImmutableList.copyOf(p)); |
| 88 | assertEquals(commits.get(9), p.getPreviousStart()); |
| 89 | assertEquals(commits.get(5), p.getNextStart()); |
| 90 | } |
| 91 | |
| 92 | public void testAtLeastOnePageIn() throws Exception { |
| 93 | List<RevCommit> commits = linearCommits(10); |
| 94 | walk.markStart(commits.get(9)); |
| 95 | Paginator p = new Paginator(walk, 3, commits.get(7)); |
| 96 | assertEquals( |
| 97 | ImmutableList.of( |
| 98 | commits.get(7), |
| 99 | commits.get(6), |
| 100 | commits.get(5)), |
| 101 | ImmutableList.copyOf(p)); |
| 102 | assertEquals(commits.get(9), p.getPreviousStart()); |
| 103 | assertEquals(commits.get(4), p.getNextStart()); |
| 104 | } |
| 105 | |
| 106 | public void testEnd() throws Exception { |
| 107 | List<RevCommit> commits = linearCommits(10); |
| 108 | walk.markStart(commits.get(9)); |
| 109 | Paginator p = new Paginator(walk, 3, commits.get(2)); |
| 110 | assertEquals( |
| 111 | ImmutableList.of( |
| 112 | commits.get(2), |
| 113 | commits.get(1), |
| 114 | commits.get(0)), |
| 115 | ImmutableList.copyOf(p)); |
| 116 | assertEquals(commits.get(5), p.getPreviousStart()); |
| 117 | assertNull(p.getNextStart()); |
| 118 | } |
| 119 | |
| 120 | public void testOnePastEnd() throws Exception { |
| 121 | List<RevCommit> commits = linearCommits(10); |
| 122 | walk.markStart(commits.get(9)); |
| 123 | Paginator p = new Paginator(walk, 3, commits.get(1)); |
| 124 | assertEquals( |
| 125 | ImmutableList.of( |
| 126 | commits.get(1), |
| 127 | commits.get(0)), |
| 128 | ImmutableList.copyOf(p)); |
| 129 | assertEquals(commits.get(4), p.getPreviousStart()); |
| 130 | assertNull(p.getNextStart()); |
| 131 | } |
| 132 | |
| 133 | public void testManyPastEnd() throws Exception { |
| 134 | List<RevCommit> commits = linearCommits(10); |
| 135 | walk.markStart(commits.get(9)); |
| 136 | Paginator p = new Paginator(walk, 5, commits.get(1)); |
| 137 | assertEquals( |
| 138 | ImmutableList.of( |
| 139 | commits.get(1), |
| 140 | commits.get(0)), |
| 141 | ImmutableList.copyOf(p)); |
| 142 | assertEquals(commits.get(6), p.getPreviousStart()); |
| 143 | assertNull(p.getNextStart()); |
| 144 | } |
| 145 | |
| 146 | private List<RevCommit> linearCommits(int n) throws Exception { |
| 147 | checkArgument(n > 0); |
| 148 | List<RevCommit> commits = Lists.newArrayList(); |
| 149 | commits.add(repo.commit().create()); |
| 150 | for (int i = 1; i < 10; i++) { |
| 151 | commits.add(repo.commit().parent(commits.get(commits.size() - 1)).create()); |
| 152 | } |
| 153 | return commits; |
| 154 | } |
| 155 | } |