|
| 1 | +package org.jabref.gui.externalfiles; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.jabref.gui.preferences.GuiPreferences; |
| 9 | +import org.jabref.logic.FilePreferences; |
| 10 | +import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences; |
| 11 | +import org.jabref.logic.citationkeypattern.GlobalCitationKeyPatterns; |
| 12 | +import org.jabref.model.database.BibDatabase; |
| 13 | +import org.jabref.model.database.BibDatabaseContext; |
| 14 | +import org.jabref.model.entry.BibEntry; |
| 15 | +import org.jabref.model.entry.LinkedFile; |
| 16 | +import org.jabref.model.entry.event.FieldChangedEvent; |
| 17 | +import org.jabref.model.entry.field.StandardField; |
| 18 | +import org.jabref.model.entry.types.StandardEntryType; |
| 19 | +import org.jabref.model.metadata.MetaData; |
| 20 | + |
| 21 | +import com.google.common.eventbus.Subscribe; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.io.TempDir; |
| 25 | + |
| 26 | +import static org.jabref.logic.citationkeypattern.CitationKeyGenerator.DEFAULT_UNWANTED_CHARACTERS; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | + |
| 32 | +class AutoRenameFileOnEntryChangeTest { |
| 33 | + private FilePreferences filePreferences; |
| 34 | + private BibEntry entry; |
| 35 | + private Path tempDir; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + void setUp(@TempDir Path tempDir) { |
| 39 | + this.tempDir = tempDir; |
| 40 | + MetaData metaData = new MetaData(); |
| 41 | + metaData.setLibrarySpecificFileDirectory(tempDir.toString()); |
| 42 | + BibDatabaseContext bibDatabaseContext = new BibDatabaseContext(new BibDatabase(), metaData); |
| 43 | + GlobalCitationKeyPatterns keyPattern = GlobalCitationKeyPatterns.fromPattern("[auth][year]"); |
| 44 | + GuiPreferences guiPreferences = mock(GuiPreferences.class); |
| 45 | + filePreferences = mock(FilePreferences.class); |
| 46 | + CitationKeyPatternPreferences patternPreferences = new CitationKeyPatternPreferences( |
| 47 | + false, |
| 48 | + true, |
| 49 | + false, |
| 50 | + CitationKeyPatternPreferences.KeySuffix.SECOND_WITH_A, |
| 51 | + "", |
| 52 | + "", |
| 53 | + DEFAULT_UNWANTED_CHARACTERS, |
| 54 | + keyPattern, |
| 55 | + "", |
| 56 | + ','); |
| 57 | + |
| 58 | + when(guiPreferences.getCitationKeyPatternPreferences()).thenReturn(patternPreferences); |
| 59 | + when(guiPreferences.getFilePreferences()).thenReturn(filePreferences); |
| 60 | + when(filePreferences.shouldStoreFilesRelativeToBibFile()).thenReturn(true); |
| 61 | + when(filePreferences.getFileNamePattern()).thenReturn("[bibtexkey]"); |
| 62 | + |
| 63 | + entry = new BibEntry(StandardEntryType.Article).withCitationKey("oldKey2081") |
| 64 | + .withField(StandardField.AUTHOR, "oldKey") |
| 65 | + .withField(StandardField.YEAR, "2081"); |
| 66 | + |
| 67 | + bibDatabaseContext.getDatabase().insertEntry(entry); |
| 68 | + AutoRenameFileOnEntryChange autoRenameFileOnEntryChange = new AutoRenameFileOnEntryChange(bibDatabaseContext, guiPreferences.getFilePreferences()); |
| 69 | + bibDatabaseContext.getDatabase().registerListener(autoRenameFileOnEntryChange); |
| 70 | + |
| 71 | + // Update citation-key when author/year changes |
| 72 | + bibDatabaseContext.getDatabase().registerListener(new Object() { |
| 73 | + @Subscribe |
| 74 | + public void listen(FieldChangedEvent event) { |
| 75 | + if (event.getField().equals(StandardField.AUTHOR)) { |
| 76 | + String author = event.getNewValue(); |
| 77 | + String year = entry.getField(StandardField.YEAR).orElse(""); |
| 78 | + entry.setCitationKey(author + year); |
| 79 | + } else if (event.getField().equals(StandardField.YEAR)) { |
| 80 | + String author = entry.getField(StandardField.AUTHOR).orElse(""); |
| 81 | + String year = event.getNewValue(); |
| 82 | + entry.setCitationKey(author + year); |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void noFileRenameByDefault() throws IOException { |
| 90 | + Files.createFile(tempDir.resolve("oldKey2081.pdf")); |
| 91 | + entry.setFiles(List.of(new LinkedFile("", "oldKey2081.pdf", "PDF"))); |
| 92 | + entry.setField(StandardField.AUTHOR, "newKey"); |
| 93 | + |
| 94 | + assertEquals("oldKey2081.pdf", entry.getFiles().getFirst().getLink()); |
| 95 | + assertTrue(Files.exists(tempDir.resolve("oldKey2081.pdf"))); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void noFileRenameOnEmptyFilePattern() throws IOException { |
| 100 | + Files.createFile(tempDir.resolve("oldKey2081.pdf")); |
| 101 | + entry.setFiles(List.of(new LinkedFile("", "oldKey2081.pdf", "PDF"))); |
| 102 | + when(filePreferences.getFileNamePattern()).thenReturn(""); |
| 103 | + when(filePreferences.shouldAutoRenameFilesOnChange()).thenReturn(true); |
| 104 | + entry.setField(StandardField.AUTHOR, "newKey"); |
| 105 | + |
| 106 | + assertEquals("oldKey2081.pdf", entry.getFiles().getFirst().getLink()); |
| 107 | + assertTrue(Files.exists(tempDir.resolve("oldKey2081.pdf"))); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void singleFileRenameOnEntryChange() throws IOException { |
| 112 | + Files.createFile(tempDir.resolve("oldKey2081.pdf")); |
| 113 | + entry.setFiles(List.of(new LinkedFile("", "oldKey2081.pdf", "PDF"))); |
| 114 | + when(filePreferences.shouldAutoRenameFilesOnChange()).thenReturn(true); |
| 115 | + |
| 116 | + // change author only |
| 117 | + entry.setField(StandardField.AUTHOR, "newKey"); |
| 118 | + assertEquals("newKey2081.pdf", entry.getFiles().getFirst().getLink()); |
| 119 | + assertTrue(Files.exists(tempDir.resolve("newKey2081.pdf"))); |
| 120 | + |
| 121 | + // change year only |
| 122 | + entry.setField(StandardField.YEAR, "2082"); |
| 123 | + assertEquals("newKey2082.pdf", entry.getFiles().getFirst().getLink()); |
| 124 | + assertTrue(Files.exists(tempDir.resolve("newKey2082.pdf"))); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void multipleFilesRenameOnEntryChange() throws IOException { |
| 129 | + // create multiple entries |
| 130 | + List<String> fileNames = List.of( |
| 131 | + "oldKey2081.pdf", |
| 132 | + "oldKey2081.jpg", |
| 133 | + "oldKey2081.csv", |
| 134 | + "oldKey2081.doc", |
| 135 | + "oldKey2081.docx" |
| 136 | + ); |
| 137 | + |
| 138 | + for (String fileName : fileNames) { |
| 139 | + Path filePath = tempDir.resolve(fileName); |
| 140 | + Files.createFile(filePath); |
| 141 | + } |
| 142 | + |
| 143 | + LinkedFile pdfLinkedFile = new LinkedFile("", "oldKey2081.pdf", "PDF"); |
| 144 | + LinkedFile jpgLinkedFile = new LinkedFile("", "oldKey2081.jpg", "JPG"); |
| 145 | + LinkedFile csvLinkedFile = new LinkedFile("", "oldKey2081.csv", "CSV"); |
| 146 | + LinkedFile docLinkedFile = new LinkedFile("", "oldKey2081.doc", "DOC"); |
| 147 | + LinkedFile docxLinkedFile = new LinkedFile("", "oldKey2081.docx", "DOCX"); |
| 148 | + |
| 149 | + entry.setFiles(List.of(pdfLinkedFile, jpgLinkedFile, csvLinkedFile, docLinkedFile, docxLinkedFile)); |
| 150 | + when(filePreferences.shouldAutoRenameFilesOnChange()).thenReturn(true); |
| 151 | + |
| 152 | + // Change author only |
| 153 | + entry.setField(StandardField.AUTHOR, "newKey"); |
| 154 | + assertTrue(Files.exists(tempDir.resolve("newKey2081.pdf"))); |
| 155 | + assertTrue(Files.exists(tempDir.resolve("newKey2081.jpg"))); |
| 156 | + assertTrue(Files.exists(tempDir.resolve("newKey2081.csv"))); |
| 157 | + assertTrue(Files.exists(tempDir.resolve("newKey2081.doc"))); |
| 158 | + assertTrue(Files.exists(tempDir.resolve("newKey2081.docx"))); |
| 159 | + |
| 160 | + // change year only |
| 161 | + entry.setField(StandardField.YEAR, "2082"); |
| 162 | + assertTrue(Files.exists(tempDir.resolve("newKey2082.pdf"))); |
| 163 | + assertTrue(Files.exists(tempDir.resolve("newKey2082.jpg"))); |
| 164 | + assertTrue(Files.exists(tempDir.resolve("newKey2082.csv"))); |
| 165 | + assertTrue(Files.exists(tempDir.resolve("newKey2082.doc"))); |
| 166 | + assertTrue(Files.exists(tempDir.resolve("newKey2082.docx"))); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void shouldHandleFileNameConflicts() throws IOException { |
| 171 | + // files that may or may not be linked to another entry |
| 172 | + Files.createFile(tempDir.resolve("newKey2081.pdf")); |
| 173 | + Files.createFile(tempDir.resolve("newKey2081 (1).pdf")); |
| 174 | + |
| 175 | + Files.createFile(tempDir.resolve("oldKey2081.pdf")); |
| 176 | + entry.setFiles(List.of(new LinkedFile("", "oldKey2081.pdf", "PDF"))); |
| 177 | + when(filePreferences.shouldAutoRenameFilesOnChange()).thenReturn(true); |
| 178 | + |
| 179 | + entry.setField(StandardField.AUTHOR, "newKey"); |
| 180 | + assertTrue(Files.exists(tempDir.resolve("newKey2081.pdf"))); |
| 181 | + assertTrue(Files.exists(tempDir.resolve("newKey2081 (1).pdf"))); |
| 182 | + assertTrue(Files.exists(tempDir.resolve("newKey2081 (2).pdf"))); |
| 183 | + assertEquals("newKey2081 (2).pdf", entry.getFiles().getFirst().getLink()); |
| 184 | + } |
| 185 | +} |
0 commit comments