Skip to content

Commit

Permalink
Sort unique library suffix by length (#9568)
Browse files Browse the repository at this point in the history
Sort unique library suffix by length

Fixes #9567
Fixes JabRef/jabref-issue-melting-pot#70
  • Loading branch information
Siedlerchr authored Jan 18, 2023
1 parent 2021de9 commit 5879c27
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
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();
}

/**
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

0 comments on commit 5879c27

Please sign in to comment.