Skip to content

Commit

Permalink
Merge pull request #9715 from Luggas4you/fix-comperator-coverage
Browse files Browse the repository at this point in the history
add missing BibStringDiff test cases
  • Loading branch information
Siedlerchr authored Apr 3, 2023
2 parents 5223d0c + 3fee7d0 commit 8437a70
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static List<BibStringDiff> compare(BibDatabase originalDatabase, BibDatab

Optional<BibtexString> match = newDatabase
.getStringValues().stream()
.filter(test -> test.getName().equals(original.getName()))
.filter(test -> test.getContent().equals(original.getContent()))
.findAny();
if (match.isPresent()) {
// We have found a string with the same content. It cannot have the same
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.logic.bibtex.comparator;

import java.util.Collections;
import java.util.List;

import org.jabref.model.database.BibDatabase;
Expand All @@ -9,6 +10,9 @@
import org.junit.jupiter.api.Test;

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

Expand All @@ -22,13 +26,100 @@ public class BibStringDiffTest {
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() {
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")));

List<BibStringDiff> result = BibStringDiff.compare(originalDataBase, newDataBase);
assertEquals(List.of(diff), result);
}

@Test
void equalTest() {
BibStringDiff other = new BibStringDiff(diff.getOriginalString(), diff.getNewString());
assertEquals(diff, other);
assertEquals(diff.hashCode(), other.hashCode());
}

@Test
void notEqualTest() {
BibStringDiff other = new BibStringDiff(diff.getNewString(), diff.getOriginalString());
assertNotEquals(diff, other);
assertNotEquals(diff.hashCode(), other.hashCode());
}

@Test
void identicalObjectsAreEqual() {
BibStringDiff other = diff;
assertTrue(other.equals(diff));
}

@Test
void compareToNullObjectIsFalse() {
assertFalse(diff.equals(null));
}

@Test
void compareToDifferentClassIsFalse() {
assertFalse(diff.equals(new Object()));
}

@Test
void testGetters() {
BibtexString bsOne = new BibtexString("aKahle", "Kahle, Brewster");
BibtexString bsTwo = new BibtexString("iMIT", "Institute of Technology");
BibStringDiff diff = new BibStringDiff(bsOne, bsTwo);
assertEquals(diff.getOriginalString(), bsOne);
assertEquals(diff.getNewString(), bsTwo);
}

@Test
void testCompareEmptyDatabases() {
when(originalDataBase.hasNoStrings()).thenReturn(true);
when(newDataBase.hasNoStrings()).thenReturn(true);

assertEquals(Collections.emptyList(), BibStringDiff.compare(originalDataBase, newDataBase));
}

@Test
void testCompareNameChange() {
when(originalDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content")));
when(newDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name2", "content")));

List<BibStringDiff> result = BibStringDiff.compare(originalDataBase, newDataBase);
BibStringDiff expectedDiff = new BibStringDiff(new BibtexString("name", "content"), new BibtexString("name2", "content"));
assertEquals(List.of(expectedDiff), result);
}

@Test
void testCompareNoDiff() {
when(originalDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content")));
when(newDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content")));

List<BibStringDiff> result = BibStringDiff.compare(originalDataBase, newDataBase);
assertEquals(Collections.emptyList(), result);
}

@Test
void testCompareRemovedString() {
when(originalDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content")));
when(newDataBase.getStringValues()).thenReturn(Collections.emptyList());

List<BibStringDiff> result = BibStringDiff.compare(originalDataBase, newDataBase);
BibStringDiff expectedDiff = new BibStringDiff(new BibtexString("name", "content"), null);
assertEquals(List.of(expectedDiff), result);
}

@Test
void testCompareAddString() {
when(originalDataBase.getStringValues()).thenReturn(Collections.emptyList());
when(newDataBase.getStringValues()).thenReturn(List.of(new BibtexString("name", "content")));

List<BibStringDiff> result = BibStringDiff.compare(originalDataBase, newDataBase);
BibStringDiff expectedDiff = new BibStringDiff(null, new BibtexString("name", "content"));
assertEquals(List.of(expectedDiff), result);
}
}

0 comments on commit 8437a70

Please sign in to comment.