Skip to content

Commit 98364b7

Browse files
author
Vladimir Kotal
committed
add basic History test
1 parent 097be24 commit 98364b7

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,22 @@ public boolean isRenamed(String file) {
123123
public Set<String> getRenamedFiles() {
124124
return renamedFiles;
125125
}
126+
127+
@Override
128+
public boolean equals(Object o) {
129+
if (this == o) {
130+
return true;
131+
}
132+
if (o == null || getClass() != o.getClass()) {
133+
return false;
134+
}
135+
History that = (History) o;
136+
return this.getHistoryEntries().equals(that.getHistoryEntries()) &&
137+
this.getRenamedFiles().equals(that.getRenamedFiles());
138+
}
139+
140+
@Override
141+
public int hashCode() {
142+
return getHistoryEntries().hashCode() * getRenamedFiles().hashCode();
143+
}
126144
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package org.opengrok.indexer.history;
2525

26+
import java.util.Collection;
2627
import java.util.Date;
2728
import java.util.SortedSet;
2829
import java.util.TreeSet;
@@ -84,6 +85,12 @@ public HistoryEntry(String revision, Date date, String author,
8485
this.files = new TreeSet<>();
8586
}
8687

88+
public HistoryEntry(String revision, Date date, String author,
89+
String tags, String message, boolean active, Collection<String> files) {
90+
this(revision, date, author, tags, message, active);
91+
this.files.addAll(files);
92+
}
93+
8794
public String getLine() {
8895
return revision + " " + date + " " + author + " " + message + "\n";
8996
}
@@ -207,4 +214,28 @@ public void stripFiles() {
207214
public void stripTags() {
208215
tags = null;
209216
}
217+
218+
@Override
219+
public boolean equals(Object o) {
220+
if (this == o) {
221+
return true;
222+
}
223+
if (o == null || getClass() != o.getClass()) {
224+
return false;
225+
}
226+
HistoryEntry that = (HistoryEntry) o;
227+
return this.getAuthor().equals(that.getAuthor()) &&
228+
this.getRevision().equals(that.getRevision()) &&
229+
this.getDate().equals(that.getDate()) &&
230+
this.getMessage().equals(that.getMessage()) &&
231+
this.getFiles().equals(that.getFiles()) &&
232+
(this.getTags() != null && that.getTags() != null ?
233+
this.getTags().equals(that.getTags()) : this.getTags() == null && that.getTags() == null);
234+
}
235+
236+
@Override
237+
public int hashCode() {
238+
return getAuthor().hashCode() + getRevision().hashCode() + getDate().hashCode() + getMessage().hashCode() +
239+
getFiles().hashCode() + (getTags() != null ? getTags().hashCode() : 0);
240+
}
210241
}

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.HashSet;
3636
import java.util.List;
3737
import java.util.Set;
38+
import java.util.SortedSet;
3839
import java.util.TreeSet;
3940

4041
import org.eclipse.jgit.api.Git;
@@ -506,12 +507,59 @@ private void runRenamedTest(String fname, String cset, String content) throws Ex
506507
}
507508
}
508509

510+
@Test
511+
public void testHistory() throws Exception {
512+
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(false);
513+
File root = new File(repository.getSourceRoot(), "git");
514+
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
515+
516+
History history = gitrepo.getHistory(root);
517+
assertNotNull(history);
518+
assertNotNull(history.getHistoryEntries());
519+
assertEquals(8, history.getHistoryEntries().size());
520+
assertEquals(0, history.getRenamedFiles().size());
521+
522+
History expectedHistory = new History(List.of(
523+
new HistoryEntry("84599b3c", new Date(1485438707000L),
524+
"Kryštof Tulinger <[email protected]>", null,
525+
" renaming directories\n\n", true,
526+
Set.of("/git/moved2/renamed2.c")),
527+
new HistoryEntry("67dfbe26", new Date(1485263397000L),
528+
"Kryštof Tulinger <[email protected]>", null,
529+
" renaming renamed -> renamed2\n\n", true,
530+
Set.of("/git/moved/renamed2.c")),
531+
new HistoryEntry("1086eaf5", new Date(1485263368000L),
532+
"Kryštof Tulinger <[email protected]>", null,
533+
" adding some lines into renamed.c\n\n", true,
534+
Set.of("/git/moved/renamed.c")),
535+
new HistoryEntry("b6413947", new Date(1485263264000L),
536+
"Kryštof Tulinger <[email protected]>", null,
537+
" moved renamed.c to new location\n\n", true,
538+
Set.of("/git/moved/renamed.c")),
539+
new HistoryEntry("ce4c98ec", new Date(1485263232000L),
540+
"Kryštof Tulinger <[email protected]>", null,
541+
" adding simple file for renamed file testing\n\n", true,
542+
Set.of("/git/renamed.c")),
543+
new HistoryEntry("aa35c258", new Date(1218571965000L),
544+
"Trond Norbye <[email protected]>", null,
545+
" Add lint make target and fix lint warnings\n\n", true,
546+
Set.of("/git/Makefile", "/git/main.c")),
547+
new HistoryEntry("84821564", new Date(1218571643000L),
548+
"Trond Norbye <[email protected]>", null,
549+
" Add the result of a make on Solaris x86\n\n", true,
550+
Set.of("/git/main.o", "/git/testsprog")),
551+
new HistoryEntry("bb74b7e8", new Date(1218571573000L),
552+
"Trond Norbye <[email protected]>", null,
553+
" Added a small test program\n\n", true,
554+
Set.of("/git/Makefile", "/git/header.h", "/git/main.c"))));
555+
assertEquals(expectedHistory, history);
556+
}
557+
509558
@Test
510559
public void testRenamedHistory() throws Exception {
511560
RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(true);
512561
File root = new File(repository.getSourceRoot(), "git");
513-
GitRepository gitrepo
514-
= (GitRepository) RepositoryFactory.getRepository(root);
562+
GitRepository gitrepo = (GitRepository) RepositoryFactory.getRepository(root);
515563

516564
History history = gitrepo.getHistory(root);
517565
assertNotNull(history);

0 commit comments

Comments
 (0)