diff --git a/src/main/java/org/jabref/logic/bibtex/comparator/BibStringDiff.java b/src/main/java/org/jabref/logic/bibtex/comparator/BibStringDiff.java index 78d78aa758d..8050af537fa 100644 --- a/src/main/java/org/jabref/logic/bibtex/comparator/BibStringDiff.java +++ b/src/main/java/org/jabref/logic/bibtex/comparator/BibStringDiff.java @@ -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; } @@ -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); + } } diff --git a/src/main/java/org/jabref/logic/bibtex/comparator/PreambleDiff.java b/src/main/java/org/jabref/logic/bibtex/comparator/PreambleDiff.java index e54074a1f40..2308a8d80c6 100644 --- a/src/main/java/org/jabref/logic/bibtex/comparator/PreambleDiff.java +++ b/src/main/java/org/jabref/logic/bibtex/comparator/PreambleDiff.java @@ -1,5 +1,6 @@ package org.jabref.logic.bibtex.comparator; +import java.util.Objects; import java.util.Optional; import org.jabref.model.database.BibDatabaseContext; @@ -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; } @@ -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); + } } diff --git a/src/main/java/org/jabref/model/entry/BibtexString.java b/src/main/java/org/jabref/model/entry/BibtexString.java index c9e00027706..84e1518cf3e 100644 --- a/src/main/java/org/jabref/model/entry/BibtexString.java +++ b/src/main/java/org/jabref/model/entry/BibtexString.java @@ -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)); } diff --git a/src/test/java/org/jabref/logic/bibtex/comparator/BibStringDiffTest.java b/src/test/java/org/jabref/logic/bibtex/comparator/BibStringDiffTest.java new file mode 100644 index 00000000000..1db4078fdfa --- /dev/null +++ b/src/test/java/org/jabref/logic/bibtex/comparator/BibStringDiffTest.java @@ -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 result = BibStringDiff.compare(originalDataBase, newDataBase); + assertEquals(List.of(diff), result); + } +} diff --git a/src/test/java/org/jabref/logic/bibtex/comparator/PreambleDiffTest.java b/src/test/java/org/jabref/logic/bibtex/comparator/PreambleDiffTest.java new file mode 100644 index 00000000000..b868a4e26bf --- /dev/null +++ b/src/test/java/org/jabref/logic/bibtex/comparator/PreambleDiffTest.java @@ -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 expected = Optional.of(new PreambleDiff("preamble", "otherPreamble")); + Optional result = PreambleDiff.compare(originalDataBaseContext, newDataBaseContext); + assertEquals(expected, result); + } +}