Skip to content

Commit e79a158

Browse files
author
Vladimir Kotal
committed
fix null handling in equals(), add tests
1 parent bbdfd41 commit e79a158

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

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

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

2020
/*
21-
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
@@ -215,6 +215,14 @@ public void stripTags() {
215215
tags = null;
216216
}
217217

218+
private boolean checkEquals(Object o1, Object o2) {
219+
if (o1 != null && o2 != null) {
220+
return o1.equals(o2);
221+
}
222+
223+
return (o1 == null && o2 == null);
224+
}
225+
218226
@Override
219227
public boolean equals(Object o) {
220228
if (this == o) {
@@ -224,14 +232,13 @@ public boolean equals(Object o) {
224232
return false;
225233
}
226234
HistoryEntry that = (HistoryEntry) o;
227-
// TODO: more robust w.r.t. null
228-
return this.getAuthor().equals(that.getAuthor()) &&
229-
this.getRevision().equals(that.getRevision()) &&
230-
this.getDate().equals(that.getDate()) &&
231-
this.getMessage().equals(that.getMessage()) &&
232-
this.getFiles().equals(that.getFiles()) &&
233-
(this.getTags() != null && that.getTags() != null ?
234-
this.getTags().equals(that.getTags()) : this.getTags() == null && that.getTags() == null);
235+
236+
return checkEquals(this.getAuthor(), that.getAuthor()) &&
237+
checkEquals(this.getRevision(), that.getRevision()) &&
238+
checkEquals(this.getDate(), that.getDate()) &&
239+
checkEquals(this.getMessage(), that.getMessage()) &&
240+
checkEquals(this.getFiles(), that.getFiles()) &&
241+
checkEquals(this.getTags(), that.getTags());
235242
}
236243

237244
@Override

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import static org.junit.jupiter.api.Assertions.assertEquals;
3434
import static org.junit.jupiter.api.Assertions.assertFalse;
35+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3536
import static org.junit.jupiter.api.Assertions.assertNotSame;
3637
import static org.junit.jupiter.api.Assertions.assertNull;
3738
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -248,10 +249,24 @@ public void strip() {
248249
}
249250

250251
@Test
251-
public void testEquals() {
252-
// TODO: add tests with nulls
252+
public void testEqualsCopyConstructor() {
253253
HistoryEntry e = new HistoryEntry(instance);
254254
assertNotSame(e, instance);
255255
assertEquals(e, instance);
256256
}
257+
258+
@Test
259+
public void testEqualsEmpty() {
260+
HistoryEntry e = new HistoryEntry();
261+
assertNotSame(e, instance);
262+
assertNotEquals(e, instance);
263+
}
264+
265+
@Test
266+
public void testEquals() {
267+
HistoryEntry e = new HistoryEntry(historyRevision, historyDate,
268+
historyAuthor, null, historyMessage, true);
269+
assertNotSame(e, instance);
270+
assertEquals(e, instance);
271+
}
257272
}

0 commit comments

Comments
 (0)