diff --git a/src/main/java/net/sf/jabref/external/DownloadExternalFile.java b/src/main/java/net/sf/jabref/external/DownloadExternalFile.java index 60d2472e59e..812a6c2d576 100644 --- a/src/main/java/net/sf/jabref/external/DownloadExternalFile.java +++ b/src/main/java/net/sf/jabref/external/DownloadExternalFile.java @@ -18,9 +18,11 @@ import net.sf.jabref.gui.JabRefFrame; import net.sf.jabref.gui.net.MonitoredURLDownload; import net.sf.jabref.logic.l10n.Localization; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.logic.net.URLDownload; import net.sf.jabref.logic.util.OS; import net.sf.jabref.logic.util.io.FileUtil; +import net.sf.jabref.preferences.JabRefPreferences; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -255,8 +257,9 @@ private void downloadFinished() { // FIXME: will break download if no bibtexkey is present! private String getSuggestedFileName(String suffix) { String plannedName = FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), - frame.getCurrentBasePanel().getSelectedEntries().get(0), Globals.journalAbbreviationLoader, - Globals.prefs); + frame.getCurrentBasePanel().getSelectedEntries().get(0), + Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN), + LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader)); if (!suffix.isEmpty()) { plannedName += "." + suffix; diff --git a/src/main/java/net/sf/jabref/external/DroppedFileHandler.java b/src/main/java/net/sf/jabref/external/DroppedFileHandler.java index bf3139a6029..0a18dbe99b1 100644 --- a/src/main/java/net/sf/jabref/external/DroppedFileHandler.java +++ b/src/main/java/net/sf/jabref/external/DroppedFileHandler.java @@ -25,6 +25,7 @@ import net.sf.jabref.gui.undo.UndoableFieldChange; import net.sf.jabref.gui.undo.UndoableInsertEntry; import net.sf.jabref.logic.l10n.Localization; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.logic.util.OS; import net.sf.jabref.logic.util.io.FileUtil; import net.sf.jabref.logic.xmp.XMPPreferences; @@ -334,8 +335,9 @@ private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileTy renameCheckBox.setText(Localization.lang("Rename file to").concat(": ")); // Determine which name to suggest: - String targetName = FileUtil.createFileNameFromPattern(database, entry, Globals.journalAbbreviationLoader, - Globals.prefs); + String targetName = FileUtil.createFileNameFromPattern(database, entry, + Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN), + LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader)); renameToTextBox.setText(targetName.concat(".").concat(fileType.getExtension())); diff --git a/src/main/java/net/sf/jabref/external/MoveFileAction.java b/src/main/java/net/sf/jabref/external/MoveFileAction.java index 7ab3e1eb36b..59f4f245ec9 100644 --- a/src/main/java/net/sf/jabref/external/MoveFileAction.java +++ b/src/main/java/net/sf/jabref/external/MoveFileAction.java @@ -20,6 +20,7 @@ import net.sf.jabref.gui.fieldeditors.FileListEditor; import net.sf.jabref.gui.util.component.CheckBoxMessage; import net.sf.jabref.logic.l10n.Localization; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.logic.util.io.FileUtil; import net.sf.jabref.preferences.JabRefPreferences; @@ -97,7 +98,9 @@ public void actionPerformed(ActionEvent event) { // Determine which name to suggest: String suggName = FileUtil .createFileNameFromPattern(eEditor.getDatabase(), eEditor.getEntry(), - Globals.journalAbbreviationLoader, Globals.prefs) + Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN), + LayoutFormatterPreferences.fromPreferences(Globals.prefs, + Globals.journalAbbreviationLoader)) .concat(entry.type.isPresent() ? "." + entry.type.get().getExtension() : ""); CheckBoxMessage cbm = new CheckBoxMessage(Localization.lang("Move file to file directory?"), Localization.lang("Rename to '%0'", suggName), diff --git a/src/main/java/net/sf/jabref/gui/actions/CleanupAction.java b/src/main/java/net/sf/jabref/gui/actions/CleanupAction.java index ad5426eb540..80d2cad1dbc 100644 --- a/src/main/java/net/sf/jabref/gui/actions/CleanupAction.java +++ b/src/main/java/net/sf/jabref/gui/actions/CleanupAction.java @@ -17,6 +17,7 @@ import net.sf.jabref.logic.cleanup.CleanupPreset; import net.sf.jabref.logic.cleanup.CleanupWorker; import net.sf.jabref.logic.l10n.Localization; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.model.FieldChange; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.preferences.JabRefPreferences; @@ -146,7 +147,9 @@ private int showDialog(CleanupPresetPanel presetPanel) { private void doCleanup(CleanupPreset preset, BibEntry entry, NamedCompound ce) { // Create and run cleaner BibDatabaseContext bibDatabaseContext = panel.getBibDatabaseContext(); - CleanupWorker cleaner = new CleanupWorker(bibDatabaseContext, Globals.journalAbbreviationLoader, Globals.prefs); + CleanupWorker cleaner = new CleanupWorker(bibDatabaseContext, + Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN), + LayoutFormatterPreferences.fromPreferences(Globals.prefs, Globals.journalAbbreviationLoader)); List changes = cleaner.cleanup(preset, entry); unsuccessfulRenames = cleaner.getUnsuccessfulRenames(); diff --git a/src/main/java/net/sf/jabref/gui/date/DatePickerButton.java b/src/main/java/net/sf/jabref/gui/date/DatePickerButton.java index 18dce6c806c..1ef0563cb10 100644 --- a/src/main/java/net/sf/jabref/gui/date/DatePickerButton.java +++ b/src/main/java/net/sf/jabref/gui/date/DatePickerButton.java @@ -12,6 +12,7 @@ import net.sf.jabref.gui.fieldeditors.FieldEditor; import net.sf.jabref.gui.util.FocusRequester; import net.sf.jabref.logic.util.date.EasyDateFormat; +import net.sf.jabref.preferences.JabRefPreferences; import com.michaelbaranov.microba.calendar.DatePicker; @@ -43,7 +44,9 @@ public void actionPerformed(ActionEvent e) { if (isoFormat) { editor.setText(EasyDateFormat.isoDateFormat().getDateAt(date)); } else { - editor.setText(EasyDateFormat.fromPreferences(Globals.prefs).getDateAt(date)); + editor.setText(EasyDateFormat + .fromTimeStampFormat(Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT)) + .getDateAt(date)); } // Set focus to editor component after changing its text: new FocusRequester(editor.getTextComponent()); diff --git a/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java index 8e097658395..8eb7847a69d 100644 --- a/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java @@ -88,8 +88,10 @@ import net.sf.jabref.logic.importer.fileformat.BibtexParser; import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.logic.search.SearchQueryHighlightListener; -import net.sf.jabref.logic.util.date.TimeStamp; +import net.sf.jabref.logic.util.UpdateField; +import net.sf.jabref.logic.util.date.EasyDateFormat; import net.sf.jabref.model.EntryTypes; +import net.sf.jabref.model.FieldChange; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.database.BibDatabaseMode; import net.sf.jabref.model.entry.BibEntry; @@ -1129,11 +1131,10 @@ public void actionPerformed(ActionEvent event) { // Add an UndoableKeyChange to the baseframe's undoManager. UndoableKeyChange undoableKeyChange = new UndoableKeyChange(panel.getDatabase(), entry, oldValue, newValue); - if (TimeStamp.updateTimeStampIsSet(Globals.prefs)) { + if (updateTimeStampIsSet()) { NamedCompound ce = new NamedCompound(undoableKeyChange.getPresentationName()); ce.addEdit(undoableKeyChange); - TimeStamp.doUpdateTimeStamp(entry, Globals.prefs) - .ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange))); + doUpdateTimeStamp().ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange))); ce.end(); panel.getUndoManager().addEdit(ce); } else { @@ -1196,11 +1197,11 @@ public void actionPerformed(ActionEvent event) { // Add an UndoableFieldChange to the baseframe's undoManager. UndoableFieldChange undoableFieldChange = new UndoableFieldChange(entry, fieldEditor.getFieldName(), oldValue, toSet); - if (TimeStamp.updateTimeStampIsSet(Globals.prefs)) { + if (updateTimeStampIsSet()) { NamedCompound ce = new NamedCompound(undoableFieldChange.getPresentationName()); ce.addEdit(undoableFieldChange); - TimeStamp.doUpdateTimeStamp(entry, Globals.prefs) + doUpdateTimeStamp() .ifPresent(fieldChange -> ce.addEdit(new UndoableFieldChange(fieldChange))); ce.end(); @@ -1503,4 +1504,20 @@ public void actionPerformed(ActionEvent event) { } } + + private boolean updateTimeStampIsSet() { + return Globals.prefs.getBoolean(JabRefPreferences.USE_TIME_STAMP) + && Globals.prefs.getBoolean(JabRefPreferences.UPDATE_TIMESTAMP); + } + + /** + * Updates the timestamp of the given entry and returns the FieldChange + */ + private Optional doUpdateTimeStamp() { + String timeStampField = Globals.prefs.get(JabRefPreferences.TIME_STAMP_FIELD); + String timeStampFormat = Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT); + String timestamp = EasyDateFormat.fromTimeStampFormat(timeStampFormat).getCurrentDate(); + return UpdateField.updateField(entry, timeStampField, timestamp); + } + } diff --git a/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java b/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java index 39c48939cb3..cbb03bdab87 100644 --- a/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java +++ b/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java @@ -6,24 +6,23 @@ import java.util.Objects; import net.sf.jabref.BibDatabaseContext; -import net.sf.jabref.logic.journals.JournalAbbreviationLoader; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.model.FieldChange; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.FieldName; -import net.sf.jabref.preferences.JabRefPreferences; public class CleanupWorker { private final BibDatabaseContext databaseContext; - private final JournalAbbreviationLoader repositoryLoader; - private final JabRefPreferences prefs; + private final String fileNamePattern; + private final LayoutFormatterPreferences prefs; private int unsuccessfulRenames; - public CleanupWorker(BibDatabaseContext databaseContext, JournalAbbreviationLoader repositoryLoader, - JabRefPreferences prefs) { + public CleanupWorker(BibDatabaseContext databaseContext, String fileNamePattern, + LayoutFormatterPreferences prefs) { this.databaseContext = databaseContext; - this.repositoryLoader = repositoryLoader; + this.fileNamePattern = fileNamePattern; this.prefs = prefs; } @@ -68,7 +67,7 @@ private List determineCleanupActions(CleanupPreset preset) { } if (preset.isRenamePDF()) { RenamePdfCleanup cleaner = new RenamePdfCleanup(preset.isRenamePdfOnlyRelativePaths(), databaseContext, - repositoryLoader, prefs); + fileNamePattern, prefs); jobs.add(cleaner); unsuccessfulRenames += cleaner.getUnsuccessfulRenames(); } diff --git a/src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java b/src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java index 7168b2c22a9..def724b6488 100644 --- a/src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java +++ b/src/main/java/net/sf/jabref/logic/cleanup/RenamePdfCleanup.java @@ -9,29 +9,28 @@ import net.sf.jabref.BibDatabaseContext; import net.sf.jabref.logic.TypedBibEntry; -import net.sf.jabref.logic.journals.JournalAbbreviationLoader; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.logic.util.OS; import net.sf.jabref.logic.util.io.FileUtil; import net.sf.jabref.model.FieldChange; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.ParsedFileField; -import net.sf.jabref.preferences.JabRefPreferences; public class RenamePdfCleanup implements CleanupJob { private final BibDatabaseContext databaseContext; private final boolean onlyRelativePaths; - private final JournalAbbreviationLoader repositoryLoader; - private final JabRefPreferences prefs; + private final String fileNamePattern; + private final LayoutFormatterPreferences prefs; private int unsuccessfulRenames; public RenamePdfCleanup(boolean onlyRelativePaths, BibDatabaseContext databaseContext, - JournalAbbreviationLoader repositoryLoader, JabRefPreferences prefs) { + String fileNamePattern, LayoutFormatterPreferences prefs) { this.databaseContext = Objects.requireNonNull(databaseContext); this.onlyRelativePaths = onlyRelativePaths; - this.repositoryLoader = Objects.requireNonNull(repositoryLoader); + this.fileNamePattern = Objects.requireNonNull(fileNamePattern); this.prefs = Objects.requireNonNull(prefs); } @@ -50,9 +49,8 @@ public List cleanup(BibEntry entry) { continue; } - StringBuilder newFilename = new StringBuilder( - FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), entry, repositoryLoader, prefs) - .trim()); + StringBuilder newFilename = new StringBuilder(FileUtil + .createFileNameFromPattern(databaseContext.getDatabase(), entry, fileNamePattern, prefs).trim()); //Add extension to newFilename newFilename.append('.').append(FileUtil.getFileExtension(realOldFilename).orElse("pdf")); diff --git a/src/main/java/net/sf/jabref/logic/util/date/EasyDateFormat.java b/src/main/java/net/sf/jabref/logic/util/date/EasyDateFormat.java index 896d47e2a6b..3eb5a03dd60 100644 --- a/src/main/java/net/sf/jabref/logic/util/date/EasyDateFormat.java +++ b/src/main/java/net/sf/jabref/logic/util/date/EasyDateFormat.java @@ -5,8 +5,6 @@ import java.time.format.DateTimeFormatter; import java.util.Date; -import net.sf.jabref.preferences.JabRefPreferences; - public class EasyDateFormat { /** @@ -59,10 +57,6 @@ public static EasyDateFormat fromTimeStampFormat(String timeStampFormat) { return new EasyDateFormat(timeStampFormat); } - public static EasyDateFormat fromPreferences(JabRefPreferences preferences) { - return new EasyDateFormat(preferences.get(JabRefPreferences.TIME_STAMP_FORMAT)); - } - public static EasyDateFormat isoDateFormat() { return new EasyDateFormat(DateTimeFormatter.ISO_LOCAL_DATE); } diff --git a/src/main/java/net/sf/jabref/logic/util/date/TimeStamp.java b/src/main/java/net/sf/jabref/logic/util/date/TimeStamp.java deleted file mode 100644 index b2ac885a185..00000000000 --- a/src/main/java/net/sf/jabref/logic/util/date/TimeStamp.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.sf.jabref.logic.util.date; - -import java.util.Optional; - -import net.sf.jabref.logic.util.UpdateField; -import net.sf.jabref.model.FieldChange; -import net.sf.jabref.model.entry.BibEntry; -import net.sf.jabref.preferences.JabRefPreferences; - -public class TimeStamp { - - public static boolean updateTimeStampIsSet(JabRefPreferences prefs) { - return prefs.getBoolean(JabRefPreferences.USE_TIME_STAMP) - && prefs.getBoolean(JabRefPreferences.UPDATE_TIMESTAMP); - } - - /** - * Updates the timestamp of the given entry and returns the FieldChange - */ - public static Optional doUpdateTimeStamp(BibEntry entry, JabRefPreferences prefs) { - String timeStampField = prefs.get(JabRefPreferences.TIME_STAMP_FIELD); - String timestamp = EasyDateFormat.fromPreferences(prefs).getCurrentDate(); - return UpdateField.updateField(entry, timeStampField, timestamp); - } - - -} 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 f14eac44788..65cf990ef16 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 @@ -21,7 +21,6 @@ import java.util.regex.Pattern; import net.sf.jabref.BibDatabaseContext; -import net.sf.jabref.logic.journals.JournalAbbreviationLoader; import net.sf.jabref.logic.layout.Layout; import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.logic.layout.LayoutHelper; @@ -31,7 +30,6 @@ import net.sf.jabref.model.entry.FieldName; import net.sf.jabref.model.entry.FileField; import net.sf.jabref.model.entry.ParsedFileField; -import net.sf.jabref.preferences.JabRefPreferences; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -371,19 +369,19 @@ public static List getListOfLinkedFiles(List bes, List f /** * Determines filename provided by an entry in a database * - * @param database the database, where the entry is located - * @param entry the entry to which the file should be linked to - * @param repositoryLoader + * @param database the database, where the entry is located + * @param entry the entry to which the file should be linked to + * @param fileNamePattern the filename pattern + * @param prefs the layout preferences * @return a suggested fileName */ public static String createFileNameFromPattern(BibDatabase database, BibEntry entry, - JournalAbbreviationLoader repositoryLoader, JabRefPreferences prefs) { + String fileNamePattern, LayoutFormatterPreferences prefs) { String targetName = entry.getCiteKeyOptional().orElse("default"); - StringReader sr = new StringReader(prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN)); + StringReader sr = new StringReader(fileNamePattern); Layout layout = null; try { - layout = new LayoutHelper(sr, LayoutFormatterPreferences.fromPreferences(prefs, repositoryLoader)) - .getLayoutFromText(); + layout = new LayoutHelper(sr, prefs).getLayoutFromText(); } catch (IOException e) { LOGGER.info("Wrong format " + e.getMessage(), e); } diff --git a/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java b/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java index 3525d6790f8..404e6885b63 100644 --- a/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java +++ b/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java @@ -19,7 +19,7 @@ import net.sf.jabref.logic.formatter.bibtexfields.NormalizePagesFormatter; import net.sf.jabref.logic.formatter.bibtexfields.UnitsToLatexFormatter; import net.sf.jabref.logic.formatter.casechanger.ProtectTermsFormatter; -import net.sf.jabref.logic.journals.JournalAbbreviationLoader; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.logic.protectedterms.ProtectedTermsLoader; import net.sf.jabref.logic.protectedterms.ProtectedTermsPreferences; import net.sf.jabref.model.FieldChange; @@ -52,16 +52,14 @@ public void setUp() throws IOException { if (Globals.prefs == null) { Globals.prefs = JabRefPreferences.getInstance(); } - if (Globals.journalAbbreviationLoader == null) { - Globals.journalAbbreviationLoader = mock(JournalAbbreviationLoader.class); - } pdfFolder = bibFolder.newFolder(); MetaData metaData = new MetaData(); metaData.setDefaultFileDirectory(pdfFolder.getAbsolutePath()); BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(), metaData, bibFolder.newFile("test.bib")); - worker = new CleanupWorker(context, mock(JournalAbbreviationLoader.class), Globals.prefs); + worker = new CleanupWorker(context, Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN), + mock(LayoutFormatterPreferences.class)); } @Test(expected = NullPointerException.class) diff --git a/src/test/java/net/sf/jabref/logic/cleanup/ISSNCleanupTest.java b/src/test/java/net/sf/jabref/logic/cleanup/ISSNCleanupTest.java index 4e69c9c719c..6fce1cb5c2d 100644 --- a/src/test/java/net/sf/jabref/logic/cleanup/ISSNCleanupTest.java +++ b/src/test/java/net/sf/jabref/logic/cleanup/ISSNCleanupTest.java @@ -3,9 +3,8 @@ import java.util.Optional; import net.sf.jabref.BibDatabaseContext; -import net.sf.jabref.logic.journals.JournalAbbreviationLoader; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.model.entry.BibEntry; -import net.sf.jabref.preferences.JabRefPreferences; import org.junit.Assert; import org.junit.Before; @@ -20,8 +19,7 @@ public class ISSNCleanupTest { @Before public void setUp() { - worker = new CleanupWorker(mock(BibDatabaseContext.class), mock(JournalAbbreviationLoader.class), - JabRefPreferences.getInstance()); + worker = new CleanupWorker(mock(BibDatabaseContext.class), "", mock(LayoutFormatterPreferences.class)); } @Test diff --git a/src/test/java/net/sf/jabref/logic/cleanup/RenamePdfCleanupTest.java b/src/test/java/net/sf/jabref/logic/cleanup/RenamePdfCleanupTest.java index e688a56bcb1..20365a203bd 100644 --- a/src/test/java/net/sf/jabref/logic/cleanup/RenamePdfCleanupTest.java +++ b/src/test/java/net/sf/jabref/logic/cleanup/RenamePdfCleanupTest.java @@ -10,6 +10,7 @@ import net.sf.jabref.Globals; import net.sf.jabref.MetaData; import net.sf.jabref.logic.journals.JournalAbbreviationLoader; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.model.database.BibDatabase; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.FileField; @@ -23,7 +24,6 @@ import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public class RenamePdfCleanupTest { @@ -49,13 +49,13 @@ public void setUp() throws Exception { */ @Test public void cleanupRenamePdfRenamesFileEvenIfOnlyDifferenceIsCase() throws IOException { - when(Globals.prefs.get("importFileNamePattern")).thenReturn("\\bibtexkey"); + String fileNamePattern = "\\bibtexkey"; File tempFile = testFolder.newFile("toot.tmp"); ParsedFileField fileField = new ParsedFileField("", tempFile.getAbsolutePath(), ""); entry.setField("file", FileField.getStringRepresentation(fileField)); - RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, mock(JournalAbbreviationLoader.class), - Globals.prefs); + RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, fileNamePattern, + mock(LayoutFormatterPreferences.class)); cleanup.cleanup(entry); ParsedFileField newFileField = new ParsedFileField("", "Toot.tmp", ""); @@ -64,15 +64,15 @@ public void cleanupRenamePdfRenamesFileEvenIfOnlyDifferenceIsCase() throws IOExc @Test public void cleanupRenamePdfRenamesWithMultipleFiles() throws IOException { - when(Globals.prefs.get("importFileNamePattern")).thenReturn("\\bibtexkey - \\title"); + String fileNamePattern = "\\bibtexkey - \\title"; File tempFile = testFolder.newFile("Toot.tmp"); entry.setField("title", "test title"); entry.setField("file", FileField.getStringRepresentation(Arrays.asList(new ParsedFileField("","",""), new ParsedFileField("", tempFile.getAbsolutePath(), ""), new ParsedFileField("","","")))); - RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, mock(JournalAbbreviationLoader.class), - Globals.prefs); + RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, fileNamePattern, + mock(LayoutFormatterPreferences.class)); cleanup.cleanup(entry); assertEquals( @@ -83,14 +83,14 @@ public void cleanupRenamePdfRenamesWithMultipleFiles() throws IOException { @Test public void cleanupRenamePdfRenamesFileStartingWithBibtexKey() throws IOException { - when(Globals.prefs.get("importFileNamePattern")).thenReturn("\\bibtexkey - \\title"); + String fileNamePattern = "\\bibtexkey - \\title"; File tempFile = testFolder.newFile("Toot.tmp"); ParsedFileField fileField = new ParsedFileField("", tempFile.getAbsolutePath(), ""); entry.setField("file", FileField.getStringRepresentation(fileField)); entry.setField("title", "test title"); - RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, mock(JournalAbbreviationLoader.class), - Globals.prefs); + RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, fileNamePattern, + mock(LayoutFormatterPreferences.class)); cleanup.cleanup(entry); ParsedFileField newFileField = new ParsedFileField("", "Toot - test title.tmp", ""); @@ -99,14 +99,14 @@ public void cleanupRenamePdfRenamesFileStartingWithBibtexKey() throws IOExceptio @Test public void cleanupRenamePdfRenamesFileInSameFolder() throws IOException { - when(Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN)).thenReturn("\\bibtexkey\\begin{title} - \\format[RemoveBrackets]{\\title}\\end{title}"); + String fileNamePattern = "\\bibtexkey\\begin{title} - \\format[RemoveBrackets]{\\title}\\end{title}"; testFolder.newFile("Toot.pdf"); ParsedFileField fileField = new ParsedFileField("", "Toot.pdf", "PDF"); entry.setField("file", FileField.getStringRepresentation(fileField)); entry.setField("title", "test title"); - RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, mock(JournalAbbreviationLoader.class), - Globals.prefs); + RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, fileNamePattern, + LayoutFormatterPreferences.fromPreferences(Globals.prefs, mock(JournalAbbreviationLoader.class))); cleanup.cleanup(entry); ParsedFileField newFileField = new ParsedFileField("", "Toot - test title.pdf", "PDF"); 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 7eb31da9cfd..9f4242d198b 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 @@ -6,53 +6,42 @@ import java.util.List; import net.sf.jabref.logic.journals.JournalAbbreviationLoader; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.preferences.JabRefPreferences; -import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public class FileUtilTest { - private JabRefPreferences prefs; - - - @Before - public void setUp() { - prefs = mock(JabRefPreferences.class); - } - - @Test public void testGetLinkedFileNameDefault() { // bibkey - title - when(prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN)) - .thenReturn("\\bibtexkey\\begin{title} - \\format[RemoveBrackets]{\\title}\\end{title}"); - + String fileNamePattern = "\\bibtexkey\\begin{title} - \\format[RemoveBrackets]{\\title}\\end{title}"; BibEntry entry = new BibEntry(); entry.setCiteKey("1234"); entry.setField("title", "mytitle"); assertEquals("1234 - mytitle", - FileUtil.createFileNameFromPattern(null, entry, mock(JournalAbbreviationLoader.class), prefs)); + FileUtil.createFileNameFromPattern(null, entry, fileNamePattern, LayoutFormatterPreferences + .fromPreferences(JabRefPreferences.getInstance(), mock(JournalAbbreviationLoader.class)))); } @Test public void testGetLinkedFileNameBibTeXKey() { // bibkey - when(prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN)).thenReturn("\\bibtexkey"); - + String fileNamePattern = "\\bibtexkey"; BibEntry entry = new BibEntry(); entry.setCiteKey("1234"); entry.setField("title", "mytitle"); assertEquals("1234", - FileUtil.createFileNameFromPattern(null, entry, mock(JournalAbbreviationLoader.class), prefs)); + FileUtil.createFileNameFromPattern(null, entry, fileNamePattern, + mock(LayoutFormatterPreferences.class))); } @Test