Skip to content

Commit

Permalink
improve-fetchers (#11715)
Browse files Browse the repository at this point in the history
* Make getTrustLevel() mandatory

* Remove obsolete `@Override`

* Really remoe TrustLevel

* Fix link

* Fix checkstyle

* Fix tests
  • Loading branch information
koppor authored Sep 7, 2024
1 parent 1c232b8 commit 8299b4e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/main/java/org/jabref/logic/importer/FulltextFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ public interface FulltextFetcher {
*
* @return The trust level of the fetcher, the higher the better
*/
default TrustLevel getTrustLevel() {
return TrustLevel.UNKNOWN;
}
TrustLevel getTrustLevel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.jabref.logic.net.URLDownload;
import org.jabref.logic.util.HeadlessExecutorService;
Expand Down Expand Up @@ -113,6 +112,6 @@ private Callable<Optional<FetcherResult>> getCallable(BibEntry entry, FulltextFe
private List<Callable<Optional<FetcherResult>>> getCallables(BibEntry entry, Set<FulltextFetcher> fetchers) {
return fetchers.stream()
.map(f -> getCallable(entry, f))
.collect(Collectors.toList());
.toList();
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/logic/importer/fetcher/CiteSeer.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException, FetcherExc

return Optional.empty();
}

@Override
public TrustLevel getTrustLevel() {
return TrustLevel.META_SEARCH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,4 @@ public Optional<URL> findFullText(BibEntry entry) throws FetcherException, IOExc
public TrustLevel getTrustLevel() {
return TrustLevel.META_SEARCH;
}

@Override
public void doPostCleanup(BibEntry entry) {
// do nothing
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jabref.logic.importer.fetcher;

/**
* Discussion on the trust levels is available at our <a href="https://devdocs.jabref.org/advanced-reading/fetchers">documentation on fetchers</a>.
* Discussion on the trust levels is available at our <a href="https://devdocs.jabref.org/code-howtos/fetchers.html#fulltext-fetchers">documentation on fulltext fetchers</a>.
*/
public enum TrustLevel {
SOURCE(3),
Expand Down
32 changes: 20 additions & 12 deletions src/test/java/org/jabref/logic/importer/FulltextFetchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,47 @@
@FetcherTest
class FulltextFetchersTest {

private BibEntry entry = new BibEntry();
/**
* Required for testing the FulltextFetchers class.
* That code is not put to the FulltextFetcher class itself, because subclasses of FulltextFetcher should implement the getTrustLevel method.
*/
private interface FulltextFetcherWithTrustLevel extends FulltextFetcher {
default TrustLevel getTrustLevel() {
return TrustLevel.UNKNOWN;
}
}

@Test
void acceptPdfUrls() throws MalformedURLException {
URL pdfUrl = new URL("http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.pdf");
FulltextFetcher finder = e -> Optional.of(pdfUrl);
FulltextFetcherWithTrustLevel finder = e -> Optional.of(pdfUrl);
FulltextFetchers fetcher = new FulltextFetchers(Set.of(finder));
assertEquals(Optional.of(pdfUrl), fetcher.findFullTextPDF(entry));
assertEquals(Optional.of(pdfUrl), fetcher.findFullTextPDF(new BibEntry()));
}

@Test
void rejectNonPdfUrls() throws MalformedURLException {
URL pdfUrl = new URL("https://github.com/JabRef/jabref/blob/master/README.md");
FulltextFetcher finder = e -> Optional.of(pdfUrl);
FulltextFetcherWithTrustLevel finder = e -> Optional.of(pdfUrl);
FulltextFetchers fetcher = new FulltextFetchers(Set.of(finder));

assertEquals(Optional.empty(), fetcher.findFullTextPDF(entry));
assertEquals(Optional.empty(), fetcher.findFullTextPDF(new BibEntry()));
}

@Test
void noTrustLevel() throws MalformedURLException {
URL pdfUrl = new URL("http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.pdf");
FulltextFetcher finder = e -> Optional.of(pdfUrl);
FulltextFetcherWithTrustLevel finder = e -> Optional.of(pdfUrl);
FulltextFetchers fetcher = new FulltextFetchers(Set.of(finder));

assertEquals(Optional.of(pdfUrl), fetcher.findFullTextPDF(entry));
assertEquals(Optional.of(pdfUrl), fetcher.findFullTextPDF(new BibEntry()));
}

@Test
void higherTrustLevelWins() throws IOException, FetcherException {
// set an (arbitrary) DOI to the test entry to skip side effects inside the "findFullTextPDF" method
BibEntry entry = new BibEntry().withField(StandardField.DOI, "10.5220/0007903201120130");

FulltextFetcher finderHigh = mock(FulltextFetcher.class);
when(finderHigh.getTrustLevel()).thenReturn(TrustLevel.SOURCE);
final URL highUrl = new URL("http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.pdf");
Expand All @@ -60,11 +71,8 @@ void higherTrustLevelWins() throws IOException, FetcherException {
final URL lowUrl = new URL("http://docs.oasis-open.org/opencsa/sca-bpel/sca-bpel-1.1-spec-cd-01.pdf");
when(finderLow.findFullText(entry)).thenReturn(Optional.of(lowUrl));

FulltextFetchers fetcher = new FulltextFetchers(Set.of(finderLow, finderHigh));

// set an (arbitrary) DOI to the test entry to skip side effects inside the "findFullTextPDF" method
entry.setField(StandardField.DOI, "10.5220/0007903201120130");
FulltextFetchers fetchers = new FulltextFetchers(Set.of(finderLow, finderHigh));

assertEquals(Optional.of(highUrl), fetcher.findFullTextPDF(entry));
assertEquals(Optional.of(highUrl), fetchers.findFullTextPDF(entry));
}
}
7 changes: 5 additions & 2 deletions src/test/java/org/jabref/logic/importer/WebFetchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
class WebFetchersTest {

private static final Logger LOGGER = LoggerFactory.getLogger(WebFetchersTest.class);
private static final Set<String> IGNORED_INACCESSIBLE_FETCHERS = Set.of("ArXivFetcher$ArXiv");

private static final Set<String> IGNORED_INACCESSIBLE_FETCHERS = Set.of(
"org.jabref.logic.importer.fetcher.ArXivFetcher$ArXiv",
"org.jabref.logic.importer.FulltextFetchersTest$FulltextFetcherWithTrustLevel");

private ImportFormatPreferences importFormatPreferences;
private ImporterPreferences importerPreferences;
Expand All @@ -49,7 +52,7 @@ void setUp() {

private Set<Class<?>> getIgnoredInaccessibleClasses() {
return IGNORED_INACCESSIBLE_FETCHERS.stream()
.map(className -> "org.jabref.logic.importer.fetcher." + className)
.map(className -> "" + className)
.map(classPath -> {
try {
return Class.forName(classPath);
Expand Down

0 comments on commit 8299b4e

Please sign in to comment.