-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renaming a file should re-add the extension if missing 11903 (#11908)
* in renameToName, the logic for storing old extension and using it if there is none is implemented * Update CHANGELOG.md updating changelog based on suggestion. Co-authored-by: Subhramit Basu Bhowmick <[email protected]> * added optionals, file cannot be renamed without any extension now, added testclass for linkedFileHandler * removed unnecesary cookiehandler, fixed the code so it doesnt violate checkstyle * removed duplicated tests, switched to parameterized tests, unnecesary comments removed * Improve readability and fix logic for no extensions present * Fix test parameter order --------- Co-authored-by: Ivo Bubeňko <[email protected]> Co-authored-by: Subhramit Basu Bhowmick <[email protected]> Co-authored-by: Oliver Kopp <[email protected]>
- Loading branch information
1 parent
96ea003
commit 60ef28a
Showing
3 changed files
with
71 additions
and
1 deletion.
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
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
60 changes: 60 additions & 0 deletions
60
src/test/java/org/jabref/logic/externalfiles/LinkedFileHandlerTest.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,60 @@ | ||
package org.jabref.logic.externalfiles; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.jabref.gui.preferences.GuiPreferences; | ||
import org.jabref.logic.FilePreferences; | ||
import org.jabref.logic.xmp.XmpPreferences; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.LinkedFile; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class LinkedFileHandlerTest { | ||
private Path tempFolder; | ||
private BibEntry entry; | ||
private BibDatabaseContext databaseContext; | ||
private final FilePreferences filePreferences = mock(FilePreferences.class); | ||
private final GuiPreferences preferences = mock(GuiPreferences.class); | ||
|
||
@BeforeEach | ||
void setUp(@TempDir Path tempFolder) { | ||
entry = new BibEntry().withCitationKey("asdf"); | ||
databaseContext = new BibDatabaseContext(); | ||
|
||
when(filePreferences.confirmDeleteLinkedFile()).thenReturn(true); | ||
when(preferences.getFilePreferences()).thenReturn(filePreferences); | ||
when(preferences.getXmpPreferences()).thenReturn(mock(XmpPreferences.class)); | ||
|
||
this.tempFolder = tempFolder; | ||
} | ||
|
||
@ParameterizedTest(name = "{1} to {2} should be {0}") | ||
@CsvSource({ | ||
"newName.pdf, test.pdf, newName", | ||
"newName.txt, test.pdf, newName.txt", | ||
"newNameWithoutExtension, test, newNameWithoutExtension", | ||
"newName.pdf, testFile, newName.pdf", | ||
"newName..pdf, test.pdf, newName." | ||
}) | ||
void renameFile(String expectedFileName, String originalFileName, String newFileName) throws Exception { | ||
final Path tempFile = tempFolder.resolve(originalFileName); | ||
Files.createFile(tempFile); | ||
|
||
final LinkedFile linkedFile = new LinkedFile("", tempFile, ""); | ||
LinkedFileHandler linkedFileHandler = new LinkedFileHandler(linkedFile, entry, databaseContext, filePreferences); | ||
|
||
linkedFileHandler.renameToName(newFileName, false); | ||
final String result = Path.of(linkedFile.getLink()).getFileName().toString(); | ||
assertEquals(expectedFileName, result); | ||
} | ||
} |