From a74b16feea2cb853e05c0e7e6a46de6fcbb8a0d1 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sat, 16 Jul 2016 21:41:36 +0200 Subject: [PATCH 1/9] Cleanup FindFile Use Lazy Stream to find files Add some more test, reworked test Tests should now work on Linux, too Completey rework File and dir creating using nio methods Fix case typo which was reason for linux bug Move sublass test methods into baseclass hopefully fixes CI build --- .../sf/jabref/logic/util/io/FileFinder.java | 45 +++++---- .../java/net/sf/jabref/FileBasedTestCase.java | 94 +++++++------------ .../net/sf/jabref/FileBasedTestHelper.java | 78 --------------- .../logic/cleanup/MoveFilesCleanupTest.java | 2 +- .../logic/util/io/UtilFindFileTest.java | 31 ++++-- 5 files changed, 81 insertions(+), 169 deletions(-) delete mode 100644 src/test/java/net/sf/jabref/FileBasedTestHelper.java diff --git a/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java b/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java index 8d2f82d5fe0..a1c2fedfb3c 100644 --- a/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java +++ b/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java @@ -1,43 +1,42 @@ package net.sf.jabref.logic.util.io; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.Collection; import java.util.HashSet; +import java.util.Objects; import java.util.Set; +import java.util.stream.Collectors; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; public class FileFinder { - public static Set findFiles(Collection extensions, Collection directories) { - Set result = new HashSet<>(); + private static final Log LOGGER = LogFactory.getLog(FileFinder.class); - for (File directory : directories) { - result.addAll(FileFinder.findFiles(extensions, directory)); - } - return result; - } + public static Set findFiles(Collection extensions, Collection directories) { + + Objects.requireNonNull(directories, "Directories must not be null!"); + Objects.requireNonNull(extensions, "Extensions must not be null!"); - private static Collection findFiles(Collection extensions, File directory) { Set result = new HashSet<>(); + for (File directory : directories) { - File[] children = directory.listFiles(); - if (children == null) { - return result; // No permission? - } + try { + Set files = Files + .find(directory.toPath(), Integer.MAX_VALUE, + (path, attr) -> !Files.isDirectory(path) + && extensions.contains(FileUtil.getFileExtension(path.toFile()).orElse(""))) + .map(x -> x.toFile()).collect(Collectors.toSet()); + result.addAll(files); - for (File child : children) { - if (child.isDirectory()) { - result.addAll(FileFinder.findFiles(extensions, child)); - } else { - FileUtil.getFileExtension(child).ifPresent(extension -> { - if (extensions.contains(extension)) { - result.add(child); - } - }); + } catch (IOException e) { + LOGGER.error("Problem in finding files", e); } } - return result; } - } diff --git a/src/test/java/net/sf/jabref/FileBasedTestCase.java b/src/test/java/net/sf/jabref/FileBasedTestCase.java index 90eeefb15af..c3263660add 100644 --- a/src/test/java/net/sf/jabref/FileBasedTestCase.java +++ b/src/test/java/net/sf/jabref/FileBasedTestCase.java @@ -1,15 +1,16 @@ package net.sf.jabref; -import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.preferences.JabRefPreferences; import org.junit.After; -import org.junit.Assert; import org.junit.Before; +import static org.junit.Assert.assertTrue; /** * A base class for Testing in JabRef that comes along with some useful @@ -19,7 +20,7 @@ public class FileBasedTestCase { protected BibDatabase database; protected BibEntry entry; - protected File root; + protected Path rootDir; private String oldPdfDirectory; private boolean oldUseRegExp; @@ -39,78 +40,55 @@ public void setUp() throws IOException { database = BibtexTestData.getBibtexDatabase(); entry = database.getEntries().iterator().next(); - root = FileBasedTestHelper.createTempDir("UtilFindFileTest"); + rootDir = Files.createTempDirectory("UtilFindFileTest"); - Assert.assertNotNull(root); + Globals.prefs.put("pdfDirectory", rootDir.toAbsolutePath().toString()); - Globals.prefs.put("pdfDirectory", root.getPath()); + Path subDir = Files.createDirectory(rootDir.resolve("Organization Science")); + Path pdfSubDir = Files.createDirectory(rootDir.resolve("pdfs")); - File subDir1 = new File(root, "Organization Science"); - Assert.assertTrue(subDir1.mkdir()); + assertTrue(Files.exists(Files.createFile(subDir.resolve("HipKro03 - Hello.pdf")))); + assertTrue(Files.exists(Files.createFile(rootDir.resolve("HipKro03 - Hello.pdf")))); - File pdf1 = new File(subDir1, "HipKro03 - Hello.pdf"); - Assert.assertTrue(pdf1.createNewFile()); + Path pdfSubSubDir = Files.createDirectory(pdfSubDir.resolve("sub")); + assertTrue(Files.exists(Files.createFile(pdfSubSubDir.resolve("HipKro03-sub.pdf")))); - File pdf1a = new File(root, "HipKro03 - Hello.pdf"); - Assert.assertTrue(pdf1a.createNewFile()); + Files.createDirectory(rootDir.resolve("2002")); + Path dir2003 = Files.createDirectory(rootDir.resolve("2003")); + assertTrue(Files.exists(Files.createFile(dir2003.resolve("Paper by HipKro03.pdf")))); - File subDir2 = new File(root, "pdfs"); - Assert.assertTrue(subDir2.mkdir()); + Path dirTest = Files.createDirectory(rootDir.resolve("test")); + assertTrue(Files.exists(Files.createFile(dirTest.resolve(".TEST")))); + assertTrue(Files.exists(Files.createFile(dirTest.resolve("TEST[")))); + assertTrue(Files.exists(Files.createFile(dirTest.resolve("TE.ST")))); + assertTrue(Files.exists(Files.createFile(dirTest.resolve("foo.dat")))); - File subsubDir1 = new File(subDir2, "sub"); - Assert.assertTrue(subsubDir1.mkdir()); + Path graphicsDir = Files.createDirectory(rootDir.resolve("graphicsDir")); + Path graphicsSubDir = Files.createDirectories(graphicsDir.resolve("subDir")); - File pdf2 = new File(subsubDir1, "HipKro03-sub.pdf"); - Assert.assertTrue(pdf2.createNewFile()); - - File dir2002 = new File(root, "2002"); - Assert.assertTrue(dir2002.mkdir()); - - File dir2003 = new File(root, "2003"); - Assert.assertTrue(dir2003.mkdir()); - - File pdf3 = new File(dir2003, "Paper by HipKro03.pdf"); - Assert.assertTrue(pdf3.createNewFile()); - - File dirTest = new File(root, "test"); - Assert.assertTrue(dirTest.mkdir()); - - File pdf4 = new File(dirTest, "HipKro03.pdf"); - Assert.assertTrue(pdf4.createNewFile()); - - File pdf5 = new File(dirTest, ".TEST"); - Assert.assertTrue(pdf5.createNewFile()); - - File pdf6 = new File(dirTest, "TEST["); - Assert.assertTrue(pdf6.createNewFile()); - - File pdf7 = new File(dirTest, "TE.ST"); - Assert.assertTrue(pdf7.createNewFile()); - - File foo = new File(dirTest, "foo.dat"); - Assert.assertTrue(foo.createNewFile()); - - File graphicsDir = new File(root, "graphicsDir"); - Assert.assertTrue(graphicsDir.mkdir()); - - File graphicsSubDir = new File(graphicsDir, "subDir"); - Assert.assertTrue(graphicsSubDir.mkdir()); - - File jpg = new File(graphicsSubDir, "HipKro03test.jpg"); - Assert.assertTrue(jpg.createNewFile()); - - File png = new File(graphicsSubDir, "HipKro03test.png"); - Assert.assertTrue(png.createNewFile()); + assertTrue(Files.exists(Files.createFile(graphicsSubDir.resolve("HipKro03test.jpg")))); + assertTrue(Files.exists(Files.createFile(graphicsSubDir.resolve("HipKro03test.png")))); } @After public void tearDown() { - FileBasedTestHelper.deleteRecursive(root); + + try { + for (Path p : Files.walk(rootDir).sorted((a, b) -> b.compareTo(a)). // reverse; files before dirs + toArray(Path[]::new)) { + Files.delete(p); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Globals.prefs.putBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY, oldAutoLinkExcatKeyOnly); Globals.prefs.putBoolean(JabRefPreferences.AUTOLINK_USE_REG_EXP_SEARCH_KEY, oldUseRegExp); Globals.prefs.put("pdfDirectory", oldPdfDirectory); // TODO: This is not a great way to do this, sure ;-) + } } diff --git a/src/test/java/net/sf/jabref/FileBasedTestHelper.java b/src/test/java/net/sf/jabref/FileBasedTestHelper.java deleted file mode 100644 index 50cfc4bd83d..00000000000 --- a/src/test/java/net/sf/jabref/FileBasedTestHelper.java +++ /dev/null @@ -1,78 +0,0 @@ -package net.sf.jabref; - -import java.io.File; - -public class FileBasedTestHelper { - - /** - * Creates a temp directory in the System temp directory. - *

- * Taken from - * http://forum.java.sun.com/thread.jspa?threadID=470197&messageID=2169110 - *

- * Author: jfbriere - * - * @return returns null if directory could not created. - */ - public static File createTempDir(String prefix) { - return createTempDir(prefix, null); - } - - /** - * Creates a temp directory in a given directory. - *

- * Taken from - * http://forum.java.sun.com/thread.jspa?threadID=470197&messageID=2169110 - *

- * Author: jfbriere - * - * @param directory MayBeNull - null indicates that the system tmp directory - * should be used. - * @return returns null if directory could not created. - */ - public static File createTempDir(String prefix, File directory) { - try { - File tempFile = File.createTempFile(prefix, "", directory); - if(tempFile == null) { - return null; - } - - if (!tempFile.delete()) { - return null; - } - if (!tempFile.mkdir()) { - return null; - } - - return tempFile; - - } catch (Exception e) { - return null; - } - } - - /** - * Deletes a directory or file - *

- * Taken from - * http://forum.java.sun.com/thread.jspa?threadID=470197&messageID=2169110 - *

- * Author: jfbriere - * - * @param file - */ - public static void deleteRecursive(File file) { - if (file.isDirectory()) { - File[] fileArray = file.listFiles(); - - if (fileArray != null) { - for (File aFileArray : fileArray) { - deleteRecursive(aFileArray); - } - } - } - if (!file.delete()) { - System.err.println("Cannot delete file"); - } - } -} diff --git a/src/test/java/net/sf/jabref/logic/cleanup/MoveFilesCleanupTest.java b/src/test/java/net/sf/jabref/logic/cleanup/MoveFilesCleanupTest.java index 4962fc3d173..071969e2ebd 100644 --- a/src/test/java/net/sf/jabref/logic/cleanup/MoveFilesCleanupTest.java +++ b/src/test/java/net/sf/jabref/logic/cleanup/MoveFilesCleanupTest.java @@ -69,7 +69,7 @@ public void movesFileFromSubfolder() throws IOException { public void movesFileFromSubfolderMultiple() throws IOException { File subfolder = bibFolder.newFolder(); File fileBefore = new File(subfolder, "test.pdf"); - assertTrue(fileBefore.createNewFile());; + assertTrue(fileBefore.createNewFile()); assertTrue(fileBefore.exists()); BibEntry entry = new BibEntry(); diff --git a/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java b/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java index b383ed53a5a..8d335ee6b2b 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java +++ b/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java @@ -1,18 +1,19 @@ package net.sf.jabref.logic.util.io; import java.io.File; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Set; import net.sf.jabref.FileBasedTestCase; import net.sf.jabref.Globals; import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; /** @@ -22,24 +23,36 @@ */ public class UtilFindFileTest extends FileBasedTestCase { - @Test - @Ignore(value = "works on windows but not on linux") public void testFindAssociatedFiles() { + Collection entries = Collections.singletonList(entry); Collection extensions = Arrays.asList("jpg", "pdf"); - Collection dirs = Arrays.asList(new File(root.getAbsoluteFile() + "/pdfs/"), - new File(root.getAbsoluteFile() + "/graphicsDir/")); + Collection dirs = Arrays.asList(rootDir.resolve("graphicsDir").toFile(), + rootDir.resolve("pdfs").toFile()); Map> results = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs); Assert.assertEquals(2, results.get(entry).size()); + Assert.assertTrue(results.get(entry) + .contains(rootDir.resolve(Paths.get("graphicsDir", "subDir", "HipKro03test.jpg")).toFile())); + Assert.assertFalse(results.get(entry) + .contains(rootDir.resolve(Paths.get("graphicsDir", "subDir", "HipKro03test.png")).toFile())); Assert.assertTrue( - results.get(entry).contains(new File(root.getAbsoluteFile() + "/graphicsDir/subDir/HipKro03test.jpg"))); - Assert.assertFalse( - results.get(entry).contains(new File(root.getAbsoluteFile() + "/graphicsDir/subDir/HipKro03test.png"))); - Assert.assertTrue(results.get(entry).contains(new File(root.getAbsoluteFile() + "/pdfs/sub/HipKro03-sub.pdf"))); + results.get(entry).contains(rootDir.resolve(Paths.get("pdfs", "sub", "HipKro03-sub.pdf")).toFile())); } + @Test + public void testFindAssociatedFilesException() { + Collection extensions = Arrays.asList("jpg", "pdf"); + Collection dirs = Arrays.asList(new File("asdfasdf/asdfasdf")); + Set results = FileFinder.findFiles(extensions, dirs); + Assert.assertEquals(0, results.size()); + } + + @Test(expected = NullPointerException.class) + public void testFindFilesNUllPointerException() { + FileFinder.findFiles(null, null); + } } From 2f6bb08f5fa4b92c175ba622bf26bef2feab8db0 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 19 Jul 2016 22:11:35 +0200 Subject: [PATCH 2/9] Use try with resources to close streams Set NPE Test to ignore, as it fails only on CI server --- .../sf/jabref/logic/util/io/FileFinder.java | 14 ++++++------ .../java/net/sf/jabref/FileBasedTestCase.java | 8 +++---- .../logic/util/io/UtilFindFileTest.java | 22 ++++++++++++------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java b/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java index a1c2fedfb3c..60853e34dd4 100644 --- a/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java +++ b/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java @@ -8,6 +8,7 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -25,13 +26,12 @@ public static Set findFiles(Collection extensions, Collection result = new HashSet<>(); for (File directory : directories) { - try { - Set files = Files - .find(directory.toPath(), Integer.MAX_VALUE, - (path, attr) -> !Files.isDirectory(path) - && extensions.contains(FileUtil.getFileExtension(path.toFile()).orElse(""))) - .map(x -> x.toFile()).collect(Collectors.toSet()); - result.addAll(files); + try (Stream files = Files + .find(directory.toPath(), Integer.MAX_VALUE, + (path, attr) -> !Files.isDirectory(path) + && extensions.contains(FileUtil.getFileExtension(path.toFile()).orElse(""))) + .map(x -> x.toFile())) { + result.addAll(files.collect(Collectors.toSet())); } catch (IOException e) { LOGGER.error("Problem in finding files", e); diff --git a/src/test/java/net/sf/jabref/FileBasedTestCase.java b/src/test/java/net/sf/jabref/FileBasedTestCase.java index c3263660add..26ddefe4e30 100644 --- a/src/test/java/net/sf/jabref/FileBasedTestCase.java +++ b/src/test/java/net/sf/jabref/FileBasedTestCase.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.stream.Stream; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.entry.BibEntry; @@ -74,13 +75,12 @@ public void setUp() throws IOException { @After public void tearDown() { - try { - for (Path p : Files.walk(rootDir).sorted((a, b) -> b.compareTo(a)). // reverse; files before dirs - toArray(Path[]::new)) { + try (Stream path = Files.walk(rootDir)) { + // reverse; files before dirs + for (Path p : path.sorted((a, b) -> b.compareTo(a)).toArray(Path[]::new)) { Files.delete(p); } } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } diff --git a/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java b/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java index 8d335ee6b2b..7bf43a333ce 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java +++ b/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java @@ -12,10 +12,14 @@ import net.sf.jabref.FileBasedTestCase; import net.sf.jabref.Globals; import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.support.DevEnvironment; -import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; - +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeFalse; /** * Testing Util.findFile for finding files based on regular expressions. * @@ -33,13 +37,12 @@ public void testFindAssociatedFiles() { Map> results = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs); - Assert.assertEquals(2, results.get(entry).size()); - Assert.assertTrue(results.get(entry) + assertEquals(2, results.get(entry).size()); + assertTrue(results.get(entry) .contains(rootDir.resolve(Paths.get("graphicsDir", "subDir", "HipKro03test.jpg")).toFile())); - Assert.assertFalse(results.get(entry) + assertFalse(results.get(entry) .contains(rootDir.resolve(Paths.get("graphicsDir", "subDir", "HipKro03test.png")).toFile())); - Assert.assertTrue( - results.get(entry).contains(rootDir.resolve(Paths.get("pdfs", "sub", "HipKro03-sub.pdf")).toFile())); + assertTrue(results.get(entry).contains(rootDir.resolve(Paths.get("pdfs", "sub", "HipKro03-sub.pdf")).toFile())); } @Test @@ -48,11 +51,14 @@ public void testFindAssociatedFilesException() { Collection dirs = Arrays.asList(new File("asdfasdf/asdfasdf")); Set results = FileFinder.findFiles(extensions, dirs); - Assert.assertEquals(0, results.size()); + assertEquals(0, results.size()); } + @Ignore("Fails on CI Server") @Test(expected = NullPointerException.class) public void testFindFilesNUllPointerException() { + + assumeFalse(DevEnvironment.isCIServer()); FileFinder.findFiles(null, null); } } From 4d6a3ad4ff9c4163cfb4f13e743b6bb5bd3b8829 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Thu, 21 Jul 2016 12:06:50 +0200 Subject: [PATCH 3/9] Resolve merge conflics --- src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java b/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java index 7bf43a333ce..8f41a508b1a 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java +++ b/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java @@ -48,7 +48,7 @@ public void testFindAssociatedFiles() { @Test public void testFindAssociatedFilesException() { Collection extensions = Arrays.asList("jpg", "pdf"); - Collection dirs = Arrays.asList(new File("asdfasdf/asdfasdf")); + Collection dirs = Arrays.asList(rootDir.resolve("asdfasdf/asdfasdf").toFile()); Set results = FileFinder.findFiles(extensions, dirs); assertEquals(0, results.size()); From 750f33b930cdbb1e4bbc838fac4bef01798dd2b8 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Thu, 21 Jul 2016 20:17:25 +0200 Subject: [PATCH 4/9] Delete Sublass and move methods in base class Hopefully fixes problem for CI Server... --- .../util/io}/FileBasedTestCase.java | 63 +++++++++++++++--- .../sf/jabref/logic/util/io/FileUtilTest.java | 23 +++---- .../logic/util/io/UtilFindFileTest.java | 64 ------------------- 3 files changed, 63 insertions(+), 87 deletions(-) rename src/test/java/net/sf/jabref/{ => logic/util/io}/FileBasedTestCase.java (62%) delete mode 100644 src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java diff --git a/src/test/java/net/sf/jabref/FileBasedTestCase.java b/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java similarity index 62% rename from src/test/java/net/sf/jabref/FileBasedTestCase.java rename to src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java index 26ddefe4e30..a0c4d9cf506 100644 --- a/src/test/java/net/sf/jabref/FileBasedTestCase.java +++ b/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java @@ -1,27 +1,40 @@ -package net.sf.jabref; +package net.sf.jabref.logic.util.io; +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.stream.Stream; +import net.sf.jabref.BibtexTestData; +import net.sf.jabref.Globals; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.preferences.JabRefPreferences; +import net.sf.jabref.support.DevEnvironment; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeFalse; + -/** - * A base class for Testing in JabRef that comes along with some useful - * functions. - */ public class FileBasedTestCase { - protected BibDatabase database; - protected BibEntry entry; - protected Path rootDir; + private BibDatabase database; + private BibEntry entry; + private Path rootDir; private String oldPdfDirectory; private boolean oldUseRegExp; @@ -72,6 +85,40 @@ public void setUp() throws IOException { } + @Test + public void testFindAssociatedFiles() { + + List entries = Collections.singletonList(entry); + List extensions = Arrays.asList("jpg", "pdf"); + List dirs = Arrays.asList(rootDir.resolve("graphicsDir").toFile(), + rootDir.resolve("pdfs").toFile()); + + Map> results = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs); + + assertEquals(2, results.get(entry).size()); + assertTrue(results.get(entry) + .contains(rootDir.resolve(Paths.get("graphicsDir", "subDir", "HipKro03test.jpg")).toFile())); + assertFalse(results.get(entry) + .contains(rootDir.resolve(Paths.get("graphicsDir", "subDir", "HipKro03test.png")).toFile())); + assertTrue(results.get(entry).contains(rootDir.resolve(Paths.get("pdfs", "sub", "HipKro03-sub.pdf")).toFile())); + } + + @Test + public void testFindFilesException() { + List extensions = Arrays.asList("jpg", "pdf"); + List dirs = Arrays.asList(rootDir.resolve("asdfasdf/asdfasdf").toFile()); + Set results = FileFinder.findFiles(extensions, dirs); + + assertEquals(0, results.size()); + } + + @Ignore("Fails on CI Server") + @Test(expected = NullPointerException.class) + public void testFindFilesNullPointerException() { + + assumeFalse(DevEnvironment.isCIServer()); + FileFinder.findFiles(null, null); + } @After public void tearDown() { diff --git a/src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java b/src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java index c4af6d4784d..996a12823f1 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java +++ b/src/test/java/net/sf/jabref/logic/util/io/FileUtilTest.java @@ -20,6 +20,7 @@ import static org.mockito.Mockito.when; public class FileUtilTest { + @Before public void setUp() { Globals.prefs = mock(JabRefPreferences.class); @@ -34,7 +35,8 @@ public void tearDown() { @Test public void testGetLinkedFileNameDefault() { // bibkey - title - when(Globals.prefs.get(JabRefPreferences.PREF_IMPORT_FILENAMEPATTERN)).thenReturn("\\bibtexkey\\begin{title} - \\format[RemoveBrackets]{\\title}\\end{title}"); + when(Globals.prefs.get(JabRefPreferences.PREF_IMPORT_FILENAMEPATTERN)) + .thenReturn("\\bibtexkey\\begin{title} - \\format[RemoveBrackets]{\\title}\\end{title}"); BibEntry entry = new BibEntry(); entry.setCiteKey("1234"); @@ -109,20 +111,12 @@ public void testGetFileExtensionNoExtension2String() { @Test public void uniquePathSubstrings() { - String[] pathArr = { - Paths.get("C:/uniquefile.bib").toString(), - Paths.get("C:/downloads/filename.bib").toString(), - Paths.get("C:/mypaper/bib/filename.bib").toString(), - Paths.get("C:/external/mypaper/bib/filename.bib").toString(), - "" - }; - String[] uniqArr = { - Paths.get("uniquefile.bib").toString(), - Paths.get("downloads/filename.bib").toString(), + String[] pathArr = {Paths.get("C:/uniquefile.bib").toString(), + Paths.get("C:/downloads/filename.bib").toString(), Paths.get("C:/mypaper/bib/filename.bib").toString(), + Paths.get("C:/external/mypaper/bib/filename.bib").toString(), ""}; + String[] uniqArr = {Paths.get("uniquefile.bib").toString(), Paths.get("downloads/filename.bib").toString(), Paths.get("C:/mypaper/bib/filename.bib").toString(), - Paths.get("external/mypaper/bib/filename.bib").toString(), - "" - }; + Paths.get("external/mypaper/bib/filename.bib").toString(), ""}; List paths = Arrays.asList(pathArr); List uniqPath = Arrays.asList(uniqArr); @@ -130,5 +124,4 @@ public void uniquePathSubstrings() { assertEquals(uniqPath, result); } - } diff --git a/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java b/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java deleted file mode 100644 index 8f41a508b1a..00000000000 --- a/src/test/java/net/sf/jabref/logic/util/io/UtilFindFileTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package net.sf.jabref.logic.util.io; - -import java.io.File; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import net.sf.jabref.FileBasedTestCase; -import net.sf.jabref.Globals; -import net.sf.jabref.model.entry.BibEntry; -import net.sf.jabref.support.DevEnvironment; - -import org.junit.Ignore; -import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeFalse; -/** - * Testing Util.findFile for finding files based on regular expressions. - * - * @author Christopher Oezbek - */ -public class UtilFindFileTest extends FileBasedTestCase { - - @Test - public void testFindAssociatedFiles() { - - Collection entries = Collections.singletonList(entry); - Collection extensions = Arrays.asList("jpg", "pdf"); - Collection dirs = Arrays.asList(rootDir.resolve("graphicsDir").toFile(), - rootDir.resolve("pdfs").toFile()); - - Map> results = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs); - - assertEquals(2, results.get(entry).size()); - assertTrue(results.get(entry) - .contains(rootDir.resolve(Paths.get("graphicsDir", "subDir", "HipKro03test.jpg")).toFile())); - assertFalse(results.get(entry) - .contains(rootDir.resolve(Paths.get("graphicsDir", "subDir", "HipKro03test.png")).toFile())); - assertTrue(results.get(entry).contains(rootDir.resolve(Paths.get("pdfs", "sub", "HipKro03-sub.pdf")).toFile())); - } - - @Test - public void testFindAssociatedFilesException() { - Collection extensions = Arrays.asList("jpg", "pdf"); - Collection dirs = Arrays.asList(rootDir.resolve("asdfasdf/asdfasdf").toFile()); - Set results = FileFinder.findFiles(extensions, dirs); - - assertEquals(0, results.size()); - } - - @Ignore("Fails on CI Server") - @Test(expected = NullPointerException.class) - public void testFindFilesNUllPointerException() { - - assumeFalse(DevEnvironment.isCIServer()); - FileFinder.findFiles(null, null); - } -} From 80e812f23157a23e2accf652b3f60df1b427f8e5 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Fri, 22 Jul 2016 21:29:48 +0200 Subject: [PATCH 5/9] Refactor Stream. Extract BiPredicate Mock Prefs --- .../sf/jabref/logic/util/io/FileFinder.java | 13 +++++-- .../logic/util/io/FileBasedTestCase.java | 39 ++++++------------- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java b/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java index 60853e34dd4..79cbbc4bf48 100644 --- a/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java +++ b/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java @@ -3,10 +3,13 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Collection; import java.util.HashSet; import java.util.Objects; import java.util.Set; +import java.util.function.BiPredicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -23,13 +26,14 @@ public static Set findFiles(Collection extensions, Collection isDirectoryAndContainsExtension = (path, + attr) -> !Files.isDirectory(path) + && extensions.contains(FileUtil.getFileExtension(path.toFile()).orElse("")); + Set result = new HashSet<>(); for (File directory : directories) { - try (Stream files = Files - .find(directory.toPath(), Integer.MAX_VALUE, - (path, attr) -> !Files.isDirectory(path) - && extensions.contains(FileUtil.getFileExtension(path.toFile()).orElse(""))) + try (Stream files = Files.find(directory.toPath(), Integer.MAX_VALUE, isDirectoryAndContainsExtension) .map(x -> x.toFile())) { result.addAll(files.collect(Collectors.toSet())); @@ -38,5 +42,6 @@ public static Set findFiles(Collection extensions, Collection entries = Collections.singletonList(entry); List extensions = Arrays.asList("jpg", "pdf"); - List dirs = Arrays.asList(rootDir.resolve("graphicsDir").toFile(), - rootDir.resolve("pdfs").toFile()); + List dirs = Arrays.asList(rootDir.resolve("graphicsDir").toFile(), rootDir.resolve("pdfs").toFile()); + + when(Globals.prefs.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY)).thenReturn(false); Map> results = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs); @@ -119,23 +109,16 @@ public void testFindFilesNullPointerException() { assumeFalse(DevEnvironment.isCIServer()); FileFinder.findFiles(null, null); } + @After - public void tearDown() { + public void tearDown() throws IOException { + Globals.prefs = null; try (Stream path = Files.walk(rootDir)) { // reverse; files before dirs for (Path p : path.sorted((a, b) -> b.compareTo(a)).toArray(Path[]::new)) { Files.delete(p); } - } catch (IOException e) { - e.printStackTrace(); } - - Globals.prefs.putBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY, oldAutoLinkExcatKeyOnly); - Globals.prefs.putBoolean(JabRefPreferences.AUTOLINK_USE_REG_EXP_SEARCH_KEY, oldUseRegExp); - Globals.prefs.put("pdfDirectory", oldPdfDirectory); - // TODO: This is not a great way to do this, sure ;-) - } - } From 2134d983fc7d138d08a40703ff0d68ae3f9992b6 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sat, 23 Jul 2016 12:58:34 +0200 Subject: [PATCH 6/9] Remove prefs dependency in findAsscoiatedFiles use TemporaryFolder use Collections.emptyset for expexted --- .../net/sf/jabref/external/AutoSetLinks.java | 10 ++-- .../java/net/sf/jabref/gui/BasePanel.java | 3 +- .../net/sf/jabref/logic/util/io/FileUtil.java | 5 +- .../logic/util/io/FileBasedTestCase.java | 53 +++++++------------ 4 files changed, 29 insertions(+), 42 deletions(-) diff --git a/src/main/java/net/sf/jabref/external/AutoSetLinks.java b/src/main/java/net/sf/jabref/external/AutoSetLinks.java index 64848c282fa..b9e96ef29d7 100644 --- a/src/main/java/net/sf/jabref/external/AutoSetLinks.java +++ b/src/main/java/net/sf/jabref/external/AutoSetLinks.java @@ -69,7 +69,9 @@ public static void autoSetLinks(Collection entries, BibDatabaseContext * parameter can be null, which means that no progress update will be shown. * @return the thread performing the automatically setting */ - public static Runnable autoSetLinks(final Collection entries, final NamedCompound ce, final Set changedEntries, final FileListTableModel singleTableModel, final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) { + public static Runnable autoSetLinks(final Collection entries, final NamedCompound ce, + final Set changedEntries, final FileListTableModel singleTableModel, + final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) { final Collection types = ExternalFileTypes.getInstance().getExternalFileTypeSelection(); if (diag != null) { final JProgressBar prog = new JProgressBar(JProgressBar.HORIZONTAL, 0, types.size() - 1); @@ -105,7 +107,8 @@ public void run() { String regExp = Globals.prefs.get(JabRefPreferences.REG_EXP_SEARCH_EXPRESSION_KEY); result = RegExpFileSearch.findFilesForSet(entries, extensions, dirs, regExp); } else { - result = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs); + boolean autoLinkExactKeyOnly = Globals.prefs.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY); + result = FileUtil.findAssociatedFiles(entries, extensions, dirs, autoLinkExactKeyOnly); } boolean foundAny = false; @@ -210,7 +213,8 @@ public void run() { * parameter can be null, which means that no progress update will be shown. * @return the runnable able to perform the automatically setting */ - public static Runnable autoSetLinks(final BibEntry entry, final FileListTableModel singleTableModel, final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) { + public static Runnable autoSetLinks(final BibEntry entry, final FileListTableModel singleTableModel, + final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) { return autoSetLinks(Collections.singletonList(entry), null, null, singleTableModel, databaseContext, callback, diag); } diff --git a/src/main/java/net/sf/jabref/gui/BasePanel.java b/src/main/java/net/sf/jabref/gui/BasePanel.java index 75e4616323e..df998d8e757 100644 --- a/src/main/java/net/sf/jabref/gui/BasePanel.java +++ b/src/main/java/net/sf/jabref/gui/BasePanel.java @@ -2427,7 +2427,8 @@ public Optional searchAndOpen() { String regExp = Globals.prefs.get(JabRefPreferences.REG_EXP_SEARCH_EXPRESSION_KEY); result = RegExpFileSearch.findFilesForSet(entries, extensions, dirs, regExp); } else { - result = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs); + boolean autoLinkExactKeyOnly = Globals.prefs.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY); + result = FileUtil.findAssociatedFiles(entries, extensions, dirs, autoLinkExactKeyOnly); } if (result.containsKey(entry)) { final List res = result.get(entry); diff --git a/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java b/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java index b8d8a4b0527..df4d7a995fb 100644 --- a/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java @@ -319,7 +319,7 @@ private static File shortenFileName(File fileName, String directory) { } public static Map> findAssociatedFiles(Collection entries, - Collection extensions, Collection directories, JabRefPreferences prefs) { + Collection extensions, Collection directories, boolean autolinkExactKeyOnly) { Map> result = new HashMap<>(); // First scan directories @@ -330,7 +330,6 @@ public static Map> findAssociatedFiles(Collection result.put(entry, new ArrayList<>()); } - boolean exactOnly = prefs.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY); // Now look for keys nextFile: for (File file : filesWithExtension) { @@ -346,7 +345,7 @@ public static Map> findAssociatedFiles(Collection } // If we get here, we didn't find any exact matches. If non-exact // matches are allowed, try to find one: - if (!exactOnly) { + if (!autolinkExactKeyOnly) { for (BibEntry entry : entries) { String citeKey = entry.getCiteKey(); if ((citeKey != null) && !citeKey.isEmpty() && name.startsWith(citeKey)) { diff --git a/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java b/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java index 47fd9341683..77136aa708f 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java +++ b/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java @@ -10,31 +10,30 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Stream; - import net.sf.jabref.BibtexTestData; import net.sf.jabref.Globals; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.preferences.JabRefPreferences; -import net.sf.jabref.support.DevEnvironment; - -import org.junit.After; import org.junit.Before; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeFalse; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public class FileBasedTestCase { private BibEntry entry; private Path rootDir; + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Before public void setUp() throws IOException { @@ -43,34 +42,32 @@ public void setUp() throws IOException { BibDatabase database = BibtexTestData.getBibtexDatabase(); entry = database.getEntries().iterator().next(); - rootDir = Files.createTempDirectory("UtilFindFileTest"); - - + rootDir = temporaryFolder.getRoot().toPath(); Path subDir = Files.createDirectory(rootDir.resolve("Organization Science")); Path pdfSubDir = Files.createDirectory(rootDir.resolve("pdfs")); - assertTrue(Files.exists(Files.createFile(subDir.resolve("HipKro03 - Hello.pdf")))); - assertTrue(Files.exists(Files.createFile(rootDir.resolve("HipKro03 - Hello.pdf")))); + Files.createFile(subDir.resolve("HipKro03 - Hello.pdf")); + Files.createFile(rootDir.resolve("HipKro03 - Hello.pdf")); Path pdfSubSubDir = Files.createDirectory(pdfSubDir.resolve("sub")); - assertTrue(Files.exists(Files.createFile(pdfSubSubDir.resolve("HipKro03-sub.pdf")))); + Files.createFile(pdfSubSubDir.resolve("HipKro03-sub.pdf")); Files.createDirectory(rootDir.resolve("2002")); Path dir2003 = Files.createDirectory(rootDir.resolve("2003")); assertTrue(Files.exists(Files.createFile(dir2003.resolve("Paper by HipKro03.pdf")))); Path dirTest = Files.createDirectory(rootDir.resolve("test")); - assertTrue(Files.exists(Files.createFile(dirTest.resolve(".TEST")))); - assertTrue(Files.exists(Files.createFile(dirTest.resolve("TEST[")))); - assertTrue(Files.exists(Files.createFile(dirTest.resolve("TE.ST")))); - assertTrue(Files.exists(Files.createFile(dirTest.resolve("foo.dat")))); + Files.createFile(dirTest.resolve(".TEST")); + Files.createFile(dirTest.resolve("TEST[")); + Files.createFile(dirTest.resolve("TE.ST")); + Files.createFile(dirTest.resolve("foo.dat")); Path graphicsDir = Files.createDirectory(rootDir.resolve("graphicsDir")); Path graphicsSubDir = Files.createDirectories(graphicsDir.resolve("subDir")); - assertTrue(Files.exists(Files.createFile(graphicsSubDir.resolve("HipKro03test.jpg")))); - assertTrue(Files.exists(Files.createFile(graphicsSubDir.resolve("HipKro03test.png")))); + Files.createFile(graphicsSubDir.resolve("HipKro03test.jpg")); + Files.createFile(graphicsSubDir.resolve("HipKro03test.png")); } @@ -81,9 +78,7 @@ public void testFindAssociatedFiles() { List extensions = Arrays.asList("jpg", "pdf"); List dirs = Arrays.asList(rootDir.resolve("graphicsDir").toFile(), rootDir.resolve("pdfs").toFile()); - when(Globals.prefs.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY)).thenReturn(false); - - Map> results = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs); + Map> results = FileUtil.findAssociatedFiles(entries, extensions, dirs, false); assertEquals(2, results.get(entry).size()); assertTrue(results.get(entry) @@ -99,26 +94,14 @@ public void testFindFilesException() { List dirs = Arrays.asList(rootDir.resolve("asdfasdf/asdfasdf").toFile()); Set results = FileFinder.findFiles(extensions, dirs); - assertEquals(0, results.size()); + assertEquals(Collections.emptySet(), results); } @Ignore("Fails on CI Server") @Test(expected = NullPointerException.class) public void testFindFilesNullPointerException() { - assumeFalse(DevEnvironment.isCIServer()); FileFinder.findFiles(null, null); } - @After - public void tearDown() throws IOException { - - Globals.prefs = null; - try (Stream path = Files.walk(rootDir)) { - // reverse; files before dirs - for (Path p : path.sorted((a, b) -> b.compareTo(a)).toArray(Path[]::new)) { - Files.delete(p); - } - } - } } From 360cd941685fe71d6e195805a513423670a13ec8 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Sat, 23 Jul 2016 13:11:54 +0200 Subject: [PATCH 7/9] Checkstyle fix --- .../java/net/sf/jabref/logic/util/io/FileBasedTestCase.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java b/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java index 77136aa708f..6842281dd6d 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java +++ b/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java @@ -10,11 +10,13 @@ import java.util.List; import java.util.Map; import java.util.Set; + import net.sf.jabref.BibtexTestData; import net.sf.jabref.Globals; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.preferences.JabRefPreferences; + import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; From 5a7af24963adc4685a2a7d43609100b884ae94b4 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 25 Jul 2016 18:51:16 +0200 Subject: [PATCH 8/9] Unignore test for trying on CI --- .../java/net/sf/jabref/logic/util/io/FileBasedTestCase.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java b/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java index 6842281dd6d..de242887151 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java +++ b/src/test/java/net/sf/jabref/logic/util/io/FileBasedTestCase.java @@ -18,7 +18,6 @@ import net.sf.jabref.preferences.JabRefPreferences; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -57,7 +56,7 @@ public void setUp() throws IOException { Files.createDirectory(rootDir.resolve("2002")); Path dir2003 = Files.createDirectory(rootDir.resolve("2003")); - assertTrue(Files.exists(Files.createFile(dir2003.resolve("Paper by HipKro03.pdf")))); + Files.createFile(dir2003.resolve("Paper by HipKro03.pdf")); Path dirTest = Files.createDirectory(rootDir.resolve("test")); Files.createFile(dirTest.resolve(".TEST")); @@ -99,7 +98,6 @@ public void testFindFilesException() { assertEquals(Collections.emptySet(), results); } - @Ignore("Fails on CI Server") @Test(expected = NullPointerException.class) public void testFindFilesNullPointerException() { From 0f3d98ca21f794e8495be8072dd3072a8f6182cb Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Mon, 25 Jul 2016 20:10:46 +0200 Subject: [PATCH 9/9] Use explicit List and Set in findFiles and caller methods --- .../java/net/sf/jabref/external/AutoSetLinks.java | 6 +++--- .../net/sf/jabref/external/ExternalFileTypes.java | 3 +-- .../net/sf/jabref/external/RegExpFileSearch.java | 12 +++++------- .../net/sf/jabref/external/SynchronizeFileField.java | 2 +- src/main/java/net/sf/jabref/gui/BasePanel.java | 5 +++-- .../sf/jabref/gui/fieldeditors/FileListEditor.java | 4 +--- .../java/net/sf/jabref/logic/util/io/FileFinder.java | 4 ++-- .../java/net/sf/jabref/logic/util/io/FileUtil.java | 5 ++--- 8 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/sf/jabref/external/AutoSetLinks.java b/src/main/java/net/sf/jabref/external/AutoSetLinks.java index b9e96ef29d7..57fd67e4238 100644 --- a/src/main/java/net/sf/jabref/external/AutoSetLinks.java +++ b/src/main/java/net/sf/jabref/external/AutoSetLinks.java @@ -40,7 +40,7 @@ public class AutoSetLinks { * @param entries the entries for which links should be set * @param databaseContext the database for which links are set */ - public static void autoSetLinks(Collection entries, BibDatabaseContext databaseContext) { + public static void autoSetLinks(List entries, BibDatabaseContext databaseContext) { autoSetLinks(entries, null, null, null, databaseContext, null, null); } @@ -69,7 +69,7 @@ public static void autoSetLinks(Collection entries, BibDatabaseContext * parameter can be null, which means that no progress update will be shown. * @return the thread performing the automatically setting */ - public static Runnable autoSetLinks(final Collection entries, final NamedCompound ce, + public static Runnable autoSetLinks(final List entries, final NamedCompound ce, final Set changedEntries, final FileListTableModel singleTableModel, final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) { final Collection types = ExternalFileTypes.getInstance().getExternalFileTypeSelection(); @@ -96,7 +96,7 @@ public void run() { dirs.addAll(dirsS.stream().map(File::new).collect(Collectors.toList())); // determine extensions - Collection extensions = new ArrayList<>(); + List extensions = new ArrayList<>(); for (final ExternalFileType type : types) { extensions.add(type.getExtension()); } diff --git a/src/main/java/net/sf/jabref/external/ExternalFileTypes.java b/src/main/java/net/sf/jabref/external/ExternalFileTypes.java index 8c84ff18047..7b2cdb82c1c 100644 --- a/src/main/java/net/sf/jabref/external/ExternalFileTypes.java +++ b/src/main/java/net/sf/jabref/external/ExternalFileTypes.java @@ -1,7 +1,6 @@ package net.sf.jabref.external; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.Set; @@ -104,7 +103,7 @@ public static List getDefaultExternalFileTypes() { return list; } - public Collection getExternalFileTypeSelection() { + public Set getExternalFileTypeSelection() { return externalFileTypes; } diff --git a/src/main/java/net/sf/jabref/external/RegExpFileSearch.java b/src/main/java/net/sf/jabref/external/RegExpFileSearch.java index 7374011a1e4..37881a8bfe2 100644 --- a/src/main/java/net/sf/jabref/external/RegExpFileSearch.java +++ b/src/main/java/net/sf/jabref/external/RegExpFileSearch.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; @@ -63,8 +62,8 @@ public class RegExpFileSearch { * @param regExp The expression deciding which names are acceptable. * @return A map linking each given entry to a list of files matching the given criteria. */ - public static Map> findFilesForSet(Collection entries, - Collection extensions, List directories, String regExp) { + public static Map> findFilesForSet(List entries, List extensions, + List directories, String regExp) { Map> res = new HashMap<>(); for (BibEntry entry : entries) { @@ -82,8 +81,8 @@ public static Map> findFilesForSet(Collection ent * @param regularExpression The expression deciding which names are acceptable. * @return A list of files paths matching the given criteria. */ - private static List findFiles(BibEntry entry, Collection extensions, - Collection directories, String regularExpression) { + private static List findFiles(BibEntry entry, List extensions, List directories, + String regularExpression) { String extensionRegExp = '(' + String.join("|", extensions) + ')'; @@ -128,8 +127,7 @@ private static List findFiles(BibEntry entry, Collection extension * @return Will return the first file found to match the given criteria or * null if none was found. */ - private static List findFile(BibEntry entry, Collection dirs, String file, - String extensionRegExp) { + private static List findFile(BibEntry entry, List dirs, String file, String extensionRegExp) { List res = new ArrayList<>(); for (File directory : dirs) { res.addAll(findFile(entry, directory.getPath(), file, extensionRegExp)); diff --git a/src/main/java/net/sf/jabref/external/SynchronizeFileField.java b/src/main/java/net/sf/jabref/external/SynchronizeFileField.java index 0a798fb1ab2..10a2a108d07 100644 --- a/src/main/java/net/sf/jabref/external/SynchronizeFileField.java +++ b/src/main/java/net/sf/jabref/external/SynchronizeFileField.java @@ -132,7 +132,7 @@ public void run() { // First we try to autoset fields if (autoSet) { - Collection entries = new ArrayList<>(sel); + List entries = new ArrayList<>(sel); // Start the automatically setting process: Runnable r = AutoSetLinks.autoSetLinks(entries, ce, changedEntries, null, panel.getBibDatabaseContext(), null, null); diff --git a/src/main/java/net/sf/jabref/gui/BasePanel.java b/src/main/java/net/sf/jabref/gui/BasePanel.java index 7d847ad2504..528860f289e 100644 --- a/src/main/java/net/sf/jabref/gui/BasePanel.java +++ b/src/main/java/net/sf/jabref/gui/BasePanel.java @@ -38,6 +38,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.Set; import java.util.TimerTask; import javax.swing.AbstractAction; @@ -2402,9 +2403,9 @@ public Optional searchAndOpen() { of stuff below. It is now disabled by default. */ // see if we can fall back to a filename based on the bibtex key - final Collection entries = Collections.singleton(entry); + final List entries = Collections.singletonList(entry); - final Collection types = ExternalFileTypes.getInstance().getExternalFileTypeSelection(); + final Set types = ExternalFileTypes.getInstance().getExternalFileTypeSelection(); final List dirs = new ArrayList<>(); if (!basePanel.getBibDatabaseContext().getFileDirectory().isEmpty()) { final List mdDirs = basePanel.getBibDatabaseContext().getFileDirectory(); diff --git a/src/main/java/net/sf/jabref/gui/fieldeditors/FileListEditor.java b/src/main/java/net/sf/jabref/gui/fieldeditors/FileListEditor.java index 5077520a1c7..187f85cba2f 100644 --- a/src/main/java/net/sf/jabref/gui/fieldeditors/FileListEditor.java +++ b/src/main/java/net/sf/jabref/gui/fieldeditors/FileListEditor.java @@ -29,7 +29,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Optional; @@ -434,8 +433,7 @@ private boolean editListEntry(FileListEntry entry, boolean openBrowse) { public void autoSetLinks() { auto.setEnabled(false); - Collection entries = new ArrayList<>(); - entries.addAll(frame.getCurrentBasePanel().getSelectedEntries()); + List entries = new ArrayList<>(frame.getCurrentBasePanel().getSelectedEntries()); // filesystem lookup JDialog dialog = new JDialog(frame, true); diff --git a/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java b/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java index 79cbbc4bf48..b50e7e51aed 100644 --- a/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java +++ b/src/main/java/net/sf/jabref/logic/util/io/FileFinder.java @@ -5,8 +5,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; -import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Objects; import java.util.Set; import java.util.function.BiPredicate; @@ -21,7 +21,7 @@ public class FileFinder { private static final Log LOGGER = LogFactory.getLog(FileFinder.class); - public static Set findFiles(Collection extensions, Collection directories) { + public static Set findFiles(List extensions, List directories) { Objects.requireNonNull(directories, "Directories must not be null!"); Objects.requireNonNull(extensions, "Extensions must not be null!"); diff --git a/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java b/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java index 23fbe7cc83f..7640d087f72 100644 --- a/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/net/sf/jabref/logic/util/io/FileUtil.java @@ -24,7 +24,6 @@ import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -317,8 +316,8 @@ private static File shortenFileName(File fileName, String directory) { } } - public static Map> findAssociatedFiles(Collection entries, - Collection extensions, Collection directories, boolean autolinkExactKeyOnly) { + public static Map> findAssociatedFiles(List entries, + List extensions, List directories, boolean autolinkExactKeyOnly) { Map> result = new HashMap<>(); // First scan directories