From f20c58dd78eeaf42b5cf4d6f857cba78604b6336 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:38:02 +0100 Subject: [PATCH 01/15] Reverse the condition and the content in RemoveLinksToNotExistentFiles Resolves comment "Reverse the condition and the content. Reason: You have BOTH (true and false) cases covered. The true case should normally come first." --- .../jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java b/src/main/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java index b9bcfe8020e..8035aa76dac 100644 --- a/src/main/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java +++ b/src/main/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java @@ -38,7 +38,9 @@ public List cleanup(BibEntry entry) { for (LinkedFile file : files) { LinkedFileHandler fileHandler = new LinkedFileHandler(file, entry, databaseContext, filePreferences); - if (!file.isOnlineLink()) { + if (file.isOnlineLink()) { + cleanedUpFiles.add(file); + } else { Optional oldFile = file.findIn(databaseContext, filePreferences); if (oldFile.isEmpty()) { @@ -46,8 +48,6 @@ public List cleanup(BibEntry entry) { } else { cleanedUpFiles.add(file); } - } else { - cleanedUpFiles.add(file); } } From 95315c61a20faa30ba7ab6589b24a6959a01231e Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:47:53 +0100 Subject: [PATCH 02/15] Fix: Store bibFolder.resolve("test.bib") in a variable --- .../logic/cleanup/RemoveLinksToNotExistentFilesTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index aeedcc7172c..984e63add35 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -42,6 +42,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { // The folder where the files are located originally Path fileFolder = bibFolder.resolve("files"); + Path testBibFolder = bibFolder.resolve("test.bib"); Files.createDirectory(fileFolder); fileBefore = fileFolder.resolve("test.pdf"); Files.createFile(fileBefore); @@ -49,8 +50,8 @@ void setUp(@TempDir Path bibFolder) throws IOException { MetaData metaData = new MetaData(); metaData.setDefaultFileDirectory(defaultFileFolder.toAbsolutePath().toString()); databaseContext = new BibDatabaseContext(new BibDatabase(), metaData); - Files.createFile(bibFolder.resolve("test.bib")); - databaseContext.setDatabasePath(bibFolder.resolve("test.bib")); + Files.createFile(testBibFolder); + databaseContext.setDatabasePath(testBibFolder); LinkedFile fileField = new LinkedFile("", fileBefore.toAbsolutePath(), ""); From 8e0c7c2a2dfbbde0245edf4085a67ba6af190e09 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:51:51 +0100 Subject: [PATCH 03/15] Refactor: Rename variable fileFolder to originalFileFolder and remove comment. --- .../logic/cleanup/RemoveLinksToNotExistentFilesTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index 984e63add35..b082da53d09 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -40,11 +40,10 @@ void setUp(@TempDir Path bibFolder) throws IOException { defaultFileFolder = bibFolder.resolve("pdf"); Files.createDirectory(defaultFileFolder); - // The folder where the files are located originally - Path fileFolder = bibFolder.resolve("files"); + Path originalFileFolder = bibFolder.resolve("files"); Path testBibFolder = bibFolder.resolve("test.bib"); - Files.createDirectory(fileFolder); - fileBefore = fileFolder.resolve("test.pdf"); + Files.createDirectory(originalFileFolder); + fileBefore = originalFileFolder.resolve("test.pdf"); Files.createFile(fileBefore); MetaData metaData = new MetaData(); From 0d24f34426c529db24507f47dfffbdf72ad2b809 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:52:57 +0100 Subject: [PATCH 04/15] Refactor: Rename defaultFileFolder to newFileFolder --- .../logic/cleanup/RemoveLinksToNotExistentFilesTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index b082da53d09..8e4c128e23f 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -27,7 +27,7 @@ import static org.mockito.Mockito.when; public class RemoveLinksToNotExistentFilesTest { - private Path defaultFileFolder; + private Path newFileFolder; private Path fileBefore; private BibEntry entry; private FilePreferences filePreferences; @@ -37,8 +37,8 @@ public class RemoveLinksToNotExistentFilesTest { @BeforeEach void setUp(@TempDir Path bibFolder) throws IOException { // The folder where the files should be moved to - defaultFileFolder = bibFolder.resolve("pdf"); - Files.createDirectory(defaultFileFolder); + newFileFolder = bibFolder.resolve("pdf"); + Files.createDirectory(newFileFolder); Path originalFileFolder = bibFolder.resolve("files"); Path testBibFolder = bibFolder.resolve("test.bib"); @@ -47,7 +47,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { Files.createFile(fileBefore); MetaData metaData = new MetaData(); - metaData.setDefaultFileDirectory(defaultFileFolder.toAbsolutePath().toString()); + metaData.setDefaultFileDirectory(newFileFolder.toAbsolutePath().toString()); databaseContext = new BibDatabaseContext(new BibDatabase(), metaData); Files.createFile(testBibFolder); databaseContext.setDatabasePath(testBibFolder); From b6768020cf00932e9cb01ee929aceb3af918fe35 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:07:23 +0100 Subject: [PATCH 05/15] Refactor: Fix indentation in RemoveLinksToNotExistentFilesTest --- .../RemoveLinksToNotExistentFilesTest.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index 8e4c128e23f..3783adf795d 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -87,20 +87,20 @@ void deleteFileInMultipleLinkedEntry() { FileFieldWriter.getStringRepresentation(new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "")) ); BibEntry expectedEntry = new BibEntry(StandardEntryType.Article) - .withField(StandardField.AUTHOR, "Shatakshi Sharma and Bhim Singh and Sukumar Mishra") - .withField(StandardField.DATE, "April 2020") - .withField(StandardField.YEAR, "2020") - .withField(StandardField.DOI, "10.1109/TII.2019.2935531") - .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""))) - .withField(StandardField.ISSUE, "4") - .withField(StandardField.ISSN, "1941-0050") - .withField(StandardField.JOURNALTITLE, "IEEE Transactions on Industrial Informatics") - .withField(StandardField.PAGES, "2346--2356") - .withField(StandardField.PUBLISHER, "IEEE") - .withField(StandardField.TITLE, "Economic Operation and Quality Control in PV-BES-DG-Based Autonomous System") - .withField(StandardField.VOLUME, "16") - .withField(StandardField.KEYWORDS, "Batteries, Generators, Economics, Power quality, State of charge, Harmonic analysis, Control systems, Battery, diesel generator (DG), distributed generation, power quality, photovoltaic (PV), voltage source converter (VSC)"); + .withField(StandardField.AUTHOR, "Shatakshi Sharma and Bhim Singh and Sukumar Mishra") + .withField(StandardField.DATE, "April 2020") + .withField(StandardField.YEAR, "2020") + .withField(StandardField.DOI, "10.1109/TII.2019.2935531") + .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation( + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""))) + .withField(StandardField.ISSUE, "4") + .withField(StandardField.ISSN, "1941-0050") + .withField(StandardField.JOURNALTITLE, "IEEE Transactions on Industrial Informatics") + .withField(StandardField.PAGES, "2346--2356") + .withField(StandardField.PUBLISHER, "IEEE") + .withField(StandardField.TITLE, "Economic Operation and Quality Control in PV-BES-DG-Based Autonomous System") + .withField(StandardField.VOLUME, "16") + .withField(StandardField.KEYWORDS, "Batteries, Generators, Economics, Power quality, State of charge, Harmonic analysis, Control systems, Battery, diesel generator (DG), distributed generation, power quality, photovoltaic (PV), voltage source converter (VSC)"); fileBefore.toFile().delete(); List changes = removeLinks.cleanup(entry); From 492335d409eaf239b8c71ff7200fd23cc662acfb Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:10:32 +0100 Subject: [PATCH 06/15] Refactor: Replaced Arrays.asList() with List.of() --- .../logic/cleanup/RemoveLinksToNotExistentFilesTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index 3783adf795d..bf8be913ae8 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -3,7 +3,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import java.util.List; import org.jabref.logic.bibtex.FileFieldWriter; @@ -60,7 +59,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { .withField(StandardField.DATE, "April 2020") .withField(StandardField.YEAR, "2020") .withField(StandardField.DOI, "10.1109/TII.2019.2935531") - .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation(Arrays.asList( + .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""), fileField))) .withField(StandardField.ISSUE, "4") @@ -81,7 +80,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { void deleteFileInMultipleLinkedEntry() { LinkedFile fileField = new LinkedFile("", fileBefore.toAbsolutePath(), ""); FieldChange expectedChange = new FieldChange(entry, StandardField.FILE, - FileFieldWriter.getStringRepresentation(Arrays.asList( + FileFieldWriter.getStringRepresentation(List.of( new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""), fileField)), FileFieldWriter.getStringRepresentation(new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "")) @@ -117,7 +116,7 @@ void keepLinksToExistingFiles() { .withField(StandardField.DATE, "April 2020") .withField(StandardField.YEAR, "2020") .withField(StandardField.DOI, "10.1109/TII.2019.2935531") - .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation(Arrays.asList( + .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""), fileField))) .withField(StandardField.ISSUE, "4") From 7f29dea11ff5b3ddb5e21691e3b6cc8896403558 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:12:49 +0100 Subject: [PATCH 07/15] Refactor: Replaced Arrays.asList() with List.of() --- .../jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index bf8be913ae8..7e6995cfe58 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -77,7 +77,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { } @Test - void deleteFileInMultipleLinkedEntry() { + void deleteFileInEntryWithMultipleFileLinks() { LinkedFile fileField = new LinkedFile("", fileBefore.toAbsolutePath(), ""); FieldChange expectedChange = new FieldChange(entry, StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( From eb98aca3609347056c7f8f688aa006549b1d52cd Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:21:07 +0100 Subject: [PATCH 08/15] Refactor: Change to use java.nio for file deletion. --- .../cleanup/RemoveLinksToNotExistentFilesTest.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index 7e6995cfe58..1ebe7f981de 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -101,7 +101,11 @@ void deleteFileInEntryWithMultipleFileLinks() { .withField(StandardField.VOLUME, "16") .withField(StandardField.KEYWORDS, "Batteries, Generators, Economics, Power quality, State of charge, Harmonic analysis, Control systems, Battery, diesel generator (DG), distributed generation, power quality, photovoltaic (PV), voltage source converter (VSC)"); - fileBefore.toFile().delete(); + try { + Files.delete(fileBefore); + } catch (IOException e) { + throw new RuntimeException(e); + } List changes = removeLinks.cleanup(entry); assertEquals(expectedChange, changes.getFirst()); @@ -155,7 +159,11 @@ void deleteLinkedFile() { .withField(StandardField.VOLUME, "16") .withField(StandardField.KEYWORDS, "Batteries, Generators, Economics, Power quality, State of charge, Harmonic analysis, Control systems, Battery, diesel generator (DG), distributed generation, power quality, photovoltaic (PV), voltage source converter (VSC)"); - fileBefore.toFile().delete(); + try { + Files.delete(fileBefore); + } catch (IOException e) { + throw new RuntimeException(e); + } List changes = removeLinks.cleanup(entry); assertEquals(expectedChange, changes.getFirst()); From c582e83aa0ecb24b721ecd04e6bbe62eba9cb7cc Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:22:22 +0100 Subject: [PATCH 09/15] Refactor: Move comment to line above. --- .../logic/cleanup/RemoveLinksToNotExistentFilesTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index 1ebe7f981de..c4594aba77b 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -141,7 +141,9 @@ void keepLinksToExistingFiles() { @Test void deleteLinkedFile() { LinkedFile fileField = new LinkedFile("", fileBefore.toAbsolutePath(), ""); - entry.setField(StandardField.FILE, FileFieldWriter.getStringRepresentation(fileField)); // There is only one linked file in entry + + // There is only one linked file in entry + entry.setField(StandardField.FILE, FileFieldWriter.getStringRepresentation(fileField)); FieldChange expectedChange = new FieldChange(entry, StandardField.FILE, FileFieldWriter.getStringRepresentation(fileField), null); From e6ae7b92eddd67e8f0bc64b0179d12c4e8cdd694 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:44:00 +0100 Subject: [PATCH 10/15] Refactor: Added "PDF" as third argument to LinkedFile for OnlineLink files --- .../cleanup/RemoveLinksToNotExistentFilesTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index c4594aba77b..dc0aaec1bfc 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -60,7 +60,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { .withField(StandardField.YEAR, "2020") .withField(StandardField.DOI, "10.1109/TII.2019.2935531") .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""), + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF"), fileField))) .withField(StandardField.ISSUE, "4") .withField(StandardField.ISSN, "1941-0050") @@ -81,9 +81,9 @@ void deleteFileInEntryWithMultipleFileLinks() { LinkedFile fileField = new LinkedFile("", fileBefore.toAbsolutePath(), ""); FieldChange expectedChange = new FieldChange(entry, StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""), + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF"), fileField)), - FileFieldWriter.getStringRepresentation(new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "")) + FileFieldWriter.getStringRepresentation(new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF")) ); BibEntry expectedEntry = new BibEntry(StandardEntryType.Article) .withField(StandardField.AUTHOR, "Shatakshi Sharma and Bhim Singh and Sukumar Mishra") @@ -91,7 +91,7 @@ void deleteFileInEntryWithMultipleFileLinks() { .withField(StandardField.YEAR, "2020") .withField(StandardField.DOI, "10.1109/TII.2019.2935531") .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""))) + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF"))) .withField(StandardField.ISSUE, "4") .withField(StandardField.ISSN, "1941-0050") .withField(StandardField.JOURNALTITLE, "IEEE Transactions on Industrial Informatics") @@ -121,7 +121,7 @@ void keepLinksToExistingFiles() { .withField(StandardField.YEAR, "2020") .withField(StandardField.DOI, "10.1109/TII.2019.2935531") .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", ""), + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF"), fileField))) .withField(StandardField.ISSUE, "4") .withField(StandardField.ISSN, "1941-0050") From 60a9100f57667113869bd5f53c082063aa764fdf Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:50:40 +0100 Subject: [PATCH 11/15] Refactor: Removed unused variables in RemoveLinksToNotExistentFiles --- .../logic/cleanup/RemoveLinksToNotExistentFiles.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java b/src/main/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java index 8035aa76dac..1d2c009aa95 100644 --- a/src/main/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java +++ b/src/main/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFiles.java @@ -7,7 +7,6 @@ import java.util.Objects; import java.util.Optional; -import org.jabref.logic.externalfiles.LinkedFileHandler; import org.jabref.model.FieldChange; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -15,13 +14,7 @@ import org.jabref.model.util.OptionalUtil; import org.jabref.preferences.FilePreferences; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - public class RemoveLinksToNotExistentFiles implements CleanupJob { - - private static final Logger LOGGER = LoggerFactory.getLogger(RemoveLinksToNotExistentFiles.class); - private final BibDatabaseContext databaseContext; private final FilePreferences filePreferences; @@ -36,8 +29,6 @@ public List cleanup(BibEntry entry) { List cleanedUpFiles = new ArrayList<>(); boolean changed = false; for (LinkedFile file : files) { - LinkedFileHandler fileHandler = new LinkedFileHandler(file, entry, databaseContext, filePreferences); - if (file.isOnlineLink()) { cleanedUpFiles.add(file); } else { From 1aa7a0b7c985a26ea67c88bfef312bfd753e7900 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 20:41:00 +0100 Subject: [PATCH 12/15] Refactor: Throw IOException in RemoveLinksToNotExistentFilesTest functions --- .../RemoveLinksToNotExistentFilesTest.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index dc0aaec1bfc..7b66a988b2c 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -77,7 +77,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { } @Test - void deleteFileInEntryWithMultipleFileLinks() { + void deleteFileInEntryWithMultipleFileLinks() throws IOException { LinkedFile fileField = new LinkedFile("", fileBefore.toAbsolutePath(), ""); FieldChange expectedChange = new FieldChange(entry, StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( @@ -101,11 +101,7 @@ void deleteFileInEntryWithMultipleFileLinks() { .withField(StandardField.VOLUME, "16") .withField(StandardField.KEYWORDS, "Batteries, Generators, Economics, Power quality, State of charge, Harmonic analysis, Control systems, Battery, diesel generator (DG), distributed generation, power quality, photovoltaic (PV), voltage source converter (VSC)"); - try { - Files.delete(fileBefore); - } catch (IOException e) { - throw new RuntimeException(e); - } + Files.delete(fileBefore); List changes = removeLinks.cleanup(entry); assertEquals(expectedChange, changes.getFirst()); @@ -139,7 +135,7 @@ void keepLinksToExistingFiles() { } @Test - void deleteLinkedFile() { + void deleteLinkedFile() throws IOException { LinkedFile fileField = new LinkedFile("", fileBefore.toAbsolutePath(), ""); // There is only one linked file in entry @@ -161,11 +157,7 @@ void deleteLinkedFile() { .withField(StandardField.VOLUME, "16") .withField(StandardField.KEYWORDS, "Batteries, Generators, Economics, Power quality, State of charge, Harmonic analysis, Control systems, Battery, diesel generator (DG), distributed generation, power quality, photovoltaic (PV), voltage source converter (VSC)"); - try { - Files.delete(fileBefore); - } catch (IOException e) { - throw new RuntimeException(e); - } + Files.delete(fileBefore); List changes = removeLinks.cleanup(entry); assertEquals(expectedChange, changes.getFirst()); From a50126fc4e6cfe6422628ef2e8d686182937d2e6 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 20:50:31 +0100 Subject: [PATCH 13/15] Refactor: Tests assert using List.of() in RemoveLinksToNotExistentFilesTest --- .../logic/cleanup/RemoveLinksToNotExistentFilesTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index 7b66a988b2c..fb789653181 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -104,7 +103,7 @@ void deleteFileInEntryWithMultipleFileLinks() throws IOException { Files.delete(fileBefore); List changes = removeLinks.cleanup(entry); - assertEquals(expectedChange, changes.getFirst()); + assertEquals(List.of(expectedChange), changes); assertEquals(expectedEntry, entry); } @@ -130,7 +129,7 @@ void keepLinksToExistingFiles() { List changes = removeLinks.cleanup(entry); - assertTrue(changes.isEmpty()); + assertEquals(List.of(), changes); assertEquals(expectedEntry, entry); } @@ -160,7 +159,7 @@ void deleteLinkedFile() throws IOException { Files.delete(fileBefore); List changes = removeLinks.cleanup(entry); - assertEquals(expectedChange, changes.getFirst()); + assertEquals(List.of(expectedChange), changes); assertEquals(expectedEntry, entry); } } From 9d1005f8e52a751c50685217f901a1a5d6e0403e Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 21:53:47 +0100 Subject: [PATCH 14/15] Refactor: Corrected Arguments for LinkedFile in RemoveLinksToNotExistentFilesTest --- .../cleanup/RemoveLinksToNotExistentFilesTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index fb789653181..de92a16f845 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -59,7 +59,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { .withField(StandardField.YEAR, "2020") .withField(StandardField.DOI, "10.1109/TII.2019.2935531") .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF"), + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912", "PDF"), fileField))) .withField(StandardField.ISSUE, "4") .withField(StandardField.ISSN, "1941-0050") @@ -80,9 +80,9 @@ void deleteFileInEntryWithMultipleFileLinks() throws IOException { LinkedFile fileField = new LinkedFile("", fileBefore.toAbsolutePath(), ""); FieldChange expectedChange = new FieldChange(entry, StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF"), + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912", "PDF"), fileField)), - FileFieldWriter.getStringRepresentation(new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF")) + FileFieldWriter.getStringRepresentation(new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912", "PDF")) ); BibEntry expectedEntry = new BibEntry(StandardEntryType.Article) .withField(StandardField.AUTHOR, "Shatakshi Sharma and Bhim Singh and Sukumar Mishra") @@ -90,7 +90,7 @@ void deleteFileInEntryWithMultipleFileLinks() throws IOException { .withField(StandardField.YEAR, "2020") .withField(StandardField.DOI, "10.1109/TII.2019.2935531") .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF"))) + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912", "PDF"))) .withField(StandardField.ISSUE, "4") .withField(StandardField.ISSN, "1941-0050") .withField(StandardField.JOURNALTITLE, "IEEE Transactions on Industrial Informatics") @@ -116,7 +116,7 @@ void keepLinksToExistingFiles() { .withField(StandardField.YEAR, "2020") .withField(StandardField.DOI, "10.1109/TII.2019.2935531") .withField(StandardField.FILE, FileFieldWriter.getStringRepresentation(List.of( - new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912:PDF", "PDF"), + new LinkedFile("", "https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8801912", "PDF"), fileField))) .withField(StandardField.ISSUE, "4") .withField(StandardField.ISSN, "1941-0050") From a17208c7147892af18f055bbce541afdf7be4e25 Mon Sep 17 00:00:00 2001 From: karlsb <36365664+karlsb@users.noreply.github.com> Date: Mon, 4 Mar 2024 21:58:26 +0100 Subject: [PATCH 15/15] Refactor: Class fields into local variables This refactor fixed 3 warnings. --- .../cleanup/RemoveLinksToNotExistentFilesTest.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java index de92a16f845..cd02a2534cd 100644 --- a/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java +++ b/src/test/java/org/jabref/logic/cleanup/RemoveLinksToNotExistentFilesTest.java @@ -25,17 +25,14 @@ import static org.mockito.Mockito.when; public class RemoveLinksToNotExistentFilesTest { - private Path newFileFolder; private Path fileBefore; private BibEntry entry; - private FilePreferences filePreferences; - private BibDatabaseContext databaseContext; private RemoveLinksToNotExistentFiles removeLinks; @BeforeEach void setUp(@TempDir Path bibFolder) throws IOException { // The folder where the files should be moved to - newFileFolder = bibFolder.resolve("pdf"); + Path newFileFolder = bibFolder.resolve("pdf"); Files.createDirectory(newFileFolder); Path originalFileFolder = bibFolder.resolve("files"); @@ -46,7 +43,8 @@ void setUp(@TempDir Path bibFolder) throws IOException { MetaData metaData = new MetaData(); metaData.setDefaultFileDirectory(newFileFolder.toAbsolutePath().toString()); - databaseContext = new BibDatabaseContext(new BibDatabase(), metaData); + + BibDatabaseContext databaseContext = new BibDatabaseContext(new BibDatabase(), metaData); Files.createFile(testBibFolder); databaseContext.setDatabasePath(testBibFolder); @@ -70,7 +68,7 @@ void setUp(@TempDir Path bibFolder) throws IOException { .withField(StandardField.VOLUME, "16") .withField(StandardField.KEYWORDS, "Batteries, Generators, Economics, Power quality, State of charge, Harmonic analysis, Control systems, Battery, diesel generator (DG), distributed generation, power quality, photovoltaic (PV), voltage source converter (VSC)"); - filePreferences = mock(FilePreferences.class); + FilePreferences filePreferences = mock(FilePreferences.class); when(filePreferences.shouldStoreFilesRelativeToBibFile()).thenReturn(false); removeLinks = new RemoveLinksToNotExistentFiles(databaseContext, filePreferences); }