Skip to content

Commit

Permalink
Fix NPEs in UrlCleanup (#10435)
Browse files Browse the repository at this point in the history
* Fix NPEs in UrlCleanup

 "Move URL in note field to url field" was causing NPEs

* add new test case for no fields

* checkstyle
  • Loading branch information
Siedlerchr committed Oct 1, 2023
1 parent 2a48a8c commit 432811c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added a privacy policy. [#10064](https://github.com/JabRef/jabref/issues/10064)
- We added a tooltip to show the number of entries in a group [#10208](https://github.com/JabRef/jabref/issues/10208)
- We fixed an issue where it was no longer possible to add or remove selected entries to groups via context menu [#10404](https://github.com/JabRef/jabref/issues/10404), [#10317](https://github.com/JabRef/jabref/issues/10317) [#10374](https://github.com/JabRef/jabref/issues/10374)
- We fixed an issue where "Move URL in note field to url field" in the cleanup dialog caused an exception if no note field was present [forum#3999](https://discourse.jabref.org/t/cleanup-entries-cant-get-it-to-work/3999)

### Changed

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/jabref/logic/cleanup/URLCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ public class URLCleanup implements CleanupJob {
final Pattern urlPattern = Pattern.compile(URL_REGEX, Pattern.CASE_INSENSITIVE);
final Pattern dateTermsPattern = Pattern.compile(DATE_TERMS_REGEX, Pattern.CASE_INSENSITIVE);
final Pattern datePattern = Pattern.compile(Date.DATE_REGEX, Pattern.CASE_INSENSITIVE);

private NormalizeDateFormatter formatter = new NormalizeDateFormatter();
private final NormalizeDateFormatter formatter = new NormalizeDateFormatter();

@Override
public List<FieldChange> cleanup(BibEntry entry) {
List<FieldChange> changes = new ArrayList<>();

String noteFieldValue = entry.getField(NOTE_FIELD).orElse(null);
String noteFieldValue = entry.getField(NOTE_FIELD).orElse("");

final Matcher urlMatcher = urlPattern.matcher(noteFieldValue);
final Matcher dateTermsMatcher = dateTermsPattern.matcher(noteFieldValue);
Expand Down Expand Up @@ -74,7 +73,7 @@ public List<FieldChange> cleanup(BibEntry entry) {
* remove it from the note field, and no other action is performed.
*/
if (entry.hasField(URL_FIELD)) {
String urlFieldValue = entry.getField(URL_FIELD).orElse(null);
String urlFieldValue = entry.getField(URL_FIELD).orElse("");
if (urlFieldValue.equals(url)) {
entry.setField(NOTE_FIELD, newNoteFieldValue).ifPresent(changes::add);
}
Expand All @@ -96,7 +95,7 @@ public List<FieldChange> cleanup(BibEntry entry) {

// Same approach with the URL cleanup.
if (entry.hasField(URLDATE_FIELD)) {
String urlDateFieldValue = entry.getField(URLDATE_FIELD).orElse(null);
String urlDateFieldValue = entry.getField(URLDATE_FIELD).orElse("");
if (urlDateFieldValue.equals(formattedDate)) {
entry.setField(NOTE_FIELD, newNoteFieldValue).ifPresent(changes::add);
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/org/jabref/logic/cleanup/URLCleanupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ private static Stream<Arguments> provideURL() {
.withField(StandardField.NOTE,
"cited by Kramer"),
new BibEntry().withField(StandardField.NOTE,
"\\url{https://example.org}, cited by Kramer, accessed on 2023-04-11"))
"\\url{https://example.org}, cited by Kramer, accessed on 2023-04-11"),
// test with no fields present
Arguments.of(
new BibEntry(),
new BibEntry())
)
);
}
}

0 comments on commit 432811c

Please sign in to comment.