Skip to content

Commit

Permalink
Merge pull request #10236 from JabRef/fix-slr-test
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
Siedlerchr authored Aug 29, 2023
2 parents c385eb4 + a840f95 commit 3fda8ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
24 changes: 9 additions & 15 deletions src/main/java/org/jabref/logic/crawler/StudyRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.metadata.SaveOrder;
import org.jabref.model.study.FetchResult;
import org.jabref.model.study.QueryResult;
Expand Down Expand Up @@ -62,11 +61,6 @@ public class StudyRepository {
private static final String WORK_BRANCH = "work";
private static final String SEARCH_BRANCH = "search";

private static final SaveOrder SAVE_ORDER = new SaveOrder(SaveOrder.OrderType.SPECIFIED,
List.of(new SaveOrder.SortCriterion(StandardField.AUTHOR, false),
new SaveOrder.SortCriterion(StandardField.YEAR, true),
new SaveOrder.SortCriterion(StandardField.TITLE, false)));

private final Path repositoryPath;
private final Path studyDefinitionFile;
private final SlrGitHandler gitHandler;
Expand All @@ -82,7 +76,7 @@ public class StudyRepository {
* @param gitHandler The git handler that manages any interaction with the remote repository
* @throws IllegalArgumentException If the repository root directory does not exist, or the root directory does not
* contain the study definition file.
* @throws IOException Thrown if the given repository does not exists, or the study definition file
* @throws IOException Thrown if the given repository does not exist, or the study definition file
* does not exist
*/
public StudyRepository(Path pathToRepository,
Expand Down Expand Up @@ -405,21 +399,21 @@ private void persistResults(List<QueryResult> crawlResults) throws IOException,
// Aggregate each fetcher result into the query result
merger.merge(queryResultEntries, fetcherEntries);

writeResultToFile(getPathToFetcherResultFile(result.getQuery(), fetcherResult.getFetcherName()), existingFetcherResult.getDatabase());
writeResultToFile(getPathToFetcherResultFile(result.getQuery(), fetcherResult.getFetcherName()), existingFetcherResult);
}
BibDatabase existingQueryEntries = getQueryResultEntries(result.getQuery()).getDatabase();
BibDatabaseContext existingQueryEntries = getQueryResultEntries(result.getQuery());

// Merge new entries into query result file
merger.merge(existingQueryEntries, queryResultEntries);
merger.merge(existingQueryEntries.getDatabase(), queryResultEntries);
// Aggregate all new entries for every query into the study result
merger.merge(newStudyResultEntries, queryResultEntries);

writeResultToFile(getPathToQueryResultFile(result.getQuery()), existingQueryEntries);
}
BibDatabase existingStudyResultEntries = getStudyResultEntries().getDatabase();
BibDatabaseContext existingStudyResultEntries = getStudyResultEntries();

// Merge new entries into study result file
merger.merge(existingStudyResultEntries, newStudyResultEntries);
merger.merge(existingStudyResultEntries.getDatabase(), newStudyResultEntries);

writeResultToFile(getPathToStudyResultFile(), existingStudyResultEntries);
}
Expand All @@ -430,10 +424,10 @@ private void generateCiteKeys(BibDatabaseContext existingEntries, BibDatabase ta
targetEntries.getEntries().stream().filter(bibEntry -> !bibEntry.hasCitationKey()).forEach(citationKeyGenerator::generateAndSetKey);
}

private void writeResultToFile(Path pathToFile, BibDatabase entries) throws SaveException {
private void writeResultToFile(Path pathToFile, BibDatabaseContext context) throws SaveException {
try (AtomicFileWriter fileWriter = new AtomicFileWriter(pathToFile, StandardCharsets.UTF_8)) {
SaveConfiguration saveConfiguration = new SaveConfiguration()
.withSaveOrder(SAVE_ORDER)
.withSaveOrder(context.getMetaData().getSaveOrder().orElse(SaveOrder.getDefaultSaveOrder()))
.withReformatOnSave(preferencesService.getLibraryPreferences().shouldAlwaysReformatOnSave());
BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE);
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(
Expand All @@ -442,7 +436,7 @@ private void writeResultToFile(Path pathToFile, BibDatabase entries) throws Save
preferencesService.getFieldPreferences(),
preferencesService.getCitationKeyPatternPreferences(),
bibEntryTypesManager);
databaseWriter.saveDatabase(new BibDatabaseContext(entries));
databaseWriter.saveDatabase(context);
} catch (UnsupportedCharsetException ex) {
throw new SaveException(Localization.lang("Character encoding UTF-8 is not supported.", ex));
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,26 @@ public class BibtexDatabaseWriterTest {
@BeforeEach
void setUp() {
fieldPreferences = new FieldPreferences(true, Collections.emptyList(), Collections.emptyList());
saveConfiguration = mock(SaveConfiguration.class, Answers.RETURNS_DEEP_STUBS);
when(saveConfiguration.getSaveOrder()).thenReturn(SaveOrder.getDefaultSaveOrder());
saveConfiguration = new SaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, false);
citationKeyPatternPreferences = mock(CitationKeyPatternPreferences.class, Answers.RETURNS_DEEP_STUBS);
entryTypesManager = new BibEntryTypesManager();
stringWriter = new StringWriter();
bibWriter = new BibWriter(stringWriter, OS.NEWLINE);
initializeDatabaseWriter();
database = new BibDatabase();
metaData = new MetaData();
bibtexContext = new BibDatabaseContext(database, metaData);
importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
when(importFormatPreferences.fieldPreferences()).thenReturn(fieldPreferences);
}

private void initializeDatabaseWriter() {
databaseWriter = new BibtexDatabaseWriter(
bibWriter,
saveConfiguration,
fieldPreferences,
citationKeyPatternPreferences,
entryTypesManager);

database = new BibDatabase();
metaData = new MetaData();
bibtexContext = new BibDatabaseContext(database, metaData);
importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
when(importFormatPreferences.fieldPreferences()).thenReturn(fieldPreferences);
}

@Test
Expand Down Expand Up @@ -672,14 +674,14 @@ void writeSavedSerializationOfEntryIfUnchanged() throws Exception {

@Test
void reformatEntryIfAskedToDoSo() throws Exception {
BibEntry entry = new BibEntry();
entry.setType(StandardEntryType.Article);
entry.setField(StandardField.AUTHOR, "Mr. author");
BibEntry entry = new BibEntry(StandardEntryType.Article)
.withField(StandardField.AUTHOR, "Mr. author");
entry.setParsedSerialization("wrong serialization");
entry.setChanged(false);
database.insertEntry(entry);

when(saveConfiguration.shouldReformatFile()).thenReturn(true);
saveConfiguration = new SaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, true);
initializeDatabaseWriter();
databaseWriter.savePartOfDatabase(bibtexContext, Collections.singletonList(entry));

assertEquals("@Article{," + OS.NEWLINE + " author = {Mr. author}," + OS.NEWLINE + "}"
Expand All @@ -704,7 +706,8 @@ void reformatStringIfAskedToDoSo() throws Exception {
string.setParsedSerialization("wrong serialization");
database.addString(string);

when(saveConfiguration.shouldReformatFile()).thenReturn(true);
saveConfiguration = new SaveConfiguration(SaveOrder.getDefaultSaveOrder(), false, BibDatabaseWriter.SaveType.WITH_JABREF_META_DATA, true);
initializeDatabaseWriter();
databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList());

assertEquals("@String{name = {content}}" + OS.NEWLINE, stringWriter.toString());
Expand Down

0 comments on commit 3fda8ef

Please sign in to comment.