From 5879c2730ccdfb0607e8d21d909eca64540369ad Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 18 Jan 2023 20:33:48 +0100 Subject: [PATCH] Sort unique library suffix by length (#9568) Sort unique library suffix by length Fixes #9567 Fixes https://github.com/JabRef/jabref-issue-melting-pot/issues/70 --- CHANGELOG.md | 2 +- .../org/jabref/logic/util/io/FileUtil.java | 4 ++- .../jabref/logic/util/io/FileUtilTest.java | 31 +++++++++++++------ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e4f2fa7f7b..ece2df2ae0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve ### Fixed - +- We fixed an issue where the "Import" -> "Library to import to" did not show the correct library name if two opened libraries had the same suffix [#9567](https://github.com/JabRef/jabref/issues/9567) diff --git a/src/main/java/org/jabref/logic/util/io/FileUtil.java b/src/main/java/org/jabref/logic/util/io/FileUtil.java index 24f21b3e02a..d670db1dd54 100644 --- a/src/main/java/org/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/org/jabref/logic/util/io/FileUtil.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Locale; import java.util.Objects; @@ -145,7 +146,8 @@ public static Optional getUniquePathDirectory(List paths, Path c public static Optional getUniquePathFragment(List paths, Path comparePath) { return uniquePathSubstrings(paths).stream() .filter(part -> comparePath.toString().contains(part)) - .findFirst(); + .sorted(Comparator.comparingInt(String::length).reversed()) + .findFirst(); } /** diff --git a/src/test/java/org/jabref/logic/util/io/FileUtilTest.java b/src/test/java/org/jabref/logic/util/io/FileUtilTest.java index a761b10e3e6..658c28fcfba 100644 --- a/src/test/java/org/jabref/logic/util/io/FileUtilTest.java +++ b/src/test/java/org/jabref/logic/util/io/FileUtilTest.java @@ -5,7 +5,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; -import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -203,19 +202,33 @@ void getFileNameWithMultipleDotsString() { @Test void uniquePathSubstrings() { - String[] pathArr = {Path.of("C:/uniquefile.bib").toString(), - Path.of("C:/downloads/filename.bib").toString(), Path.of("C:/mypaper/bib/filename.bib").toString(), - Path.of("C:/external/mypaper/bib/filename.bib").toString(), ""}; - String[] uniqArr = {Path.of("uniquefile.bib").toString(), Path.of("downloads/filename.bib").toString(), - Path.of("C:/mypaper/bib/filename.bib").toString(), - Path.of("external/mypaper/bib/filename.bib").toString(), ""}; - List paths = Arrays.asList(pathArr); - List uniqPath = Arrays.asList(uniqArr); + List paths = List.of("C:/uniquefile.bib", + "C:/downloads/filename.bib", + "C:/mypaper/bib/filename.bib", + "C:/external/mypaper/bib/filename.bib", + ""); + List uniqPath = List.of("uniquefile.bib", + "downloads/filename.bib", + "C:/mypaper/bib/filename.bib", + "external/mypaper/bib/filename.bib", + ""); List result = FileUtil.uniquePathSubstrings(paths); assertEquals(uniqPath, result); } + @Test + void testUniquePathFragmentWithSameSuffix() { + List dirs = List.of("/users/jabref/bibliography.bib", "/users/jabref/koppor-bibliograsphy.bib"); + assertEquals(Optional.of("bibliography.bib"), FileUtil.getUniquePathFragment(dirs, Path.of("/users/jabref/bibliography.bib"))); + } + + @Test + void testUniquePathFragmentWithSameSuffixAndLongerName() { + List dirs = List.of("/users/jabref/bibliography.bib", "/users/jabref/koppor-bibliography.bib"); + assertEquals(Optional.of("koppor-bibliography.bib"), FileUtil.getUniquePathFragment(dirs, Path.of("/users/jabref/koppor-bibliography.bib"))); + } + @Test void testCopyFileFromEmptySourcePathToEmptyDestinationPathWithOverrideExistFile() { assertFalse(FileUtil.copyFile(nonExistingTestPath, nonExistingTestPath, true));