Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort unique library suffix by length #9568

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)



Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,7 +146,8 @@ public static Optional<String> getUniquePathDirectory(List<String> paths, Path c
public static Optional<String> getUniquePathFragment(List<String> paths, Path comparePath) {
return uniquePathSubstrings(paths).stream()
.filter(part -> comparePath.toString().contains(part))
.findFirst();
.sorted(Comparator.comparingInt(String::length).reversed())
.findFirst();
Siedlerchr marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
31 changes: 22 additions & 9 deletions src/test/java/org/jabref/logic/util/io/FileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> paths = Arrays.asList(pathArr);
List<String> uniqPath = Arrays.asList(uniqArr);
List<String> paths = List.of("C:/uniquefile.bib",
"C:/downloads/filename.bib",
"C:/mypaper/bib/filename.bib",
"C:/external/mypaper/bib/filename.bib",
"");
List<String> uniqPath = List.of("uniquefile.bib",
"downloads/filename.bib",
"C:/mypaper/bib/filename.bib",
"external/mypaper/bib/filename.bib",
"");

List<String> result = FileUtil.uniquePathSubstrings(paths);
assertEquals(uniqPath, result);
}

@Test
void testUniquePathFragmentWithSameSuffix() {
List<String> 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<String> 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));
Expand Down