blob: 2ac3f962ffe89e657ed0a7c98e4b9a9a103404f4 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// 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
17import static com.google.common.base.Preconditions.checkArgument;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070018import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNull;
Dave Borowitz9de65952012-08-13 16:09:45 -070020
Dave Borowitzd40bdf12014-04-19 19:33:56 -070021import com.google.common.collect.ImmutableList;
22import com.google.common.collect.Lists;
Dave Borowitz9de65952012-08-13 16:09:45 -070023
Shawn Pearceb43b2d52013-03-18 10:55:15 -070024import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
25import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
26import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
Dave Borowitz9de65952012-08-13 16:09:45 -070027import org.eclipse.jgit.junit.TestRepository;
28import org.eclipse.jgit.revwalk.RevCommit;
29import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070030import org.junit.After;
31import org.junit.Before;
32import org.junit.Test;
Dave Borowitz9de65952012-08-13 16:09:45 -070033
Dave Borowitzd40bdf12014-04-19 19:33:56 -070034import java.util.List;
Dave Borowitz9de65952012-08-13 16:09:45 -070035
36/** Unit tests for {@link LogServlet}. */
Dave Borowitzd40bdf12014-04-19 19:33:56 -070037public class PaginatorTest {
Dave Borowitz9de65952012-08-13 16:09:45 -070038 private TestRepository<DfsRepository> repo;
39 private RevWalk walk;
40
Dave Borowitzd40bdf12014-04-19 19:33:56 -070041 @Before
42 public void setUp() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070043 repo = new TestRepository<DfsRepository>(
44 new InMemoryRepository(new DfsRepositoryDescription("test")));
45 walk = new RevWalk(repo.getRepository());
46 }
47
Dave Borowitzd40bdf12014-04-19 19:33:56 -070048 @After
49 public void tearDown() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070050 walk.release();
51 }
52
Dave Borowitzd40bdf12014-04-19 19:33:56 -070053 @Test
54 public void start() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070055 List<RevCommit> commits = linearCommits(10);
56 walk.markStart(commits.get(9));
57 Paginator p = new Paginator(walk, 3, commits.get(9));
58 assertEquals(
59 ImmutableList.of(
60 commits.get(9),
61 commits.get(8),
62 commits.get(7)),
63 ImmutableList.copyOf(p));
64 assertNull(p.getPreviousStart());
65 assertEquals(commits.get(6), p.getNextStart());
66 }
67
Dave Borowitzd40bdf12014-04-19 19:33:56 -070068 @Test
69 public void noStartCommit() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070070 List<RevCommit> commits = linearCommits(10);
71 walk.markStart(commits.get(9));
72 Paginator p = new Paginator(walk, 3, null);
73 assertEquals(
74 ImmutableList.of(
75 commits.get(9),
76 commits.get(8),
77 commits.get(7)),
78 ImmutableList.copyOf(p));
79 assertNull(p.getPreviousStart());
80 assertEquals(commits.get(6), p.getNextStart());
81 }
82
Dave Borowitzd40bdf12014-04-19 19:33:56 -070083 @Test
84 public void lessThanOnePageIn() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -070085 List<RevCommit> commits = linearCommits(10);
86 walk.markStart(commits.get(9));
87 Paginator p = new Paginator(walk, 3, commits.get(8));
88 assertEquals(
89 ImmutableList.of(
90 commits.get(8),
91 commits.get(7),
92 commits.get(6)),
93 ImmutableList.copyOf(p));
94 assertEquals(commits.get(9), p.getPreviousStart());
95 assertEquals(commits.get(5), p.getNextStart());
96 }
97
Dave Borowitzd40bdf12014-04-19 19:33:56 -070098 @Test
99 public void atLeastOnePageIn() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -0700100 List<RevCommit> commits = linearCommits(10);
101 walk.markStart(commits.get(9));
102 Paginator p = new Paginator(walk, 3, commits.get(7));
103 assertEquals(
104 ImmutableList.of(
105 commits.get(7),
106 commits.get(6),
107 commits.get(5)),
108 ImmutableList.copyOf(p));
109 assertEquals(commits.get(9), p.getPreviousStart());
110 assertEquals(commits.get(4), p.getNextStart());
111 }
112
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700113 @Test
114 public void end() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -0700115 List<RevCommit> commits = linearCommits(10);
116 walk.markStart(commits.get(9));
117 Paginator p = new Paginator(walk, 3, commits.get(2));
118 assertEquals(
119 ImmutableList.of(
120 commits.get(2),
121 commits.get(1),
122 commits.get(0)),
123 ImmutableList.copyOf(p));
124 assertEquals(commits.get(5), p.getPreviousStart());
125 assertNull(p.getNextStart());
126 }
127
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700128 @Test
129 public void onePastEnd() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -0700130 List<RevCommit> commits = linearCommits(10);
131 walk.markStart(commits.get(9));
132 Paginator p = new Paginator(walk, 3, commits.get(1));
133 assertEquals(
134 ImmutableList.of(
135 commits.get(1),
136 commits.get(0)),
137 ImmutableList.copyOf(p));
138 assertEquals(commits.get(4), p.getPreviousStart());
139 assertNull(p.getNextStart());
140 }
141
Dave Borowitzd40bdf12014-04-19 19:33:56 -0700142 @Test
143 public void manyPastEnd() throws Exception {
Dave Borowitz9de65952012-08-13 16:09:45 -0700144 List<RevCommit> commits = linearCommits(10);
145 walk.markStart(commits.get(9));
146 Paginator p = new Paginator(walk, 5, commits.get(1));
147 assertEquals(
148 ImmutableList.of(
149 commits.get(1),
150 commits.get(0)),
151 ImmutableList.copyOf(p));
152 assertEquals(commits.get(6), p.getPreviousStart());
153 assertNull(p.getNextStart());
154 }
155
156 private List<RevCommit> linearCommits(int n) throws Exception {
157 checkArgument(n > 0);
158 List<RevCommit> commits = Lists.newArrayList();
159 commits.add(repo.commit().create());
160 for (int i = 1; i < 10; i++) {
161 commits.add(repo.commit().parent(commits.get(commits.size() - 1)).create());
162 }
163 return commits;
164 }
165}