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

Conversation

InAnYan
Copy link
Member

@InAnYan InAnYan commented Mar 4, 2025

Fixes failing test: PdfMergeMetadataImporterTest#importRelativizesFilePath.

Fixes #12620

Mandatory checks

  • I own the copyright of the code submitted and I license it under the MIT license
  • [/] Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • Tests created for changes (if applicable)
  • [/] Manually tested changed features in running JabRef (always required)
  • [/] Screenshots added in PR description (for UI changes)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

@koppor
Copy link
Member

koppor commented Mar 4, 2025

Blocked by #12619

@koppor koppor requested a review from Copilot March 4, 2025 13:42

Choose a reason for hiding this comment

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

PR Overview

This PR fixes a failing test for PDF metadata file path relativization. The changes include:

  • Modifying the test to use a manually instantiated FilePreferences rather than a mocked one.
  • Adding an overloaded importDatabase method in PdfMergeMetadataImporter that relativizes the file path.
  • Introducing a corresponding overloaded importDatabase method in PdfImporter to support the new file path handling.

Reviewed Changes

File Description
src/test/java/org/jabref/logic/importer/fileformat/pdf/PdfMergeMetadataImporterTest.java Updates test to use manual file preferences instantiation for correct working directory setup
src/main/java/org/jabref/logic/importer/fileformat/PdfMergeMetadataImporter.java Introduces a new method that relativizes the file path before database import
src/main/java/org/jabref/logic/importer/fileformat/pdf/PdfImporter.java Adds an overloaded method accepting FilePreferences to resolve the file path correctly

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

@@ -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

Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

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

Ah, let's go this through. Better than nothing. :)

I have a small suggestion, which would make the logic more clear.

Maybe, also the JavaDoc should be copied into PdfImporter.

Comment on lines 201 to 203
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?

@InAnYan InAnYan marked this pull request as draft March 17, 2025 20:18
@InAnYan
Copy link
Member Author

InAnYan commented Mar 17, 2025

I should think about it more

@koppor
Copy link
Member

koppor commented Mar 17, 2025

I should think about it more

The quick fix is OK for now, isn't it?

@InAnYan InAnYan marked this pull request as ready for review March 20, 2025 09:49
} catch (IOException e) {
return ParserResult.fromError(e);
}
return new PdfMergeMetadataImporter(importFormatPreferences).importDatabase(file, context, filePreferences);
Copy link

Choose a reason for hiding this comment

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

The try-catch block removal shifts exception handling responsibility to the caller without documenting this change in method signature. Method should declare throws IOException.

Copy link
Member Author

Choose a reason for hiding this comment

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

IntelliJ said that it is not thrown at all

@@ -25,8 +25,8 @@ public PdfGrobidImporter(ImportFormatPreferences importFormatPreferences) {
this.importFormatPreferences = importFormatPreferences;
}

public List<BibEntry> importDatabase(Path filePath, PDDocument document) throws IOException, ParseException {
return grobidService.processPDF(filePath, importFormatPreferences);
public List<BibEntry> importDatabase(Path fullPath, PDDocument document) throws IOException, ParseException {
Copy link

Choose a reason for hiding this comment

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

The method accepts PDDocument parameter but doesn't use it in the implementation, violating the Single Responsibility Principle by having unused parameters.

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, it just uses grobid.

Is there any way to mark this argument as explicitly unused?

Copy link
Member

Choose a reason for hiding this comment

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

Why can't it be removed - I don't see any @Override annotation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh! It should be overriden

Copy link
Member

Choose a reason for hiding this comment

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

Then you can add JavaDoc and use @param and then say that it is ignored ^^

Copy link
Contributor

If you made changes that are visible to the user, please add a brief description along with the issue number to the CHANGELOG.md file. More details can be found in our Developer Documentation about the changelog.

@@ -22,8 +22,8 @@ public PdfXmpImporter(XmpPreferences xmpPreferences) {
this.xmpPreferences = xmpPreferences;
}

public List<BibEntry> importDatabase(Path filePath, PDDocument document) throws IOException {
return new XmpUtilReader().readXmp(filePath, xmpPreferences);
public List<BibEntry> importDatabase(Path fullPath, PDDocument document) throws IOException {
Copy link

Choose a reason for hiding this comment

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

Method accepts PDDocument parameter but doesn't use it in the implementation, violating Single Responsibility Principle by having unused parameters.

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, this is expected.

Is there any way to mark this argument as explicitly unused?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix PdfMergeMetadataImporterTest
3 participants