-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(DatenDeleteAttachmentTest): NullPointerException in setUp
- Loading branch information
1 parent
b32252d
commit 8b1c083
Showing
2 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/de/danielluedecke/zettelkasten/database/DatenDeleteAttachmentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package de.danielluedecke.zettelkasten.database; | ||
|
||
import de.danielluedecke.zettelkasten.ZettelkastenView; | ||
import de.danielluedecke.zettelkasten.settings.Settings; | ||
import org.jdom2.Element; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class DatenDeleteAttachmentTest { | ||
private Daten daten; | ||
private int entryNumber; | ||
|
||
@BeforeMethod | ||
public void setUp() { | ||
// Mock the required dependencies | ||
ZettelkastenView view = Mockito.mock(ZettelkastenView.class); | ||
Settings settings = Mockito.mock(Settings.class); | ||
Synonyms synonyms = Mockito.mock(Synonyms.class); | ||
BibTeX bibTeX = Mockito.mock(BibTeX.class); | ||
|
||
// Initialize Daten with mocks | ||
daten = new Daten(view, settings, synonyms, bibTeX); | ||
entryNumber = 1; | ||
|
||
// Set up multiple duplicate attachments using addAttachments | ||
String[] attachments = {"attachment1.pdf", "attachment1.pdf", "attachment1.pdf"}; | ||
daten.addAttachments(entryNumber, attachments); | ||
|
||
// Confirm attachments are added | ||
List<Element> initialAttachments = daten.getAttachments(entryNumber); | ||
Assert.assertEquals(initialAttachments.size(), 3, "Three instances of the attachment should have been added."); | ||
} | ||
|
||
@Test | ||
public void testDeleteAttachmentListedMultipleTimes() { | ||
// Attempt to delete an attachment that is listed three times | ||
daten.deleteAttachment("attachment1.pdf", entryNumber); | ||
|
||
// Get remaining attachments as Elements | ||
List<Element> remainingElements = daten.getAttachments(entryNumber); | ||
|
||
// Extract text values from Element objects for assertion | ||
List<String> remainingAttachments = remainingElements == null | ||
? Collections.emptyList() | ||
: remainingElements.stream() | ||
.map(Element::getText) | ||
.collect(Collectors.toList()); | ||
|
||
// Assert that all instances are deleted | ||
Assert.assertTrue(remainingAttachments.isEmpty(), | ||
"All instances of the attachment should be deleted, but duplicates may remain."); | ||
} | ||
} |