Skip to content

Commit

Permalink
add simple test cases for crossref inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
sfo committed Sep 5, 2019
1 parent ce72cb6 commit 7330207
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/test/java/org/jabref/model/entry/CrossrefTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.jabref.model.entry;

import org.jabref.logic.importer.ImportException;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.xmp.XmpPreferences;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.util.DummyFileUpdateMonitor;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;

import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class CrossrefTest {

private static BibDatabase database;

@BeforeAll
static void SetUp() throws ImportException, URISyntaxException {
ImportFormatReader reader = new ImportFormatReader();
ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
when(importFormatPreferences.getEncoding()).thenReturn(StandardCharsets.UTF_8);
reader.resetImportFormats(importFormatPreferences, mock(XmpPreferences.class), new DummyFileUpdateMonitor());

Path file = Paths.get(CrossrefTest.class.getResource("crossref.bib").toURI());
database = reader.importFromFile("bibtex", file).getDatabase();
}

private BibEntry getEntry(String key) {
var entries = database.getEntriesByKey(key);
assertEquals(1, entries.size());
return entries.get(0);
}

@Test
void inproceedings_proceedings_inheritance() {
var source = getEntry("pr_001");
var target = getEntry("inpr_001");

assertEquals(
source.getResolvedFieldOrAlias(StandardField.YEAR, database),
target.getResolvedFieldOrAlias(StandardField.YEAR, database)
);

assertEquals(
source.getResolvedFieldOrAlias(StandardField.TITLE, database),
target.getResolvedFieldOrAlias(StandardField.BOOKTITLE, database)
);
}

@Test
void inproceedings_proceedings_no_inheritance() {
var target = getEntry("inpr_001");

assertFalse(target.getResolvedFieldOrAlias(StandardField.TITLE, database).isPresent());
}

@Test
void inproceedings_proceedings_no_overwrite() {
var source = getEntry("pr_001");
var target = getEntry("inpr_002");

assertNotEquals(
source.getResolvedFieldOrAlias(StandardField.YEAR, database),
target.getResolvedFieldOrAlias(StandardField.YEAR, database)
);

assertNotEquals(
source.getResolvedFieldOrAlias(StandardField.TITLE, database),
target.getResolvedFieldOrAlias(StandardField.BOOKTITLE, database)
);
}
}
14 changes: 14 additions & 0 deletions src/test/resources/org/jabref/model/entry/crossref.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@Proceedings{pr_001,
title = {Proceedings_001},
year = {2019},
}

@InProceedings{inpr_001,
crossref = {pr_001},
}

@InProceedings{inpr_002,
crossref = {pr_001},
booktitle = {foobar},
year = {42}
}

0 comments on commit 7330207

Please sign in to comment.