Skip to content

Commit

Permalink
Renaming a file should re-add the extension if missing 11903 (#11908)
Browse files Browse the repository at this point in the history
* 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
4 people authored Oct 14, 2024
1 parent 96ea003 commit 60ef28a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- When fulltext search is selected but indexing is deactivated, a dialog is now shown asking if the user wants to enable indexing now [#9491](https://github.com/JabRef/jabref/issues/9491)
- We changed instances of 'Search Selected' to 'Search Pre-configured' in Web Search Preferences UI. [#11871](https://github.com/JabRef/jabref/pull/11871)
- We added a new CSS style class `main-table` for the main table. [#11881](https://github.com/JabRef/jabref/pull/11881)
- When renaming a file, the old extension is now used if there is none provided in the new name. [#11903](https://github.com/JabRef/jabref/issues/11903)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ public boolean renameToName(String targetFileName, boolean overwriteExistingFile
}

final Path oldPath = oldFile.get();
final Path newPath = oldPath.resolveSibling(targetFileName);
Optional<String> oldExtension = FileUtil.getFileExtension(oldPath);
Optional<String> newExtension = FileUtil.getFileExtension(targetFileName);

Path newPath;
if (newExtension.isPresent() || (oldExtension.isEmpty() && newExtension.isEmpty())) {
newPath = oldPath.resolveSibling(targetFileName);
} else {
assert oldExtension.isPresent() && newExtension.isEmpty();
newPath = oldPath.resolveSibling(targetFileName + "." + oldExtension.get());
}

String expandedOldFilePath = oldPath.toString();
boolean pathsDifferOnlyByCase = newPath.toString().equalsIgnoreCase(expandedOldFilePath)
Expand Down
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);
}
}

0 comments on commit 60ef28a

Please sign in to comment.