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

Add date checker #4007

Merged
merged 7 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#

### Changed
We added RFC, a new entry generator ID type. [#3971](https://github.com/JabRef/jabref/issues/3971)
We added a validity check for dates in the `date` and `urldate` fields.

### Fixed
We fixed an issue where the export to clipboard functionality could not be invoked [#3994](https://github.com/JabRef/jabref/issues/3994)

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/jabref/logic/integrity/DateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jabref.logic.integrity;

import java.util.Optional;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.Date;
import org.jabref.model.strings.StringUtil;

public class DateChecker implements ValueChecker {

@Override
public Optional<String> checkValue(String value) {
if (StringUtil.isBlank(value)) {
return Optional.empty();
}

Optional<Date> parsedDate = Date.parse(value);
if (!parsedDate.isPresent()) {
return Optional.of(Localization.lang("incorrect format"));
}

return Optional.empty();
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/jabref/logic/integrity/FieldCheckers.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ private static Multimap<String, ValueChecker> getAllMap(BibDatabaseContext datab
fieldCheckers.put(FieldName.YEAR, new YearChecker());
fieldCheckers.put(FieldName.KEY, new ValidBibtexKeyChecker(enforceLegalKey));

if (databaseContext.isBiblatexMode()) {
fieldCheckers.put(FieldName.DATE, new DateChecker());
fieldCheckers.put(FieldName.URLDATE, new DateChecker());
}

return fieldCheckers;
}

Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/jabref/logic/integrity/DateCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jabref.logic.integrity;

import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class DateCheckerTest {

private DateChecker checker;

@BeforeEach
void setUp() {
checker = new DateChecker();
}

@Test
void complainsAboutInvalidIsoLikeDate() {
assertEquals(Optional.of("incorrect format"), checker.checkValue("2018-04-21TZ"));
}

@Test
void acceptsValidIsoDate() {
assertEquals(Optional.empty(), checker.checkValue("2018-04-21"));
}
}