Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PDF relativization test #12621

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -186,11 +186,23 @@ private static BibEntry mergeCandidates(Stream<BibEntry> candidates) {
return entry;
}

/**
* A modified version of {@link PdfMergeMetadataImporter#importDatabase(Path)}, but it
* relativizes the {@code filePath} if there are working directories before parsing it
* into {@link PdfMergeMetadataImporter#importDatabase(Path)}
* (Otherwise no path modification happens).
*
* @param filePath The unrelativized {@code filePath}.
*/
public ParserResult importDatabase(Path filePath, BibDatabaseContext context, FilePreferences filePreferences) throws IOException {
Objects.requireNonNull(context);
Objects.requireNonNull(filePreferences);

return importDatabase(filePath);
List<Path> directories = context.getFileDirectories(filePreferences);

filePath = FileUtil.relativize(filePath, directories);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved down to ´importDatabase`, then I understand it more...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in importDatabase?


return importDatabase(filePath, filePreferences);
}

@Override
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import java.nio.file.Path;
import java.util.List;

import org.jabref.logic.FilePreferences;
import org.jabref.logic.importer.Importer;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.ParserResult;
@@ -61,6 +62,18 @@ public ParserResult importDatabase(Path filePath) {
}
}

public ParserResult importDatabase(Path filePath, FilePreferences filePreferences) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this is necessary. Shouldn't the caller take care about the absolute path? - Since this method is a) a duplicate of public ParserResult importDatabase(Path filePath) and b) used only once, I propose to move Path readPath = filePreferences.getWorkingDirectory().resolve(filePath); to the caller.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... the problem is:

XmpUtilReader can't read relativized path. This was the reason why I (mistakenly) removed relativization.

So in this method I leave relativized path in BibEntry, but when I'm reading I use the full path.

Yeah, something's wrong here

Path readPath = filePreferences.getWorkingDirectory().resolve(filePath);

try (PDDocument document = new XmpUtilReader().loadWithAutomaticDecryption(readPath)) {
return new ParserResult(importDatabase(filePath, document));
} catch (EncryptedPdfsNotSupportedException e) {
return ParserResult.fromErrorMessage(Localization.lang("Decryption not supported."));
} catch (IOException | ParseException exception) {
return ParserResult.fromError(exception);
}
}

@Override
public StandardFileType getFileType() {
return StandardFileType.PDF;
Original file line number Diff line number Diff line change
@@ -90,16 +90,18 @@ void importWorksAsExpected() throws Exception {

@Test
void importRelativizesFilePath() throws Exception {
// Initialize database and preferences
FilePreferences preferences = mock(FilePreferences.class);
BibDatabaseContext database = new BibDatabaseContext();

// Initialize file and working directory
Path file = Path.of(PdfMergeMetadataImporter.class.getResource("/pdfs/minimal.pdf").toURI());
Path directory = Path.of(PdfMergeMetadataImporter.class.getResource("/pdfs/").toURI());
preferences.setWorkingDirectory(directory);

List<BibEntry> result = importer.importDatabase(file, database, preferences).getDatabase().getEntries();
// If you use a mocked class, and then call `set`ters, they won't work... Thus, we have to create the class manually.
FilePreferences filePreferences = new FilePreferences("", "", false, "", "", false, false, Path.of(""), false, Path.of(""), false, false, false);
filePreferences.setStoreFilesRelativeToBibFile(true);
filePreferences.setWorkingDirectory(directory);

BibDatabaseContext database = new BibDatabaseContext();
database.setDatabasePath(directory.resolve("db.bib"));

List<BibEntry> result = importer.importDatabase(file, database, filePreferences).getDatabase().getEntries();

BibEntry expected = new BibEntry(StandardEntryType.InProceedings)
.withField(StandardField.AUTHOR, "1 ")