Skip to content

Commit

Permalink
Add more unit tests (#7638)
Browse files Browse the repository at this point in the history
* Add more unit tests

* Delete unnecessary test

* Override equals method for easier testing

Address the requested changes from #7638

* Fix checkstyle

* Suggested changes & no Id comparison in BibtexString

* Compare optionals and compare lists
  • Loading branch information
Davfon authored May 21, 2021
1 parent 3b3e7d6 commit 7196eb3
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class BibStringDiff {
private final BibtexString originalString;
private final BibtexString newString;

private BibStringDiff(BibtexString originalString, BibtexString newString) {
BibStringDiff(BibtexString originalString, BibtexString newString) {
this.originalString = originalString;
this.newString = newString;
}
Expand Down Expand Up @@ -88,4 +88,22 @@ public BibtexString getOriginalString() {
public BibtexString getNewString() {
return newString;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if ((other == null) || (getClass() != other.getClass())) {
return false;
}

BibStringDiff that = (BibStringDiff) other;
return Objects.equals(newString, that.newString) && Objects.equals(originalString, that.originalString);
}

@Override
public int hashCode() {
return Objects.hash(originalString, newString);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.logic.bibtex.comparator;

import java.util.Objects;
import java.util.Optional;

import org.jabref.model.database.BibDatabaseContext;
Expand All @@ -9,7 +10,7 @@ public class PreambleDiff {
private final String originalPreamble;
private final String newPreamble;

private PreambleDiff(String originalPreamble, String newPreamble) {
PreambleDiff(String originalPreamble, String newPreamble) {
this.originalPreamble = originalPreamble;
this.newPreamble = newPreamble;
}
Expand All @@ -31,4 +32,22 @@ public String getNewPreamble() {
public String getOriginalPreamble() {
return originalPreamble;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if ((other == null) || (getClass() != other.getClass())) {
return false;
}

PreambleDiff that = (PreambleDiff) other;
return Objects.equals(newPreamble, that.newPreamble) && Objects.equals(originalPreamble, that.originalPreamble);
}

@Override
public int hashCode() {
return Objects.hash(originalPreamble, newPreamble);
}
}
1 change: 0 additions & 1 deletion src/main/java/org/jabref/model/entry/BibtexString.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ public boolean equals(Object o) {
return (Objects.equals(hasChanged, that.hasChanged) &&
Objects.equals(name, that.name) &&
Objects.equals(content, that.content) &&
Objects.equals(id, that.id) &&
Objects.equals(type, that.type) &&
Objects.equals(parsedSerialization, that.parsedSerialization));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jabref.logic.bibtex.comparator;

import java.util.List;

import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibtexString;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BibStringDiffTest {

private final BibDatabase originalDataBase = mock(BibDatabase.class);
private final BibDatabase newDataBase = mock(BibDatabase.class);
private final BibStringDiff diff = new BibStringDiff(new BibtexString("name2", "content2"), new BibtexString("name2", "content3"));

@BeforeEach
void setUp() {
when(originalDataBase.hasNoStrings()).thenReturn(false);
when(newDataBase.hasNoStrings()).thenReturn(false);
when(originalDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content"), new BibtexString("name2", "content2")));
when(newDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content"), new BibtexString("name2", "content3")));
}

@Test
void compareTest() {
List<BibStringDiff> result = BibStringDiff.compare(originalDataBase, newDataBase);
assertEquals(List.of(diff), result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jabref.logic.bibtex.comparator;

import java.util.Optional;

import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PreambleDiffTest {

private final BibDatabaseContext originalDataBaseContext = mock(BibDatabaseContext.class);
private final BibDatabaseContext newDataBaseContext = mock(BibDatabaseContext.class);
private final BibDatabase originalDataBase = mock(BibDatabase.class);
private final BibDatabase newDataBase = mock(BibDatabase.class);

@BeforeEach
void setUp() {
when(originalDataBaseContext.getDatabase()).thenReturn(originalDataBase);
when(newDataBaseContext.getDatabase()).thenReturn(newDataBase);
}

@Test
void compareSamePreambleTest() {
when(originalDataBase.getPreamble()).thenReturn(Optional.of("preamble"));
when(newDataBase.getPreamble()).thenReturn(Optional.of("preamble"));

assertEquals(Optional.empty(), PreambleDiff.compare(originalDataBaseContext, newDataBaseContext));
}

@Test
void compareDifferentPreambleTest() {
when(originalDataBase.getPreamble()).thenReturn(Optional.of("preamble"));
when(newDataBase.getPreamble()).thenReturn(Optional.of("otherPreamble"));

Optional<PreambleDiff> expected = Optional.of(new PreambleDiff("preamble", "otherPreamble"));
Optional<PreambleDiff> result = PreambleDiff.compare(originalDataBaseContext, newDataBaseContext);
assertEquals(expected, result);
}
}

0 comments on commit 7196eb3

Please sign in to comment.