-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Added static (stream & preferences) constructors to BibDatabaseContext #13694
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
Conversation
- two new constructors, one that takes a String, the other an InputStream. both of these construct Reader instances and delegate to the now slightly altered, original, 'of' factory method - original 'of' factor method cut-back a bit to take a Reader. - updated all tests and use-cases I could find.
- removed single 'throws Exception' clauses in test methods - removed multiple exception class declarations in test throws clauses
|
|
||
| assertEquals(bibtexEntry, stringWriter.toString()); | ||
| assertEquals(bibtexEntry, stringWriter.toString()); | ||
| } catch (JabRefException e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting checked exceptions to runtime exceptions violates proper exception handling practices and obscures the actual error. Should let JUnit handle the exception directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate this trag-bot, but you have me in endless loop. Why are you never happy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some rules, we don't know why TragBot does this. The support team is responsive, but not content-wise ^^.
| try { | ||
| BibDatabaseContext context = BibDatabaseContext.of(bibtexEntry, importFormatPreferences); | ||
| databaseWriter.saveDatabase(context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try block covers more statements than necessary. It should only wrap the BibDatabaseContext.of() call since that's the only statement that throws JabRefException.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to change this if required, it's a bit pedantic imo in this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trag bot has to be taken with some nuances. Not always right
| } catch (JabRefException e) { | ||
| throw new RuntimeException(e); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception handling wraps and rethrows exception instead of declaring throws in method signature. This creates unnecessary exception wrapping and hides the original exception type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a workaround to make you happy, which you never seem to be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just add a throws Exception to the test method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it complains about the declared exception being too generic IIRC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the exception thrown in the test. @subhramit and me once discussed, and I think, we agreed to have the most "proper" exception to make the IDE happy.
subhramit
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! two or three minor comments from my side.
And no need to pay much attention to tragbot comments - they are not always helpful.
| try { | ||
| try (Reader reader = new BufferedReader(new InputStreamReader(bibContentStream))) { | ||
| return of(reader, importFormatPreferences); | ||
| } | ||
| } catch (IOException e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can collapse these
| try { | |
| try (Reader reader = new BufferedReader(new InputStreamReader(bibContentStream))) { | |
| return of(reader, importFormatPreferences); | |
| } | |
| } catch (IOException e) { | |
| try (Reader reader = new BufferedReader(new InputStreamReader(bibContentStream))) { | |
| return of(reader, importFormatPreferences); | |
| } catch (IOException e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
| try (InputStream bibContentStream = new ByteArrayInputStream(bibContent.getBytes(StandardCharsets.UTF_8))) { | ||
| BibDatabaseContext context = BibDatabaseContext.of(bibContentStream, importPrefs); | ||
| BibEntry expected = new BibEntry(StandardEntryType.Article) | ||
| .withCitationKey("a") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| .withCitationKey("a") | |
| .withCitationKey("Alice2023") |
| @Test | ||
| void ofParsesValidBibtexStreamCorrectly() throws Exception { | ||
| String bibContent = """ | ||
| @article{a, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets try to keep test articles near realistic
| @article{a, | |
| @article{Alice2023, |
And corresponding change in the expected entry below
Edit - somehow I realized GitHub messed the order of comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just looked into the code and learnt something, all is good.
|
I've erred on the side of 'cleaner' test code. I'm not capturing and re-throwing RuntimeExceptions. Rather, i'm just declaring 'throws Exception' on the test case methods. Minor other things that should hopefully keep trag-bot happy. |
| "}" + OS.NEWLINE; | ||
| // @formatter:on | ||
|
|
||
| BibEntry entry = firstEntryFrom(bibtexEntry); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, moving the donig code AFTER the expection is not in line with "given, when, then", was the expected is part of the "then".
If you agree, please move the "expected" part to the assert.
You can also remove @formatter statements and convert the string to a multi line string ( """) followed by .replace("\n", OS.NEWLINE) (if this is not too frustrating; I was in thtat and nearly gave up)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, understood. Done.
I've sorted all manual string concat. instances and removed all IDEA warnings.
| // check month field | ||
| Set<Field> fields = entry.getFields(); | ||
| assertTrue(fields.contains(StandardField.MONTH)); | ||
| assertTrue(entry.getField(StandardField.MONTH).isPresent()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using assertTrue for Optional presence check instead of asserting the actual value. Should directly assert the expected value using assertEquals for better test clarity.
subhramit
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm
koppor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The wrapping into JabRef exception is OK for me. This is also backed via https://devdocs.jabref.org/code-howtos/error-handling.html.
* upstream/main: New translations jabref_en.properties (Italian) (#13725) Minor code style updates (#13722) Fix: Make FileUtil.relativize symlink-aware (#13553) New Crowdin updates (#13720) Bump org.glassfish.jersey.core:jersey-server in /versions (#13714) Enable UseObjectNotifyAll (#13718) Bump com.dlsc.gemsfx:gemsfx from 3.3.5 to 3.4.2 in /versions (#13717) Update on-issue-comment.yml Bump org.openrewrite.recipe:rewrite-recipe-bom from 3.12.1 to 3.13.0 (#13716) Bump org.openrewrite.rewrite from 7.12.1 to 7.14.0 (#13715) Bump org.glassfish.jersey.inject:jersey-hk2 in /versions (#13713) feat(git): add “Share to GitHub” flow (#13677) Bump jablib/src/main/resources/csl-styles from `292aec3` to `1194364` (#13712) Bump jablib/src/main/abbrv.jabref.org from `cfe719f` to `a97f9c6` (#13711) Bump jablib/src/main/resources/csl-locales from `e2de1e3` to `fa56de1` (#13710) Add noop Git Config System Reader to prevent usage of real world stuff into jgit (#13703) Added static (stream & preferences) constructors to BibDatabaseContext (#13694) New Crowdin updates (#13698) fix git modules requires and uses (#13696) Focus "Specify Bib(La)TeX" when Bib(La)TeX is in clipboard (#13633)
koppor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all blocks of BibEntries with mixed newlines could be translated to Java multiline strings. Tests failed on Windows. https://github.com/JabRef/jabref/actions/runs/17166820007/job/48709003363
I submitted a fix at #13739.
| Note = {some note},\r | ||
| Number = {1}\r | ||
| }\r | ||
| """.replace("\n", OS.NEWLINE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This replacement was wrong - original, it was \r\n, which is NOT OS.NEWLINE on macOS.
| BibEntry entry = result.getDatabase().getEntries().getFirst(); | ||
| void roundTripWithKeepsCRLFLineBreakStyle() throws Exception { | ||
| String bibtexEntry = """ | ||
| @Article{test,\r |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\r is wrong - this is automatically added by Java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks and sorry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries! Would have happened to me, too! (Excuse the brievty in the comment, I was in a hurry! We really like your speed and contributions! Looking forward for more!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, that's kind. I've taken a little step-back recently to improve my JavaFX knowledge... I hope i'll be more useful before long.
Note that this info is just for a post-mortem - nobody could detect it during reviews as we have disabled a Windows-based test on CI that used to take very long 😅 |
Closes #13617
Steps to test
Ran tests before and after changes. Ensured new code covered in unit tests. Manual testing.
Mandatory checks
CHANGELOG.mddescribed in a way that is understandable for the average user (if change is visible to the user)