Skip to content

Commit

Permalink
Tried to fix JabRef#1251, but all tests pass (JabRef#1278)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Apr 22, 2016
1 parent ea62aae commit f248162
Showing 1 changed file with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.sf.jabref.model.entry.FileField;
import net.sf.jabref.model.entry.ParsedFileField;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -26,28 +27,65 @@ public class RenamePdfCleanupTest {

@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
private BibDatabaseContext context;
private BibEntry entry;

@Before
public void setUp() throws Exception {
Globals.prefs = mock(JabRefPreferences.class);

MetaData metaData = new MetaData();
context = new BibDatabaseContext(new BibDatabase(), metaData, new Defaults());
context.setDatabaseFile(testFolder.newFile("test.bib"));

entry = new BibEntry();
entry.setCiteKey("Toot");
}

/**
* Test for #466
*/
@Test
public void cleanupRenamePdfRenamesFileEvenIfOnlyDifferenceIsCase() throws IOException {
Globals.prefs = mock(JabRefPreferences.class);
when(Globals.prefs.get("importFileNamePattern")).thenReturn("\\bibtexkey");
File tempFile = testFolder.newFile("toot.tmp");
ParsedFileField fileField = new ParsedFileField("", tempFile.getAbsolutePath(), "");
entry.setField("file", FileField.getStringRepresentation(fileField));

MetaData metaData = new MetaData();
BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(), metaData, new Defaults());
context.setDatabaseFile(testFolder.newFile("test.bib"));
RenamePdfCleanup cleanup = new RenamePdfCleanup(false, context, mock(JournalAbbreviationRepository.class));
cleanup.cleanup(entry);

File tempFile = testFolder.newFile("toot.tmp");
BibEntry entry = new BibEntry();
entry.setField(BibEntry.KEY_FIELD, "Toot");
ParsedFileField newFileField = new ParsedFileField("", "Toot.tmp", "");
assertEquals(FileField.getStringRepresentation(newFileField), entry.getField("file"));
}

@Test
public void cleanupRenamePdfRenamesFileStartingWithBibtexKey() throws IOException {
when(Globals.prefs.get("importFileNamePattern")).thenReturn("\\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(JournalAbbreviationRepository.class));
cleanup.cleanup(entry);
ParsedFileField newFileField = new ParsedFileField("", "Toot.tmp", "");

ParsedFileField newFileField = new ParsedFileField("", "Toot - test title.tmp", "");
assertEquals(FileField.getStringRepresentation(newFileField), entry.getField("file"));
}

@Test
public void cleanupRenamePdfRenamesFileInSameFolder() throws IOException {
when(Globals.prefs.get("importFileNamePattern")).thenReturn("\\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(JournalAbbreviationRepository.class));
cleanup.cleanup(entry);

ParsedFileField newFileField = new ParsedFileField("", "Toot - test title.pdf", "PDF");
assertEquals(FileField.getStringRepresentation(newFileField), entry.getField("file"));
}
}
}

0 comments on commit f248162

Please # to comment.