Skip to content

Commit 2c202eb

Browse files
harrisricvladak
authored andcommitted
Fix AnnotationData and HistoryEntry. Add serialisation tests.
1 parent ffb5b48 commit 2c202eb

File tree

6 files changed

+113
-23
lines changed

6 files changed

+113
-23
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,15 @@ public String getRevision(int line) {
112112
}
113113

114114
/**
115-
* Gets the revision for the last change to the specified line.
115+
* Gets the representation of the revision to be used for display purposes, which may be abbreviated, for the last change to the specified line.
116116
*
117117
* @param line line number (counting from 1)
118118
* @return revision string, or an empty string if there is no information
119119
* about the specified line
120120
*/
121121
public String getRevisionForDisplay(int line) {
122122
try {
123-
AnnotationLine annotationLine = annotationLines.get(line - 1);
124-
return annotationLine.getRevision();
123+
return annotationLines.get(line - 1).getDisplayRevision();
125124
} catch (IndexOutOfBoundsException e) {
126125
return "";
127126
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ public HistoryEntry(String revision, Date date, String author, String message, b
8888
this(revision, null, date, author, message, active, null);
8989
}
9090

91-
public HistoryEntry(String revision, Date date, String author,
92-
String message, boolean active, Collection<String> files) {
93-
this(revision, null, date, author, message, active, files);
94-
}
95-
9691
@VisibleForTesting
9792
HistoryEntry(String revision) {
9893
this();
@@ -176,6 +171,10 @@ public void setRevision(String revision) {
176171
this.revision = revision;
177172
}
178173

174+
public void setDisplayRevision(String displayRevision) {
175+
this.displayRevision = displayRevision;
176+
}
177+
179178
public void appendMessage(String message) {
180179
this.message.append(message);
181180
this.message.append("\n");
@@ -207,7 +206,7 @@ public void setTags(String tags) {
207206
@Override
208207
public String toString() {
209208
return String.join(" ",
210-
getRevision(), getDate().toString(), getAuthor(), getMessage(), getFiles().toString());
209+
getRevision(), getDisplayRevision(), getDate().toString(), getAuthor(), getMessage(), getFiles().toString());
211210
}
212211

213212
/**

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,48 @@ void testEqualsPositive() {
129129

130130
assertNotEquals(annotationData1, annotationData2);
131131
}
132+
133+
134+
135+
/**
136+
* Test retrieval of the revision from annotation data lines.
137+
*/
138+
@Test
139+
void testRevisionFromLine() {
140+
final AnnotationLine annotationLine1 = new AnnotationLine("1.0.aaaaaaaaa", "Me", true, "1.0");
141+
final AnnotationLine annotationLine2 = new AnnotationLine("1.1.aaaaaaaaa", "Me", true, "1.1");
142+
final AnnotationLine annotationLine3 = new AnnotationLine("1.2.aaaaaaaaa", "Me", true, "1.2");
143+
144+
AnnotationData annotationData1 = new AnnotationData();
145+
annotationData1.addLine(annotationLine1);
146+
annotationData1.addLine(annotationLine2);
147+
annotationData1.addLine(annotationLine3);
148+
149+
assertEquals("1.0.aaaaaaaaa", annotationData1.getRevision(1));
150+
assertEquals("1.2.aaaaaaaaa", annotationData1.getRevision(3));
151+
assertEquals("", annotationData1.getRevision(0), "Line not present, return empty string");
152+
assertEquals("", annotationData1.getRevision(4), "Line not present, return empty string");
153+
}
154+
155+
156+
/**
157+
* Test retrieval of the revision from annotation data lines.
158+
*/
159+
@Test
160+
void testDisplayRevisionFromLine() {
161+
final AnnotationLine annotationLine1 = new AnnotationLine("1.0.aaaaaaaaa", "Me", true, "1.0");
162+
final AnnotationLine annotationLine2 = new AnnotationLine("1.1.aaaaaaaaa", "Me", true, "1.1");
163+
final AnnotationLine annotationLine3 = new AnnotationLine("1.2.aaaaaaaaa", "Me", true, "1.2");
164+
165+
AnnotationData annotationData1 = new AnnotationData();
166+
annotationData1.addLine(annotationLine1);
167+
annotationData1.addLine(annotationLine2);
168+
annotationData1.addLine(annotationLine3);
169+
170+
assertEquals("1.0", annotationData1.getRevisionForDisplay(1));
171+
assertEquals("1.2", annotationData1.getRevisionForDisplay(3));
172+
assertEquals("", annotationData1.getRevisionForDisplay(0), "Line not present, return empty string");
173+
assertEquals("", annotationData1.getRevisionForDisplay(4), "Line not present, return empty string");
174+
}
132175
}
133176

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ public class HistoryEntryTest {
4848

4949
private HistoryEntry instance;
5050
private final Date historyDate = new Date();
51-
private final String historyRevision = "1.0";
51+
private final String historyRevision = "1.0.aaaaaa";
52+
private final String historyDisplayRevision = "1.0";
5253
private final String historyAuthor = "test author";
5354
private final String historyMessage = "history entry message";
5455

5556
@BeforeEach
5657
public void setUp() {
57-
instance = new HistoryEntry(historyRevision, historyDate,
58-
historyAuthor, historyMessage, true);
58+
instance = new HistoryEntry(historyRevision, historyDisplayRevision, historyDate,
59+
historyAuthor, historyMessage, true, null);
5960
}
6061

6162
@AfterEach
@@ -118,6 +119,14 @@ public void getRevision() {
118119
assertEquals(historyRevision, instance.getRevision());
119120
}
120121

122+
/**
123+
* Test of getDisplayRevision method, of class HistoryEntry.
124+
*/
125+
@Test
126+
public void getDisplayRevision() {
127+
assertEquals(historyDisplayRevision, instance.getDisplayRevision());
128+
}
129+
121130
/**
122131
* Test of setAuthor method, of class HistoryEntry.
123132
*/
@@ -179,6 +188,18 @@ public void setRevision() {
179188
assertEquals(revision, instance.getRevision());
180189
}
181190

191+
/**
192+
* Test of setDisplayRevision method, of class HistoryEntry.
193+
*/
194+
@Test
195+
public void setDisplayRevision() {
196+
String displayRevision = "1.2";
197+
instance.setDisplayRevision(displayRevision);
198+
assertEquals(displayRevision, instance.getDisplayRevision());
199+
instance.setDisplayRevision(null);
200+
assertEquals(historyRevision, instance.getDisplayRevision());
201+
}
202+
182203
/**
183204
* Test of appendMessage method, of class HistoryEntry.
184205
*/
@@ -265,15 +286,15 @@ public void testEqualsEmpty() {
265286

266287
@Test
267288
public void testEquals() {
268-
HistoryEntry e = new HistoryEntry(historyRevision, historyDate,
269-
historyAuthor, historyMessage, true);
289+
HistoryEntry e = new HistoryEntry(historyRevision, historyDisplayRevision, historyDate,
290+
historyAuthor, historyMessage, true, null);
270291
assertNotSame(e, instance);
271292
assertEquals(e, instance);
272293
}
273294

274295
@Test
275296
public void testEqualsWithFilesInstance() {
276-
HistoryEntry e = new HistoryEntry(historyRevision, historyDate,
297+
HistoryEntry e = new HistoryEntry(historyRevision, historyDisplayRevision, historyDate,
277298
historyAuthor, historyMessage, true,
278299
Set.of(File.separator + Paths.get("foo", "main.o"),
279300
File.separator + Paths.get("foo", "testsprog")));
@@ -285,9 +306,9 @@ public void testEqualsWithFilesInstance() {
285306
public void testEqualsWithFilesPositive() {
286307
Set<String> files = Set.of(File.separator + Paths.get("foo", "main.o"),
287308
File.separator + Paths.get("foo", "testsprog"));
288-
HistoryEntry e1 = new HistoryEntry(historyRevision, historyDate,
309+
HistoryEntry e1 = new HistoryEntry(historyRevision, historyDisplayRevision, historyDate,
289310
historyAuthor, historyMessage, true, files);
290-
HistoryEntry e2 = new HistoryEntry(historyRevision, historyDate,
311+
HistoryEntry e2 = new HistoryEntry(historyRevision, historyDisplayRevision, historyDate,
291312
historyAuthor, historyMessage, true, files);
292313
assertNotSame(e1, e2);
293314
assertEquals(e1, e2);
@@ -297,10 +318,10 @@ public void testEqualsWithFilesPositive() {
297318
public void testEqualsWithFilesNegative() {
298319
String file1 = File.separator + Paths.get("foo", "main.o");
299320
String file2 = File.separator + Paths.get("foo", "testsprog");
300-
HistoryEntry e1 = new HistoryEntry(historyRevision, historyDate,
321+
HistoryEntry e1 = new HistoryEntry(historyRevision, historyDisplayRevision, historyDate,
301322
historyAuthor, historyMessage, true,
302323
Set.of(file1, file2));
303-
HistoryEntry e2 = new HistoryEntry(historyRevision, historyDate,
324+
HistoryEntry e2 = new HistoryEntry(historyRevision, historyDisplayRevision, historyDate,
304325
historyAuthor, historyMessage, true,
305326
Set.of(file1, file2 + "X"));
306327
assertNotSame(e1, e2);

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
package org.opengrok.indexer.history;
2424

2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.io.TempDir;
2627

2728
import java.io.File;
29+
import java.io.IOException;
30+
import java.nio.file.Path;
2831
import java.nio.file.Paths;
32+
import java.util.ArrayList;
2933
import java.util.Date;
3034
import java.util.List;
3135
import java.util.Map;
@@ -39,16 +43,27 @@
3943
import static org.junit.jupiter.api.Assertions.assertTrue;
4044

4145
class HistoryTest {
46+
47+
48+
private static final String HASH_67DFBE26 = "67dfbe26somethinglonger";
49+
private static final String HASH_84599B3C = "84599b3canotherstring11";
50+
private static final String ABBREV_HASH_67DFBE26 = "67dfbe26";
51+
private static final String ABBREV_HASH_84599B3C = "84599b3c";
52+
4253
private final List<HistoryEntry> entries = List.of(
43-
new HistoryEntry("84599b3c", new Date(1485438707000L),
54+
new HistoryEntry(HASH_84599B3C, ABBREV_HASH_84599B3C, new Date(1485438707000L),
4455
"Kryštof Tulinger <[email protected]>",
4556
" renaming directories\n\n", true,
4657
Set.of(File.separator + Paths.get("git", "moved2", "renamed2.c"))),
47-
new HistoryEntry("67dfbe26", new Date(1485263397000L),
58+
new HistoryEntry(HASH_67DFBE26, ABBREV_HASH_67DFBE26, new Date(1485263397000L),
4859
"Kryštof Tulinger <[email protected]>",
4960
" renaming renamed -> renamed2\n\n", true,
5061
Set.of(File.separator + Paths.get("git", "moved", "renamed2.c"))));
5162

63+
@TempDir
64+
Path temporaryPath;
65+
66+
5267
@Test
5368
void testEqualsRenamed() {
5469
History history = new History(entries,
@@ -128,4 +143,17 @@ void testGetLastHistoryEntry() {
128143
History history = new History(entries);
129144
assertEquals(entries.get(0), history.getLastHistoryEntry());
130145
}
146+
147+
/**
148+
* Serialises and then deserialises a history and checks for equality.
149+
*/
150+
@Test
151+
void testSerialisation() throws IOException {
152+
ArrayList<HistoryEntry> serialisableEntryList = new ArrayList<>(entries);
153+
History history = new History(serialisableEntryList);
154+
File tempFile = File.createTempFile("tmpHistory1", "gz", temporaryPath.toFile());
155+
CacheUtil.writeCache(history, tempFile);
156+
History deserialised = FileHistoryCache.readCache(tempFile);
157+
assertEquals(history, deserialised);
158+
}
131159
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class TestHistoryCollectorVsMergeChangesets {
5050

5151
private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
5252

53-
private static final String lastRevision = "4d1b7cfb";
53+
private static final String lastRevision = "4d1b7cfb1e7f3fc7360bd2f213d9e634e6a40cf8";
5454

5555
@BeforeAll
5656
public static void setUpClass() throws Exception {
@@ -80,7 +80,7 @@ void testReindexWithHistoryBasedRepository(boolean usePerPartes) throws Exceptio
8080
.setDirectory(localPath)
8181
.call()) {
8282

83-
final String intermediateRevision = "f3ddb4ba";
83+
final String intermediateRevision = "f3ddb4ba641598015e04701fc3a598b882775644";
8484

8585
gitClone.reset().setMode(ResetCommand.ResetType.HARD).
8686
setRef(intermediateRevision).call();

0 commit comments

Comments
 (0)