Skip to content

Commit afcc90f

Browse files
committed
add tests for CVSRepository HistoryEntry comparison
1 parent e615743 commit afcc90f

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/CVSHistoryParser.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.List;
3636

3737
import io.github.g00fy2.versioncompare.Version;
38+
import org.jetbrains.annotations.VisibleForTesting;
3839
import org.opengrok.indexer.util.Executor;
3940

4041
/**
@@ -157,6 +158,17 @@ public void processStream(InputStream input) throws IOException {
157158
history.setHistoryEntries(entries);
158159
}
159160

161+
/**
162+
* Sort history entries in the object according to semantic ordering of the revision string.
163+
* @param history {@link History} object
164+
*/
165+
@VisibleForTesting
166+
static void sortHistoryEntries(History history) {
167+
List<HistoryEntry> entries = history.getHistoryEntries();
168+
entries.sort((h1, h2) -> new Version(h2.getRevision()).compareTo(new Version(h1.getRevision())));
169+
history.setHistoryEntries(entries);
170+
}
171+
160172
/**
161173
* Parse the history for the specified file.
162174
*
@@ -183,9 +195,7 @@ History parse(File file, Repository repos) throws HistoryException {
183195
// unsorted order (as a result of using '-r1.1:branch' for 'cvs log')
184196
// so they need to be sorted according to revision.
185197
if (cvsrepo.getBranch() != null && !cvsrepo.getBranch().isEmpty()) {
186-
List<HistoryEntry> entries = history.getHistoryEntries();
187-
entries.sort((o1, o2) -> new Version(o2.getRevision()).compareTo(new Version(o1.getRevision())));
188-
history.setHistoryEntries(entries);
198+
sortHistoryEntries(history);
189199
}
190200

191201
return history;

opengrok-indexer/src/main/java/org/opengrok/indexer/history/HistoryEntry.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.logging.Level;
3333
import java.util.logging.Logger;
3434

35+
import org.jetbrains.annotations.VisibleForTesting;
3536
import org.opengrok.indexer.logger.LoggerFactory;
3637

3738
/**
@@ -89,6 +90,12 @@ public HistoryEntry(String revision, Date date, String author,
8990
this.files.addAll(files);
9091
}
9192

93+
@VisibleForTesting
94+
HistoryEntry(String revision) {
95+
this();
96+
this.revision = revision;
97+
}
98+
9299
public String getLine() {
93100
return String.join(" ",
94101
getRevision(), getDate().toString(), getAuthor(), message, "\n");

opengrok-indexer/src/test/java/org/opengrok/indexer/history/CVSRepositoryTest.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, 2019, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
@@ -31,8 +31,10 @@
3131
import java.net.URISyntaxException;
3232
import java.nio.channels.FileChannel;
3333
import java.util.ArrayList;
34+
import java.util.Arrays;
3435
import java.util.Collections;
3536
import java.util.List;
37+
import java.util.stream.Collectors;
3638

3739
import org.junit.jupiter.api.AfterEach;
3840
import org.junit.jupiter.api.BeforeEach;
@@ -113,12 +115,10 @@ public static void runCvsCommand(File reposRoot, String... args) {
113115
}
114116

115117
/**
116-
* Get the CVS repository, test that getBranch() returns null if there is
117-
* no branch.
118-
* @throws Exception
118+
* Get the CVS repository, test that getBranch() returns null if there is no branch.
119119
*/
120120
@Test
121-
public void testGetBranchNoBranch() throws Exception {
121+
void testGetBranchNoBranch() throws Exception {
122122
setUpTestRepository();
123123
File root = new File(repository.getSourceRoot(), "cvs_test/cvsrepo");
124124
CVSRepository cvsrepo = (CVSRepository) RepositoryFactory.getRepository(root);
@@ -131,10 +131,9 @@ public void testGetBranchNoBranch() throws Exception {
131131
* with branch revision numbers.
132132
* Last, check that history entries of the file follow through before the
133133
* branch was created.
134-
* @throws Exception
135134
*/
136135
@Test
137-
public void testNewBranch() throws Exception {
136+
void testNewBranch() throws Exception {
138137
setUpTestRepository();
139138
File root = new File(repository.getSourceRoot(), "cvs_test/cvsrepo");
140139

@@ -145,8 +144,7 @@ public void testNewBranch() throws Exception {
145144

146145
// Now the repository object can be instantiated so that determineBranch()
147146
// will be called.
148-
CVSRepository cvsrepo
149-
= (CVSRepository) RepositoryFactory.getRepository(root);
147+
CVSRepository cvsrepo = (CVSRepository) RepositoryFactory.getRepository(root);
150148

151149
assertEquals("mybranch", cvsrepo.getBranch());
152150

@@ -171,11 +169,35 @@ public void testNewBranch() throws Exception {
171169
assertEquals("1.1", mainCHistory.getHistoryEntries().get(2).getRevision());
172170
}
173171

172+
/**
173+
* Assert that revision strings in history entries are sorted semantically.
174+
* This is necessary for displaying revisions on a branch in correct order.
175+
*/
176+
@Test
177+
void testRevisionSorting() {
178+
HistoryEntry[] entries = {
179+
new HistoryEntry("1.1"),
180+
new HistoryEntry("1.12.200.2.2.3.50.2"),
181+
new HistoryEntry("1.9"),
182+
new HistoryEntry("1.12.200.2.2.3"),
183+
new HistoryEntry("1.2"),
184+
new HistoryEntry("1.12.200.1"),
185+
new HistoryEntry("1.12.200.2.2.2"),
186+
};
187+
History history = new History(Arrays.stream(entries).collect(Collectors.toList()));
188+
CVSHistoryParser.sortHistoryEntries(history);
189+
List<String> revisionsActual = history.getHistoryEntries().stream().
190+
map(HistoryEntry::getRevision).collect(Collectors.toList());
191+
List<String> revisionsExpected = List.of("1.12.200.2.2.3.50.2",
192+
"1.12.200.2.2.3", "1.12.200.2.2.2", "1.12.200.1", "1.9", "1.2", "1.1");
193+
assertEquals(revisionsExpected, revisionsActual);
194+
}
195+
174196
/**
175197
* Test of fileHasAnnotation method, of class CVSRepository.
176198
*/
177199
@Test
178-
public void testFileHasAnnotation() {
200+
void testFileHasAnnotation() {
179201
boolean result = instance.fileHasAnnotation(null);
180202
assertTrue(result);
181203
}
@@ -184,17 +206,16 @@ public void testFileHasAnnotation() {
184206
* Test of fileHasHistory method, of class CVSRepository.
185207
*/
186208
@Test
187-
public void testFileHasHistory() {
209+
void testFileHasHistory() {
188210
boolean result = instance.fileHasHistory(null);
189211
assertTrue(result);
190212
}
191213

192214
/**
193215
* Test of parseAnnotation method, of class CVSRepository.
194-
* @throws java.lang.Exception
195216
*/
196217
@Test
197-
public void testParseAnnotation() throws Exception {
218+
void testParseAnnotation() throws Exception {
198219
String revId1 = "1.1";
199220
String revId2 = "1.2.3";
200221
String revId3 = "1.0";
@@ -226,5 +247,4 @@ public void testParseAnnotation() throws Exception {
226247
assertEquals(revId2.length(), result.getWidestRevision());
227248
assertEquals(fileName, result.getFilename());
228249
}
229-
230250
}

0 commit comments

Comments
 (0)